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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.example.springboot.service;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springboot.component.PLCAutoMes;
import com.example.springboot.component.S7control;
import com.example.springboot.entity.GlassInfo;
import com.example.springboot.entity.StorageCage;
import com.example.springboot.entity.StorageTask;
import com.example.springboot.entity.device.PlcParameterObject;
import com.example.springboot.mapper.AlbaniaMapper;
import com.google.common.primitives.Bytes;
 
@Service
public class SpianServiceNew {
    @Autowired
    private AlbaniaMapper albaniaMapper;
    @Autowired
    private StorageCageService storageCageService;
    // 读取DB105区文件
    private PlcParameterObject plcmes = PLCAutoMes.PlcMesObject;
 
    public Short selectAll(GlassInfo glassInfo) {
        if (glassInfo == null) {
            // 有进片请求但是测量台没有玻璃
            return 400;
        }
 
        System.out.println(glassInfo + "初始");
        // 定义初始字段
        StorageCage cage;
        String glassid = glassInfo.getGlassid();
        Double width = glassInfo.getWidth();
        Double height = glassInfo.getHeight();
        String flowcard = glassInfo.getFlowcard();
        Integer glasstype = glassInfo.getGlasstype();
        Double thickness = glassInfo.getThickness();
        GlassInfo glassfilms = albaniaMapper.SelectGlassinfo(glasstype);
        // 判断是否有同类型的
        cage = albaniaMapper.SelectCage(glasstype, width, 300);
        // 如果有同类型时直接增加
        if (cage != null) {
            // 发送plc任务
            Mestast(glassid, 1001, cage.getId(), 1, "MESID1", 1);
            System.out.println("任务发送玻璃id:" + glassid + "起始位置:" + 1001 + "格子号:" + cage.getId() + "启动:" + 1);
 
        } else {
            // 判断玻璃是否进入大片笼
            if (glassInfo.getThickness() > 14) {
                cage = albaniaMapper.SelectNewCell(3, 5);
            } else {
                cage = albaniaMapper.SelectNewCell(0, 4);
 
                // System.out.println(cage + "理片笼判断");
            }
            // 当小片笼子不够,则符合条件的可以进入大片笼
            if (cage == null && glassInfo.getWidth() >= 1500 && glassInfo.getThickness() > 4) {
                cage = albaniaMapper.SelectNewCell(3, 5);
            }
            // 当返回的格子号为空时,返回400笼子已满
            if (cage != null) {
                albaniaMapper.AddCage(cage.getId(), glassid, width, height, 1, glasstype, 0, thickness,
                        glassfilms.getFilms(), glassfilms.getFlowcard(), glassfilms.getMateid(), glassfilms.getTier());
                Mestast(glassid, 1001, cage.getId(), 1, "MESID1", 1);
                System.out.println("任务发送玻璃id:" + glassid + "起始位置:" + 1001 + "格子号:" + cage.getId() + "启动:" + 1);
                albaniaMapper.UpdateStorage(width, height, cage.getId());
            } else {
                return (400);
            }
        }
        // 增加任务记录
        albaniaMapper.Inserttask(0, 0, 1001, cage.getId(), glassid, glasstype, flowcard, glassfilms.getMateid(),
                glassfilms.getTier());
        albaniaMapper.UpdateQueueState(glassid);
        return (200);
 
    }
 
