wangfei
2025-05-13 0b41a5c59028a61bff22f60e74cb1d6d53a40e61
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
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.service.MiloService;
import com.mes.bigstorage.entity.BigStorageCageDetails;
import com.mes.bigstorage.entity.dto.BigStorageSummaryDTO;
import com.mes.bigstorage.service.BigStorageCageDetailsService;
import com.mes.bigstorage.service.BigStorageCageService;
import com.mes.bigstoragecagetask.entity.BigStorageCageHistoryTask;
import com.mes.bigstoragecagetask.entity.BigStorageCageTask;
import com.mes.bigstoragecagetask.entity.BigStorageTaskVO;
import com.mes.bigstoragecagetask.service.BigStorageCageHistoryTaskService;
import com.mes.bigstoragecagetask.service.BigStorageCageTaskService;
import com.mes.common.config.Const;
import com.mes.glassinfo.service.GlassInfoService;
import com.mes.largenscreen.entity.PieChartVO;
import com.mes.largenscreen.entity.RunTime;
import com.mes.temperingglass.entity.TemperingGlassInfo;
import com.mes.temperingglass.service.TemperingGlassInfoService;
import com.mes.tools.WebSocketServer;
import com.mes.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @author SNG-015
 */
@Component
@Slf4j
public class OPCPlcSlicecage {
 
    @Resource
    private BigStorageCageService bigStorageCageService;
    @Resource
    private BigStorageCageDetailsService bigStorageCageDetailsService;
    @Resource
    private TemperingGlassInfoService temperingGlassInfoService;
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private GlassInfoService glassInfoService;
    @Resource
    private BigStorageCageTaskService bigStorageCageTaskService;
    @Resource
    private BigStorageCageHistoryTaskService bigStorageCageHistoryTaskService;
 
    @Autowired(required = false)
    MiloService miloService;
 
    private JSONObject jsonObject = new JSONObject();
 
    public void queryDataSource1() throws Exception {
        List<Double> carPostion = new ArrayList<>();
        carPostion.add(0.25);
        carPostion.add(0.5);
        jsonObject.append("carPostion", carPostion);
        //界面展示笼子信息
        jsonObject.append("bigStorageCageInfos", bigStorageCageDetailsService.querybigStorageCageDetail());
 
        try {
            //进片任务数据
            List<BigStorageTaskVO> inTaskList = new ArrayList();
            ReadWriteEntity fromOpcUa = miloService.readFromOpcUa("DLP1A.DLP1A.FROM1");
            for (int i = 1; i <= 6; i++) {
                ReadWriteEntity requestWord = miloService.readFromOpcUa("DLP1A.DLP1A.DI" + i);
                ReadWriteEntity targetSlotWord = miloService.readFromOpcUa("DLP1A.DLP1A.TO" + i);
                ReadWriteEntity stateWord = miloService.readFromOpcUa("DLP1A.DLP1A.STATE" + i);
                if (null != requestWord.getValue()) {
                    BigStorageTaskVO task = new BigStorageTaskVO();
                    task.setGlassId(requestWord.getValue() + "");
                    int isExistCount = bigStorageCageDetailsService.count(new LambdaQueryWrapper<BigStorageCageDetails>().in(BigStorageCageDetails::getGlassId, task.getGlassId())
                            .in(BigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL));
                    if (isExistCount > 0) {
                        task.setIsSame(1);
                    } else {
                        task.setIsSame(0);
                    }
                    task.setStartSlot(Integer.parseInt(fromOpcUa.getValue() + ""));
                    task.setTargetSlot(Integer.parseInt(targetSlotWord.getValue() + ""));
                    task.setTaskState(Integer.parseInt(stateWord.getValue() + ""));
                    inTaskList.add(task);
                    continue;
                }
            }
            jsonObject.append("bigStorageCageDetailsFeedTask", inTaskList);
        } catch (Exception e) {
            //todo:不做任务处理
        }
        try {
            //进片联机
            ReadWriteEntity inkageEntity = miloService.readFromOpcUa("DLP1A.DLP1A.mesControl");
            jsonObject.append("inkageEntity", inkageEntity.getValue());
            //进片请求
            ReadWriteEntity requestEntity = miloService.readFromOpcUa("DLP1A.DLP1A.RequestMes");
            jsonObject.append("requestEntity", requestEntity.getValue());
            //启动命令
            ReadWriteEntity mesReplyEntity = miloService.readFromOpcUa("DLP1A.DLP1A.MesReply");
            jsonObject.append("mesReplyEntity", mesReplyEntity.getValue());
            //出片联机
            ReadWriteEntity outInkageEntity = miloService.readFromOpcUa("DLP1B.DLP1B.mesControl");
            jsonObject.append("outInkageEntity", outInkageEntity.getValue());
            //出片请求
            ReadWriteEntity outRequestEntity = miloService.readFromOpcUa("DLP1B.DLP1B.RequestMes");
            jsonObject.append("outRequestEntity", outRequestEntity.getValue());
 
 
            //出片任务数据
            List<BigStorageCageTask> outTaskList = bigStorageCageTaskService.queryTaskMessage("big_storage_cage_out_one_task");
            jsonObject.append("bigStorageCageDetailsOutTask", outTaskList);
        } catch (Exception e) {
            //todo:不做任务处理
        }
 
        //钢化开关
        boolean temperingSwitch = false;
        if (redisUtil.getCacheObject("temperingSwitch") == null) {
            redisUtil.setCacheObject("temperingSwitch", false);
        } else {
            temperingSwitch = redisUtil.getCacheObject("temperingSwitch");
        }
        jsonObject.append("temperingSwitch", temperingSwitch);
 
        //调度开关
        boolean dispatchSwitch = false;
        if (redisUtil.getCacheObject("dispatchSwitch") == null) {
            redisUtil.setCacheObject("dispatchSwitch", false);
        } else {
            dispatchSwitch = redisUtil.getCacheObject("dispatchSwitch");
        }
        jsonObject.append("dispatchSwitch", dispatchSwitch);
 
        //理片笼使用情况
        List<Map<String, Object>> bigStorageCageUsage = bigStorageCageService.selectBigStorageCageUsage();
        jsonObject.append("bigStorageCageUsage", bigStorageCageUsage);
 
        //大理片汇总信息
        List<BigStorageSummaryDTO> bigStorageSummary = bigStorageCageService.selectBigStorageSummary();
        jsonObject.append("bigStorageSummary", bigStorageSummary);
 
        //打印开关
        boolean autoPrint = false;
        if (redisUtil.getCacheObject("autoPrint") == null) {
            redisUtil.setCacheObject("autoPrint", false);
        } else {
            autoPrint = redisUtil.getCacheObject("autoPrint");
        }
        jsonObject.append("autoPrint", autoPrint);
 
        //当前指定工程
        String temperingengineerId = "";
        if (redisUtil.getCacheObject("temperingengineerId") == null) {
            redisUtil.setCacheObject("temperingengineerId", temperingengineerId);
        } else {
            temperingengineerId = redisUtil.getCacheObject("temperingengineerId");
        }
        jsonObject.append("temperingengineerId", temperingengineerId);
 
 
    }
 
