jiazx0107@163.com
2023-10-22 dfd793f14e51c48c3322f1b36f543179043bd45d
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.ld.igds.lamp;
 
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.common.CoreSerService;
import com.ld.igds.constant.*;
import com.ld.igds.io.RemoteControlService;
import com.ld.igds.io.RemoteManager;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.DeviceControlRequest;
import com.ld.igds.io.request.ExeDevice;
import com.ld.igds.io.response.DeviceControlResponse;
import com.ld.igds.lamp.dto.LampParam;
import com.ld.igds.models.Device;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.order.ExeOrderService;
import com.ld.igds.order.data.ExeRequest;
 
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Desc: 等控管理
 * @author: Andy
 * @update-time: 2022/11/27
 */
@Slf4j
@Component
public class LampManager {
 
    @Resource
    private CoreDeviceService coreDeviceService;
    @Resource
    private ExeOrderService exeOrderService;
    @Resource
    private CoreSerService coreSerService;
    @Resource
    private RemoteManager remoteManager;
 
    private RemoteControlService remoteControlService;
 
    /**
     * 获取灯控设备列表,请注意等控设备操作只用于分组,按照仓库 和位置进行分组合并
     *
     * @param param
     * @return
     */
    public List<Device> listDeviceGroup(LampParam param) {
        // 获取所有的设备列表
        List<Device> listAll = listDevice(param);
        if (null == listAll) return null;
 
        // 照明分组,位置分组,按照内外分组,其他设备通道使用逗号拼接在LINK字段上
        Map<String, Device> tempMap = new HashMap<>();
        Map<String, String> tempStatus = new HashMap<>();
 
        String key;
        Device tempDevice;
        for (Device device : listAll) {
            if (!DeviceType.TYPE_06.getCode().equals(device.getType())) continue;
 
            if (null == device.getLocation()) device.setLocation(DeviceLocation.L_05.getCode());
 
            key = device.getDepotId() + "_" + device.getLocation();
 
            //封装状态-只要有一个灯是开则认为当前分组开灯
            if (null == tempStatus.get(key)) {
                tempStatus.put(key, DeviceStatus.CLOSE.getCode());
            }
            if (DeviceStatus.OPEN.getCode().equals(device.getStatus())) {
                tempStatus.put(key, DeviceStatus.OPEN.getCode());
            }
 
            //封装设备
            tempDevice = tempMap.get(key);
            if (null == tempDevice) {
                tempDevice = device;
                tempDevice.setLink("");
            } else {
                if (null != device.getPosX()) {
                    tempDevice.setId(device.getId());
                    tempDevice.setPosX(device.getPosX());
                    tempDevice.setPosY(device.getPosY());
                }
                tempDevice.setLink(tempDevice.getLink() + device.getPassCode() + ",");
            }
 
            tempDevice.setStatus(tempStatus.get(key));
            tempMap.put(key, tempDevice);
        }
 
        if (tempMap.isEmpty()) return null;
 
        return new ArrayList<>(tempMap.values());
    }
 
 
    public List<Device> listDevice(LampParam param) {
        List<DeviceSer> listSer = coreSerService.getCacheSerList(param.getCompanyId());
 
        if (null == listSer || listSer.isEmpty()) {
            return null;
        }
 
        // 获取所有的设备列表
        List<Device> listAll = new ArrayList<>();
        List<Device> tempList;
        for (DeviceSer ser : listSer) {
            tempList = coreDeviceService.getCacheDeviceBySerId(
                    ser.getCompanyId(), ser.getId());
 
            if (null != tempList && tempList.size() > 0) listAll.addAll(tempList);
        }
 
        if (listAll.isEmpty()) {
            return null;
        }
 
        return listAll;
    }
 
    public DeviceControlResponse deviceCtrl(LampParam param) {
 
        // 获取页面中所有的照明设备
        List<ExeDevice> list = param.getDeviceList();
        if (null == list) {
            return new DeviceControlResponse(
                    OrderRespEnum.ORDER_ERROR.getCode(), "没有获取到需要操作的设备!!");
        }
 
        // 对设备进行调整,按照分机进行分组以便调用执行
        Map<String, List<ExeDevice>> deviceMap = new HashMap<>();
        String type = param.getType();
        String depotId = param.getDepotId();
 
        // 执行仓内
        if ("IN".equals(type)) {
            for (ExeDevice exeDevice : list) {
                exeDevice.setTargetStatus(param.getTargetStatus());
 
                if (depotId.equals(exeDevice.getDepotId())
                        && DeviceLocation.L_05.getCode().equals(
                        exeDevice.getLocation())) {
 
                    if (null == deviceMap.get(exeDevice.getSerId()))
                        deviceMap.put(exeDevice.getSerId(), new ArrayList<>());
 
                    deviceMap.get(exeDevice.getSerId()).add(exeDevice);
                }
            }
        }
 
        // 执行仓外
        if ("OUT".equals(type)) {
            for (ExeDevice exeDevice : list) {
                exeDevice.setTargetStatus(param.getTargetStatus());
 
                if (depotId.equals(exeDevice.getDepotId())
                        && !DeviceLocation.L_05.getCode().equals(
                        exeDevice.getLocation())) {
                    if (null == deviceMap.get(exeDevice.getSerId()))
                        deviceMap.put(exeDevice.getSerId(), new ArrayList<>());
 
                    deviceMap.get(exeDevice.getSerId()).add(exeDevice);
                }
            }
        }
 
        // 执行所有
        if ("ALL".equals(type)) {
            for (ExeDevice exeDevice : list) {
                exeDevice.setTargetStatus(param.getTargetStatus());
 
                if (null == deviceMap.get(exeDevice.getSerId()))
                    deviceMap.put(exeDevice.getSerId(), new ArrayList<>());
                deviceMap.get(exeDevice.getSerId()).add(exeDevice);
            }
        }
 
        if (deviceMap.isEmpty()) {
            return new DeviceControlResponse(
                    OrderRespEnum.ORDER_ERROR.getCode(), "当前条件下无设备执行");
        }
 
        String msg = "", temp;
        for (String key : deviceMap.keySet()) {
            param.setSerId(key);
            param.setDeviceList(deviceMap.get(key));
            temp = controlDevice2(param);
            if (null != temp) {
                msg += temp;
            }
        }
        if ("".equals(msg)) {
            return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS);
        }
 
