From dfaf0332889a83f9c8437befdfff32866cf29d5e Mon Sep 17 00:00:00 2001
From: yxFwq <1>
Date: 星期五, 13 六月 2025 15:16:12 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/JarThread.java | 160 +++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 125 insertions(+), 35 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/JarThread.java b/src/main/java/com/zy/core/thread/JarThread.java
index c0e3e55..3f3fdc2 100644
--- a/src/main/java/com/zy/core/thread/JarThread.java
+++ b/src/main/java/com/zy/core/thread/JarThread.java
@@ -40,6 +40,8 @@
private JarSlave slave;
private JarProtocol jarProtocol;
private short heartBeatVal = 1;
+ private boolean connectJar = false;
+
public JarThread(JarSlave slave) {
this.slave = slave;
@@ -48,7 +50,33 @@
@Override
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
- this.connect();
+
+ connectJar = this.connect();
+ while(!connectJar){
+ try {
+ connectJar = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
+
+ }
+ }
+
+ // 鍚姩绾跨▼鑷姩閲嶈繛
+ new Thread(this::jarConnect).start();
+
+ // 鍚姩璇绘暟鎹嚎绋�
+ new Thread(this::readStatusJar).start();
+
+ // 鍚姩浠诲姟涓嬪彂绾跨▼
+ new Thread(this::taskIssued).start();
+
+
+ }
+
+ /**
+ * 浠诲姟涓嬪彂
+ */
+ private void taskIssued() {
while (true) {
try {
int step = 1;
@@ -59,7 +87,7 @@
switch (step) {
// 璇绘暟鎹�
case 1:
- readStatus();
+// readStatus();
break;
// 鍐欐暟鎹� ID+鐩爣绔�
case 2:
@@ -70,46 +98,96 @@
}
// 蹇冭烦
// heartbeat();
- Thread.sleep(500);
+ Thread.sleep(200);
} catch (Exception e) {
- e.printStackTrace();
+ log.error("JAR鍐欑嚎绋嬪紓甯�"+e.getMessage());
+
+// e.printStackTrace();
}
}
+ }
+
+ private void jarConnect() {
+ while (true) {
+ try {
+ Thread.sleep(1000);
+ if(!connectJar){
+ try {
+ connectJar = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
+
+ }
+ }
+ } catch (Exception e) {
+ log.error("jar杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ initSte();
+// e.printStackTrace();
+ }
+ }
+ }
+
+ private void readStatusJar() {
+ while (true) {
+ try {
+ Thread.sleep(50);
+// System.out.println("璇荤嚎绋�"+ slave.getId());
+
+ readStatus();
+
+ } catch (Exception e) {
+ log.error("JAR鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+
+ initSte();
+// e.printStackTrace();
+ }
+
+ }
+
}
/**
* 鍒濆鍖栧爢鍨涙満鐘舵��
*/
private void initSte() {
- if (null == jarProtocol) {
- jarProtocol = new JarProtocol();
+ try{
+ if (null == jarProtocol) {
+ jarProtocol = new JarProtocol();
+ }
+ jarProtocol.setJarNo(slave.getId());
+ jarProtocol.setMode( 0);
+ jarProtocol.setStatus(JarStatusType.OFF_LINE);
+ jarProtocol.setJarErr(0);
+ } catch (Exception e){
+ log.error("JAR鍒濆鍖栧紓甯革紒锛侊紒 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
}
- jarProtocol.setJarNo(slave.getId());
- jarProtocol.setMode( 0);
- jarProtocol.setStatus(JarStatusType.OFF_LINE);
- jarProtocol.setJarErr(0);
}
@Override
public boolean connect() {
boolean result = false;
- // 涓嶈兘鏈� 妲藉彿 鍜� 鏈烘灦鍙�
- siemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart, slave.getIp());
+
+ try{
+ // 涓嶈兘鏈� 妲藉彿 鍜� 鏈烘灦鍙�
+ siemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart, slave.getIp());
// siemensS7Net.setRack(slave.getRack().byteValue());
// siemensS7Net.setSlot(slave.getSlot().byteValue());
- OperateResult connect = siemensS7Net.ConnectServer();
- if(connect.IsSuccess){
- result = true;
- OutputQueue.STE.offer(MessageFormat.format( "銆恵0}銆戠~鍖栫綈plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
- News.info("纭寲缃恜lc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
- } else {
- OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆戠~鍖栫綈plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
- News.error("纭寲缃恜lc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
- initSte();
+ OperateResult connect = siemensS7Net.ConnectServer();
+ if(connect.IsSuccess){
+ result = true;
+ OutputQueue.STE.offer(MessageFormat.format( "銆恵0}銆戠~鍖栫綈plc杩炴帴鎴愬姛 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ News.info("纭寲缃恜lc杩炴帴鎴愬姛 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ } else {
+ OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆戠~鍖栫綈plc杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ News.error("纭寲缃恜lc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ initSte();
+ }
+ } catch (Exception e){
+ log.error("纭寲缃恜lc杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
}
-// melsecMcNet.ConnectClose();
return result;
+// melsecMcNet.ConnectClose();
}
/**
@@ -201,25 +279,37 @@
jarProtocol.setOpenDoorSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus()));
} else if (jarProtocol.statusType .equals(JarStatusType.WAITING5) || jarProtocol.statusType .equals(JarStatusType.OFF_LINE)){
jarProtocol.setCloseDoorSign(!jarProtocol.getStatus().equals(jarProtocol.getUpStatus()));
- } else if (jarProtocol.statusType .equals(JarStatusType.WAITING1) || jarProtocol.statusType .equals(JarStatusType.MOVING)){
- if (!jarProtocol.rightDoorOpen.equals(jarProtocol.leftDoorOpen)) {
- if (jarProtocol.rightDoorOpen == 0 || jarProtocol.leftDoorOpen == 0){
- if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){
- jarProtocol.setLeftDoorOpen(0);//open the left door //杩涙枡闂�
- jarProtocol.setRightDoorOpen(0);//open the right door //鍑烘枡闂�
- }
+ } else if (jarProtocol.leftDoorOpen==1 || jarProtocol.leftDoorClose==1 ||
+ jarProtocol.rightDoorOpen==1 || jarProtocol.rightDoorClose==1){
+
+ } else {
+ if (jarProtocol.statusType .equals(JarStatusType.WAITING2)){
+
+ if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){
+ jarProtocol.setLeftDoorOpen(0);//open the left door //杩涙枡闂�
+ jarProtocol.setRightDoorOpen(0);//open the right door //鍑烘枡闂�
}
- }
- if (jarProtocol.rightDoorClose == 0 || jarProtocol.leftDoorClose == 0){
if (jarProtocol.rightDoorClose == 2 || jarProtocol.leftDoorClose == 2){
jarProtocol.setLeftDoorClose(0);//close the left door //杩涙枡闂�
jarProtocol.setRightDoorClose(0);//close the right door //鍑烘枡闂�
}
+ } else if (jarProtocol.statusType .equals(JarStatusType.WAITING1) || jarProtocol.statusType .equals(JarStatusType.MOVING)){
+ if (!jarProtocol.rightDoorOpen.equals(jarProtocol.leftDoorOpen)) {
+ if (jarProtocol.rightDoorOpen == 0 || jarProtocol.leftDoorOpen == 0){
+ if (jarProtocol.rightDoorOpen == 2 || jarProtocol.leftDoorOpen == 2){
+ jarProtocol.setLeftDoorOpen(0);//open the left door //杩涙枡闂�
+ jarProtocol.setRightDoorOpen(0);//open the right door //鍑烘枡闂�
+ }
+ }
+ }
+ if (jarProtocol.rightDoorClose == 0 || jarProtocol.leftDoorClose == 0){
+ if (jarProtocol.rightDoorClose == 2 || jarProtocol.leftDoorClose == 2){
+ jarProtocol.setLeftDoorClose(0);//close the left door //杩涙枡闂�
+ jarProtocol.setRightDoorClose(0);//close the right door //鍑烘枡闂�
+ }
+ }
}
}
-
-
-
jarProtocol.setUpStatus(jarProtocol.getStatus());
//
@@ -248,7 +338,7 @@
}
} catch (Exception e) {
// e.printStackTrace();
- log.error(e.getMessage());
+ log.error("纭寲缃恜lc鐘舵�佷俊鎭け璐�"+e.getMessage());
OutputQueue.STE.offer(MessageFormat.format("銆恵0}銆戣鍙栫~鍖栫綈plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
// News.error("璇诲彇绌挎杞lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
initSte();
--
Gitblit v1.9.1