激光打印机调用修改

This commit is contained in:
2025-07-01 15:07:16 +08:00
parent 4d888fb8a3
commit 28aa307f8b
7 changed files with 137 additions and 129 deletions

View File

@@ -1,10 +1,8 @@
package com.dpkj.common.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
@@ -16,10 +14,9 @@ import java.io.Serializable;
* @since 2025-02-08 11:03:06
*/
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class LexMarkResultDTO <T> implements Serializable {
@Accessors(chain = true)
public class LexMarkResultDTO<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
@@ -35,12 +32,12 @@ public class LexMarkResultDTO <T> implements Serializable {
/**
* 对应发送请求的callID。
*/
private int callID;
private Integer callID;
/**
* 错误码0表示成功其他表示失败比如-4表示取消-48表示超时-14表示硬件故障
*/
private int result;
private Integer result;
/**
* 发送请求中的actionName
@@ -63,53 +60,5 @@ public class LexMarkResultDTO <T> implements Serializable {
*/
private T data;
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Param {
/**
* 请求ID
*/
private int RequestID;
/**
* 命令编码
*/
private int dwCommandCode;
/**
* 事件名由发送请求中的actionName+Over组成。
*/
private String eventName;
/**
* 事件类型编码
*/
private int eventType;
/**
* 服务
*/
private int hService;
/**
* 对应发送请求中的devName
*/
private String cmdName;
/**
* 错误码0表示成功其他表示失败比如-4表示取消-48表示超时-14表示硬件故障
*/
private int result;
/**
* 详情描述
*/
private String desc;
}
}

View File

@@ -1,6 +1,7 @@
package com.dpkj.common.utils;
import com.alibaba.fastjson.JSON;
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;
@@ -8,14 +9,7 @@ import com.dpkj.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;
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;
import java.util.HashMap;
import java.util.Map;
@@ -37,68 +31,48 @@ public class ThirdService {
private String lexMarkServicePort;
private static final Map<String, String> devNameMap = new HashMap<>();
static {
devNameMap.put("HtmPrinter", "激光打印机");
devNameMap.put("ReceiptPrinter", "凭条打印机");
}
/**
* 利盟台式机-立体机 接口请求
*
* @param lexMarkDTO 请求DTO
* @param clazz 返回类型
* @param clazz 返回类型
* @return result
*/
public <T> LexMarkResultDTO<T> callDevice(LexMarkDTO lexMarkDTO, Class<T> clazz) {
try {
URL url = new URL(lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
String urlStr = lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice";
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-url{}", urlStr);
// 将LexMarkDTO对象转换为JSON字符串
String jsonInputString = JSON.toJSONString(lexMarkDTO);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
wr.write(input, 0, input.length);
log.info(jsonInputString);
}
String params = JSONObject.toJSONString(lexMarkDTO);
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-入参:{}", params);
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
log.error("利盟服务请求失败,响应码:{},请求参数:{}", responseCode, lexMarkDTO);
throw new RRException("利盟服务请求失败,响应码:" + responseCode);
}
String bodyStr = HttpRequest.post(urlStr)
.header("Content-Type", "application/json")
.body(params)
.timeout(30000)
.execute()
.body();
log.info("[ThirdService][callDevice][激光打印机打印] 利盟打印接口-响应:{}", bodyStr);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
JSONObject bodyJson = (JSONObject) JSONObject.parse(bodyStr);
if (StrUtil.isEmpty(bodyJson.getString("param"))) {
throw new RRException("利盟服务请求失败,结果响应为空");
}
in.close();
// 将响应JSON字符串转换为LexMarkResultDTO对象
LexMarkResultDTO<T> lexMarkResultDTO = JSON.parseObject(response.toString(), LexMarkResultDTO.class);
// if (lexMarkResultDTO.getResult() != 0) {
// log.error("利盟服务请求出错:{}", lexMarkResultDTO);
// throw new RRException(lexMarkResultDTO.toString());
// }
LexMarkResultDTO obj = JSONObject.toJavaObject(bodyJson, LexMarkResultDTO.class);
// 实例化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时类型与实际类型不匹配");
}
}
T paramObj = JSONObject.toJavaObject(bodyJson.getJSONObject("param"), clazz);
obj.setData(paramObj);
return lexMarkResultDTO;
return obj;
} catch (Exception e) {
log.error("利盟服务请求失败:{}", lexMarkDTO, e);
if (e instanceof RRException) {
@@ -121,7 +95,8 @@ public class ThirdService {
/**
* 打开设备连接或者 链接+初始化
* @param devName 设备名称
*
* @param devName 设备名称
* @param resterType 初始化类型,如果不设置,那么就不会进行初始化
*/
public void open(String devName, Integer resterType) {
@@ -131,7 +106,7 @@ public class ThirdService {
lexMarkDTO.setCallID(19283);
lexMarkDTO.setDevName(devName);
lexMarkDTO.setPluginMethod("exec");
JSONObject param = new JSONObject();
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);
@@ -143,7 +118,7 @@ public class ThirdService {
}
// 打开后直接进行重置
if ( resterType != null && data != null) {
if (resterType != null && data != null) {
lexMarkDTO.setActionName("Reset");
JSONObject jsonObject = new JSONObject();
jsonObject.put("", String.format("{\"ResetAction\":%d,\"binNumber\":0}", resterType));
@@ -160,6 +135,7 @@ public class ThirdService {
/**
* 关闭设备练级
*
* @param devName 设备名称
*/
public void close(String devName) {
@@ -179,17 +155,18 @@ public class ThirdService {
/**
* 切纸但是目前凭条和ms439打印机都是可以自动切纸的
* @param devName 设备名称
*
* @param devName 设备名称
* @param actionName action名称实际的动作函数名称
* @param mediaType 媒介类型
* @param mediaType 媒介类型
*/
public void cutPaper(String devName, String actionName, Integer 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();
JSONObject param = new JSONObject();
param.put("", String.format("{\"mediaCtrol\":%d}", mediaType));
lexMarkDTO.setParam(param.toString());