#
Junjie
2026-01-24 241ab413828a3627a123ae001fc05f5c35031861
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
package com.zy.asrs.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.Cools;
import com.core.common.R;
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.TvLocDataDto;
import com.zy.asrs.entity.dto.WcsStationDto;
import com.zy.asrs.enums.RedisKeyType;
import com.zy.asrs.service.BasStationTvService;
import com.zy.asrs.service.TvDeviceService;
import com.zy.asrs.utils.StationUtils;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.RedisUtil;
import com.zy.system.entity.Announcement;
import com.zy.system.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.servlet.http.HttpServletRequest;
 
import java.util.*;
 
@Slf4j
@RestController
@RequestMapping("/openapi")
public class OpenController {
 
    private static final String[] WEEK = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
 
    @Autowired
    private TvDeviceService tvDeviceService;
    @Autowired
    private BasStationTvService basStationTvService;
    @Autowired
    private StationUtils stationUtils;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AnnouncementService announcementService;
 
    @Value("${app.version:1.0.0}")
    private String appVersion;
    @Value("${app.version-type:stable}")
    private String appVersionType;
 
    @GetMapping("/systemStatus")
    public R systemStatus() {
        return R.ok();
    }
 
    @GetMapping("/getSystemVersion")
    public R getSystemVersion() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("version", appVersion);
        map.put("versionType", appVersionType);
        return R.ok().add(map);
    }
 
    @GetMapping("/getLedInfos")
    public R getLedInfos(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        TvDevice tvDevice = tvDeviceService.selectOne(
                new EntityWrapper<TvDevice>().eq("ip", remoteAddr));
        if (tvDevice == null) {
            return R.error("未找到IP对应的电视机设备: " + remoteAddr);
        }
 
        List<BasStationTv> relations = basStationTvService
                .selectList(new EntityWrapper<BasStationTv>().eq("tv_id", tvDevice.getId()));
        if (relations == null || relations.isEmpty()) {
            R r = R.ok();
            r.put("data", new ArrayList<>());
            return r;
        }
 
 
        String manualError = "";
        Object manualErrorObj = redisUtil.get(RedisKeyType.TV_MANUAL_ERROR_MSG.key);
        if (manualErrorObj != null) {
            manualError = String.valueOf(manualErrorObj);
        }
 
        List<TvDataDto> list = new ArrayList<>();
        for (BasStationTv relation : relations) {
            WcsStationDto wcsStationDto = stationUtils.stationMap.get(relation.getStationId());
 
            if (wcsStationDto == null) {
                continue;
            }
 
            if (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(manualError)) {
                if (!Cools.isEmpty(errorMsg)) {
                    errorMsg += ";";
                }
                errorMsg += manualError;
            }
 
            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());
 
            if (Cools.isEmpty(errorMsg)) {
                tvDataDto.setErrorCode(0);
            }else {
                tvDataDto.setErrorCode(1);
            }
 
            list.add(tvDataDto);
        }
 
        return R.ok().add(list);
    }
 
    @GetMapping("/getError")
    public R getError(HttpServletRequest request) {
        Set<String> errors = new LinkedHashSet<>();
        Object manualErrorObj = redisUtil.get(RedisKeyType.TV_MANUAL_ERROR_MSG.key);
        String manualError = manualErrorObj == null ? "" : String.valueOf(manualErrorObj);
        if (!Cools.isEmpty(manualError)) {
            errors.add(manualError);
        }
 
        String remoteAddr = request.getRemoteAddr();
        TvDevice tvDevice = tvDeviceService.selectOne(
                new EntityWrapper<TvDevice>().eq("ip", remoteAddr));
        if (tvDevice != null) {
            List<BasStationTv> relations = basStationTvService
                    .selectList(new EntityWrapper<BasStationTv>().eq("tv_id", tvDevice.getId()));
            if (relations != null && !relations.isEmpty()) {
                for (BasStationTv relation : relations) {
                    WcsStationDto wcsStationDto = stationUtils.stationMap.get(relation.getStationId());
                    if (wcsStationDto == null) {
                        continue;
                    }
                    if (wcsStationDto.getLoading() != 1) {
                        continue;
                    }
                    String deviceError = "";
                    if (!Cools.isEmpty(wcsStationDto.getErrorMsg())) {
                        deviceError += wcsStationDto.getErrorMsg();
                    }
                    if (!Cools.isEmpty(wcsStationDto.getSystemWarning())) {
                        if (!Cools.isEmpty(deviceError)) {
                            deviceError += ";";
                        }
                        deviceError += wcsStationDto.getSystemWarning();
                    }
                    if (!Cools.isEmpty(deviceError)) {
                        errors.add(deviceError);
                    }
                }
            }
        }
 
        String errorMsg = String.join(";", errors);
        Map<String, Object> result = new HashMap<>();
        result.put("errorMsg", errorMsg);
        return R.ok(result);
    }
 
    @GetMapping("/manualError/auth")
    @ManagerAuth
    public R getManualErrorAuth() {
        Object manualErrorObj = redisUtil.get(RedisKeyType.TV_MANUAL_ERROR_MSG.key);
        String manualError = manualErrorObj == null ? "" : String.valueOf(manualErrorObj);
        Map<String, Object> result = new HashMap<>();
        result.put("manualError", manualError);
        return R.ok(result);
    }
 
    @PostMapping("/manualError/auth")
    @ManagerAuth
    public R setManualError(@RequestBody JSONObject param) {
        String manualError = param.getString("manualError");
        if (Cools.isEmpty(manualError)) {
            redisUtil.del(RedisKeyType.TV_MANUAL_ERROR_MSG.key);
            return R.ok();
        }
        redisUtil.set(RedisKeyType.TV_MANUAL_ERROR_MSG.key, manualError);
        return R.ok();
    }
 
    @GetMapping("/announcement")
    public R top5(){
        EntityWrapper<Announcement> wrapper = new EntityWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderBy("create_time", false);
        wrapper.last("limit 5");
        return R.ok(announcementService.selectList(wrapper));
    }
 
    /**
     * 获取当前时间
     */
    @GetMapping("/monitor/date")
    public R monitorDate() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        return R.ok(
                Cools.add("year", calendar.get(Calendar.YEAR))
                        .add("month", Utils.zerofill(String.valueOf(calendar.get(Calendar.MONTH)+1), 2))
                        .add("day", Utils.zerofill(String.valueOf(calendar.get(Calendar.DATE)), 2))
                        .add("hour", Utils.zerofill(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), 2))
                        .add("minute", Utils.zerofill(String.valueOf(calendar.get(Calendar.MINUTE)), 2))
                        .add("second", Utils.zerofill(String.valueOf(calendar.get(Calendar.SECOND)) , 2))
                        .add("week", WEEK[calendar.get(Calendar.DAY_OF_WEEK)-1])
        );
    }
 
    /**
     * 获取库位数据
     */
    @GetMapping("/queryLoc")
    public R queryLoc() {
        TvLocDataDto tvLocDataDto = new TvLocDataDto();
        Object o = redisUtil.get(RedisKeyType.TV_LOC_DATA_DTO.key);
        if(o == null){
            return R.ok().add(tvLocDataDto);
        }
 
        tvLocDataDto = JSON.parseObject(o.toString(), TvLocDataDto.class);
        return R.ok().add(tvLocDataDto);
    }
 
    /**
     * WMS入出库折线图
     */
    @GetMapping("/inOutLineCharts")
    public R inOutLineCharts() {
        Object o = redisUtil.get(RedisKeyType.TV_LINE_CHARTS.key);
        if(o == null){
            return R.error();
        }
 
        return R.ok().add(JSON.parseObject(o.toString()));
    }
 
    /**
     * WMS库存数据统计
     */
    @GetMapping("/locDetlStatistics")
    public R locDetlStatistics() {
        Object o = redisUtil.get(RedisKeyType.TV_LOC_DETL_STATISTICS.key);
        if(o == null){
            return R.error();
        }
 
        return R.ok().add(JSON.parseArray(o.toString()));
    }
 
}