From bb6bc1f4030cf813e128f021ccecd42420f35628 Mon Sep 17 00:00:00 2001
From: lsh <lsh@163.com>
Date: 星期五, 06 六月 2025 13:38:10 +0800
Subject: [PATCH] *
---
src/main/java/com/zy/core/thread/RgvThread.java | 271 +++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 228 insertions(+), 43 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/RgvThread.java b/src/main/java/com/zy/core/thread/RgvThread.java
index 0be65f8..e60fe8f 100644
--- a/src/main/java/com/zy/core/thread/RgvThread.java
+++ b/src/main/java/com/zy/core/thread/RgvThread.java
@@ -11,6 +11,7 @@
import com.zy.asrs.entity.BasRgvOpt;
import com.zy.asrs.service.BasRgvOptService;
import com.zy.asrs.service.BasRgvService;
+import com.zy.asrs.utils.NumUtils;
import com.zy.asrs.utils.RouteUtils;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
@@ -47,6 +48,7 @@
* 宸ヤ綅2澶嶄綅淇″彿
*/
private boolean resetFlag2 = false;
+ private boolean connectRgv = false;
public RgvThread(RgvSlave slave) {
this.slave = slave;
@@ -55,18 +57,51 @@
@Override
@SuppressWarnings("InfiniteLoopStatement")
public void run() {
- this.connect();
+ connectRgv = this.connect();
+ while(!connectRgv){
+ try {
+ connectRgv = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
+
+ }
+ }
+
+ // 鍚姩绾跨▼鑷姩閲嶈繛
+ new Thread(this::rgvConnect).start();
+
+ // 鍚姩璇绘暟鎹嚎绋�
+ new Thread(this::readStatusRgv).start();
+
+ // 鍚姩浠诲姟涓嬪彂绾跨▼
+ new Thread(this::taskIssued).start();
+ }
+
+
+ /**
+ * 浠诲姟涓嬪彂
+ */
+ private void taskIssued() {
while (true) {
try {
+ if(!connectRgv){
+ try {
+ Thread.sleep(1000L);
+ } catch (Exception e){
+
+ }
+ continue;
+ }
int step = 1;
Task task = MessageQueue.poll(SlaveType.Rgv, slave.getId());
if (task != null) {
step = task.getStep();
}
switch (step) {
- // 璇绘暟鎹�
+ //婕父浠诲姟瀹屾垚淇″彿
case 1:
- readStatus();
+// readStatus();
+ taskComplete();
break;
//宸ヤ綅1鍐欏叆鏁版嵁
case 2:
@@ -84,17 +119,84 @@
command.setTaskMode1(RgvTaskModeType.NONE); // 浠诲姟妯″紡
command.setSourceStaNo1((short)0); // 婧愮珯
command.setDestinationStaNo1((short)0); // 鐩爣绔�
- write(command);
+ write3(command);
+ break;
+ // 澶嶄綅
+ case 4:
+ RgvCommand command4 = (RgvCommand) task.getData();
+ if (null == command4) {
+ command4 = new RgvCommand();
+ }
+ command4.setRgvNo(slave.getId()); // RGV缂栧彿
+ command4.setTaskNo1((short) 0); // 宸ヤ綔鍙�
+ command4.setAckFinish1((short) 1); // 浠诲姟瀹屾垚纭浣�
+ command4.setTaskMode1(RgvTaskModeType.X_MOVE); // 浠诲姟妯″紡
+ command4.setSourceStaNo1((short)0); // 婧愮珯
+ command4.setDestinationStaNo1((short)0); // 鐩爣绔�
+ write3(command4);
+ break;
+ // 婕父
+ case 5:
+ write5((Long) task.getData());
break;
default:
break;
}
- Thread.sleep(500);
+ Thread.sleep(50);
} catch (Exception e) {
+ log.error("RGV鍐欑嚎绋嬪紓甯�"+e.getMessage());
// e.printStackTrace();
}
}
+ }
+
+ private void rgvConnect() {
+ while (true) {
+ try {
+ Thread.sleep(1000);
+ if(!connectRgv){
+ try {
+ connectRgv = this.connect();
+ Thread.sleep(100);
+ } catch (Exception e){
+
+ }
+ }
+ } catch (Exception e) {
+
+ log.error("rgv杩炴帴澶辫触锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ initRgv();
+// e.printStackTrace();
+ }
+ }
+ }
+
+ private void readStatusRgv() {
+ while (true) {
+ try {
+ if(!connectRgv){
+ try {
+ Thread.sleep(1000L);
+ } catch (Exception e){
+
+ }
+ initRgv();
+ continue;
+ }
+ Thread.sleep(40);
+ readStatus();
+
+ } catch (Exception e) {
+ log.error("RGV璇荤嚎绋嬪紓甯�"+e.getMessage());
+
+ log.error("RGV鏁版嵁璇诲彇绾跨▼寮傚父锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ initRgv();
+// e.printStackTrace();
+ }
+
+ }
+
}
/**
@@ -104,6 +206,7 @@
if (null == rgvProtocol) {
rgvProtocol = new RgvProtocol();
}
+ rgvProtocol.setRgvNo(slave.getId());
rgvProtocol.setMode((short) -1);
rgvProtocol.setStatus((short)-1);
rgvProtocol.setTaskNo1((short)0);
@@ -146,11 +249,14 @@
private void readStatus(){
try {
OperateResultExOne<byte[]> result = siemensNet.Read("DB100.0", (short) 20);
- if (result.IsSuccess) {
+ OperateResultExOne<byte[]> resultV = siemensNet.Read("DB20.16", (short) 2);
+ OperateResultExOne<byte[]> resultE = siemensNet.Read("DB20.26", (short) 2);
+ if (result.IsSuccess && resultV.IsSuccess && resultE.IsSuccess) {
if (null == rgvProtocol) {
rgvProtocol = new RgvProtocol();
rgvProtocol.setRgvNo(slave.getId());
}
+ rgvProtocol.setRgvNo(slave.getId());
rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 2));
rgvProtocol.setStartSta(siemensNet.getByteTransform().TransInt16(result.Content, 4));
rgvProtocol.setEndSta(siemensNet.getByteTransform().TransInt16(result.Content, 6));
@@ -159,19 +265,10 @@
rgvProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 12));
rgvProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 14));
rgvProtocol.setRgvPos(siemensNet.getByteTransform().TransUInt32(result.Content, 16));
- rgvProtocol.setRgvPos(slave.getId().longValue()*100000);
+ rgvProtocol.setInstantaneousSpeed(Double.valueOf(siemensNet.getByteTransform().TransInt16(resultV.Content, 0)));
+ rgvProtocol.setEndStaM(siemensNet.getByteTransform().TransInt16(resultE.Content, 0));
+// rgvProtocol.setRgvPos((long)NumUtils.GetRandomIntInRange(1737000));
OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
-
-// // 宸ヤ綅1澶嶄綅淇″彿
-// if (rgvProtocol.getStatusType1().equals(RgvStatusType.WAITING)) {
-// if (resetFlag1) {
-// RgvCommand rgvCommand = new RgvCommand();
-// rgvCommand.setAckFinish1((short)1);
-// if (write(rgvCommand)) {
-// resetFlag1 = false;
-// }
-// }
-// }
try {
// 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
@@ -213,19 +310,22 @@
siemensNet.Write("DB24.10.1", false);
command.setRgvNo(slave.getId());
- short[] array = new short[4];
- array[0] = command.getSourceStaNo1();
- array[1] = command.getDestinationStaNo1();
- array[2] = command.getTaskMode1();//浠诲姟妯″紡
- array[3] = command.getTaskNo1();
-// siemensNet.Write("DB100.20", 0);
+ short[] array = new short[5];
+ array[0] = command.getRgvNo().shortValue();
+ array[1] = command.getSourceStaNo1();
+ array[2] = command.getDestinationStaNo1();
+ array[3] = command.getTaskMode1();//浠诲姟妯″紡
+ array[4] = command.getTaskNo1();
- OperateResult result = siemensNet.Write("DB24.2", array);
+
+
+ OperateResult result = siemensNet.Write("DB24.0", array);
if (command.getAckFinish1().equals((short)0)) {
- boolean commandFinish = true; //宸ヤ綅1浠诲姟鍐欏叆
- Thread.sleep(100L);
- result = siemensNet.Write("DB24.10.0", commandFinish);
+// Thread.sleep(100L);
+ siemensNet.Write("DB24.10.7", command.getRgvSome() == 1);
+ Thread.sleep(20L);
+ result = siemensNet.Write("DB24.10.0", true);
} else {
siemensNet.Write("DB24.10.1", true);
}
@@ -251,8 +351,93 @@
} catch (Exception ignore) {}
if (result != null && result.IsSuccess) {
- Thread.sleep(200);
- this.readStatus();
+// Thread.sleep(200);
+// this.readStatus();
+ log.info("RGV 宸ヤ綅1鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
+ OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅1鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
+ return true;
+ } else {
+ OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆戝啓鍏GV plc宸ヤ綅1鏁版嵁澶辫触 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ log.error("鍐欏叆RGV plc宸ヤ綅1鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ return false;
+ }
+ }
+
+ /**
+ * 瀹屾垚
+ */
+ private void taskComplete() {
+ try {
+ OperateResultExOne<byte[]> result = siemensNet.Read("DB24.11", (short) 1);
+ boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, 0, 1);
+ if (status[0]){
+ OperateResult result4 = siemensNet.Write("DB24.11.0", false);
+ }
+ } catch (Exception e) {
+ log.error("RGV鏁版嵁浠诲姟涓嬪彂澶嶄綅绾跨▼寮傚父锛侊紒锛� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ }
+ }
+
+ private void write5(Long devperimeter){
+ try {
+ siemensNet.Write("DB24.12", devperimeter.intValue());
+ Thread.sleep(10L);
+ siemensNet.Write("DB24.11.0", true);
+
+ } catch (Exception ignore) {
+ log.error("鍐欏叆RGV plc宸ヤ綅1婕父鏁版嵁澶辫触 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ }
+ }
+
+ private boolean write3(RgvCommand command) throws InterruptedException {
+ if (null == command) {
+ log.error("RGV鍐欏叆鍛戒护涓虹┖");
+ return false;
+ }
+
+ siemensNet.Write("DB24.10.0", false);
+ siemensNet.Write("DB24.10.1", false);
+
+ command.setRgvNo(slave.getId());
+ short[] array = new short[5];
+ array[0] = command.getRgvNo().shortValue();
+ array[1] = command.getSourceStaNo1();
+ array[2] = command.getDestinationStaNo1();
+ array[3] = command.getTaskMode1();//浠诲姟妯″紡
+ array[4] = command.getTaskNo1();
+
+// OperateResult result = siemensNet.Write("DB24.0", array);
+ OperateResult result = null;
+ if (command.getAckFinish1().equals((short)0)) {
+ Thread.sleep(20L);
+ result = siemensNet.Write("DB24.10.0", true);
+ } else {
+ siemensNet.Write("DB24.10.1", true);
+ }
+
+ try {
+ // 鏃ュ織璁板綍
+ BasRgvOptService bean = SpringUtils.getBean(BasRgvOptService.class);
+ BasRgvOpt basRgvOpt = new BasRgvOpt(
+ command.getTaskNo1().intValue(),
+ command.getTaskNo2().intValue(),
+ command.getRgvNo(),
+ new Date(),
+ command.getTaskModeType1().toString(),
+ command.getSourceStaNo1().intValue(),
+ command.getDestinationStaNo1().intValue(),
+ command.getSourceStaNo2().intValue(),
+ command.getDestinationStaNo2().intValue(),
+ null,
+ new Date(),
+ null
+ );
+ bean.insert(basRgvOpt);
+ } catch (Exception ignore) {}
+
+ if (result != null && result.IsSuccess) {
+// Thread.sleep(200);
+// this.readStatus();
log.info("RGV 宸ヤ綅1鍛戒护涓嬪彂[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
OutputQueue.RGV.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 宸ヤ綅1鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
return true;
@@ -271,18 +456,18 @@
/******************************************************************************************/
/**************************************** 娴嬭瘯涓撶敤 *****************************************/
/*****************************************************************************************/
- public static void main(String[] args) throws InterruptedException {
- RgvSlave slave = new RgvSlave();
- slave.setId(1);
- slave.setIp("192.168.6.9");
- slave.setRack(0);
- slave.setSlot(0);
- RgvThread rgvThread = new RgvThread(slave);
- rgvThread.connect();
- rgvThread.readStatus();
- System.out.println(JSON.toJSONString(rgvThread.rgvProtocol));
- Thread.sleep(3000L);
-
- }
+// public static void main(String[] args) throws InterruptedException {
+// RgvSlave slave = new RgvSlave();
+// slave.setId(1);
+// slave.setIp("192.168.6.9");
+// slave.setRack(0);
+// slave.setSlot(0);
+// RgvThread rgvThread = new RgvThread(slave);
+// rgvThread.connect();
+// rgvThread.readStatus();
+// System.out.println(JSON.toJSONString(rgvThread.rgvProtocol));
+// Thread.sleep(3000L);
+//
+// }
}
--
Gitblit v1.9.1