Junjie
9 天以前 1c7abc2dd0e4a4f2879af4eb00fb7eb4346fbf07
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
package com.zy.asrs.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.Cools;
import com.zy.system.entity.Announcement;
import com.zy.asrs.entity.BasStationTv;
import com.zy.asrs.entity.TvDevice;
import com.zy.asrs.entity.dto.TvDataDto;
import com.zy.asrs.entity.dto.WcsStationDto;
import com.zy.asrs.enums.RedisKeyType;
import com.zy.asrs.utils.StationUtils;
import com.zy.asrs.websocket.TvWebSocketServer;
import com.zy.common.model.WebSocketMessage;
import com.zy.common.utils.RedisUtil;
import com.zy.system.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
@Slf4j
@Service
public class TvDataPushService {
 
    @Autowired
    private TvWebSocketServer tvWebSocketServer;
    @Autowired
    private TvDeviceService tvDeviceService;
    @Autowired
    private BasStationTvService basStationTvService;
    @Autowired
    private StationUtils stationUtils;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AnnouncementService announcementService;
 
    private static final String[] WEEK = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
 
 
    @Scheduled(fixedRate = 1000)
    public void pushAll() {
        if (tvWebSocketServer.getOnlineIps().isEmpty()) {
            return;
        }
        pushLedInfosAndError();
        pushLocData();
        pushLineCharts();
        pushLocDetlStatistics();
        pushAnnouncement();
        pushDate();
    }
 
    private void pushLedInfosAndError() {
        String manualError = "";
        Object manualErrorObj = redisUtil.get(RedisKeyType.TV_MANUAL_ERROR_MSG.key);
        if (manualErrorObj != null) {
            manualError = String.valueOf(manualErrorObj);
        }
 
        for (String ip : tvWebSocketServer.getOnlineIps()) {
            TvDevice tvDevice = tvDeviceService.getOne(
                    new QueryWrapper<TvDevice>().eq("ip", ip));
            if (tvDevice == null) {
                // 非注册设备(如调试页面),推送空数据和通用错误
                Map<String, Object> ledData = new HashMap<>();
                ledData.put("deviceName", "未注册设备(" + ip + ")");
                ledData.put("data", new ArrayList<>());
                tvWebSocketServer.sendMessageToDevice(ip, buildMessage("ledInfos", ledData));
 
                Map<String, Object> errorData = new HashMap<>();
                errorData.put("errorMsg", manualError != null ? manualError : "");
                tvWebSocketServer.sendMessageToDevice(ip, buildMessage("error", errorData));
                continue;
            }
 
            List<BasStationTv> relations = basStationTvService
                    .list(new QueryWrapper<BasStationTv>().eq("tv_id", tvDevice.getId()));
 
            List<TvDataDto> dataList = new ArrayList<>();
            Set<String> errors = new LinkedHashSet<>();
 
            if (manualError != null && !manualError.isEmpty()) {
                errors.add(manualError);
            }
 
            if (relations != null && !relations.isEmpty()) {
                for (BasStationTv relation : relations) {
                    WcsStationDto wcsStationDto = stationUtils.stationMap.get(relation.getStationId());
                    if (wcsStationDto == null || wcsStationDto.getLoading() != 1) {
                        continue;
                    }
 
                    String errorMsg = "";
                    if (!Cools.isEmpty(wcsStationDto.getErrorMsg())) {
                        errorMsg += wcsStationDto.getErrorMsg();
                    }
                    if (!Cools.isEmpty(wcsStationDto.getSystemWarning())) {
                        errorMsg += wcsStationDto.getSystemWarning();
                    }
                    if (!Cools.isEmpty(errorMsg)) {
                        errors.add(errorMsg);
                    }
 
                    TvDataDto tvDataDto = new TvDataDto();
                    tvDataDto.setStationId(wcsStationDto.getStationId());
                    tvDataDto.setTaskNo(wcsStationDto.getTaskNo());
                    tvDataDto.setBarcode(wcsStationDto.getBarcode());
                    tvDataDto.setErrorMsg(errorMsg);
                    tvDataDto.setIoType(wcsStationDto.getIoType());
                    tvDataDto.setWrkDetls(wcsStationDto.getWrkDetls());
                    tvDataDto.setErrorCode(Cools.isEmpty(errorMsg) ? 0 : 1);
                    dataList.add(tvDataDto);
                }
            }
 
            // 推送 ledInfos
            Map<String, Object> ledData = new HashMap<>();
            ledData.put("deviceName", tvDevice.getName());
            ledData.put("data", dataList);
            tvWebSocketServer.sendMessageToDevice(ip, buildMessage("ledInfos", ledData));
 
            // 推送 error
            Map<String, Object> errorData = new HashMap<>();
            errorData.put("errorMsg", String.join(";", errors));
            tvWebSocketServer.sendMessageToDevice(ip, buildMessage("error", errorData));
        }
    }
 
    private void pushLocData() {
        Object o = redisUtil.get(RedisKeyType.TV_LOC_DATA_DTO.key);
        String currentData = o != null ? o.toString() : "{}";
        tvWebSocketServer.sendMessageToAll(buildMessage("locData", JSON.parseObject(currentData)));
    }
 
    private void pushLineCharts() {
        Object o = redisUtil.get(RedisKeyType.TV_LINE_CHARTS.key);
        if (o == null) {
            return;
        }
        String currentData = o.toString();
        tvWebSocketServer.sendMessageToAll(buildMessage("lineCharts", JSON.parseObject(currentData)));
    }
 
    private void pushLocDetlStatistics() {
        Object o = redisUtil.get(RedisKeyType.TV_LOC_DETL_STATISTICS.key);
        if (o == null) {
            return;
        }
        String currentData = o.toString();
        tvWebSocketServer.sendMessageToAll(buildMessage("locDetlStatistics", JSON.parseArray(currentData)));
    }
 
    private void pushAnnouncement() {
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderBy(true, false, "create_time");
        wrapper.last("limit 5");
        List<Announcement> list = announcementService.list(wrapper);
        tvWebSocketServer.sendMessageToAll(buildMessage("announcement", list));
    }
 
    private void pushDate() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        Map<String, Object> dateMap = new HashMap<>();
        dateMap.put("year", calendar.get(Calendar.YEAR));
        dateMap.put("month", zerofill(String.valueOf(calendar.get(Calendar.MONTH) + 1), 2));
        dateMap.put("day", zerofill(String.valueOf(calendar.get(Calendar.DATE)), 2));
        dateMap.put("hour", zerofill(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), 2));
        dateMap.put("minute", zerofill(String.valueOf(calendar.get(Calendar.MINUTE)), 2));
        dateMap.put("second", zerofill(String.valueOf(calendar.get(Calendar.SECOND)), 2));
        dateMap.put("week", WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1]);
        tvWebSocketServer.sendMessageToAll(buildMessage("date", dateMap));
    }
 
    private String buildMessage(String url, Object data) {
        WebSocketMessage message = new WebSocketMessage();
        message.setUrl(url);
        message.setData(JSON.toJSONString(data));
        return JSON.toJSONString(message);
    }
 
    private String zerofill(String str, int length) {
        while (str.length() < length) {
            str = "0" + str;
        }
        return str;
    }
}