CZT
2023-08-30 38ab8fbd4355070def9e3b39f362731dd4dea6d1
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
package com.ld.igds.common.impl;
 
import java.util.*;
 
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.common.dto.DepotSerData;
import com.ld.igds.common.mapper.DeviceMapper;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.DeviceStatus;
import com.ld.igds.constant.DeviceType;
 
import com.ld.igds.models.Depot;
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.Device;
import com.ld.igds.temp.dto.TempParam;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
 
@Slf4j
@Component
public class CoreDeviceServiceImpl implements CoreDeviceService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceMapper deviceMapper;
 
 
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void setCacheAllDevice(List<Device> listAll, String companyId) {
 
        if (null == listAll || listAll.isEmpty()) return;
 
        //按照分机分类
        Map<String, List<Device>> mapSer = new HashMap<>();
 
        //仓库-设备-分机对象分类
        Map<String, DepotSerData> mapDepot = new HashMap<>();
 
 
        //把设备按照分机进行分组,同事吧设备添加到缓存列表中
        String deviceKey, depotDataKey;
        for (Device device : listAll) {
 
            //封装仓库-设备-分机对象关系
            depotDataKey = companyId + "-" + device.getDepotId() + "-" + device.getSerId();
            if (mapDepot.get(depotDataKey) == null) {
                mapDepot.put(depotDataKey, new DepotSerData(companyId, device.getDepotId(), device.getSerId()));
            }
 
 
            //单个设备进行缓存
            deviceKey = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE, device.getId());
            redisUtil.set(deviceKey, device);
 
            //按照分机分类
            if (null == mapSer.get(device.getSerId())) mapSer.put(device.getSerId(), new ArrayList<Device>());
            mapSer.get(device.getSerId()).add(device);
 
        }
 
        //以分机为单位缓存设备
        for (String serId : mapSer.keySet()) {
            updateCacheDeviceBySerId(mapSer.get(serId), companyId, serId);
        }
 
 
        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(mapDepot.values()));
    }
 
 
