From 044c32c15ee9ccac6f3b60eb6745025e2f4480f2 Mon Sep 17 00:00:00 2001
From: 严智鑫 <test>
Date: 星期三, 11 十二月 2024 16:59:54 +0800
Subject: [PATCH] 报工回传+库位显示

---
 JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/job/PLCEdging.java |  344 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 303 insertions(+), 41 deletions(-)

diff --git a/JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/job/PLCEdging.java b/JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/job/PLCEdging.java
index 8ae22bb..95460bb 100644
--- a/JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/job/PLCEdging.java
+++ b/JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/job/PLCEdging.java
@@ -1,19 +1,32 @@
 package com.mes.job;
 
 import cn.hutool.json.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.mes.common.S7object;
 import com.mes.device.PlcParameterObject;
+import com.mes.md.entity.LineConfiguration;
+import com.mes.md.entity.Machine;
+import com.mes.md.entity.PrimitiveTask;
 import com.mes.md.entity.Tasking;
-import com.mes.md.service.TaskingService;
+import com.mes.md.mapper.MachineMapper;
+import com.mes.md.mapper.TaskingMapper;
+import com.mes.md.service.*;
+import com.mes.service.ModbusTcp;
+import com.mes.service.PlcAgreement;
+import com.mes.service.PlcParameter;
 import com.mes.tools.WebSocketServer;
