ZengTao
2025-06-26 849442e4f63a5fd7ae154ef6bc77c967a82f40b8
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
package com.mes.job;
 
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.xingshuangs.iot.protocol.s7.serializer.S7Serializer;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.mes.common.config.Const;
import com.mes.damage.service.DamageService;
import com.mes.largenscreen.entity.PieChartVO;
import com.mes.s7.entity.S7DataGHTwo;
import com.mes.temperingglass.entity.TemperingGlassInfo;
import com.mes.temperingglass.service.TemperingGlassInfoService;
import com.mes.tools.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author SNG-010
 */
@Component
@Slf4j
public class PlcTemperingGlassTask {
 
    @Autowired
    private TemperingGlassInfoService temperingAgoService;
    @Autowired
    private DamageService damageService;
 
    @Autowired
    @Qualifier("s7SerializerGHTwo")
    private S7Serializer s7SerializerGHTwo;
 
    /**
     * fixedRate : 上一个调用开始后再次调用的延时(不用等待上一次调用完成)
     * fixedDelay : 上一个调用结束后再次调用的延时
     */
 
    private List<TemperingGlassInfo> selectWaitingGlassByOpc() {
        //获取等待进炉中的玻璃信息
        try {
            S7DataGHTwo s7DataGHTwo = s7SerializerGHTwo.read(S7DataGHTwo.class);
            String engineerEntity = s7DataGHTwo.getF09EngineerId();
            Integer temperingLayoutIdEntity = s7DataGHTwo.getF09TemperingLayoutId();
            if (null == engineerEntity || null == temperingLayoutIdEntity) {
                engineerEntity = s7DataGHTwo.getF08EngineerId();
                temperingLayoutIdEntity = s7DataGHTwo.getF08TemperingLayoutId();
                if (null == engineerEntity || null == temperingLayoutIdEntity) {
                    log.info("获取参数异常,结束本次任务");
                    return new ArrayList<>();
                }
            }
            return temperingAgoService.list(new LambdaQueryWrapper<TemperingGlassInfo>()
                    .eq(TemperingGlassInfo::getEngineerId, engineerEntity)
                    .eq(TemperingGlassInfo::getTemperingLayoutId, temperingLayoutIdEntity));
        } catch (Exception e) {
            log.info("获取钢化参数异常:{}", e);
            return new ArrayList<>();
        }
    }
 
    @Scheduled(fixedDelay = 1000)
    public void temperingGlassHome() {
        JSONObject jsonObject = new JSONObject();
        //正在等待进片的玻璃
        List<TemperingGlassInfo> waitingGlass = selectWaitingGlassByOpc();
        if (waitingGlass != null) {
            jsonObject.append("waitingGlass", waitingGlass);
        }
 
        //获取整在炉中的两个版图id
        List<TemperingGlassInfo> layoutId = temperingAgoService.selectLayoutId();
        //进炉中的玻璃
        if (!layoutId.isEmpty()) {
            List<TemperingGlassInfo> intoGlass = temperingAgoService.selectIntoGlass(layoutId.get(0));
            jsonObject.append("intoGlass", intoGlass);
            //进炉中的第二个版图
            if (layoutId.size() > 1) {
                List<TemperingGlassInfo> intoGlass2 = temperingAgoService.selectIntoGlass(layoutId.get(1));
                jsonObject.append("intoGlass2", intoGlass2);
            }
        }
        //出炉后的玻璃
        List<TemperingGlassInfo> outGlass = temperingAgoService.selectOutGlass();
        if (outGlass != null) {
            jsonObject.append("outGlass", outGlass);
        }
 
        //过旋转台钢化后的玻璃
        List<TemperingGlassInfo> overGlass = temperingAgoService.selectOverGlass();
        if (outGlass != null) {
            jsonObject.append("overGlass", overGlass);
        }
 
 
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("temperingGlass");
        if (sendwServer != null) {
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null) {
                    webserver.sendMessage(jsonObject.toString());
                } else {
                    log.info("Home is closed");
                }
            }
        }
    }
 
    @Scheduled(fixedDelay = 1000)
    public void temperingIsRun() {
        JSONObject jsonObject = new JSONObject();
        //正在进行的任务
        List<TemperingGlassInfo> temperingTaskType = temperingAgoService.selectTaskType();
        jsonObject.append("temperingTaskType", temperingTaskType);
 
 
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("temperingIsRun");
        if (sendwServer != null) {
            for (WebSocketServer webserver : sendwServer) {
                if (webserver != null) {
                    webserver.sendMessage(jsonObject.toString());
                } else {
                    log.info("Home is closed");
                }
            }
        }
 
    }
 
    @Scheduled(fixedDelay = 1000)
    public void largenScreen() {
        JSONObject jsonObject = new JSONObject();
        //大屏钢化信息
        Date startOfToday = new Date(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
 
        Integer putGlass = temperingAgoService.count(
                new QueryWrapper<TemperingGlassInfo>()
                        .eq("state", Const.TEMPERING_NEW)
                        .gt("create_time", startOfToday)
        );
        jsonObject.append("temperingTaskType", putGlass);
        //大屏钢化信息
        List<TemperingGlassInfo> temperingGlassInfoList = temperingAgoService.list(
                new QueryWrapper<TemperingGlassInfo>()
                        .select("engineer_id", "tempering_layout_id")
                        .eq("state", Const.TEMPERING_START)
                        .gt("create_time", startOfToday)
                        .groupBy("engineer_id", "tempering_layout_id")
        );
        jsonObject.append("temperingGlassInfoList", temperingGlassInfoList.size());
        //大屏钢化信息
        List<TemperingGlassInfo> temperingGlassInfoInList = temperingAgoService.list(
                new LambdaQueryWrapper<TemperingGlassInfo>()
                        .select(TemperingGlassInfo::getEngineerId, TemperingGlassInfo::getTemperingLayoutId) // 选择要去重的字段
                        .eq(TemperingGlassInfo::getState, Const.TEMPERING_DROP)
                        .groupBy(TemperingGlassInfo::getEngineerId, TemperingGlassInfo::getTemperingLayoutId) // 按 engineerId 和 temperingLayoutId 分组
        );
        jsonObject.append("temperingGlassInfoInList", temperingGlassInfoInList.size());
        //钢化饼图数据
        List<PieChartVO> pieChartVOS = temperingAgoService.queryPieChart();
        jsonObject.append("pieChartVOS", pieChartVOS);
        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("largenScreen");
        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("largenScreen is closed");
                }
            }
        }
    }
 
}