严智鑫
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import com.mes.common.S7object;
import com.mes.common.S7objectMarking;
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.mapper.TaskingMapper;
import com.mes.md.service.TaskingService;
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.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static cn.hutool.core.io.FileUtil.writeString;
 
/**
 * 自动丝印机
 * @Author :
 * @Date: 2024/8/20 11:19
 * @Description:
 */
@Component
@Slf4j
public class SilkScreenTask {
 
    @Autowired
    TaskingMapper taskingMapper;
    @Autowired
    MachineMapper machineMapper;
    @Autowired
    TaskingService taskingService;
    public static Long machineId1 = 13L;
    public static Long machineId2 = 14L;
    //public ModbusTcp modbusTcp1 = null;
    public ModbusTcp modbusTcp2 = new ModbusTcp();
    public List<Map> sendRecords = new ArrayList<>();
    //@Scheduled(fixedDelay = 1000)
    public void plcSilkScreenTask() {
        Machine machine=machineMapper.selectById(machineId2);
        String fileName="PlcSilkScreen";
        ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),fileName);
        modbusTcp1.connect();
        PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement(fileName);
        try {
            //while(true){
                if(!modbusTcp1.read(plcAgreement)){
                    log.info("通讯读取新数据失败");
                    modbusTcp1.close();
                    return;
                }
                PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//请求字
                PlcParameter plcRequestID =plcAgreement.getPlcParameter("plcRequestID");//请求ID
                PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//发送字
                PlcParameter mesSendID =plcAgreement.getPlcParameter("mesSendID");//发送ID
                PlcParameter length =plcAgreement.getPlcParameter("length");//长
                PlcParameter width =plcAgreement.getPlcParameter("width");//宽
                PlcParameter thickness =plcAgreement.getPlcParameter("thickness");//厚
                PlcParameter X =plcAgreement.getPlcParameter("X");//X
                PlcParameter Y =plcAgreement.getPlcParameter("Y");//Y
                //PlcParameter isSilkScreen =plcAgreement.getPlcParameter("isSilkScreen");//是否丝印
 
                PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//汇报字
                PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//汇报ID
                PlcParameter mesConfirm =plcAgreement.getPlcParameter("mesConfirm");//确认字
                PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//确认ID
                PlcParameter machineStatusWord =plcAgreement.getPlcParameter("machineStatusWord");
                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(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==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
                    int finishCount=taskingService.glassDownLineOne(machine);
                    if(finishCount>0){//有任务
                        log.info("4、人工拿走");
                        String send=HexUtil.intTo2ByteHex(4);
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                        log.info("人工拿走发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                        modbusTcp1.close();
                        return;
                    }
                    String send=HexUtil.intTo2ByteHex(4);
                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                    log.info("人工拿走异常 发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                    modbusTcp1.close();
                    return;
                }
                if(2==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
                    int finishCount=taskingService.loseMachineTask(machine);
                    if(finishCount>0){//有任务
                        log.info("2、重发");
                        String send=HexUtil.intTo2ByteHex(2);
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                        log.info("重发发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                        modbusTcp1.close();
                        return;
                    }else{
                        log.info("2、异常重发");
                        String send=HexUtil.intTo2ByteHex(2);
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                        log.info("异常重发发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                        modbusTcp1.close();
                        return;
                    }
                }
 
                if(3==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
                    int finishCount=taskingService.finishMachineTask(machine);
                    if(finishCount>0){//有任务
                        log.info("3、设备上人工点破损");
                        String send=HexUtil.intTo2ByteHex(3);
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                        log.info("人工点破损发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                        modbusTcp1.close();
                        return;
                    }else{
                        //是否自动清除破损
                        log.info("3、无效破损 (没有找到此ID的玻璃)");
                        String send=HexUtil.intTo2ByteHex(3);
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                        log.info("发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                        modbusTcp1.close();
                        //modbusTcp1.close();
                        return;
                    }
                }
                List<Tasking> taskingCount=taskingService.findMachineWorkStateTask(machine,"正在工作");
                if((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
                        &&"开工".equals(machine.getState())
                        &&taskingCount.size()<machine.getMaxTaskCount()){
                    Tasking tasking=taskingService.startMachineTask(machine);
                    if(tasking!=null){
                        log.info("4、发送任务  任务数据:{}, 设备状态:{}, ",machine,tasking);
                        String send=
                                HexUtil.intTo2ByteHex(1)+
                                        HexUtil.intTo2ByteHex(tasking.getTaskSequence().intValue())+
                                        HexUtil.intTo2ByteHex(((Double)(tasking.getWidth()*10)).intValue())+
                                        HexUtil.intTo2ByteHex(((Double)(tasking.getLength()*10)).intValue())+
                                        HexUtil.intTo2ByteHex(tasking.getThickness().intValue())+
                                        HexUtil.intTo2ByteHex(((Double)(tasking.getSilkScreenX()*10)).intValue())+
                                        HexUtil.intTo2ByteHex(((Double)(tasking.getSilkScreenY()*10)).intValue())+
                                        HexUtil.intTo2ByteHex(tasking.getIsSilkScreen());//是否丝印: 0丝印 1不丝印
                        modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
                        log.info("发送任务发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
                        modbusTcp1.close();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String sendRecord=df.format(new Date())+"  "+
                                tasking.getGlassId()+"-"+
                                tasking.getWidth()+"-"+
                                tasking.getLength()+"-"+
                                tasking.getThickness()+"-"+
                                tasking.getSilkScreenX()+"-"+
                                tasking.getSilkScreenX()+"-"+
                                tasking.getSilkScreenY()+"-"+
                                tasking.getIsSilkScreen();
                        if (sendRecords.size()>7){
                            sendRecords.remove(0);
                        }
                        Map sendContentMap=new HashMap();
                        sendContentMap.put("sendContent",sendRecord);
                        sendRecords.add(sendContentMap);
                        return;
                    }
                }
            //}
 
        } catch (Exception e) {
            log.info("交互逻辑错误");
        }
 
        modbusTcp1.close();
        //log.info("无可执行的条件");
    }
 
    @Scheduled(fixedDelay = 500)
    public void SilkScreenTasks() {
        JSONObject jsonObject = new JSONObject();
        Machine machine=machineMapper.selectById(14L);
        List<Tasking> taskingList=taskingService.findMachineTask(machine);
        jsonObject.append("taskingList", taskingList);
        jsonObject.append("machine", machine);
        jsonObject.append("sendRecords", sendRecords);
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("silkScreenTask");
        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");
                }
            }
        }
    }
 
}