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);
|
}
|
}
|