    public void queryDataSource2() throws InterruptedException {
        //出片队列
        List<TemperingGlassInfo> temperingGlassInfoList = temperingGlassInfoService.list(
                new LambdaQueryWrapper<TemperingGlassInfo>()
                        .inSql(TemperingGlassInfo::getEngineerId, "select distinct engineer_id from tempering_glass_info where state<4")
        );
        jsonObject.append("temperingGlassInfoList", temperingGlassInfoList);
 
    }
 
    /**
     * fixedRate : 上一个调用开始后再次调用的延时(不用等待上一次调用完成)
     * fixedDelay : 上一个调用结束后再次调用的延时
     */
    @Scheduled(fixedDelay = 3000)
    public void plcStorageCageTask() throws InterruptedException {
        jsonObject = new JSONObject();
        try {
            //查询使用数据源1查询数据
            queryDataSource1();
            //查询使用数据源2查询数据
//            queryDataSource2();
 
            ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("slicecage");
            if (sendwServer != null) {
                for (WebSocketServer webserver : sendwServer) {
                    if (webserver != null) {
                        webserver.sendMessage(jsonObject.toString());
                        List<String> messages = webserver.getMessages();
                        if (!messages.isEmpty()) {
                            // // 将最后一个消息转换为整数类型的列表
                            webserver.clearMessages();
                        }
                    } else {
                        log.info("Home is closed");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Scheduled(fixedDelay = 1000)
    public void largenScreen() {
        JSONObject jsonObject = new JSONObject();
        //理片笼使用情况
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("largenScreen");
        if (sendwServer != null) {
            List<Map<String, Object>> bigStorageCageUsage = bigStorageCageService.selectBigStorageCageUsage();
            jsonObject.append("bigStorageCageUsage", bigStorageCageUsage);
            List<PieChartVO> pieChartVOS = bigStorageCageService.queryPieChart();
            jsonObject.append("pieChartVOS", pieChartVOS);
            List<RunTime> tempRunTimes = bigStorageCageHistoryTaskService.queryRunTimes();
            jsonObject.append("tempRunTimes", tempRunTimes);
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null) {
                    webserver.sendMessage(jsonObject.toString());
                    List<String> messages = webserver.getMessages();
                    if (!messages.isEmpty()) {
                        // // 将最后一个消息转换为整数类型的列表
                        webserver.clearMessages();
                    }
                } else {
                    log.info("largenScreen is closed");
                }
            }
        }
    }
}