严智鑫
2024-12-11 044c32c15ee9ccac6f3b60eb6745025e2f4480f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.mes.common.S7objectMachine;
import com.mes.device.PlcParameterInfo;
import com.mes.device.PlcParameterObject;
import com.mes.md.entity.Machine;
import com.mes.md.entity.Tasking;
import com.mes.md.mapper.MachineMapper;
import com.mes.md.service.ProjectService;
import com.mes.md.service.TaskingService;
import com.mes.tools.WebSocketServer;
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.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author SNG-010
 */
@Component
@Slf4j
public class PLCRotate {
 
    PlcParameterObject plcParameterObject =null;
    @Autowired
    TaskingService taskingService;
    @Autowired
    MachineMapper machineMapper;
 
    public S7objectMachine s7objectMachine;
 
    @Autowired
    ProjectService projectService;
 
    public List<Map> sendRecords = new ArrayList<>();
 
    //@Scheduled(fixedDelay = 1000)
    public void plcRotate() {
        //旋转台
        Long machineId=20L;
        Machine machine=machineMapper.selectById(machineId);
 
        if (s7objectMachine==null){
            s7objectMachine=new S7objectMachine(machine.getIp(),machine.getPort(),"PlcRotate", EPlcType.S1200);
            s7objectMachine.start();
        }
        PlcParameterObject plcParameterObject = s7objectMachine.PlcMesObject;
        PlcParameterInfo plcRequest =plcParameterObject.getPlcParameter("plcRequest");//请求字
        PlcParameterInfo plcRequestID =plcParameterObject.getPlcParameter("plcRequestID");//请求ID
        PlcParameterInfo mesSend =plcParameterObject.getPlcParameter("mesSend");//发送字
        PlcParameterInfo mesSendId =plcParameterObject.getPlcParameter("mesSendID");//发送ID
        PlcParameterInfo length =plcParameterObject.getPlcParameter("length");//长
        PlcParameterInfo width =plcParameterObject.getPlcParameter("width");//宽
        PlcParameterInfo rotateType =plcParameterObject.getPlcParameter("rotateType");//旋转类型
        String scan_id="";
        if (machine.getIsLog()>0){
            s7objectMachine.consoleLogInfo();
        }
        if ("0".equals(plcRequest.getValue())&&!"0".equals(mesSend.getValue())) {
            //发送字置0
            int finishCount=taskingService.finishMachineTask(machine);
            log.info("1.发送字置零,清除上次发送的数据{},{},{},{},{}"
                    ,finishCount
                    ,length.getValue()
                    ,width.getValue()
                    ,mesSendId.getValue()
                    ,mesSend.getValue());
            s7objectMachine.plccontrol.writeWord(length.getAddress(), 0);
            s7objectMachine.plccontrol.writeWord(width.getAddress(), 0);
            s7objectMachine.plccontrol.writeWord(mesSendId.getAddress(), 0);
            s7objectMachine.plccontrol.writeWord(rotateType.getAddress(), 0);
            s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 0);
            return;
        }
        List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作");
        if("1".equals(plcRequest.getValue())&&"0".equals(mesSend.getValue())
                &&"开工".equals(machine.getState())
                &&taskingCount.size()<machine.getMaxTaskCount()){
            Tasking tasking;
            if(machine.getMode()==1){
                //标准模式
                tasking=taskingService.startMachineTask(machine);
            }else{
                //定制模式
                String PlcRequestID_=plcRequestID.getValue();
                scan_id= PlcRequestID_.substring(PlcRequestID_.lastIndexOf(",")+1);
                //标记当前工序 以前的玻璃 直接到当前工序
                tasking=taskingService.startScanIdMachineTask(machine,scan_id);//不管顺序,直接找从翻片台扫码后到旋转台的数据
            }
            if(tasking!=null){
                log.info("4、发送数据:{},{},{},{},{}"
                        ,tasking.getGlassId(),tasking.getProgramId()
                        ,tasking.getLength().intValue(),tasking.getWidth().intValue(),1);
                s7objectMachine.plccontrol.writeWord(length.getAddress(), tasking.getLength().intValue());
                s7objectMachine.plccontrol.writeWord(width.getAddress(), tasking.getWidth().intValue());
                s7objectMachine.plccontrol.writeString(mesSendId.getAddress(), tasking.getGlassId().toString());
                s7objectMachine.plccontrol.writeWord(rotateType.getAddress(), 1);
                s7objectMachine.plccontrol.writeWord(mesSend.getAddress(), 1);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sendRecord=df.format(new Date())+"  "+
                        tasking.getGlassId()+"-"+
                        tasking.getLength()+"-"+
                        tasking.getWidth();
                if (sendRecords.size()>7){
                    sendRecords.remove(0);
                }
                nullMachine(machine);
                Map sendContentMap=new HashMap();
                sendContentMap.put("sendContent",sendRecord);
                sendRecords.add(sendContentMap);
            }else if(!scan_id.isEmpty()){
                taskingService.scanMachineAdd(machine,scan_id);
            }
        }
 
    }
    public void nullMachine(Machine machine) {
        if (!"".equals(machine.getRemark())){
            machine.setRemark("");
            machineMapper.updateById(machine);
        }
    }
    @Scheduled(fixedDelay = 500)
    public void rotateTask() {
        //获取报警状态
        JSONObject jsonObject = new JSONObject();
        Machine machine=machineMapper.selectById(20L);
        List<Tasking> taskingList=taskingService.findMachineTask(machine);
        jsonObject.append("taskingList", taskingList);
        jsonObject.append("machine", machine);
        jsonObject.append("sendRecords", sendRecords);
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("rotate");
        if (sendwServer != null) {
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null) {
                    try {
                        webserver.sendMessage(jsonObject.toString());
                        List<String> messages = webserver.getMessages();
                        if (!messages.isEmpty()) {
                            // // 将最后一个消息转换为整数类型的列表
                            webserver.clearMessages();
                        }
                    }catch (Exception e) {
 
                    }
                } else {
                    log.info("Home is closed");
                }
            }
 
        }
    }
 
}