yinyitong-zhongyuyuan-dll-s.../src/main/java/com/dpkj/common/utils/ThirdService.java

183 lines
6.8 KiB
Java
Raw Normal View History

package com.dpkj.common.utils;
2025-07-01 15:07:16 +08:00
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.dto.LexMarkDTO;
import com.dpkj.common.dto.LexMarkResultDTO;
import com.dpkj.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
2025-07-01 15:07:16 +08:00
2025-03-14 16:02:00 +08:00
import java.util.HashMap;
import java.util.Map;
2025-02-08 15:40:47 +08:00
/**
* 第三方服务主要是调用打印机等
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @version 1.0
* @since 2025-02-08 10:45:01
*/
@Slf4j
@Component
public class ThirdService {
@Value("${app.custom.lexMarkServiceIp}")
private String lexMarkServiceIp;
@Value("${app.custom.lexMarkServicePort}")
private String lexMarkServicePort;
2025-03-14 16:02:00 +08:00
private static final Map<String, String> devNameMap = new HashMap<>();
2025-07-01 15:07:16 +08:00
2025-03-14 16:02:00 +08:00
static {
devNameMap.put("HtmPrinter", "激光打印机");
devNameMap.put("ReceiptPrinter", "凭条打印机");
}
2025-07-01 15:07:16 +08:00
/**
* 利盟台式机-立体机 接口请求
*
* @param lexMarkDTO 请求DTO
2025-07-01 15:07:16 +08:00
* @param clazz 返回类型
* @return result
*/
public <T> LexMarkResultDTO<T> callDevice(LexMarkDTO lexMarkDTO, Class<T> clazz) {
try {
2025-07-01 15:07:16 +08:00
String urlStr = lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice";
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-url{}", urlStr);
String params = JSONObject.toJSONString(lexMarkDTO);
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-入参:{}", params);
String bodyStr = HttpRequest.post(urlStr)
.header("Content-Type", "application/json")
.body(params)
.timeout(30000)
.execute()
.body();
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-响应:{}", bodyStr);
JSONObject bodyJson = (JSONObject) JSONObject.parse(bodyStr);
if (StrUtil.isEmpty(bodyJson.getString("param"))) {
throw new RRException("利盟服务请求失败,结果响应为空");
}
// 将响应JSON字符串转换为LexMarkResultDTO对象
2025-07-01 15:07:16 +08:00
LexMarkResultDTO obj = JSONObject.toJavaObject(bodyJson, LexMarkResultDTO.class);
T paramObj = JSONObject.toJavaObject(bodyJson.getJSONObject("param"), clazz);
obj.setData(paramObj);
2025-07-01 15:07:16 +08:00
return obj;
} catch (Exception e) {
log.error("利盟服务请求失败:{}", lexMarkDTO, e);
if (e instanceof RRException) {
2025-02-08 15:40:47 +08:00
throw new RRException(((RRException) e).getCode(), e.getMessage());
}
throw new RRException("利盟服务请求失败");
}
}
/**
* 数据对象的data采用JSONObject进行接收
*
* @param lexMarkDTO 请求参数
* @return result
*/
public LexMarkResultDTO<JSONObject> callDevice(LexMarkDTO lexMarkDTO) {
return callDevice(lexMarkDTO, JSONObject.class);
}
2025-03-14 16:02:00 +08:00
/**
* 打开设备连接或者 链接+初始化
2025-07-01 15:07:16 +08:00
*
* @param devName 设备名称
2025-03-14 16:02:00 +08:00
* @param resterType 初始化类型,如果不设置那么就不会进行初始化
*/
public void open(String devName, Integer resterType) {
// 优先自动连接对应的设备
LexMarkDTO lexMarkDTO = new LexMarkDTO();
lexMarkDTO.setActionName("OpenConnection");
lexMarkDTO.setCallID(19283);
lexMarkDTO.setDevName(devName);
lexMarkDTO.setPluginMethod("exec");
2025-07-01 15:07:16 +08:00
JSONObject param = new JSONObject();
2025-03-14 16:02:00 +08:00
param.put("", String.format("{\"ServiceName\":\"%s\",\"TimeOut\":90000}", devName));
lexMarkDTO.setParam(param.toString());
LexMarkResultDTO<JSONObject> jsonObjectLexMarkResultDTO = this.callDevice(lexMarkDTO, JSONObject.class);
JSONObject data = jsonObjectLexMarkResultDTO.getData();
if (data != null) {
if (!String.valueOf(data.get("result")).equals("0")) {
throw new RRException("设备: " + devNameMap.get(devName) + " 打开失败!");
}
}
// 打开后直接进行重置
2025-07-01 15:07:16 +08:00
if (resterType != null && data != null) {
2025-03-14 16:02:00 +08:00
lexMarkDTO.setActionName("Reset");
JSONObject jsonObject = new JSONObject();
jsonObject.put("", String.format("{\"ResetAction\":%d,\"binNumber\":0}", resterType));
lexMarkDTO.setParam(jsonObject.toString());
LexMarkResultDTO<JSONObject> resultDTO = this.callDevice(lexMarkDTO, JSONObject.class);
JSONObject data1 = resultDTO.getData();
if (data1 != null) {
if (!String.valueOf(data1.get("result")).equals("0")) {
throw new RRException("设备: " + devNameMap.get(devName) + " _ " + resterType + " 初始化失败!");
}
}
}
}
/**
* 关闭设备练级
2025-07-01 15:07:16 +08:00
*
2025-03-14 16:02:00 +08:00
* @param devName 设备名称
*/
public void close(String devName) {
LexMarkDTO lexMarkDTO = new LexMarkDTO();
lexMarkDTO.setActionName("CloseConnection");
lexMarkDTO.setCallID(19283);
lexMarkDTO.setDevName(devName);
lexMarkDTO.setPluginMethod("exec");
LexMarkResultDTO<JSONObject> jsonObjectLexMarkResultDTO = this.callDevice(lexMarkDTO, JSONObject.class);
JSONObject data = jsonObjectLexMarkResultDTO.getData();
if (data != null) {
if (!String.valueOf(data.get("result")).equals("0")) {
throw new RRException("设备: " + devNameMap.get(devName) + " 关闭失败!");
}
}
}
/**
* 切纸但是目前凭条和ms439打印机都是可以自动切纸的
2025-07-01 15:07:16 +08:00
*
* @param devName 设备名称
2025-03-14 16:02:00 +08:00
* @param actionName action名称实际的动作函数名称
2025-07-01 15:07:16 +08:00
* @param mediaType 媒介类型
2025-03-14 16:02:00 +08:00
*/
2025-07-01 15:07:16 +08:00
public void cutPaper(String devName, String actionName, Integer mediaType) {
2025-03-14 16:02:00 +08:00
LexMarkDTO lexMarkDTO = new LexMarkDTO();
lexMarkDTO.setActionName(actionName);
lexMarkDTO.setCallID(19283);
lexMarkDTO.setDevName(devName);
lexMarkDTO.setPluginMethod("exec");
2025-07-01 15:07:16 +08:00
JSONObject param = new JSONObject();
2025-03-14 16:02:00 +08:00
param.put("", String.format("{\"mediaCtrol\":%d}", mediaType));
lexMarkDTO.setParam(param.toString());
LexMarkResultDTO<JSONObject> jsonObjectLexMarkResultDTO = this.callDevice(lexMarkDTO, JSONObject.class);
JSONObject data = jsonObjectLexMarkResultDTO.getData();
if (data != null) {
if (!String.valueOf(data.get("result")).equals("0")) {
throw new RRException("设备: " + devNameMap.get(devName) + " 切纸失败!");
}
}
}
}