//    private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) {
//        // 处理仓库与分机的关系缓存
//        Map<String, DepotSerData> tempMap = new HashMap<>();
//        String key;
//        for (Device item : listAll) {
//            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
//            if (tempMap.get(key) == null) {
//                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
//            }
//        }
//
//        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values()));
//    }
 
    @Override
    public void updateCacheDeviceBySerId(List<Device> listBySer, String companyId, String serId) {
 
        String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE_LIST, serId);
 
        log.debug("分机-设备-KEY={}", key);
 
        redisUtil.set(key, listBySer);
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<Device> getCacheDeviceBySerId(String companyId, String serId) {
        String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE_LIST, serId);
 
        List<Device> list = (List<Device>) redisUtil.get(key);
        if (null == list || list.isEmpty()) {
            log.error("字典信息:没有获取到缓存信息,KEY={}", key);
            return null;
        }
        return list;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<Device> getCacheDeviceByDepotId(String companyId, String depotId) {
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_LIST);
 
        Set<String> keys = redisUtil.keys(patten);
        if (null == keys) return null;
 
        List<Device> result = new ArrayList<>();
        List<Device> list = new ArrayList<>();
        for (String key : keys) {
            list.addAll((List<Device>) redisUtil.get(key));
        }
 
        if (list.isEmpty()) {
            return null;
        }
 
        for (Device device : list) {
            if(depotId.equals(device.getDepotId())){
                result.add(device);
            }
        }
        return result;
    }
 
    @Override
    public Device getCacheDeviceById(String companyId, String id) {
        String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE, id);
        return (Device) redisUtil.get(key);
    }
 
    @Override
    public void refreshCache(String companyId) {
        // 获取所有的设备信息
        List<Device> listAll = deviceMapper.queryAll(companyId);
        if (null != listAll) {
            this.setCacheAllDevice(listAll, companyId);
        }
    }
 
 
    /**
     * @param companyId
     * @param serId
     * @param statusMap key = serId + "_STATUS_" + passCode; value = status
     */
    @Override
    public void updateStatus(String companyId, String serId, Map<String, String> statusMap) {
        List<Device> listCache = getCacheDeviceBySerId(companyId, serId);
 
        if (null == listCache || listCache.isEmpty()) return;
 
        Device curDevice;
        String key, minStatus, linkStatus;
        for (int i = 0; i < listCache.size(); i++) {
 
            curDevice = listCache.get(i);
            key = ContextUtil.buildDeviceStatusKey(companyId, serId, curDevice.getPassCode());
 
            minStatus = statusMap.get(key);
 
            key = ContextUtil.buildDeviceStatusKey(companyId, serId, curDevice.getLink());
            linkStatus = statusMap.get(key);
 
            if (null == minStatus) continue;
 
            //如果是轴流风窗 或者 混流风口
            if (DeviceType.TYPE_02.getCode().equals(curDevice.getType()) || DeviceType.TYPE_0C.getCode().equals(curDevice.getType())) {
 
                if (null == linkStatus) linkStatus = "";
                //调整状态
                if (DeviceStatus.OPEN.getCode().equals(linkStatus)) linkStatus = DeviceStatus.F_OPEN.getCode();
                if (DeviceStatus.CLOSE.getCode().equals(linkStatus)) linkStatus = DeviceStatus.F_CLOSE.getCode();
 
                if (DeviceStatus.OPEN.getCode().equals(minStatus)) minStatus = DeviceStatus.W_OPEN.getCode();
                if (DeviceStatus.CLOSE.getCode().equals(minStatus)) minStatus = DeviceStatus.W_CLOSE.getCode();
 
                //如果口开,风机关
                if (DeviceStatus.W_OPEN.getCode().equals(minStatus) && DeviceStatus.F_CLOSE.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.W_OPEN.getCode());
                    listCache.set(i, curDevice);
                }
 
                //如果口开,风机开
                if (DeviceStatus.W_OPEN.getCode().equals(minStatus) && DeviceStatus.F_OPEN.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.F_OPEN.getCode());
                    listCache.set(i, curDevice);
                }
 
                //如果口开,风机开
                if (DeviceStatus.W_OPEN.getCode().equals(minStatus) && DeviceStatus.F_OPEN_F.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.F_OPEN_F.getCode());
                    listCache.set(i, curDevice);
                }
 
                //如果口关,风机关
                if (DeviceStatus.W_CLOSE.getCode().equals(minStatus) && DeviceStatus.F_CLOSE.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.W_CLOSE.getCode());
                    listCache.set(i, curDevice);
                }
 
                //如果口关,风机开
                if (DeviceStatus.W_CLOSE.getCode().equals(minStatus) && DeviceStatus.F_OPEN.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.ERROR.getCode());
                    listCache.set(i, curDevice);
                }
                //如果口关,风机开
                if (DeviceStatus.W_CLOSE.getCode().equals(minStatus) && DeviceStatus.F_OPEN_F.getCode().equals(linkStatus)) {
                    curDevice.setStatus(DeviceStatus.ERROR.getCode());
                    listCache.set(i, curDevice);
                }
            } else {
                curDevice.setStatus(minStatus);
                listCache.set(i, curDevice);
            }
            log.debug("解析设备状态--主通道={}---关联通道={}--解析后状态={}", curDevice.getPassCode(), curDevice.getLink(), curDevice.getStatus());
        }
 
        // 更新缓存
        this.updateCacheDeviceBySerId(listCache, companyId, serId);
    }
 
    /**
     * 2021年5月12日 19:48:05 暂停使用
     *
     * @param companyId
     * @param serId
     * @param statusMap key = serId + "_STATUS_" + passCode; value = status
     */
    public void updateStatus2(String companyId, String serId, Map<String, String> statusMap) {
        List<Device> listCache = getCacheDeviceBySerId(companyId, serId);
 
        if (null == listCache || listCache.isEmpty()) return;
 
        List<Device> newListCache = new ArrayList<>(listCache.size());
        String key;
        String linkStatus = null, status;
 
        for (Device device : listCache) {
            if (StringUtils.isNotEmpty(device.getLink()) && "NULL".equalsIgnoreCase(device.getLink())) {
                device.setLink(null);
            }
            key = ContextUtil.buildDeviceStatusKey(companyId, serId, device.getPassCode());
            status = statusMap.get(key);
 
            // 如果当前状态=空,则使用设备原来状态
            if (Constant.EMPTY.equals(status)) {
                status = device.getStatus();
            }
            if (StringUtils.isNotEmpty(device.getLink())) {
                key = ContextUtil.buildDeviceStatusKey(companyId, serId, device.getLink());
                linkStatus = statusMap.get(key);
                if (Constant.EMPTY.equals(status)) {
                    linkStatus = this.getLinkDeviceStatus(listCache, device.getLink());
                }
            }
            status = DeviceStatus.getStatus(status, linkStatus, device.getType());
 
            device.setStatus(status);
            device.setStatusName(DeviceStatus.getDeviceStatus(device.getStatus()));
 
            //log.debug("更新到缓存状态--通道={}---关联={}--当前状态{}",device.getPassCode(),device.getLink(),device.getStatus());
            newListCache.add(device);
        }
        // 更新缓存
        this.updateCacheDeviceBySerId(newListCache, companyId, serId);
    }
 
    @Override
    public void updateDeviceNoId(Device device, boolean flushCache) {
        int result = deviceMapper.updateDeviceNoId(device);
        if (0 == result) {
            device.setId(ContextUtil.buildDeviceId(device.getCompanyId(), device.getDepotId(), device.getSerId(), device.getPassCode()));
            deviceMapper.addDevice(device);
        }
        if (flushCache) {
            refreshCache(device.getCompanyId());
        }
    }
 
    @Override
    public void updatePos(String deviceId, Double posX, Double posY) {
        deviceMapper.updatePos(deviceId, posX, posY);
    }
 
    private String getLinkDeviceStatus(List<Device> listCache, String passCode) {
        for (Device device : listCache) {
            if (passCode.equals(device.getPassCode())) {
                return device.getStatus();
            }
        }
        return null;
    }
 
    @Override
    public void updateTempControlInfo(TempParam param) {
        deviceMapper.updateTempControlInfo(param);
 
 
        String serId = param.getDeviceList().get(0).getSerId();
        // 调整缓存
        List<Device> listCache = getCacheDeviceBySerId(param.getCompanyId(), serId);
        if (null == listCache || listCache.isEmpty())
            return;
 
        // 更新缓存
        Device temp;
        for (int i = 0; i < listCache.size(); i++) {
            temp = listCache.get(i);
            if (param.getId().equals(temp.getId())) {
                temp.setTargetStatus(param.getTargetStatus());
                temp.setExt1(param.getTargetTemp());
                temp.setExt2(param.getTargetModel());
                temp.setStatus(param.getTargetStatus());
                listCache.set(i, temp);
                break;
            }
        }
 
        this.updateCacheDeviceBySerId(listCache, param.getCompanyId(), serId);
    }
 
    @Override
    public List<Device> getDeviceByTimId(String companyId, String timId) {
        return deviceMapper.queryByTimId(companyId, timId);
    }
}