        return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                msg);
    }
 
    /**
     * 单个分机设备操作接口管理
     *
     * @param param
     * @return
     */
    public String controlDevice2(LampParam param) {
        // 获取分机信息
        DeviceSer deviceSer = coreSerService.getCacheSer(param.getCompanyId(),
                param.getSerId());
 
        if (Constant.YN_N.equals(deviceSer.getStatus())) {
            return deviceSer.getName() + "不在线";
        }
 
 
        List<ExeDevice> exeList = new ArrayList<ExeDevice>();
 
        // 照明设备有分组,将LINK中的分组转换为照明设备
        String[] passCodes;
 
        for (ExeDevice exDevice : param.getDeviceList()) {
            if (null == exDevice) continue;
 
            exeList.add(exDevice);
 
            if (StringUtils.isAllEmpty(exDevice.getLink())) continue;
 
            passCodes = exDevice.getLink().split(",");
            if (passCodes.length == 0) continue;
 
            for (String passCode : passCodes) {
                exeList.add(new ExeDevice(exDevice, passCode));
            }
 
        }
 
        DeviceControlRequest request = new DeviceControlRequest();
        request.setDepotId(param.getDepotId());
        request.setCompanyId(param.getCompanyId());
        request.setBizType(BizType.SECURITY.getCode());
        request.setIp(deviceSer.getIp());
        request.setPort(deviceSer.getPort());
        request.setSerId(deviceSer.getId());
        request.setControlModel(deviceSer.getControlModel());
        request.setDeviceList(exeList);
        request.setRealNum(exeList.size());
 
        // 避免重复发送命令和添加操作记录
        ExeRequest exeRequest = new ExeRequest(request);
        exeRequest.setRepeatTag(true);
        exeOrderService.checkExecute(exeRequest);
 
        remoteControlService = remoteManager.getRemoteControlService(deviceSer
                .getProtocol());
 
        if (null == remoteControlService) {
            return deviceSer.getName() + "配置协议后台为实现,请核对。";
        }
 
        DeviceControlResponse response = remoteControlService
                .deviceControl(request);
 
        if (OrderRespEnum.ORDER_SUCCESS.getCode().equals(response.getCode())) {
            exeOrderService.addCache(exeRequest);
        } else {
            return response.getMsg();
        }
 
        return null;
    }
 
    public DeviceControlResponse queryDeviceStatus(LampParam param) {
        // 获取页面中所有的照明设备
        List<ExeDevice> list = param.getDeviceList();
 
        if (null == list) {
            return new DeviceControlResponse(
                    OrderRespEnum.ORDER_ERROR.getCode(), "没有获取到需要操作的设备!!");
        }
 
 
        //设备分组,需要考虑到,如果当前条件有仓库编码,说明只执行当前仓库,那么取消其他仓库的状态查询
        Map<String, List<ExeDevice>> deviceMap = new HashMap<>();
        for (ExeDevice exDevice : list) {
            if (null != param.getDepotId()) {
                if (!param.getDepotId().equals(exDevice.getDepotId())) continue;
            }
 
            if (null == deviceMap.get(exDevice.getSerId())) deviceMap.put(exDevice.getSerId(), new ArrayList<>());
 
            deviceMap.get(exDevice.getSerId()).add(exDevice);
        }
 
        if (deviceMap.size() == 0) {
            return new DeviceControlResponse(
                    OrderRespEnum.ORDER_ERROR.getCode(), "没有获取到需要操作的设备!!");
        }
 
        DeviceControlResponse response = new DeviceControlResponse(
                OrderRespEnum.ORDER_SUCCESS.getCode(), "命令发送成功!");
 
        String temp = "";
        DeviceSer deviceSer;
        DeviceControlRequest request;
        String depotId;
        for (String serId : deviceMap.keySet()) {
            deviceSer = coreSerService.getCacheSer(param.getCompanyId(), serId);
            if (null == deviceSer) {
                log.info("分机编码{}的分机不存在,取消执行。", serId);
                continue;
            }
            depotId = deviceMap.get(serId).get(0).getDepotId();
 
            request = new DeviceControlRequest();
            request.setDepotId(depotId);
            request.setCompanyId(param.getCompanyId());
            request.setBizType(BizType.SECURITY.getCode());
            request.setIp(deviceSer.getIp());
            request.setPort(deviceSer.getPort());
            request.setSerId(deviceSer.getId());
 
            remoteControlService = remoteManager.getRemoteControlService(deviceSer.getProtocol());
 
            if (null == remoteControlService) {
                return new DeviceControlResponse(
                        OrderRespEnum.ORDER_ERROR.getCode(), "系统没有"
                        + deviceSer.getProtocol() + "的协议实现,执行被拒绝!");
            }
 
            response = remoteControlService.queryStatus(request);
            if (!OrderRespEnum.ORDER_SUCCESS.getCode().equals(response.getCode())) {
                temp += response.getMsg();
            }
        }
 
        if (!"".equals(temp)) {
            response.setMsg(temp);
            return new DeviceControlResponse(
                    OrderRespEnum.ORDER_ERROR.getCode(), temp);
        }
 
 
        return response;
    }
}