严智鑫
2024-12-25 490df5d4d92877171076756af73a96dee1fc053f
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.mes.common.S7object;
import com.mes.common.S7objectFlipSlice;
import com.mes.common.S7objectMachine;
import com.mes.common.S7objectMarking;
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 PLCFlipSlice {
 
    @Autowired
    TaskingService taskingService;
    @Autowired
    MachineMapper machineMapper;
 
    public S7objectMachine s7objectMachine;
 
    @Autowired
    ProjectService projectService;
 
    public List<Map> sendRecords = new ArrayList<>();
 
    //@Scheduled(fixedDelay = 1000)
    public void plcFlipSlice() {
        Long machineId=26L;
        Machine machine=machineMapper.selectById(machineId);
        if (s7objectMachine==null){
            s7objectMachine=new S7objectMachine(machine.getIp(),machine.getPort(),"PlcFlipSlice", EPlcType.S1200);
            s7objectMachine.start();
        }
        PlcParameterObject plcParameterObject = s7objectMachine.PlcMesObject;
        PlcParameterInfo plcRequest =plcParameterObject.getPlcParameter("plcRequest");//请求字
        PlcParameterInfo plcRequestID =plcParameterObject.getPlcParameter("plcRequestID");//请求ID
        PlcParameterInfo plcRequestType =plcParameterObject.getPlcParameter("plcRequestType");//PLC请求
 
        PlcParameterInfo mesSend =plcParameterObject.getPlcParameter("mesSend");//发送字
        PlcParameterInfo mesSendId =plcParameterObject.getPlcParameter("mesSendID");//发送ID
        PlcParameterInfo length =plcParameterObject.getPlcParameter("length");//长
        PlcParameterInfo width =plcParameterObject.getPlcParameter("width");//宽
        PlcParameterInfo mesSendType =plcParameterObject.getPlcParameter("mesSendType");//  扫码1   不扫码2
 
        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(mesSend.getAddress(), 0);
            return;
        }
        List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作");
        if(("1".equals(plcRequest.getValue())&&"0".equals(mesSend.getValue())&& !plcRequestID.getValue().isEmpty()
                &&"开工".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(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);
                }
                Map sendContentMap=new HashMap();
                sendContentMap.put("sendContent",sendRecord);
                sendRecords.add(sendContentMap);
 
                nullMachine(machine);
                return;
            }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 flipSliceTask() {
        //获取报警状态
        JSONObject jsonObject = new JSONObject();
        Machine machine=machineMapper.selectById(26L);
        List<Tasking> taskingList=taskingService.findMachineTask(machine);
        jsonObject.append("taskingList", taskingList);
        jsonObject.append("machine", machine);
        jsonObject.append("sendRecords", sendRecords);
        ArrayList<WebSocketServer> sendServer = WebSocketServer.sessionMap.get("flipSlice");
        if (sendServer != null) {
            for (WebSocketServer webserver : sendServer) {
                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");
                }
            }
 
        }
    }
 
    @Scheduled(fixedDelay = 1000)
    public void downLineTask() {
        //线下任务
        JSONObject jsonObject = new JSONObject();
        List<Tasking> downLineTask=taskingService.findDownLineTask();
        jsonObject.append("downLineTask", downLineTask);
        ArrayList<WebSocketServer> sendServer = WebSocketServer.sessionMap.get("downLineTask");
        if (sendServer != null) {
            for (WebSocketServer webserver : sendServer) {
                if (webserver != null) {
                    webserver.sendMessage(jsonObject.toString());
                    List<String> messages = webserver.getMessages();
                    if (!messages.isEmpty()) {
                        // // 将最后一个消息转换为整数类型的列表
                        webserver.clearMessages();
                    }
                } else {
                    log.info("Home is closed");
                }
            }
 
        }
    }
 
}