    // 补充出片
    public Short mateOut() {
        double carwidth = -300;
 
        List<StorageTask> glassmate = albaniaMapper.SelectOutTask();
        String type = glassmate.get(0).getTaskType();
        if (glassmate.size() == 0) {
            return (400);
        } else {
            for (int i = 0; i < glassmate.size(); i++) {
                GlassInfo glass = albaniaMapper.SelectGlassInfo(glassmate.get(i).getGlasstype());
                carwidth = carwidth + glass.getWidth() + 300;
                if (carwidth >= 4300 && type != glassmate.get(i).getTaskType()) {
                    S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 出片任务启动
                    MestoPlc();
                    return (200);
                } else {
                    // 增加一条任务记录
                    albaniaMapper.Inserttask(1, 0, Integer.parseInt(glassmate.get(i).getShelfRack()), 2001,
                            glassmate.get(i).getGlassId(), glassmate.get(i).getGlasstype(),
                            glassmate.get(i).getFlowcard(), glassmate.get(i).getMateid(), glassmate.get(i).geTier());
                    // 完成出片队列的任务
                    albaniaMapper.UpdateOutTask(glassmate.get(i).getId());
                    // 发送plc任务
                    Mestast(glassmate.get(i).getGlassId(),
                            Integer.parseInt(glassmate.get(i).getShelfRack()), 2001, 0, "MESID" + (i + 1), i + 1);
                    System.out.println("发送出片队列配片任务:" + glassmate.get(i).getShelfRack());
                }
            }
            // 本次配片完成发送启动命令
            // S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),
            // (short) 1);// 出片任务启动
            while (true) {
                String currentValue = plcmes.getPlcParameter("MESToPLC").getValue();
                // 检查 MESToPLC 的值是否为 0
                if (currentValue.equals("1")) {
                    System.out.println("发送启动字现在为1,停止循环写入");
                    break; // 退出循环
                }
                // 如果不为 1,继续写入
                S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 任务发送字
                System.out.println("继续写入 MESToPLC,当前值:" + currentValue);
                try {
                    Thread.sleep(100); // 例如每秒检查一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            MestoPlc();
 
        }
        return (200);
    }
 
    // 出片
    public Short selectout(int line) {
        GlassInfo glassmate = new GlassInfo();
        GlassInfo method = new GlassInfo();
        StorageCage glass = new StorageCage();
        int glassend = 0;
        double carwidth = -300;
        double width = 0;
 
        method = albaniaMapper.SelectMethod(line);
        if (method == null) {
            return (400);
        }
        // 如果不是直通模式只出有完整配片的流程卡
        if (method.getMethod() == 0) {
            // glassmate= albaniaMapper.SelectOutGlass(1);
            // 判断有数量的配片id
            List<GlassInfo> peipianid = albaniaMapper.SelectOutGlass3(method.getFlowcard());
            if (peipianid.size() == 0) {
                System.out.println("peipianid:" + peipianid);
                return (400);
            } else {
                for (int j = 0; j < peipianid.size(); j++) {
                    // 判断配片宽度是否够上大车
                    // GlassInfo glasswidth =
                    // albaniaMapper.SelectOutGlass6(peipianid.get(j).getFlowcard(),peipianid.get(j).getMateid());
                    // if (glasswidth.getWidth() <= 4300) {
                    // 是否有足够的库存数量
                    List<String> peipiancage = albaniaMapper.SelectOutGlass4(peipianid.get(j).getFlowcard(),
                            peipianid.get(j).getMateid());
                    // 是否有足够的可出片数量
                    List<String> peipiancage2 = albaniaMapper.SelectOutGlass8(peipianid.get(j).getFlowcard(),
                            peipianid.get(j).getMateid());
                    // System.out.println("peipiancage:"+peipiancage+peipianid.get(j).getMateid());
                    // System.out.println("peipiancage2:"+peipiancage2);
                    if (!peipiancage.contains("不足")) {
                        List<GlassInfo> chupian = albaniaMapper.SelectOutGlass7(peipianid.get(j).getFlowcard(),
                                peipianid.get(j).getMateid());
                        for (int k = 0; k < chupian.size(); k++) {
                            glass = albaniaMapper.SelectCageGlass(chupian.get(k).getGlasstype());
                            carwidth = carwidth + glass.getGlassWidth() + 300;
                            if (carwidth <= 4300) {
                                // 发送配片数据
                                Mestast(chupian.get(k).getGlassid(), glass.getId(), 2001, 0, "MESID" + (k + 1),
                                        k + 1);
                                albaniaMapper.AddFinishNumber(chupian.get(k).getFlowcard(),
                                        chupian.get(k).getMateid(), chupian.get(k).getTier());
                                storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
                                albaniaMapper.Inserttask(1, 0, glass.getId(), 2001, chupian.get(k).getGlassid(),
                                        chupian.get(k).getGlasstype(), chupian.get(k).getFlowcard(),
                                        chupian.get(k).getMateid(), chupian.get(k).getTier());
                            } else {
                                albaniaMapper.AddFinishNumber(chupian.get(k).getFlowcard(),
                                        chupian.get(k).getMateid(), chupian.get(k).getTier());
                                storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
                                // 增加出片队列的任务
                                albaniaMapper.InsertOutTask(1, 0, glass.getId(), 2001, chupian.get(k).getGlassid(),
                                        chupian.get(k).getGlasstype(), chupian.get(k).getFlowcard(),
                                        chupian.get(k).getMateid(), chupian.get(k).getTier());
                            }
 
                        }
                        // 本次配片完成发送启动命令
                        // S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),(short)
                        // 1);// 出片任务启动
                        MestoPlc();
                        return (200);// 结束
                    }
                    // }
                    // else {
                    // continue;
                    // }
                }
                System.out.println("未有完整配片");
                return (400);
            }
 
        } else {
            for (int i = 1; i <= 7; i++) {
                // 直通模式时按顺序直出
                // glassmate = albaniaMapper.SelectOutGlass(1);
                glassmate = albaniaMapper.SelectOutGlass2();
 
                System.err.println("出片:" + glassmate);
                if (glassmate == null && i > 1) {
                    // 本次配片完成发送启动命令
                    // 出片任务启动
                    MestoPlc();
                    System.out.println("出完任务:1,任务状态:0,起始:" + glass.getId() + ",目标" + "2001");
                    return (200);// 结束
                }
                if (glassmate == null) {
                    // 未领取任务
                    System.out.println("未领取任务:" + i);
                    return (400);
 
                } else {
                    glass = albaniaMapper.SelectCageGlass(glassmate.getGlasstype());
                    System.out.println("出片glass:" + glass);
                    if (glass != null) {
                        // 发送任务之前判断车上的剩余宽度是否大于此玻璃
                        width = width + glassmate.getWidth() + 300;
                        // 大于车上空余宽度时提前发送出片启动
                        if (width > 4300) {
                            // 本次配片完成发送启动命令
                            MestoPlc();
                            return (200);// 结束
                        }
                        // 发送配片数据
                        Mestast(glass.getGlassId(), glass.getId(), 2001, 0, "MESID" + i, i);
                        albaniaMapper.AddFinishNumber(glassmate.getFlowcard(), glassmate.getMateid(),
                                glassmate.getTier());
                        storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
                        albaniaMapper.Inserttask(1, 0, glass.getId(), 2001, glassmate.getGlassid(),
                                glassmate.getGlasstype(), glass.getFlowcard(), glassmate.getMateid(),
                                glassmate.getTier());
                    } else {
                        return (400);// 结束
                    }
 
                }
            }
        }
        return (200);// 结束
    }
 
    // 派发任务玻璃id
    public void outmesid(String glassid, String address) {
        // 读取DB105区文件
        PlcParameterObject plcmes = PLCAutoMes.PlcMesObject;
        // System.out.println("outmesid:" + glassid);
        List<Byte> glassidlist = new ArrayList();
        char ds[] = glassid.toCharArray();
        for (char iditem : ds) {
            glassidlist.add((byte) iditem);
        }
        byte[] bytes = Bytes.toArray(glassidlist);
        // writeString
        System.out.println("outmesidbytes:" + bytes.length);
        System.out.println(plcmes.getPlcParameter(address).getAddress());
        System.out.println();
 
    }
 
    // 获取地址内的玻璃id转字符串
    public StringBuilder queGlassid(String address, int count) {
        StringBuilder writedstrIdOut = new StringBuilder();
        byte[] writedglassidbytesOut = S7control.getinstance().ReadByte(address, count);
        if (writedglassidbytesOut != null) {
            // 获取玻璃id
            for (byte iditem : writedglassidbytesOut) {
                writedstrIdOut.append((char) iditem);
            }
        }
        return writedstrIdOut;
    }
 
    // char数组转化成bit数组
    public static byte[] toBytes(char[] chars) {
        String s = new String(chars);
        return s.getBytes(StandardCharsets.UTF_8);
    }
 
    // bit数组转化成char数组
    public static char[] toChars(byte[] bytes) {
        String s = new String(bytes, StandardCharsets.UTF_8);
        return s.toCharArray();
    }
 
    // 判断集合里是否为1
    public boolean listbool(String address) {
        List<Short> list = S7control.getinstance().ReadWord(address, 1);// 返回为ture时为1
        boolean listbool = list.contains((short) 1);
        return listbool;
    }
 
    // 下发理片任务
    public void Mestast(String glassid, int MESToPLCStart1, int MESToPLCTarget1, int MESToPLC, String address, int i) {
        S7control.getinstance().writeString(plcmes.getPlcParameter(address).getAddress(), glassid);// 玻璃id
        S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStart" + i).getAddress(),
                (short) MESToPLCStart1);// 起始位置
        S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCTarget" + i).getAddress(),
                (short) MESToPLCTarget1);// 目标位置
        if (MESToPLC != 0) {
            // S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),
            // (short) MESToPLC);// mes理片任务类型
            MestoPlc();
        }
    }
 
    // 扫码增加队列任务
    public boolean scan(String address) {
        List<Short> list = S7control.getinstance().ReadWord(address, 1);// 返回为ture时为1
        boolean listbool = list.contains((short) 1);
        return listbool;
    }
 
    public void MestoPlc() {
        while (true) {
            String currentValue = plcmes.getPlcParameter("MESToPLC").getValue();
            // 检查 MESToPLC 的值是否为 0
            if (currentValue.equals("1")) {
                System.out.println("发送启动字现在为1,停止循环写入");
                break; // 退出循环
            }
            // 如果不为 1,继续写入
            S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 任务发送字
            System.out.println("继续写入 MESToPLC,当前值:" + currentValue);
            try {
                Thread.sleep(100); // 例如每秒检查一次
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
}