JAVA集成-精臣打印机打印取件码
简述:通过扫码枪进行扫描传参或者模拟调用接口都可;
代码可修改;
@Slf4j
@Component
public class SocketConfig {
private static String billNo;
private static String code;
public static void setBillNo(String billNo) {
SocketConfig.billNo = billNo;
}
public static void setCode(String code) {
SocketConfig.code = code;
}
@Bean
public WebSocketClient pickSocketClient() {
try {
WebSocketClient client = new WebSocketClient(new URI("ws://127.0.0.1:端口")) {
@Override
public void onOpen(ServerHandshake handshakedata) {
log.info("[取件码-websocket] 连接成功");
send("{\"apiName\":\"getAllPrinters\"}"); //获取所有打印机名
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String message) {
log.info("[websocket] 收到消息={}", message);
JSONObject data = JSONObject.parseObject(message);
String apiName = data.getString("apiName");
if (apiName.equals("getAllPrinters")) {
int port = Integer.parseInt(data.getJSONObject("resultAck").getJSONObject("info").getString("自己的打印机型号"));
send("{\"apiName\":\"selectPrinter\",\"parameter\":{\"printerName\":\"自己的打印机型号\",\"port\":" + port + "}}"); //选择打印机
send("{\"apiName\":\"initSdk\",\"parameter\":{\"fontDir\":\"\"}}"); //初始化
} else if (apiName.equals("commitJob")) {
JSONObject resultAck = data.getJSONObject("resultAck");
if (resultAck.containsKey("onPrintPageCompleted")) {
send("{\"apiName\":\"endJob\"}");
//释放线程
ApiController.commitJobInProgress.set(false);
} else if (resultAck.containsKey("cacheStatus") && resultAck.getString("cacheStatus").equals("cancel")) {
send("{\"apiName\":\"startJob\",\"parameter\":{\"printDensity\":3,\"printLabelType\":1,\"printMode\":1,\"count\":1}}");
}
} else if (apiName.equals("startJob")) {
if (data.getJSONObject("resultAck").getInteger("errorCode") != 0) {
//提交任务返回错误重新提交一次
send("{\"apiName\":\"startJob\",\"parameter\":{\"printDensity\":3,\"printLabelType\":1,\"printMode\":1,\"count\":1}}");
} else {
//初始化画布
send("{\"apiName\":\"InitDrawingBoard\",\"parameter\":{\"width\":40,\"height\":30,\"rotate\":0,\"path\":\"ZT001.ttf\",\"verticalShift\":0,\"HorizontalShift\":0}}");
}
} else if (apiName.equals("InitDrawingBoard")) {
if (data.getJSONObject("resultAck").getInteger("errorCode") == 0) {
//画布回调成功,发送
send("{\"apiName\":\"DrawLableText\",\"parameter\":{\"x\":1,\"y\":2,\"height\":6,\"width\":36,\"value\":\"物流单号:\",\"fontFamily\":\"宋体\",\"rotate\":0,\"fontSize\":4,\"textAlignHorizonral\":0,\"textAlignVertical\":1,\"letterSpacing\":0,\"lineSpacing\":1,\"lineMode\":6,\"fontStyle\":[false,false,false,false]}}");
send("{\"apiName\":\"DrawLableText\",\"parameter\":{\"x\":1,\"y\":9,\"height\":6,\"width\":36,\"value\":\"" + billNo + "\",\"fontFamily\":\"宋体\",\"rotate\":0,\"fontSize\":4,\"textAlignHorizonral\":0,\"textAlignVertical\":1,\"letterSpacing\":0,\"lineSpacing\":1,\"lineMode\":6,\"fontStyle\":[false,false,false,false]}}");
send("{\"apiName\":\"DrawLableText\",\"parameter\":{\"x\":1,\"y\":16,\"height\":6,\"width\":36,\"value\":\"取件码:\",\"fontFamily\":\"宋体\",\"rotate\":0,\"fontSize\":4,\"textAlignHorizonral\":0,\"textAlignVertical\":1,\"letterSpacing\":0,\"lineSpacing\":1,\"lineMode\":6,\"fontStyle\":[false,false,false,false]}}");
send("{\"apiName\":\"DrawLableText\",\"parameter\":{\"x\":1,\"y\":23,\"height\":6,\"width\":36,\"value\":\"" + code + "\",\"fontFamily\":\"宋体\",\"rotate\":0,\"fontSize\":5,\"textAlignHorizonral\":0,\"textAlignVertical\":1,\"letterSpacing\":0,\"lineSpacing\":1,\"lineMode\":6,\"fontStyle\":[false,false,false,false]}}");
send("{\"apiName\":\"commitJob\",\"parameter\":{\"printData\":null,\"printerImageProcessingInfo\":{\"printQuantity\":1}}}");
}
}
}
@Override
public void onClose(int code, String reason, boolean remote) {
log.info("[websocket] 退出连接");
}
@Override
public void onError(Exception ex) {
log.info("[websocket] 连接错误={}", ex.getMessage());
}
};
client.connect();
return client;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@Data
public class MakeParamDTO implements Serializable {
//物流单号
private String billNo;
//取件码
private String code;
}
调用打印机:
@Resource
private final WebSocketClient pickUpCodeClient;
public final static AtomicBoolean commitJobInProgress = new AtomicBoolean(false);//用来检测上一个提交的任务有没有打印完成,进行阻塞
public static final LinkedList<MakeParamDTO> dtos = new LinkedList<>();//将数据先保存起来,先进先出
@PostMapping("/handleCode")
public void handleCode(@RequestBody MakeParamDTO dto) {
log.info("取件码回调成功");
dtos.add(dto);
while (commitJobInProgress.get()) {
try {
Thread.sleep(100); // 等待一段时间后重试
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("线程断开了");
}
}
log.info("线程名: " + Thread.currentThread().getName());
commitJobInProgress.set(true);
MakeParamDTO first = dtos.getFirst();
SocketConfig.setBillNo(first.getBillNo());
SocketConfig.setCode(first.getCode());
dtos.removeFirst();
//提交打印任务
pickUpCodeClient.send("{\"apiName\":\"startJob\",\"parameter\":{\"printDensity\":3,\"printLabelType\":1,\"printMode\":1,\"count\":1}}");
log.info("取件码回调结束");
}
原文地址:https://blog.csdn.net/m0_47460678/article/details/142984271
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!