ZengTao
2025-03-28 f68d3c71819feb59e7a227a5d992b059b900916c
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
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import cn.smallbun.screw.core.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mes.common.config.Const;
import com.mes.downglassinfo.entity.DownGlassInfo;
import com.mes.downglassinfo.entity.DownGlassTask;
import com.mes.downglassinfo.entity.request.DownGlassInfoRequest;
import com.mes.downglassinfo.service.DownGlassInfoService;
import com.mes.downglassinfo.service.DownGlassTaskService;
import com.mes.downstorage.service.DownStorageCageService;
import com.mes.downworkstation.entity.DownWorkstation;
import com.mes.downworkstation.service.DownWorkstationService;
import com.mes.glassinfo.entity.GlassInfo;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.tools.DateUtil;
import com.mes.tools.WebSocketServer;
import com.mes.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class Downpush {
 
    @Autowired
    private DownWorkstationService downWorkstationService;
    @Autowired
    private DownStorageCageService downStorageCageService;
    @Autowired
    private DownGlassInfoService downGlassInfoService;
    @Autowired
    private DownGlassTaskService downGlassTaskService;
    @Autowired
    private GlassInfoService glassInfoService;
 
    @Value("${mes.scan.ip}")
    private String scanIp;
 
    @Value("${mes.scan.port}")
    private Integer scanPort;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Scheduled(fixedDelay = 3000)
    public void sendDownWorkstations() {
 
        JSONObject jsonObject = new JSONObject();
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unloadglass");
        if (sendwServer != null) {
            log.info("发送工位玻璃信息");
            List<DownWorkstation> data = downWorkstationService.list();
            List<DownGlassTask> downGlassTasks = downGlassTaskService.list(
                    new LambdaQueryWrapper<DownGlassTask>()
                            .eq(DownGlassTask::getEndCell, 7)
                            .eq(DownGlassTask::getTaskType, 3)
                            .lt(DownGlassTask::getTaskStatus, 3)
            );
            DownWorkstation downWorkstation = new DownWorkstation();
            downWorkstation.setWorkstationId(7);
            downWorkstation.setEnableState(1);
            downWorkstation.setRacksNumber(downGlassTasks.size());
            downWorkstation.setTotalQuantity(downGlassTasks.size());
            data.add(downWorkstation);
            jsonObject.append("params", data);
            log.info(jsonObject.toString());
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null && webserver.session.isOpen()) {
                    log.info("已发送");
                    webserver.sendMessage(jsonObject.toString());
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 3000)
    public void sendGlassInfoData() {
 
        ArrayList<WebSocketServer> sendwServer2 = WebSocketServer.sessionMap.get("unloadglass2");
        if (sendwServer2 != null) {
            log.info("发送工位显示图信息1");
            JSONObject jsonObject2 = new JSONObject();
            List<Map<String, Object>> glassinfodata = downWorkstationService.getTotalGlassDimensionsByWorkstation(1, 3);
            jsonObject2.append("glassinfo", glassinfodata);
            log.info(jsonObject2.toString());
            for (WebSocketServer webserver : sendwServer2) {
                if (webserver != null && webserver.session.isOpen()) {
                    webserver.sendMessage(jsonObject2.toString());
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 3000)
    public void sendGlassInfoData2() {
        ArrayList<WebSocketServer> sendwServer3 = WebSocketServer.sessionMap.get("unloadglass3");
        if (sendwServer3 != null) {
            log.info("发送工位显示图信息2");
            JSONObject jsonObject3 = new JSONObject();
            List<Map<String, Object>> glassinfodata2 = downWorkstationService.getTotalGlassDimensionsByWorkstation(4, 6);
            jsonObject3.append("glassinfo2", glassinfodata2);
            for (WebSocketServer webserver : sendwServer3) {
                if (webserver != null && webserver.session.isOpen()) {
                    webserver.sendMessage(jsonObject3.toString());
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 3000)
    public void sendDownStorageCages() {
        log.info("发送缓存玻璃信息");
        JSONObject jsonObject4 = new JSONObject();
        List<Map<String, Object>> list = downStorageCageService.selectDownStorageCages();
        jsonObject4.append("params2", list);
        log.info(jsonObject4.toString());
        ArrayList<WebSocketServer> sendwServer4 = WebSocketServer.sessionMap.get("downcache");
        if (sendwServer4 != null) {
            for (WebSocketServer webserver : sendwServer4) {
                if (webserver != null && webserver.session.isOpen()) {
                    webserver.sendMessage(jsonObject4.toString());
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 3000)
    public void sendDownGlassInfo() {
 
        ArrayList<WebSocketServer> sendwServer4 = WebSocketServer.sessionMap.get("unloadglass");
        if (sendwServer4 != null) {
            DownGlassInfoRequest request = redisUtil.getCacheObject("downGlassRequest");
            if (null == request) {
                request = new DownGlassInfoRequest();
            }
            if (request.getBeginDate() == null) {
                request.setBeginDate(DateUtil.getBeginDate());
                request.setEndDate(DateUtil.getEndDate());
            }
            log.info("发送当前正在执行工程已落架的玻璃信息");
            LambdaQueryWrapper<DownGlassInfo> wrapper = new LambdaQueryWrapper<DownGlassInfo>()
                    .between(null != request.getBeginDate(), DownGlassInfo::getGmtCreate, request.getBeginDate(), request.getEndDate())
                    .eq(StringUtils.isNotBlank(request.getEngineerId()), DownGlassInfo::getEngineerId, request.getEngineerId());
            if (null == request.getWorkStationId() || 0 == request.getWorkStationId()) {
                wrapper.in(DownGlassInfo::getWorkStationId, Const.WORK_STATION_ALL);
            } else {
                wrapper.eq(DownGlassInfo::getWorkStationId, request.getWorkStationId());
            }
            List<DownGlassInfo> downGlassInfos = downGlassInfoService.list(wrapper);
//        Map<Integer, List<DownGlassInfo>> listMap = downGlassInfos.stream().collect(Collectors.groupingBy(DownGlassInfo::getWorkStationId));
            List<Object> engineerIdListTemp = downGlassInfoService.listObjs(new QueryWrapper<DownGlassInfo>().select("distinct engineer_id"));
            List<String> engineerIdList = engineerIdListTemp.stream().map(String::valueOf).collect(Collectors.toList());
            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.append("downGlassInfos", downGlassInfos);
            jsonObject4.append("engineerIdList", engineerIdList);
 
            //打印开关
            boolean autoPrint = false;
            if (redisUtil.getCacheObject("autoPrint") == null) {
                redisUtil.setCacheObject("autoPrint", false);
            } else {
                autoPrint = redisUtil.getCacheObject("autoPrint");
            }
            jsonObject4.append("autoPrint", autoPrint);
            //人工下片位置最后一片
            DownGlassTask takeGlass = downGlassTaskService.getOne(
                    new QueryWrapper<DownGlassTask>()
                            .select("Top 1 *")
                            .eq("end_cell", Const.G13_WORK_STATION)
                            .eq("task_type", 3)
                            .lt("task_status", Const.UNLOAD_GLASS_DOWN)
                            .orderByAsc("id")
            );
            if (takeGlass != null) {
                jsonObject4.append("takeGlass", takeGlass);
            }
 
            for (WebSocketServer webserver : sendwServer4) {
                if (webserver != null && webserver.session.isOpen()) {
                    webserver.sendMessage(jsonObject4.toString());
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 1000)
    public void isRun() {
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unLoadGlassIsRun");
        if (sendwServer != null) {
            JSONObject jsonObject = new JSONObject();
            //正在进行的任务
            List<DownWorkstation> downWorkstation = downWorkstationService.getoneDownWorkstations(1, 6);
            jsonObject.append("downWorkstation", downWorkstation);
            List<DownGlassTask> downGlassTask = downGlassTaskService.selectOutTaskCache();
            jsonObject.append("downGlassTask", downGlassTask);
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null) {
                    webserver.sendMessage(jsonObject.toString());
                } else {
                    log.info("unLoadGlassIsRun is closed");
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = Long.MAX_VALUE)
    public void scanCodeTask() {
        log.info("扫描任务已启动");
        while (true) {
            JSONObject jsonObject = new JSONObject();
            try (Socket socket = new Socket(scanIp, scanPort)) {
                socket.setSoTimeout(1000);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 接收服务器响应
                log.info("等待扫码中......");
                String glassId = in.readLine();
                log.info("扫描到的玻璃id:{}", glassId);
                List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unloadglass");
                if (CollectionUtils.isNotEmpty(sendwServer)) {
                    //按照玻璃id获取玻璃信息返回给前端界面,具体需要哪些数据待确认
                    GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>()
                            .eq(GlassInfo::getGlassId, glassId).last("limit 1"));
                    if (null == glassInfo) {
                        log.info("按照玻璃id:{},无法找到玻璃信息", glassId);
                    } else {
                        for (WebSocketServer webserver : sendwServer) {
                            jsonObject.append("scanGlass", glassInfo);
                            webserver.sendMessage(jsonObject.toString());
                        }
                    }
                }
            } catch (Exception exception) {
                log.info("读取异常,原因为{}", exception.getMessage());
            }
        }
    }
}