| package com.ld.igds.verb.manager; | 
|   | 
| import com.ld.igds.common.CoreCommonService; | 
| import com.ld.igds.common.CoreDeviceService; | 
| import com.ld.igds.common.CoreSerService; | 
| import com.ld.igds.common.DepotStatusService; | 
| import com.ld.igds.common.dto.DepotSerData; | 
| import com.ld.igds.constant.*; | 
| import com.ld.igds.data.PageResponse; | 
| 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.models.Depot; | 
| import com.ld.igds.models.Device; | 
| import com.ld.igds.models.DeviceSer; | 
| import com.ld.igds.models.DicTrigger; | 
| import com.ld.igds.order.ExeOrderService; | 
| import com.ld.igds.order.data.ExeRequest; | 
| import com.ld.igds.util.ContextUtil; | 
| import com.ld.igds.verb.AreationModel; | 
| import com.ld.igds.verb.dto.VerbParam; | 
| import com.ld.igds.verb.service.CoreAreationService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
| import java.util.stream.Collectors; | 
|   | 
|   | 
| /** | 
|  * 通风模块-业务管理模块 | 
|  */ | 
| @Slf4j | 
| @Component | 
| public class VerbManager { | 
|   | 
|     @Autowired | 
|     private CoreCommonService coreCommonService; | 
|     @Autowired | 
|     private CoreDeviceService coreDeviceService; | 
|     @Autowired | 
|     private ExeOrderService exeOrderService; | 
|     @Autowired | 
|     private RemoteManager remoteManager; | 
|     @Autowired | 
|     private CoreSerService coreSerService; | 
|     @Autowired | 
|     private CoreAreationService areationService; | 
|     @Autowired | 
|     private DepotStatusService depotCheckService; | 
|   | 
|     private RemoteControlService remoteControlService; | 
|   | 
|   | 
|     /** | 
|      * 获取手动通风仓库的设备列表 | 
|      * | 
|      * @param param | 
|      * @return | 
|      */ | 
|     public PageResponse<List<Device>> listDeviceManual(VerbParam param) { | 
|         List<String> depotIds; | 
|         if (param.isOnlyCurDepot()) { | 
|             depotIds = new ArrayList<>(); | 
|             depotIds.add(param.getDepotId()); | 
|         } else { | 
|             // 根据仓库缓存获取相同建筑物下的仓库集合 | 
|             List<Depot> depotList = coreCommonService.getCacheCommonBuildingDepot(param.getCompanyId(), param.getDepotId()); | 
|   | 
|             // 获取ID信息 | 
|             depotIds = depotList.stream().map(Depot::getId).collect(Collectors.toList()); | 
|         } | 
|   | 
|         // 根据仓库多个ID获取仓库与分机的关系数据 | 
|         List<DepotSerData> depotSerDataList = coreCommonService.getCacheDepotSerByDepots(param.getCompanyId(), depotIds); | 
|   | 
|         if (null == depotSerDataList) { | 
|             log.error("没有获取到当前仓库{}的相关分机,执行失败。", param.getDepotId()); | 
|             return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), | 
|                     "没有获取到当前仓的分机信息,执行被拒绝!"); | 
|         } | 
|   | 
|         List<Device> listAll = new ArrayList<>();// 分机的所有设备 | 
|         List<Device> tempList; | 
|         for (DepotSerData depotSerData : depotSerDataList) { | 
|             tempList = coreDeviceService.getCacheDeviceBySerId(param.getCompanyId(), depotSerData.getSerId()); | 
|             listAll.addAll(tempList); | 
|         } | 
|   | 
|         if (listAll.isEmpty()) { | 
|             return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), | 
|                     "当前仓库没有获取到设备配置信息,请联管理员!!"); | 
|         } | 
|   | 
|         //过滤只获取通风设备:01-风窗,02-轴流窗,0B-通风口,0C-环流风机 | 
|         List<Device> listResult = new ArrayList<>(); | 
|         for (Device item : listAll) { | 
|             if (null == item.getType()) continue; | 
|   | 
|             if (param.isOnlyCurDepot() && !param.getDepotId().equals(item.getDepotId())) continue; | 
|   | 
|             if (item.getType().equals(DeviceType.TYPE_01.getCode())) | 
|                 listResult.add(item); | 
|             if (item.getType().equals(DeviceType.TYPE_02.getCode())) | 
|                 listResult.add(item); | 
|             if (item.getType().equals(DeviceType.TYPE_0B.getCode())) | 
|                 listResult.add(item); | 
|             if (item.getType().equals(DeviceType.TYPE_0C.getCode())) | 
|                 listResult.add(item); | 
|   | 
|             //浅圆仓加上其他设备 | 
|             if (DepotType.TYPE_02.getCode().equals(param.getDepotType())) { | 
|                 if (item.getType().equals(DeviceType.TYPE_03.getCode())) | 
|                     listResult.add(item); | 
|                 if (item.getType().equals(DeviceType.TYPE_04.getCode())) | 
|                     listResult.add(item); | 
|                 if (item.getType().equals(DeviceType.TYPE_07.getCode())) | 
|                     listResult.add(item); | 
|             } | 
|         } | 
|   | 
|         if (listResult.isEmpty()) { | 
|             return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), | 
|                     "当前仓库没有获取到通风设备信息,请联系管理员!!"); | 
|         } | 
|   | 
|         return new PageResponse<>(RespCodeEnum.CODE_0000, | 
|                 listResult); | 
|     } | 
|   | 
|     /** | 
|      * 支持多个分机的设备操作,也就是批量设备操作时候是多个分机执行 | 
|      * | 
|      * @param param | 
|      * @return | 
|      */ | 
|     public DeviceControlResponse controlDevice(VerbParam param) { | 
|         if (null == param.getCompanyId()) { | 
|             param.setCompanyId(ContextUtil.getCompanyId()); | 
|         } | 
|         List<ExeDevice> deviceList = param.getDeviceList(); | 
|   | 
|         if (null == deviceList || deviceList.isEmpty()) { | 
|             return new DeviceControlResponse( | 
|                     OrderRespEnum.ORDER_ERROR.getCode(), | 
|                     "没有获取到需要被执行的设备信息,请重新选择执行"); | 
|         } | 
|         // 添加逻辑验证 | 
|         String msg = depotCheckService.checkDepotStatus(param.getCompanyId(), param.getDepotId(), deviceList); | 
|         if (null != msg) { | 
|             return new DeviceControlResponse( | 
|                     OrderRespEnum.ORDER_ERROR.getCode(), msg); | 
|         } | 
|   | 
|         if (!param.isBatchTag()) { | 
|             return controlDevice2(param); | 
|         } | 
|   | 
|         // 如果多个设备,需要对设备按分机进行分组 | 
|         Map<String, List<ExeDevice>> deviceMap = this.updateGroupExecute(param); | 
|   | 
|         String temp = ""; | 
|         DeviceControlResponse tempResp; | 
|         for (String key : deviceMap.keySet()) { | 
|             param.setDeviceList(deviceMap.get(key)); | 
|             tempResp = controlDevice2(param); | 
|             if (OrderRespEnum.ORDER_ERROR.getCode().equals(tempResp.getCode())) { | 
|                 temp += tempResp.getMsg(); | 
|             } | 
|         } | 
|   | 
|         if ("".equals(temp)) { | 
|             return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); | 
|         } | 
|   | 
|         return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), | 
|                 "发送存在异常:" + temp); | 
|     } | 
|   | 
|     /** | 
|      * 针对批量执行,整个廒间所有设备,需要注意风窗 和轴流窗 类型合并 | 
|      * 对设备进行分组,根据设备所在的控制柜进行分组 | 
|      * | 
|      * @param param | 
|      * @return | 
|      */ | 
|     private Map<String, List<ExeDevice>> updateGroupExecute(VerbParam param) { | 
|   | 
|         Map<String, List<ExeDevice>> result = new HashMap<>(); | 
|   | 
|         for (ExeDevice exeDevice : param.getDeviceList()) { | 
|   | 
|             //批量开启通风窗 | 
|             if (DeviceType.TYPE_02.getCode().equals(param.getDeviceType()) || DeviceType.TYPE_01.getCode().equals(param.getDeviceType())) { | 
|   | 
|                 if (DeviceType.TYPE_02.getCode().equals(exeDevice.getType()) || DeviceType.TYPE_01.getCode().equals(exeDevice.getType())) { | 
|                     exeDevice.setTargetStatus(param.getTargetStatus()); | 
|                     if (null == result.get(exeDevice.getSerId())) { | 
|                         result.put(exeDevice.getSerId(), new ArrayList<>()); | 
|                     } | 
|                     result.get(exeDevice.getSerId()).add(exeDevice); | 
|                 } | 
|   | 
|             } | 
|   | 
|   | 
|             //批量开启通风口 | 
|             if (DeviceType.TYPE_0B.getCode().equals(param.getDeviceType()) || DeviceType.TYPE_0C.getCode().equals(param.getDeviceType())) { | 
|   | 
|                 if (DeviceType.TYPE_0B.getCode().equals(exeDevice.getType()) || DeviceType.TYPE_0C.getCode().equals(exeDevice.getType())) { | 
|   | 
|                     exeDevice.setTargetStatus(param.getTargetStatus()); | 
|   | 
|                     if (null == result.get(exeDevice.getSerId())) { | 
|                         result.put(exeDevice.getSerId(), new ArrayList<>()); | 
|                     } | 
|                     result.get(exeDevice.getSerId()).add(exeDevice); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return result; | 
|     } | 
|   | 
|     /** | 
|      * 单个分机设备操作接口管理 | 
|      * | 
|      * @param param | 
|      * @return | 
|      */ | 
|     public DeviceControlResponse controlDevice2(VerbParam param) { | 
|   | 
|         List<ExeDevice> deviceList = param.getDeviceList(); | 
|         String serId = deviceList.get(0).getSerId(); | 
|   | 
|         // 获取分机信息 | 
|         DeviceSer deviceSer = coreSerService.getCacheSer(param.getCompanyId(), serId); | 
|   | 
|         if (Constant.YN_N.equals(deviceSer.getStatus())) { | 
|             return new DeviceControlResponse( | 
|                     OrderRespEnum.ORDER_ERROR.getCode(), | 
|                     "当前设备所属控制柜[" + deviceSer.getName() + "]不在线,执行失败。"); | 
|         } | 
|   | 
|         DeviceControlRequest request = new DeviceControlRequest(); | 
|         request.setDepotId(param.getDepotId()); | 
|         request.setCompanyId(param.getCompanyId()); | 
|         request.setDeviceList(deviceList); | 
|         request.setBizType(BizType.AREATION.getCode()); | 
|         request.setIp(deviceSer.getIp()); | 
|         request.setPort(deviceSer.getPort()); | 
|         request.setSerId(serId); | 
|         request.setControlModel(deviceSer.getControlModel()); | 
|         request.setRealNum(deviceList.size()); | 
|         request.setDeptId(param.getDeptId()); | 
|         request.setSerName(deviceSer.getName()); | 
|         request.setDepotName(request.getDepotName() == null ? request.getDepotId() : request.getDepotName()); | 
|   | 
|         remoteControlService = remoteManager.getRemoteControlService(deviceSer.getProtocol()); | 
|   | 
|         if (null == remoteControlService) { | 
|             return new DeviceControlResponse( | 
|                     OrderRespEnum.ORDER_ERROR.getCode(), "系统没有" | 
|                     + deviceSer.getProtocol() + "的协议实现,执行被拒绝!"); | 
|         } | 
|   | 
|         DeviceControlResponse response = remoteControlService.deviceControl(request); | 
|   | 
|         if (OrderRespEnum.ORDER_SUCCESS.getCode().equals(response.getCode())) { | 
|             exeOrderService.addControlCacheAndLog(new ExeRequest(request, true)); | 
|         } | 
|   | 
|         return response; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 根据页面所操作的设备,结合其所所属分机进行调整,当前方法可以避免不需要的分机状态查询 | 
|      * | 
|      * @param param | 
|      * @return | 
|      */ | 
|     public DeviceControlResponse queryDeviceStatus(VerbParam param) { | 
|         if (null == param.getCompanyId()) { | 
|             param.setCompanyId(ContextUtil.getCompanyId()); | 
|         } | 
|         if (null == param.getDepotId()) { | 
|             return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "系统没有获取到仓库参数,执行被拒绝。"); | 
|         } | 
|   | 
|         if (null == param.getDeviceList() || param.getDeviceList().isEmpty()) { | 
|             return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "没有需要更新的设备信息!!"); | 
|         } | 
|   | 
|         //根据设备所属分机进行分组 | 
|         Map<String, List<ExeDevice>> groupBy = param.getDeviceList().stream().collect(Collectors.groupingBy(ExeDevice::getSerId)); | 
|   | 
|         DeviceControlResponse response = new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS.getCode(), "命令发送成功!"); | 
|   | 
|         DeviceSer deviceSer; | 
|         DeviceControlRequest request; | 
|   | 
|         try { | 
|             for (String serId : groupBy.keySet()) { | 
|                 deviceSer = coreSerService.getCacheSer(param.getCompanyId(), serId); | 
|                 if (null == deviceSer) { | 
|                     log.info("根据仓库{}获取设备状态,分机编码{}的分机不存在,取消执行。", param.getDepotId(), serId); | 
|                     continue; | 
|                 } | 
|                 request = new DeviceControlRequest(); | 
|                 request.setDepotId(param.getDepotId()); | 
|                 request.setCompanyId(param.getCompanyId()); | 
|                 request.setBizType(BizType.AREATION.getCode()); | 
|                 request.setIp(deviceSer.getIp()); | 
|                 request.setPort(deviceSer.getPort()); | 
|                 request.setSerId(deviceSer.getId()); | 
|                 request.setControlModel(deviceSer.getControlModel()); | 
|                 request.setDeptId(param.getDeptId()); | 
|                 request.setSerName(deviceSer.getName()); | 
|                 request.setDepotName(request.getDepotName() == null ? request.getDepotId() : request.getDepotName()); | 
|   | 
|                 remoteControlService = remoteManager.getRemoteControlService(deviceSer.getProtocol()); | 
|   | 
|                 if (null == remoteControlService) { | 
|                     return new DeviceControlResponse( | 
|                             OrderRespEnum.ORDER_ERROR.getCode(), "系统没有" | 
|                             + deviceSer.getProtocol() + "的协议实现,执行被拒绝!"); | 
|                 } | 
|                 response = remoteControlService.queryStatus(request); | 
|   | 
|                 //添加操作缓存日志 | 
|                 exeOrderService.addControlCache(new ExeRequest(request)); | 
|   | 
|                 Thread.sleep(1000); | 
|             } | 
|   | 
|         } catch (Exception e) { | 
|             return new DeviceControlResponse( | 
|                     OrderRespEnum.ORDER_ERROR.getCode(), param.getDepotId() + " 执行失败:" + e.getMessage()); | 
|         } | 
|         return response; | 
|     } | 
|   | 
|     public List<DicTrigger> listAreationModelTag() { | 
|         List<DicTrigger> list = new ArrayList<>(); | 
|         list.add(new DicTrigger(AreationModel.MODEL_01.getCode(), AreationModel.MODEL_01.getMsg())); | 
|         list.add(new DicTrigger(AreationModel.MODEL_02.getCode(), AreationModel.MODEL_02.getMsg())); | 
|         list.add(new DicTrigger(AreationModel.MODEL_03.getCode(), AreationModel.MODEL_03.getMsg())); | 
|         list.add(new DicTrigger(AreationModel.MODEL_04.getCode(), AreationModel.MODEL_04.getMsg())); | 
|         list.add(new DicTrigger(AreationModel.MODEL_05.getCode(), AreationModel.MODEL_05.getMsg())); | 
|   | 
|         list.add(new DicTrigger(AreationModel.MODEL_99.getCode(), AreationModel.MODEL_99.getMsg())); | 
|         return list; | 
|     } | 
| } |