+import com.mes.utils.HexUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 /**
  * @author SNG-010
@@ -22,58 +35,307 @@
 @Slf4j
 public class PLCEdging {
 
-    PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject;
+    @Autowired
+    TaskingMapper taskingMapper;
+    @Autowired
+    MachineMapper machineMapper;
     @Autowired
     TaskingService taskingService;
-    @Scheduled(fixedDelay = 500)
-    public void PLCEdging() {
-        //纾ㄨ竟杩涚墖璇锋眰淇″彿
-        String edgingSign = plcParameterObject.getPlcParameter("edgingSign").getValue();
-        if (!Objects.equals(edgingSign, "")){
-            Tasking glass = new Tasking();
-            glass.setWorkState("宸ヤ綔");
-            //glass.setCurrentCraft("纾ㄨ竟");
-            glass.setState("绾夸笂");
-            Tasking tasking =new Tasking();
-            //Tasking tasking =taskingService.selectTasking(glass);
-            //鍙戦�侀暱瀹藉帤灏哄
-            S7object.getinstance().plccontrol.writeWord(plcParameterObject.getPlcParameter("length").getAddress(), Integer.parseInt(tasking.getLength().toString()));
-            S7object.getinstance().plccontrol.writeWord(plcParameterObject.getPlcParameter("width").getAddress(), Integer.parseInt(tasking.getWidth().toString()));
-            S7object.getinstance().plccontrol.writeWord(plcParameterObject.getPlcParameter("thickness").getAddress(), Integer.parseInt(tasking.getThickness().toString()));
+    @Autowired
+    ProjectService projectService;
 
-        }
-        //鎸佺画鑾峰彇閫熷害鍚屾缁欎笅涓�涓満鍣�
-        String edgingSpeed = plcParameterObject.getPlcParameter("edgingSpeed").getValue();
-        if(!Objects.equals(edgingSpeed, "")){
-            S7object.getinstance().plccontrol.writeWord(plcParameterObject.getPlcParameter("cleaningSpeed").getAddress(), Integer.parseInt(edgingSpeed));
+    @Autowired
+    PrimitiveTaskService primitiveTaskService;
+    @Autowired
+    KBBTJPDrawingBPService kBBTJPDrawingBPService;
+    @Autowired
+    LineConfigurationService lineConfigurationService;
+    public static int thinness = 40;
+    public static int edgSpeed = 40;
+    public static Long machineId1 = 5L;
+    public static Long machineId2 = 6L;
+    public List<Map> sendRecords = new ArrayList<>();
+
+    //@Scheduled(fixedDelay = 1000)
+    public void plcEdging() {
+        Machine machine=machineMapper.selectById(machineId2);
+        ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),"PlcEdging");
+        try {
+            modbusTcp1.connect();
+            PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement("PlcEdging");
+            if(!modbusTcp1.read(plcAgreement)){
+                log.info("閫氳璇诲彇鏂版暟鎹け璐�");
+                modbusTcp1.close();
+                return;
+            }
+            PlcParameter inputOrOut2 =plcAgreement.getPlcParameter("inputOrOut2");
+            edgSpeed=inputOrOut2.getValueInt();
+
+            PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//璇锋眰瀛�
+            PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//鍙戦�佸瓧
+
+
+            PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//姹囨姤瀛�
+            PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//姹囨姤ID
+            PlcParameter plcReport2 =plcAgreement.getPlcParameter("plcReport2");//鎵嬪姩姹囨姤瀛�
+            PlcParameter plcReportID2 =plcAgreement.getPlcParameter("plcReportID2");//鎵嬪姩姹囨姤ID
+
+            PlcParameter mesConfirm =plcAgreement.getPlcParameter("mesConfirm");//纭瀛�
+            PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//纭ID
+            PlcParameter mesConfirm2 =plcAgreement.getPlcParameter("mesConfirm2");//鎵嬪姩纭瀛�
+            PlcParameter mesConfirmID2 =plcAgreement.getPlcParameter("mesConfirmID2");//鎵嬪姩纭ID
+            if (machine.getIsLog()>0){
+                modbusTcp1.consoleLogInfo(plcAgreement);
+            }
+            if (0==plcRequest.getValueInt()&&1==mesSend.getValueInt()) {
+                log.info("1.鍙戦�佸瓧缃浂");
+                //鍙戦�佸瓧缃�0
+                String send= HexUtil.intTo2ByteHex(0);
+                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
+                log.info("鍙戦�佸瓧 娓呯┖鎴愬姛锛涘唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
+                modbusTcp1.close();
+                return;
+            }
+            if (0==plcReport.getValueInt()&&0!=mesConfirm.getValueInt()) {
+                log.info("2.纭瀛楃疆闆�");
+                //鍙戦�佸瓧缃�0
+                String send=HexUtil.intTo2ByteHex(0);
+                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
+                log.info("浠诲姟瀹屾垚 娓呯┖鎴愬姛锛涘唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
+                modbusTcp1.close();
+                return;
+            }
+            if (0==plcReport2.getValueInt()&&0!=mesConfirm2.getValueInt()) {
+                log.info("2.纭瀛楃疆闆�");
+                //鍙戦�佸瓧缃�0
+                String send=HexUtil.intTo2ByteHex(0);
+                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                log.info("浠诲姟瀹屾垚 娓呯┖鎴愬姛锛涘唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                modbusTcp1.close();
+                return;
+            }
+            if(1==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
+                int finishCount=taskingService.finishMachineTask(machine);
+                log.info("3銆佷换鍔″畬鎴�");
+                if(finishCount>0) {//鏈変换鍔�
+                    String send = HexUtil.intTo2ByteHex(1);
+                    modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
+                    log.info("浠诲姟瀹屾垚 鎴愬姛 鍐呭锛歿},鍙戦�佽捣濮嬪湴鍧�:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }
+                String send = HexUtil.intTo2ByteHex(1);
+                modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
+                log.info("浠诲姟瀹屾垚 寮傚父鎴愬姛 鍐呭锛歿},鍙戦�佽捣濮嬪湴鍧�:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
+                modbusTcp1.close();
+                return;
+            }
+            if(4==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
+                int finishCount=taskingService.glassDownLineOne(machine);
+                if(finishCount>0){//鏈変换鍔�
+                    log.info("4銆佷汉宸ユ嬁璧�");
+                    String send=HexUtil.intTo2ByteHex(4);
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                    log.info("浜哄伐鎷胯蛋鍙戦�佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }
+                String send=HexUtil.intTo2ByteHex(4);
+                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                log.info("浜哄伐鎷胯蛋寮傚父 鍙戦�佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                modbusTcp1.close();
+                return;
+            }
+            if(2==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
+                int finishCount=taskingService.loseMachineTask(machine);
+                if(finishCount>0){//鏈変换鍔�
+                    log.info("2銆侀噸鍙�");
+                    String send=HexUtil.intTo2ByteHex(2);
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                    log.info("閲嶅彂鍙戦�佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }else{
+                    log.info("2銆佸紓甯搁噸鍙�");
+                    String send=HexUtil.intTo2ByteHex(2);
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                    log.info("寮傚父閲嶅彂鍙戦�佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }
+            }
+
+            if(3==plcReport2.getValueInt()&&0==mesConfirm2.getValueInt()){
+                int finishCount=taskingService.finishMachineTask(machine);
+                if(finishCount>0){//鏈変换鍔�
+                    log.info("3銆佽澶囦笂浜哄伐鐐圭牬鎹�");
+                    String send=HexUtil.intTo2ByteHex(3);
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                    log.info("浜哄伐鐐圭牬鎹熷彂閫佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }else{
+                    //鏄惁鑷姩娓呴櫎鐮存崯
+                    log.info("3銆佹棤鏁堢牬鎹� 锛堟病鏈夋壘鍒版ID鐨勭幓鐠冿級");
+                    String send=HexUtil.intTo2ByteHex(3);
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()/2));
+                    log.info("鍙戦�佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesConfirm2.getAddressStart()));
+                    modbusTcp1.close();
+                    return;
+                }
+            }
+            List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"姝e湪宸ヤ綔");
+
+            if((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
+                    &&"寮�宸�".equals(machine.getState())
+                    &&taskingCount.size()<machine.getMaxTaskCount()){
+                Machine loadMachine=machineMapper.selectById(2L);
+                Tasking tasking;
+                String scan_id="";
+                if(loadMachine.getMode()==1){
+                    //鏍囧噯妯″紡  鎵惧凡棰嗗彇鏈畬鎴愮殑绗竴鏉′换鍔�
+                    LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
+                    List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
+                    //娣诲姞鏁版嵁:
+                    for(PrimitiveTask primitiveTask:findPrimitiveTasking){
+                        List<Tasking> taskingTopList=taskingMapper.selectList(new QueryWrapper<Tasking>().lambda()
+                                .eq(Tasking::getScanId,primitiveTask.getScanId())
+                        );
+                        //浠诲姟鏁�>宸蹭笂鏁伴噺
+                        if(taskingTopList.size()<primitiveTask.getTaskQuantity()){
+                            Tasking taskingAdd=primitiveTaskService.convertListTasking(primitiveTask,lineConfiguration.getId());
+                            Integer taskSequence=1;
+                            if (machine.getTodayCount()>0&&machine.getTodayCount()<5000){
+                                taskSequence=machine.getTodayCount()+1;
+                            }else{
+                                taskSequence=1;
+                            }
+                            machine.setTodayCount(taskSequence);
+                            taskingAdd.setTaskSequence(taskSequence);
+                            machineMapper.updateById(machine);
+                            taskingMapper.insert(taskingAdd);
+                        }
+                        break;
+                    }
+                    tasking=taskingService.startMachineTask(machine);
+                }else{
+                    //瀹氬埗妯″紡
+                    String PlcRequestID_=clientTCPSocket();
+                    //String PlcRequestID_=",,46607221";
+                    if (PlcRequestID_==null||PlcRequestID_.lastIndexOf(",")<0){
+                        modbusTcp1.close();
+                        errorMachine(machine,("鎵笉鍒扮爜:"+PlcRequestID_));
+                        return;
+                    }
+                    scan_id= PlcRequestID_.substring(PlcRequestID_.lastIndexOf(",")+1);
+                    tasking=taskingService.startMachineTask(machine,scan_id);
+                }
+                if(tasking!=null){
+                    log.info("4銆佸彂閫佷换鍔�  浠诲姟鏁版嵁锛歿}, 璁惧鐘舵��:{}, ",machine,tasking);
+                    String send=
+                            HexUtil.intTo2ByteHex(1)+
+                                    HexUtil.intTo2ByteHex(tasking.getGlassId().intValue())+
+                                    HexUtil.intTo2ByteHex(((Double)(tasking.getLength()*10)).intValue())+
+                                    HexUtil.intTo2ByteHex(((Double)(tasking.getWidth()*10)).intValue())+
+                                    HexUtil.intTo2ByteHex(((Double)(tasking.getThickness()*10)).intValue());
+                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
+                    log.info("鍙戦�佷换鍔″彂閫佹姤鏂囧唴瀹癸細{},鍙戦�佽捣濮嬪湴鍧�:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
+                    modbusTcp1.close();
+                    errorMachine(machine,"");
+                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                    String sendRecord=df.format(new Date())+"  "+
+                            tasking.getGlassId()+"-"+
+                            tasking.getWidth()+"-"+
+                            tasking.getLength()+"-"+
+                            tasking.getThickness();
+                    if (sendRecords.size()>7){
+                        sendRecords.remove(0);
+                    }
+                    Map sendContentMap=new HashMap();
+                    sendContentMap.put("sendContent",sendRecord);
+                    sendRecords.add(sendContentMap);
+                    return;
+                }else if(!scan_id.isEmpty()){
+                    taskingService.scanMachineAdd(machine,scan_id);
+                    modbusTcp1.close();
+                    return;
+                }
+            }
+            //}
+
+        } catch (Exception e) {
+            log.info("浜や簰閫昏緫閿欒");
         }
 
+        modbusTcp1.close();
+        //log.info("鏃犲彲鎵ц鐨勬潯浠�");
     }
-    @Scheduled(fixedDelay = 1000)
-    public void edgingWeb() {
-        //鑾峰彇褰撳墠浠诲姟琛ㄦ渶鏂扮殑涓�鍧椾换鍔$姸鎬�
-        //鑾峰彇鎶ヨ鐘舵��
-        String warning = plcParameterObject.getPlcParameter("warning").getValue();
-        JSONObject jsonObject = new JSONObject();
-        if (!Objects.equals(warning, "0")) {
-            //log.info("鎶ヨ淇℃伅锛�"+warning);
-            jsonObject.append("warning", warning);
+
+
+    public void errorMachine(Machine machine,String error) {
+        if (!error.equals(machine.getRemark())){
+            machine.setRemark(error);
+            machineMapper.updateById(machine);
         }
-        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("scanQrCode");
+    }
+    public String clientTCPSocket() {
+        try {
+            Machine machine=machineMapper.selectById(4L);
+            Socket clientSocket = new Socket(machine.getIp(), machine.getPort());
+            try {
+                // 鍒涘缓杈撳叆娴佸拰杈撳嚭娴�
+                InputStream inputStream = clientSocket.getInputStream();
+                OutputStream outputStream = clientSocket.getOutputStream();
+                // 鍙戦�佸惎鍔ㄥ懡浠�
+                String message = "start";
+                outputStream.write(message.getBytes());
+                // 璇诲彇鏈嶅姟鍣ㄥ搷搴旂殑鏁版嵁
+                byte[] buffer = new byte[1024];
+                int bytesRead = inputStream.read(buffer);
+                String receivedMessage = new String(buffer, 0, bytesRead);
+                System.out.println("鏀跺埌鎵爜鏋繑鍥炲唴瀹癸細" + receivedMessage);
+                // 鍏抽棴杩炴帴
+                String message2 = "stop";
+                outputStream.write(message2.getBytes());
+                clientSocket.close();
+                return receivedMessage;
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            clientSocket.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+    @Scheduled(fixedDelay = 500)
+    public void EdgingTasks() {
+
+        JSONObject jsonObject = new JSONObject();
+        Machine machine=machineMapper.selectById(6L);
+        List<Tasking> taskingList=taskingService.findMachineTask(machine);
+        jsonObject.append("taskingList", taskingList);
+        jsonObject.append("machine", machine);
+        jsonObject.append("sendRecords", sendRecords);
+        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("edgingTask");
         if (sendwServer != null) {
             for (WebSocketServer webserver : sendwServer) {
                 if (webserver != null) {
-                    webserver.sendMessage(jsonObject.toString());
-                    List<String> messages = webserver.getMessages();
-                    if (!messages.isEmpty()) {
-                        // // 灏嗘渶鍚庝竴涓秷鎭浆鎹负鏁存暟绫诲瀷鐨勫垪琛�
-                        webserver.clearMessages();
+                    try {
+                        webserver.sendMessage(jsonObject.toString());
+                        List<String> messages = webserver.getMessages();
+                        if (!messages.isEmpty()) {
+                            // // 灏嗘渶鍚庝竴涓秷鎭浆鎹负鏁存暟绫诲瀷鐨勫垪琛�
+                            webserver.clearMessages();
+                        }
+                    }catch (Exception e) {
+
                     }
+
                 } else {
                     log.info("Home is closed");
                 }
             }
-
         }
     }
 

--
Gitblit v1.8.0