From b99858aa6d24d6e5f0c300701bcf3f5fa8f05a23 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@63.com>
Date: 星期三, 29 十二月 2021 16:22:58 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/CrnThread.java | 146 ++++++++++++++++++++++++++++++++----------------
1 files changed, 97 insertions(+), 49 deletions(-)
diff --git a/src/main/java/com/zy/core/thread/CrnThread.java b/src/main/java/com/zy/core/thread/CrnThread.java
index 4d25cfd..868a52f 100644
--- a/src/main/java/com/zy/core/thread/CrnThread.java
+++ b/src/main/java/com/zy/core/thread/CrnThread.java
@@ -7,11 +7,14 @@
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
+import com.zy.asrs.entity.BasCrnOpt;
import com.zy.asrs.entity.BasCrnp;
+import com.zy.asrs.service.BasCrnOptService;
import com.zy.asrs.service.BasCrnpService;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
+import com.zy.core.enums.CrnStatusType;
import com.zy.core.enums.CrnTaskModeType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.CrnSlave;
@@ -35,6 +38,7 @@
private MelsecMcNet melsecMcNet;
private CrnSlave slave;
private CrnProtocol crnProtocol;
+ private boolean resetFlag = false;
public CrnThread(CrnSlave slave) {
this.slave = slave;
@@ -47,7 +51,7 @@
while (true) {
try {
int step = 1;
- Task task = MessageQueue.poll(SlaveType.Devp, slave.getId());
+ Task task = MessageQueue.poll(SlaveType.Crn, slave.getId());
if (task != null) {
step = task.getStep();
}
@@ -111,13 +115,13 @@
*/
private void readStatus(){
try {
- if (null == crnProtocol) {
- crnProtocol = new CrnProtocol();
- }
/* 鏂规硶1 */
- OperateResultExOne<byte[]> result = melsecMcNet.Read("D2231", (short) 62);
+ OperateResultExOne<byte[]> result = melsecMcNet.Read("D2231", (short) 70);
if (result.IsSuccess) {
+ if (null == crnProtocol) {
+ crnProtocol = new CrnProtocol();
+ }
crnProtocol.setMode(melsecMcNet.getByteTransform().TransInt16(result.Content, 0));
crnProtocol.setTaskNo(melsecMcNet.getByteTransform().TransInt16(result.Content, 4));
crnProtocol.setStatus(melsecMcNet.getByteTransform().TransInt16(result.Content, 6));
@@ -140,10 +144,32 @@
crnProtocol.setXDistance(melsecMcNet.getByteTransform().TransSingle(result.Content, 46));
crnProtocol.setYDistance(melsecMcNet.getByteTransform().TransSingle(result.Content, 50));
crnProtocol.setXDuration(melsecMcNet.getByteTransform().TransSingle(result.Content, 54));
- crnProtocol.setYDuration(melsecMcNet.getByteTransform().TransSingle(result.Content, 58));
+ crnProtocol.setYDuration(melsecMcNet.getByteTransform().TransSingle(result.Content, 62));
+
+ OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
+
+ // 澶嶄綅淇″彿
+ if (crnProtocol.getStatusType().equals(CrnStatusType.WAITING)) {
+ log.error("-------------------------------------------绗竴姝ャ�乕鍫嗗灈鏈哄彿锛歿}][宸ヤ綔鍙凤細{}]==>> 鐘舵�佷负90锛岀瓑寰呯‘璁わ紒锛�",slave.getId(),crnProtocol.getTaskNo());
+ if (resetFlag) {
+ if (melsecMcNet.Write("D2218", (short) 1).IsSuccess) {
+ resetFlag = false;
+ }
+ }
+ }
+
+ // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
+ BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
+ BasCrnp basCrnp = new BasCrnp();
+ basCrnp.setCrnErr(crnProtocol.getAlarm()==null?0:crnProtocol.getAlarm().longValue());
+ basCrnp.setCrnNo(slave.getId());
+ if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
+ log.error("鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
+ }
} else {
- throw new CoolException("鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐�");
+ OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆憑1}鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐�",DateUtils.convert(new Date()), slave.getId()));
+ throw new CoolException(MessageFormat.format( "鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
}
/* 鏂规硶2 */
// crnProtocol.setMode(melsecMcNet.ReadInt16("D2231").Content);
@@ -173,17 +199,6 @@
e.printStackTrace();
OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆戣鍙栧爢鍨涙満plc鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
log.error("璇诲彇鍫嗗灈鏈簆lc鐘舵�佷俊鎭け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
- return;
- }
-
- OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), slave.getId()));
-
- // 鏍规嵁瀹炴椂淇℃伅鏇存柊鏁版嵁搴�
- BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
- BasCrnp basCrnp = new BasCrnp();
- basCrnp.setCrnNo(slave.getId());
- if (basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
- log.error("鍫嗗灈鏈簆lc鏁版嵁搴撴洿鏂板け璐� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
}
}
@@ -199,17 +214,58 @@
command.setCrnNo(slave.getId());
/* 鏂规硶 1 */
do {
- if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2216", command.getTaskMode()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2214", command.getSourcePosX()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2212", command.getSourcePosY()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2210", command.getSourcePosZ()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2208", command.getDestinationPosX()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2204", command.getDestinationPosY()).IsSuccess) { break; }
- if (!melsecMcNet.Write("D2202", command.getDestinationPosZ()).IsSuccess) { break; }
+ if (command.getDestinationPosZ() != null) {
+ if (!melsecMcNet.Write("D2202", command.getDestinationPosZ()).IsSuccess) { break; }
+ }
+ if (command.getDestinationPosY() != null) {
+ if (!melsecMcNet.Write("D2204", command.getDestinationPosY()).IsSuccess) { break; }
+ }
+ if (command.getDestinationPosX() != null) {
+ if (!melsecMcNet.Write("D2208", command.getDestinationPosX()).IsSuccess) { break; }
+ }
+ if (command.getSourcePosZ() != null) {
+ if (!melsecMcNet.Write("D2210", command.getSourcePosZ()).IsSuccess) { break; }
+ }
+ if (command.getSourcePosY() != null) {
+ if (!melsecMcNet.Write("D2212", command.getSourcePosY()).IsSuccess) { break; }
+ }
+ if (command.getDestinationPosX() != null) {
+ if (!melsecMcNet.Write("D2214", command.getSourcePosX()).IsSuccess) { break; }
+ }
+ if (command.getTaskMode() != null) {
+ if (!melsecMcNet.Write("D2216", command.getTaskMode()).IsSuccess) { break; }
+ }
+ if (command.getTaskNo() != null) {
+ if (!melsecMcNet.Write("D2217", command.getTaskNo()).IsSuccess) { break; }
+ }
+ if (command.getAckFinish() != null) {
+ if (!melsecMcNet.Write("D2218", command.getAckFinish()).IsSuccess) { break; }
+ }
- System.out.println(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
+ try {
+ // 鏃ュ織璁板綍
+ BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
+ BasCrnOpt basCrnOpt = new BasCrnOpt(
+ command.getTaskNo().intValue(), // 浠诲姟鍙�
+ command.getCrnNo(), // 鍫嗗灈鏈篬闈炵┖]
+ new Date(), // 涓嬪彂鏃堕棿
+ command.getTaskModeType().toString(), // 妯″紡
+ command.getSourcePosX().intValue(), // 婧愭帓
+ command.getSourcePosY().intValue(), // 婧愬垪
+ command.getSourcePosZ().intValue(), // 婧愬眰
+ null, // 婧愮珯
+ command.getDestinationPosX().intValue(), // 鐩爣鎺�
+ command.getDestinationPosY().intValue(), // 鐩爣鍒�
+ command.getDestinationPosZ().intValue(), // 鐩爣灞�
+ null, // 鐩爣绔�
+ null, // 鍝嶅簲缁撴灉
+ null, // 淇敼鏃堕棿
+ null // 淇敼浜哄憳
+ );
+ bean.insert(basCrnOpt);
+ } catch (Exception ignore) {}
+
+ log.info("鍫嗗灈鏈哄懡浠や笅鍙慬id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
OutputQueue.CRN.offer(MessageFormat.format("銆恵0}銆慬id:{1}] >>>>> 鍛戒护涓嬪彂锛� {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
return;
@@ -240,13 +296,6 @@
melsecMcNet.ConnectClose();
}
- public CrnProtocol getCrnProtocol(){
- if (crnProtocol == null) {
- readStatus();
- }
- return this.crnProtocol;
- }
-
/******************************************************************************************/
/**************************************** 娴嬭瘯涓撶敤 *****************************************/
@@ -262,7 +311,6 @@
crnThread.connect();
crnThread.readStatus();
System.out.println(JSON.toJSONString(crnThread.crnProtocol));
- Thread.sleep(3000L);
// 1.鍏ュ簱 婧愬拰鐩爣閮藉彂
// CrnCommand command = new CrnCommand();
@@ -335,19 +383,19 @@
// command.setDestinationPosZ((short) 0); // 鐩爣搴撲綅灞�
// crnThread.write(command);
-// // 7.鍧愭爣绉昏 鐩爣鍙� pass
-// CrnCommand command = new CrnCommand();
-// command.setCrnNo(1); // 鍫嗗灈鏈虹紪鍙�
-// command.setTaskNo((short) 0); // 宸ヤ綔鍙�
-// command.setAckFinish((short) 0); // 浠诲姟瀹屾垚纭浣�
-// command.setTaskMode(CrnTaskModeType.OFFSET_MOVE); // 浠诲姟妯″紡
-// command.setSourcePosX((short) 0); // 婧愬簱浣嶆帓
-// command.setSourcePosY((short) 0); // 婧愬簱浣嶅垪
-// command.setSourcePosZ((short) 0); // 婧愬簱浣嶅眰
-// command.setDestinationPosX((short) 2); // 鐩爣搴撲綅鎺�
-// command.setDestinationPosY((short) 1); // 鐩爣搴撲綅鍒�
-// command.setDestinationPosZ((short) 1); // 鐩爣搴撲綅灞�
-// crnThread.write(command);
+ // 7.鍧愭爣绉昏 鐩爣鍙� pass
+ CrnCommand command = new CrnCommand();
+ command.setCrnNo(1); // 鍫嗗灈鏈虹紪鍙�
+ command.setTaskNo((short) 0); // 宸ヤ綔鍙�
+ command.setAckFinish((short) 0); // 浠诲姟瀹屾垚纭浣�
+ command.setTaskMode(CrnTaskModeType.OFFSET_MOVE); // 浠诲姟妯″紡
+ command.setSourcePosX((short) 0); // 婧愬簱浣嶆帓
+ command.setSourcePosY((short) 0); // 婧愬簱浣嶅垪
+ command.setSourcePosZ((short) 0); // 婧愬簱浣嶅眰
+ command.setDestinationPosX((short) 1); // 鐩爣搴撲綅鎺�
+ command.setDestinationPosY((short) 3); // 鐩爣搴撲綅鍒�
+ command.setDestinationPosZ((short) 3); // 鐩爣搴撲綅灞�
+ crnThread.write(command);
// 鍙湁鍑虹幇鎸囧畾寮傚父鎵嶈繘琛屽浣�
// if (crnThread.crnProtocol.getCrnError2().leftTakeNoneErr
--
Gitblit v1.9.1