From 2eb8dfe138c549a2f7112f01acec4c67d32f2281 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期四, 10 九月 2020 17:05:34 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java |  152 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 137 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
index 557eff8..13fa3bd 100644
--- a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -9,21 +9,26 @@
 import com.zy.asrs.mapper.WaitPakinMapper;
 import com.zy.asrs.mapper.WrkMastMapper;
 import com.zy.asrs.service.*;
+import com.zy.common.model.MatDto;
 import com.zy.common.model.StartupDto;
 import com.zy.common.service.CommonService;
+import com.zy.common.utils.CollectionUtils;
 import com.zy.core.cache.MessageQueue;
 import com.zy.core.cache.SlaveConnection;
 import com.zy.core.enums.*;
 import com.zy.core.model.CrnSlave;
 import com.zy.core.model.DevpSlave;
+import com.zy.core.model.LedSlave;
 import com.zy.core.model.Task;
 import com.zy.core.model.command.CrnCommand;
+import com.zy.core.model.command.LedCommand;
 import com.zy.core.model.protocol.CrnProtocol;
 import com.zy.core.model.protocol.StaProtocol;
 import com.zy.core.properties.SlaveProperties;
 import com.zy.core.thread.BarcodeThread;
 import com.zy.core.thread.CrnThread;
 import com.zy.core.thread.DevpThread;
+import com.zy.core.thread.LedThread;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -31,8 +36,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -80,6 +84,9 @@
                 // 鑾峰彇鍏ュ簱绔欎俊鎭�
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                 StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
+                if (staProtocol == null) {
+                    continue;
+                }
                 // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
                 if (staProtocol.isAutoing() && staProtocol.isLoading()
                         && !staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0
@@ -148,12 +155,10 @@
                     staProtocol.setWorkNo((short) workNo);
                     staProtocol.setStaNo(startupDto.getStaNo().shortValue());
                     staProtocol.setPakMk(false);
-                    staProtocol.setInreq1(false);
                     boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                     if (!result) {
                         throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
                     }
-                    staProtocol.setPakMk(false);
 
                 }
 
@@ -175,6 +180,7 @@
                 // 鑾峰彇鍏ュ簱绔欎俊鎭�
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                 StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
+                if (staProtocol == null) { continue; }
                 // 鍒ゆ柇鏄惁婊¤冻鍏ュ簱鏉′欢
                 if (staProtocol.isAutoing() && staProtocol.isLoading()
                         && !staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0
@@ -198,11 +204,11 @@
                     // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
                     staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                     staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
+                    staProtocol.setPakMk(false);
                     boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                     if (!result) {
                         throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
                     }
-                    staProtocol.setPakMk(false);
 
                 }
 
@@ -223,15 +229,15 @@
                 // 鑾峰彇鎷f枡鍏ュ簱绔欎俊鎭�
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                 StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
-
-                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInreq1()
+                if (staProtocol == null) { continue; }
+                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
                     && staProtocol.getWorkNo() > 0 && staProtocol.isPakMk()){
                     WrkMast wrkMast = wrkMastMapper.selectPickStep(staProtocol.getWorkNo().intValue());
                     if (wrkMast == null) {
                         // 鏃犳嫞鏂欐暟鎹�
                         continue;
                     }
-                    if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 1070)
+                    if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107)
                         || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo()) ) {
                         continue;
                     }
@@ -277,7 +283,6 @@
                     staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                     staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                     staProtocol.setPakMk(false);
