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