2025-02-08 14:45:56 +08:00
|
|
|
|
package com.dpkj.common.utils;
|
|
|
|
|
|
2025-02-08 15:40:47 +08:00
|
|
|
|
import com.alibaba.fastjson.JSON;
|
2025-02-08 17:20:50 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2025-02-08 14:45:56 +08:00
|
|
|
|
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-02-08 17:20:50 +08:00
|
|
|
|
import org.thymeleaf.util.StringUtils;
|
2025-02-08 14:45:56 +08:00
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2025-03-14 16:02:00 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2025-02-08 14:45:56 +08:00
|
|
|
|
|
2025-02-08 15:40:47 +08:00
|
|
|
|
|
2025-02-08 14:45:56 +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<>();
|
|
|
|
|
static {
|
|
|
|
|
devNameMap.put("HtmPrinter", "激光打印机");
|
|
|
|
|
devNameMap.put("ReceiptPrinter", "凭条打印机");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 14:45:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 利盟台式机-立体机 接口请求
|
2025-02-08 17:20:50 +08:00
|
|
|
|
*
|
2025-02-08 14:45:56 +08:00
|
|
|
|
* @param lexMarkDTO 请求DTO
|
2025-03-14 16:02:00 +08:00
|
|
|
|
* @param clazz 返回类型
|
2025-02-08 14:45:56 +08:00
|
|
|
|
* @return result
|
|
|
|
|
*/
|
2025-02-08 17:20:50 +08:00
|
|
|
|
public <T> LexMarkResultDTO<T> callDevice(LexMarkDTO lexMarkDTO, Class<T> clazz) {
|
2025-02-08 14:45:56 +08:00
|
|
|
|
try {
|
|
|
|
|
URL url = new URL(lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice");
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
|
connection.setDoOutput(true);
|
2025-02-08 15:40:47 +08:00
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
2025-02-08 14:45:56 +08:00
|
|
|
|
|
|
|
|
|
// 将LexMarkDTO对象转换为JSON字符串
|
2025-02-08 15:40:47 +08:00
|
|
|
|
String jsonInputString = JSON.toJSONString(lexMarkDTO);
|
2025-02-08 14:45:56 +08:00
|
|
|
|
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
|
|
|
|
|
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
wr.write(input, 0, input.length);
|
2025-02-08 15:40:47 +08:00
|
|
|
|
log.info(jsonInputString);
|
2025-02-08 14:45:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
2025-02-08 17:20:50 +08:00
|
|
|
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
2025-02-08 14:45:56 +08:00
|
|
|
|
log.error("利盟服务请求失败,响应码:{},请求参数:{}", responseCode, lexMarkDTO);
|
|
|
|
|
throw new RRException("利盟服务请求失败,响应码:" + responseCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
String inputLine;
|
|
|
|
|
StringBuilder response = new StringBuilder();
|
2025-02-08 17:20:50 +08:00
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
2025-02-08 14:45:56 +08:00
|
|
|
|
response.append(inputLine);
|
|
|
|
|
}
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
|
|
// 将响应JSON字符串转换为LexMarkResultDTO对象
|
2025-02-08 17:20:50 +08:00
|
|
|
|
LexMarkResultDTO<T> lexMarkResultDTO = JSON.parseObject(response.toString(), LexMarkResultDTO.class);
|
2025-03-19 12:49:35 +08:00
|
|
|
|
// if (lexMarkResultDTO.getResult() != 0) {
|
|
|
|
|
// log.error("利盟服务请求出错:{}", lexMarkResultDTO);
|
|
|
|
|
// throw new RRException(lexMarkResultDTO.toString());
|
|
|
|
|
// }
|
2025-02-08 15:40:47 +08:00
|
|
|
|
|
2025-02-08 17:20:50 +08:00
|
|
|
|
// 实例化param位data
|
|
|
|
|
String param = lexMarkResultDTO.getParam();
|
|
|
|
|
if (!StringUtils.isEmpty(param)) {
|
|
|
|
|
try {
|
|
|
|
|
T t = JSON.parseObject(param, clazz);
|
|
|
|
|
lexMarkResultDTO.setData(t);
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("类型转换失败");
|
|
|
|
|
throw new RRException("转换param位data时,类型与实际类型不匹配");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 15:40:47 +08:00
|
|
|
|
return lexMarkResultDTO;
|
2025-02-08 17:20:50 +08:00
|
|
|
|
} catch (Exception e) {
|
2025-02-08 14:45:56 +08:00
|
|
|
|
log.error("利盟服务请求失败:{}", lexMarkDTO, e);
|
2025-02-08 17:20:50 +08:00
|
|
|
|
if (e instanceof RRException) {
|
2025-02-08 15:40:47 +08:00
|
|
|
|
throw new RRException(((RRException) e).getCode(), e.getMessage());
|
|
|
|
|
}
|
2025-02-08 14:45:56 +08:00
|
|
|
|
throw new RRException("利盟服务请求失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 17:20:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据对象的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
|
|
|
|
/**
|
|
|
|
|
* 打开设备连接或者 链接+初始化
|
|
|
|
|
* @param devName 设备名称
|
|
|
|
|
* @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");
|
|
|
|
|
JSONObject param = new JSONObject();
|
|
|
|
|
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) + " 打开失败!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打开后直接进行重置
|
|
|
|
|
if ( resterType != null && data != null) {
|
|
|
|
|
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 + " 初始化失败!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关闭设备练级
|
|
|
|
|
* @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打印机都是可以自动切纸的
|
|
|
|
|
* @param devName 设备名称
|
|
|
|
|
* @param actionName action名称,实际的动作函数名称
|
|
|
|
|
* @param mediaType 媒介类型
|
|
|
|
|
*/
|
|
|
|
|
public void cutPaper(String devName, String actionName, Integer mediaType){
|
|
|
|
|
LexMarkDTO lexMarkDTO = new LexMarkDTO();
|
|
|
|
|
lexMarkDTO.setActionName(actionName);
|
|
|
|
|
lexMarkDTO.setCallID(19283);
|
|
|
|
|
lexMarkDTO.setDevName(devName);
|
|
|
|
|
lexMarkDTO.setPluginMethod("exec");
|
|
|
|
|
JSONObject param = new JSONObject();
|
|
|
|
|
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) + " 切纸失败!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-08 14:45:56 +08:00
|
|
|
|
}
|
|
|
|
|
|