-                    staProtocol.setInreq1(false);
                     boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                     if (!result) {
                         throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
@@ -301,6 +306,7 @@
                 // 鑾峰彇鍫嗗灈鏈哄嚭搴撶珯淇℃伅
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
                 StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
+                if (staProtocol == null) { continue; }
                 if (staProtocol.isAutoing() && staProtocol.isLoading() && (staProtocol.getWorkNo() == 0 || staProtocol.getStaNo() == null)) {
                     // 鏌ヨ宸ヤ綔妗�
                     WrkMast wrkMast = wrkMastMapper.selectPakOutStep2(staProtocol.getSiteId());
@@ -358,13 +364,14 @@
             // 鑾峰彇鍫嗗灈鏈轰俊鎭�
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
             CrnProtocol crnProtocol = crnThread.getCrnProtocol();
+            if (crnProtocol == null) { continue; }
             BasCrnp basCrnp = basCrnpService.selectById(crn.getId());
             if (basCrnp == null) {
                 log.error("{}鍙峰爢鍨涙満灏氭湭鍦ㄦ暟鎹簱杩涜缁存姢锛�", crn.getId());
                 continue;
             }
             // 鍙湁褰撳爢鍨涙満绌洪棽 骞朵笖 鏃犱换鍔℃椂鎵嶇户缁墽琛�
-            if (crnProtocol.getStatusType() == CrnStatusType.IDLE && crnProtocol.getTaskNo() == 0) {
+            if (crnProtocol.getStatusType() == CrnStatusType.IDLE && crnProtocol.getTaskNo() == 0 && crnProtocol.getModeType() == CrnModeType.AUTO) {
                 // 濡傛灉鏈�杩戜竴娆℃槸鍏ュ簱妯″紡
                 if (crnProtocol.getLastIo().equals("I")) {
                     if (basCrnp.getInEnable().equals("Y")) {
@@ -401,13 +408,15 @@
             // 鑾峰彇鍫嗗灈鏈哄叆搴撶珯淇℃伅
             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
             StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
+            if (staProtocol == null) { continue; }
             // 鏌ヨ绔欑偣璇︾粏淇℃伅
             BasDevp staDetl = basDevpService.selectById(crnStn.getStaNo());
             if (staDetl == null) {
                 log.error("鍏ュ簱 ===>> 鍫嗗灈鏈虹珯鐐瑰湪鏁版嵁搴撲笉瀛樺湪, 绔欑偣缂栧彿={}", crnStn.getStaNo());
                 continue;
             }
-            if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() > 0 && staDetl.getCanining()!=null && staDetl.getCanining().equals("Y")) {
+            if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() > 0
+                    && staDetl.getCanining()!=null && staDetl.getCanining().equals("Y")) {
                 flag = true;
             }
             if (!flag) {
@@ -487,6 +496,7 @@
             // 鑾峰彇鍫嗗灈鏈哄嚭搴撶珯淇℃伅
             DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
             StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
+            if (staProtocol == null) { continue; }
             // 鏌ヨ绔欑偣璇︾粏淇℃伅
             BasDevp staDetl = basDevpService.selectById(crnStn.getStaNo());
             if (staDetl == null) {
@@ -604,6 +614,7 @@
             // 鑾峰彇鍫嗗灈鏈轰俊鎭�
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
             CrnProtocol crnProtocol = crnThread.getCrnProtocol();
+            if (crnProtocol == null) { continue; }
             //  鐘舵�侊細绛夊緟纭 骞朵笖  浠诲姟瀹屾垚浣� = 1
             if (crnProtocol.statusType == CrnStatusType.WAITING && crnProtocol.getTaskNo() != 0) {
                 // 鑾峰彇鍏ュ簱寰呯‘璁ゅ伐浣滄。
@@ -661,9 +672,7 @@
                 // 鑾峰彇绌烘澘鍏ュ簱绔欎俊鎭�
                 DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                 StaProtocol staProtocol = devpThread.getStation().get(emptyInSta.getStaNo());
-                if (staProtocol == null) {
-                    continue;
-                }
+                if (staProtocol == null) { continue; }
                 // 绔欑偣鏉′欢鍒ゆ柇
                 if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()
                         && staProtocol.isEmptyMk() && staProtocol.getWorkNo() == 0 && staProtocol.isPakMk()) {
@@ -708,12 +717,13 @@
                     // 鏇存柊绔欑偣淇℃伅 涓� 涓嬪彂plc鍛戒护
                     staProtocol.setWorkNo((short) workNo);
                     staProtocol.setStaNo(startupDto.getStaNo().shortValue());
+                    staProtocol.setPakMk(false);
                     boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                     if (!result) {
                         throw new CoolException("鏇存柊plc绔欑偣淇℃伅澶辫触");
                     }
 
-                    staProtocol.setPakMk(false);
+
                 }
 
 
@@ -721,4 +731,116 @@
         }
     }
 
+    /**
+     * 鍑哄簱  ===>> 宸ヤ綔妗d俊鎭啓鍏ed鏄剧ず鍣�
+     */
+    @Async
+    public void ledExecute() {
+        for (LedSlave led : slaveProperties.getLed()) {
+            // 鑾峰彇杈撻�佺嚎plc绾跨▼
+            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
+            // 鍛戒护闆嗗悎
+            List<LedCommand> commands = new ArrayList<>();
+            // 宸ヤ綔妗i泦鍚�
+            List<WrkMast> wrkMasts = new ArrayList<>();
+            for (Integer staNo : led.getStaArr()) {
+                // 鑾峰彇鍙夎溅绔欑偣
+                StaProtocol staProtocol = devpThread.getStation().get(staNo);
+                if (null == staProtocol || null == staProtocol.getWorkNo() || 0 == staProtocol.getWorkNo() || !staProtocol.isLoading()) { continue; }
+                // 鑾峰彇宸ヤ綔妗f暟鎹�
+                WrkMast wrkMast = wrkMastMapper.selectById(staProtocol.getWorkNo());
+                if (null == wrkMast || wrkMast.getWrkSts() < 14 || wrkMast.getIoType() < 100) { continue; }
+                wrkMasts.add(wrkMast);
+                // 缁勮鍛戒护
+                LedCommand ledCommand = new LedCommand();
+                ledCommand.setWorkNo(wrkMast.getWrkNo());
+                // 鍑哄簱妯″紡
+                switch (wrkMast.getIoType()) {
+                    case 101:
+                        ledCommand.setTitle("鍏ㄦ澘鍑哄簱");
+                        break;
+                    case 103:
+                        ledCommand.setTitle("鎷f枡鍑哄簱");
+                        break;
+                    case 104:
+                        ledCommand.setTitle("骞舵澘鍑哄簱");
+                        break;
+                    case 107:
+                        ledCommand.setTitle("鐩樼偣鍑哄簱");
+                        break;
+                    case 110:
+                        ledCommand.setTitle("绌烘澘鍑哄簱");
+                        ledCommand.setEmptyMk(true);
+                        break;
+                    default:
+                        log.error("浠诲姟鍏ュ嚭搴撶被鍨嬮敊璇紒锛侊紒[宸ヤ綔鍙凤細{}] [鍏ュ嚭搴撶被鍨嬶細{}]", wrkMast.getWrkNo(), wrkMast.getIoType());
+                        break;
+                }
+                ledCommand.setSourceLocNo(wrkMast.getSourceLocNo());
+                ledCommand.setStaNo(wrkMast.getStaNo());
+                if (wrkMast.getIoType() != 110) {
+                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
+                    wrkDetls.forEach(wrkDetl -> ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMatnr(), wrkDetl.getAnfme())));
+                }
+                commands.add(ledCommand);
+            }
+            Set<Integer> workNos = wrkMasts.stream().map(WrkMast::getWrkNo).collect(Collectors.toSet());
+            // 鑾峰彇LED绾跨▼
+            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
+            // 鐩稿悓宸ヤ綔鍙烽泦鍚堝垯杩囨护
+            if (CollectionUtils.equals(ledThread.getWorkNos(), workNos)) {
+                continue;
+            }
+            // 鍛戒护涓嬪彂 -------------------------------------------------------------------------------
+            if (!commands.isEmpty()) {
+                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
+                    log.error("{}鍙稬ED鍛戒护涓嬪彂澶辫触锛侊紒锛乕ip锛歿}] [port锛歿}]", led.getId(), led.getIp(), led.getPort());
+                    continue;
+                }
+            }
+
+            // 淇敼涓绘。led鏍囪
+            for (WrkMast wrkMast : wrkMasts) {
+                wrkMast.setOveMk("Y");
+                wrkMast.setModiTime(new Date());
+                if (wrkMastMapper.updateById(wrkMast) == 0) {
+                    throw new CoolException("鏇存柊宸ヤ綔妗eけ璐�");
+                }
+            }
+
+            // 鏇存柊绾跨▼褰撳墠宸ヤ綔鍙烽泦鍚�
+            ledThread.setWorkNos(workNos);
+
+        }
+    }
+
+    /**
+     * 鍏朵粬  ===>> LED鏄剧ず鍣ㄥ浣嶏紝鏄剧ず榛樿淇℃伅
+     */
+    @Async
+    public void ledReset() {
+        for (LedSlave led : slaveProperties.getLed()) {
+            // 鑾峰彇杈撻�佺嚎plc绾跨▼
+            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
+            // 鍛戒护闆嗗悎
+            boolean reset = true;
+            for (Integer staNo : led.getStaArr()) {
+                // 鑾峰彇鍙夎溅绔欑偣
+                StaProtocol staProtocol = devpThread.getStation().get(staNo);
+                if (staProtocol.getWorkNo() != 0) {
+                    reset = false;
+                    break;
+                }
+            }
+            // 鑾峰彇led绾跨▼
+            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getDevpPlcId());
+            // led鏄剧ず榛樿鍐呭
+            if (!ledThread.isResetStatus() && reset) {
+                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(2, new ArrayList<>()))) {
+                    log.error("{}鍙稬ED鍛戒护涓嬪彂澶辫触锛侊紒锛乕ip锛歿}] [port锛歿}]", led.getId(), led.getIp(), led.getPort());
+                }
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.1