From 96f987b030f4c961a985f07079ba12abd865fdb2 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 17 十二月 2025 08:12:24 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java | 67 +++++++++++++++++++++++++++++----
1 files changed, 59 insertions(+), 8 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
index ee135e2..d8decf5 100644
--- a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
@@ -5,9 +5,11 @@
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnp;
+import com.zy.asrs.entity.BasCrnpOpt;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.service.BasCrnpService;
+import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
@@ -41,7 +43,10 @@
private RedisUtil redisUtil;
private ZyCrnConnectDriver zyCrnConnectDriver;
private CrnProtocol crnProtocol;
+ private int deviceLogCollectTime = 200;
private boolean resetFlag = false;
+ private volatile boolean closed = false;
+ private Thread mainThread;
public ZySiemensCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
this.deviceConfig = deviceConfig;
@@ -53,15 +58,16 @@
public void run() {
this.connect();
this.initCrn();
- while (true) {
+ mainThread = Thread.currentThread();
+ while (!closed && !Thread.currentThread().isInterrupted()) {
try {
+ deviceLogCollectTime = Utils.getDeviceLogCollectTime();
int step = 1;
Task task = MessageQueue.poll(SlaveType.Crn, deviceConfig.getDeviceNo());
if (task != null) {
step = task.getStep();
}
switch (step) {
- // 璇绘暟鎹�
case 1:
readStatus();
break;
@@ -72,6 +78,9 @@
break;
}
Thread.sleep(200);
+ } catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ break;
} catch (Exception e) {
e.printStackTrace();
}
@@ -108,7 +117,7 @@
@Override
public boolean connect() {
zyCrnConnectDriver = new ZyCrnConnectDriver(deviceConfig);
- new Thread(zyCrnConnectDriver).start();
+ zyCrnConnectDriver.start();
DeviceConnectPool.put(SlaveType.Crn, deviceConfig.getDeviceNo(), zyCrnConnectDriver);
return true;
}
@@ -120,7 +129,6 @@
ZyCrnStatusEntity crnStatus = zyCrnConnectDriver.getStatus();
if (crnStatus == null) {
OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戣鍙栧爢鍨涙満plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
- News.error("SiemensCrn"+" - 5"+" - 璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
return;
}
@@ -170,7 +178,7 @@
crnProtocol.setLastCommandTime(System.currentTimeMillis());
}
- if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > 200) {
+ if (System.currentTimeMillis() - crnProtocol.getDeviceDataLog() > deviceLogCollectTime) {
//淇濆瓨鏁版嵁璁板綍
DeviceDataLog deviceDataLog = new DeviceDataLog();
deviceDataLog.setOriginData(JSON.toJSONString(crnStatus));
@@ -184,13 +192,22 @@
crnProtocol.setDeviceDataLog(System.currentTimeMillis());
}
- BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
+ BasCrnpService basCrnpService = null;
+ try {
+ basCrnpService = SpringUtils.getBean(BasCrnpService.class);
+ }catch (Exception e){
+
+ }
if (basCrnpService != null) {
BasCrnp basCrnp = basCrnpService.selectOne(new EntityWrapper<BasCrnp>().eq("crn_no", deviceConfig.getDeviceNo()));
if(basCrnp == null) {
basCrnp = new BasCrnp();
basCrnp.setCrnNo(deviceConfig.getDeviceNo());
basCrnp.setStatus(1);
+ basCrnp.setInEnable("N");
+ basCrnp.setOutEnable("N");
+ basCrnp.setMaxInTask(5);
+ basCrnp.setMaxOutTask(5);
basCrnp.setCreateTime(new Date());
basCrnpService.insert(basCrnp);
}
@@ -199,7 +216,14 @@
@Override
public void close() {
- zyCrnConnectDriver.close();
+ closed = true;
+ Thread t = mainThread;
+ if (t != null) {
+ try { t.interrupt(); } catch (Exception ignore) {}
+ }
+ if (zyCrnConnectDriver != null) {
+ zyCrnConnectDriver.close();
+ }
}
@Override
@@ -257,6 +281,33 @@
@Override
public synchronized CommandResponse sendCommand(CrnCommand command) {
this.crnProtocol.setLastCommandTime(System.currentTimeMillis());
- return zyCrnConnectDriver.sendCommand(command);
+ CommandResponse response = null;
+ try {
+ response = zyCrnConnectDriver.sendCommand(command);
+ return response;
+ } finally {
+ String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
+ String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
+ BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
+ ZyCrnStatusEntity statusEntity = zyCrnConnectDriver.getStatus();
+ BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
+ command.getTaskNo().intValue(),
+ command.getCrnNo(),
+ new Date(),
+ String.valueOf(command.getTaskMode()),
+ sourceLocNo,
+ targetLocNo,
+ null,
+ null,
+ null,
+ JSON.toJSONString(command),
+ JSON.toJSONString(statusEntity),
+ 1,
+ JSON.toJSONString(response)
+ );
+ if (bean != null) {
+ bean.insert(basCrnpOpt);
+ }
+ }
}
}
--
Gitblit v1.9.1