ZengTao
2024-01-05 03920f72fe2051349ba5c8872d243388765d3408
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.example.springboot.component;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
 
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import com.example.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.example.springboot.entity.Out_slice;
import com.example.springboot.entity.Queue;
import com.example.springboot.entity.StorageCage;
import com.example.springboot.entity.alarmmg;
import com.example.springboot.mapper.HomeMapper;
import com.example.springboot.mapper.SpianMapper;
 
public class Plchome extends Thread {
 
    @Autowired
    private HomeMapper homeMapper;
    private OutSliceServive outSliceServive;
    private SpianMapper spianMapper;
    private SpianService spianService;
    private North_Glass_Buffer1Service north_Glass_Buffer1Service;
    private JdbcConnections dbserve;
    // 出片队列是否允许出片
    public static Boolean isAllowQueue = true;
    public static Boolean isAllowReordering = true;
    //出片队列警告
    public static Boolean isQueueWarning=false;
    // 铝框id
    public static String FrameNo = "";
    // 是否需要手动确认玻璃
    public static Boolean isConfirm = false;
 
    private Configuration config;
 
    private StorageCageService storageCageService;
 
    public Plchome() throws IOException {
        config = new Configuration("config.properties");
    }
 
    // public static Map b=new HashMap<>();
    // b.put()
    @Override
    public void run() {
        while (this != null) {
            try {
                Thread.sleep(1000);
 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            JSONObject jsonObject = new JSONObject();
 
            // 注入mapper
 
            homeMapper = WebSocketServer.applicationContext.getBean(HomeMapper.class);
            spianMapper = WebSocketServer.applicationContext.getBean(SpianMapper.class);
            spianService = WebSocketServer.applicationContext.getBean(SpianService.class);
            outSliceServive = WebSocketServer.applicationContext.getBean(OutSliceServive.class);
            north_Glass_Buffer1Service = WebSocketServer.applicationContext.getBean(North_Glass_Buffer1Service.class);
            // 笼子使用情况
            List<StorageCage> tableData = homeMapper.selectAll();
            jsonObject.append("tableData", tableData);
            // 格理片笼格子状态
            List<StorageCage> cagelist1 = homeMapper.selectRack1();
            List<StorageCage> cagelist2 = homeMapper.selectRack2();
            List<StorageCage> cagelist3 = homeMapper.selectRack3();
            List<StorageCage> cagelist4 = homeMapper.selectRack4();
            jsonObject.append("cagelist1", cagelist1);
            jsonObject.append("cagelist2", cagelist2);
            jsonObject.append("cagelist3", cagelist3);
            jsonObject.append("cagelist4", cagelist4);
            // 进出片任务
            List<StorageCage> tasklist1 = homeMapper.selectinout(3);
            List<StorageCage> tasklist2 = homeMapper.selectinout(2);
            jsonObject.append("tasklist1", tasklist1);
            jsonObject.append("tasklist2", tasklist2);
            // 查询报警信息
            List<alarmmg> alarmmg = homeMapper.SelectAlarmmgInfo();
            jsonObject.append("alarmmg", alarmmg);
 
            // 伍 获取进是否有待确认的玻璃id
            String queid = spianMapper.Selectqueueid();
            int state = spianMapper.Selectqueuestate();
            jsonObject.append("queid", queid);
            jsonObject.append("state", state);
            // 获取扫码位与上片位玻璃信息
            Queue form2 = homeMapper.GetQueueInfo(2);
            Queue form3 = homeMapper.GetQueueInfo(1);
            if (form2.getglassId() != null && form2.getglassId() != "") {
                jsonObject.append("form2", form2);
            }
            if (form3.getglassId() != null && form3.getglassId() != "") {
                jsonObject.append("form3", form3);
            }
            // 获取出片队列信息
            List<Out_slice> listoutslice = outSliceServive.SelectProductionqueue();
            jsonObject.append("listoutslice", listoutslice);
            // 获取当前出片队列出片状态
            jsonObject.append("isAllowQueue", Plchome.isAllowQueue);
            //获取出片队列警告状态
            jsonObject.append("isQueueWarning", Plchome.isQueueWarning);
            // 获取当前出片队列调序状态
            jsonObject.append("isAllowReordering", Plchome.isAllowReordering);
            // 是否需要人工确认上片玻璃
            jsonObject.append("isConfirm", isConfirm);
            // String framenos="X21763329601FB";
            //     if (!framenos.equals(Plchome.FrameNo)) {
            //         Plchome.FrameNo = framenos;
            //         try {
            //             north_Glass_Buffer1Service.AddOutSliceS(Plchome.FrameNo);
            //         } catch (SQLException e) {
            //             // TODO Auto-generated catch block
            //             e.printStackTrace();
            //         }
            //     }
            if (S7control.getinstance().CheckConnected() == true) {
                // 读去Plc进片车与出片车位置W
                List<String> addressList = new ArrayList<String>();
                addressList.add("DB106.12");
                addressList.add("DB106.0");
                List<Short> paramlist = S7control.getinstance().ReadWord(addressList);
                if (paramlist != null) {
                    jsonObject.append("params", paramlist);
                }
                // 获取进片车状态
                List<Short> datas1ListState = S7control.getinstance().ReadWord("DB106.8",
                        1);// 获取进片车状态
                if (datas1ListState != null) {
                    boolean exist1 = datas1ListState.contains((short) 0);
                    jsonObject.append("zhuangtai", exist1);
                }
 
                // 获取进片玻璃信息
                List<Short> inglassInfo = S7control.getinstance().ReadWord("DB106.24", 1);
                if (inglassInfo != null) {
                    if (inglassInfo.size() > 0) {
                        jsonObject.append("loadglassheight", inglassInfo.get(0) * 70);
                    }
 
                }
 
                // 获取铝框idDB106.DBW64,添加到出片队列
                String frameno = spianService.queGlassid("DB103.270", 14).toString();
                // String frameno="X12345611002GV";
                // frameno="X21763329601FB";
                if (!frameno.equals(Plchome.FrameNo)) {
                    Plchome.FrameNo = frameno;
                    try {
                        north_Glass_Buffer1Service.AddOutSliceS(Plchome.FrameNo);
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
 
                // 获取进片请求状态
                List<Short> D01Request = S7control.getinstance().ReadWord("DB106.24", 1);
                boolean D01RequestState = D01Request.contains((short) 1);
                // boolean D01RequestState =true;
                jsonObject.append("D01RequestState", D01RequestState);
 
                // 复位倒计时
                // 地址104.9.6
                List<Boolean> countdown = S7control.getinstance().ReadBits("DB104.9.6", 1);
                for (Boolean countdowns : countdown) {
                    jsonObject.append("countdown", countdowns);
                }
                // jsonObject.append("countdown", true);
                // 复位完成信号ss
                List<Boolean> resetss = S7control.getinstance().ReadBits("DB103.284", 1);
                for (Boolean reset : resetss) {
                    jsonObject.append("reset", reset);
                }
 
                // 是否急停
                List<Boolean> emergencystops = S7control.getinstance().ReadBits("DB104.5.1", 1);
                for (Boolean emergencystop : emergencystops) {
                    jsonObject.
                    append("emergencystop", emergencystop);
                }
            
 
                // // 终止继续按钮
                // List<String> niuanaddressList3 = Arrays.asList(config.getProperty("Parameter2.stop").split(","));
                // List<Boolean> anniuread = S7control.getinstance().readBits(niuanaddressList3);
                // // Boolean[] value4 = { false, false};
                // // List<Boolean> anniuread = new ArrayList<>(Arrays.asList(value4));
                // Boolean[] value5 = { true };
                // List<Boolean> resets = new ArrayList<>(Arrays.asList(value5));
                // short[] anniuparams = new short[anniuread.size()];
                // for (int i = 0; i < anniuread.size(); i++) {
                //     boolean value = anniuread.get(i);
                //     anniuparams[i] = value ? (short) 1 : (short) 0;
                // }
 
                // short[] resets2 = new short[resets.size()];
                // for (int i = 0; i < resets.size(); i++) {
                //     boolean value = resets.get(i);
                //     resets2[i] = value ? (short) 1 : (short) 0;
                // }
 
                // for (short number : resets2) {
 
                //     if (number == 1) {
                //         jsonObject.append("Abort", anniuparams);
 
                //     }
 
                // }
            }
 
            // 铝框线交互
 
            // //铝框id请求字
            // List<Short> framerequest = S7control.getinstance().ReadWord("铝框id请求字地址", 1);
            // if(framerequest!=null){
            // //铝框id
            // String franmeno= spianService.queGlassid("铝框id地址", 14).toString();
            // String position;
            // try {
            // position=dbserve.SelectPositionByFrameBarcode(franmeno);
            // } catch (SQLException e) {
            // // TODO Auto-generated catch block
            // position="0";
            // e.printStackTrace();
            // }
            // Short send;
            // if(position=="0"){
            // send=0;
            // }else{
            // send=1;
            // }
            // S7control.getinstance().WriteWord("铝框翻转发送地址", (short)send);
            // }
            storageCageService = WebSocketServer.applicationContext.getBean(StorageCageService.class);
            dbserve = WebSocketServer.applicationContext.getBean(JdbcConnections.class);
            boolean dbconnected = false;
            try {
                dbserve.getConn();
                dbconnected = true;
            } catch (Exception e) {
                // TODO: handle exception
                dbconnected = false;
            }
            jsonObject.append("dbconnected", dbconnected);
            // jsonObject.append("params", new short[] { 30, 40, });
            ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("Home");
            if (sendwServer != null) {
                for (WebSocketServer webserver : sendwServer) {
                    webserver.sendMessage(jsonObject.toString());
 
                    if (webserver != null) {
 
                        List<String> messages = webserver.getMessages();
 
                        if (!messages.isEmpty()) {
                            // // 将最后一个消息转换为整数类型的列表
                            // String lastMessage = messages.get(messages.size() - 1);
                            // System.out.println("lastMessage:" + lastMessage);
                            // JSONArray messageArray = new JSONArray(lastMessage);
                            
                            // try {
                            //     // 检查索引是否有效
                            //     if (messageArray.getJSONArray(0).size() > 0) {
                            //         // 获取消息数组
                            //         JSONArray jsonArray = messageArray.getJSONArray(0);
                            //         // 创建一个布尔值列表
                            //         List<Boolean> sValue = new ArrayList<>();
                            //         // 遍历消息数组
                            //         for (int i = 0; i < jsonArray.size(); i++) {
                            //             // 获取消息数组中的值
                            //             Object value = jsonArray.get(i);
                            //             // 检查值是否有效
                            //             if (value != null && !value.toString().equals("null")) {
                            //                 try {
                            //                     // 移除非数字和数字字符
                            //                     String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                            //                     // 解析为布尔值
                            //                     boolean val = "1".equals(cleanedValue.trim());
                            //                     // 将布尔值添加到布尔值列表中
                            //                     sValue.add(val);
                            //                     System.out.println(
                            //                             "messageValue: " + Arrays.asList(val) + " added to the list");
 
                            //                 } catch (NumberFormatException e) {
                            //                     // 如果无法解析为 boolean 类型,则忽略该部分
                            //                     System.err.println("Could not parse value: " + value);
                            //                 }
                            //             }
                            //         }
                            //         // 终止进片任务
                            //         boolean Value1 = sValue.get(0);
                            //         boolean Value2 = sValue.get(1);
 
                            //         if (Value1) {
                            //             storageCageService.EndTask(0);
 
                            //         }
                            //         if (Value2) {
                            //             storageCageService.EndTask(1);
 
                            //         }
                            //         // 检查布尔值列表是否为空
                            //         if (!sValue.isEmpty()) {
 
                            //             // 调用 S7control.getinstance().WriteBit 方法将布尔值列表写入地址列表
                            //             S7control.getinstance().WriteBit(niuanaddressList3, sValue);
                            //             System.out.println(
                            //                     "Values " + sValue + " written to PLC at address " + niuanaddressList3);
                            //         }
                            //     }
                            // } catch (Exception e) {
                            //     System.err.println("An error occurred while writing bit to PLC: " + e.getMessage());
                            // }
                            webserver.clearMessages();
                        }
 
                    }
 
                }
            }
        }
    }
}