| | |
| | | import com.ld.igds.io.response.DeviceControlResponse; |
| | | import com.ld.igds.models.Device; |
| | | import com.ld.igds.protocol.modbus.ModbusUtil; |
| | | import com.ld.igds.protocol.modbus.ServerUtil; |
| | | import com.ld.igds.protocol.modbus.data.ModbusTcp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | @Resource |
| | | private CoreDeviceService deviceService; |
| | | @Resource |
| | | private AnalysisService analysisService; |
| | | @Resource |
| | | private ModbusUtil modbusUtil; |
| | | |
| | | |
| | | @Override |
| | | public String getProtocol() { |
| | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "没有需要执行的设备"); |
| | | } |
| | | |
| | | |
| | | try { |
| | | |
| | | |
| | | Device device; |
| | | ModbusTcp modbusTcp; |
| | | String[] addr; |
| | | String temp; |
| | | ModbusTcp modbusTcpLink = null; |
| | | String temp = Constant.YN_N; |
| | | int addrExe = 65535; |
| | | for (ExeDevice exeDevice : deviceList) { |
| | | device = deviceService.getCacheDeviceById(exeDevice.getCompanyId(), exeDevice.getId()); |
| | |
| | | |
| | | if (null == device.getModbus()) continue; |
| | | |
| | | modbusTcp = new ModbusTcp(); |
| | | modbusTcp = new ModbusTcp(device.getModbus()); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceId(device.getId()); |
| | | modbusTcp.setDeviceCode(device.getPassCode() + ""); |
| | | modbusTcp.setSerId(request.getSerId()); |
| | | modbusTcp.setCompanyId(request.getCompanyId()); |
| | | |
| | | //modbus规则:开地址-关地址-停地址-开到位-关到位-开故障-关故障,无地址用N代替 |
| | | addr = device.getModbus().split("-"); |
| | | temp = addr[0]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpen(temp); |
| | | temp = addr[1]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrClose(temp); |
| | | temp = addr[2]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrStop(temp); |
| | | temp = addr[3]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpenEnd(temp); |
| | | temp = addr[4]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrCloseEnd(temp); |
| | | temp = addr[5]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpenError(temp); |
| | | temp = addr[6]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrCloseError(temp); |
| | | |
| | | if (null != device.getLinkModbus()) { |
| | | modbusTcpLink = new ModbusTcp(device.getLinkModbus()); |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setDeviceCode(device.getLink()); |
| | | modbusTcpLink.setSerId(request.getSerId()); |
| | | modbusTcpLink.setCompanyId(request.getCompanyId()); |
| | | } |
| | | |
| | | //根据设备目标目标状态,选择需要执行的通道 |
| | | if (DeviceStatus.CLOSE.getCode().equals(device.getTargetStatus())) { |
| | | addrExe = Integer.valueOf(modbusTcp.getAddrClose()); |
| | | temp = modbusTcp.getAddrClose(); |
| | | } |
| | | |
| | | if(DeviceStatus.OPEN.getCode().equals(device.getTargetStatus())){ |
| | | addrExe = Integer.valueOf(modbusTcp.getAddrOpen()); |
| | | if (DeviceStatus.OPEN.getCode().equals(device.getTargetStatus())) { |
| | | temp = modbusTcp.getAddrOpen(); |
| | | } |
| | | |
| | | if (null == modbusTcpLink) { |
| | | //根据设备目标状态,调用MODBUS执行 |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcp.setAddrExe(addrExe); |
| | | doExe(modbusTcp, device.getTargetStatus()); |
| | | continue; |
| | | } |
| | | |
| | | |
| | | //TODO 其他状态待定 |
| | | //如果是开风机 |
| | | if (DeviceStatus.F_OPEN.getCode().equals(device.getTargetStatus())) { |
| | | |
| | | //根据设备目标状态,调用MODBUS执行 |
| | | modbusUtil.writeCoil(modbusTcp.getIp(), modbusTcp.getPort(),addrExe, true); |
| | | //先开窗口 |
| | | temp = modbusTcp.getAddrOpen(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcp.setAddrExe(addrExe); |
| | | |
| | | doExe(modbusTcp, DeviceStatus.OPEN.getCode()); |
| | | |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | //再开风机 |
| | | temp = modbusTcpLink.getAddrOpen(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcpLink.setAddrExe(addrExe); |
| | | |
| | | doExe(modbusTcpLink, DeviceStatus.OPEN.getCode()); |
| | | } |
| | | |
| | | //如果关风机 |
| | | if(DeviceStatus.F_CLOSE.equals(device.getTargetStatus())){ |
| | | temp = modbusTcpLink.getAddrClose(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcpLink.setAddrExe(addrExe); |
| | | |
| | | doExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | | |
| | | |
| | | //如果开窗 |
| | | if(DeviceStatus.W_OPEN.equals(device.getTargetStatus())){ |
| | | temp = modbusTcp.getAddrOpen(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcp.setAddrExe(addrExe); |
| | | |
| | | doExe(modbusTcp, device.getTargetStatus()); |
| | | } |
| | | |
| | | //如果是关窗户 |
| | | if(DeviceStatus.W_CLOSE.equals(device.getTargetStatus())){ |
| | | |
| | | //先关风机 |
| | | temp = modbusTcpLink.getAddrClose(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcpLink.setAddrExe(addrExe); |
| | | doExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | //在关窗户 |
| | | temp = modbusTcp.getAddrClose(); |
| | | addrExe = Integer.valueOf(temp) - 1; |
| | | modbusTcp.setAddrExe(addrExe); |
| | | |
| | | doExe(modbusTcp, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | log.error("调用MODBUS-TCP执行异常:{}", e); |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "后台执行异常:" + e.getMessage()); |
| | | } |
| | | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); |
| | | } |
| | | |
| | | private void doExe(ModbusTcp modbusTcp, String targetStatus) throws Exception { |
| | | boolean exeResult = modbusUtil.writeValue05(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); |
| | | if (exeResult) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), targetStatus); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public DeviceControlResponse closeAll(DeviceControlRequest request) { |
| | | return null; |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "终端柜体不支持"); |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse queryStatus(DeviceControlRequest request) { |
| | | return null; |
| | | try { |
| | | List<Device> list = deviceService.getCacheDeviceBySerId(request.getCompanyId(), request.getSerId()); |
| | | |
| | | if (null == list || list.isEmpty()) { |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "未获取到设备列表信息"); |
| | | } |
| | | |
| | | ModbusTcp modbusTcp; |
| | | ModbusTcp modbusTcpLink; |
| | | int addrStart; |
| | | boolean[] result; |
| | | for (Device device : list) { |
| | | if (null == device.getModbus()) continue; |
| | | modbusTcp = new ModbusTcp(device.getModbus()); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceCode(device.getPassCode() + ""); |
| | | modbusTcp.setSerId(request.getSerId()); |
| | | |
| | | addrStart = Integer.valueOf(modbusTcp.getAddrOpenEnd()) - 1; |
| | | |
| | | result = modbusUtil.readStatus02(modbusTcp.getIp(), modbusTcp.getPort(), addrStart, 2); |
| | | log.debug("---------读取状态------{}--{}", modbusTcp.getDeviceCode(), result.toString()); |
| | | analysisResult(device.getCompanyId(), device.getSerId(), modbusTcp.getDeviceCode(), result); |
| | | |
| | | if (null != device.getLinkModbus()) { |
| | | Thread.sleep(300); |
| | | modbusTcpLink = new ModbusTcp(device.getModbus()); |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setSerId(request.getSerId()); |
| | | modbusTcpLink.setDeviceCode(device.getLink()); |
| | | addrStart = Integer.valueOf(modbusTcpLink.getAddrOpenEnd()) - 1; |
| | | result = modbusUtil.readStatus02(modbusTcpLink.getIp(), modbusTcpLink.getPort(), addrStart, 2); |
| | | log.debug("---------读取状态-LINK------{}--{}", modbusTcpLink.getDeviceCode(), result.toString()); |
| | | |
| | | analysisResult(device.getCompanyId(), device.getSerId(), modbusTcpLink.getDeviceCode(), result); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("调用MODBUS-TCP执行异常:{}", e); |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "后台执行异常:" + e.getMessage()); |
| | | } |
| | | |
| | | analysisService.analysisDevice(request, 3000); |
| | | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); |
| | | } |
| | | |
| | | /** |
| | | * 返回两位,第一位是开到位,第二位是关到位,根据这两个标记确认当前设备的状态 |
| | | * |
| | | * @param deviceCode |
| | | * @param result |
| | | */ |
| | | private void analysisResult(String companyId, String serId, String deviceCode, boolean[] result) { |
| | | if (result[0]) { |
| | | ServerUtil.add2StatusMap(companyId, serId, deviceCode, DeviceStatus.OPEN.getCode()); |
| | | } |
| | | if (result[1]) { |
| | | ServerUtil.add2StatusMap(companyId, serId, deviceCode, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse tempControl(TempControlRequest request) { |
| | | return null; |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "终端柜体未接入当前控制"); |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse airAutoControl(DeviceAutoControlRequest request) { |
| | | |
| | | |
| | | return null; |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "终端柜体未接入当前控制"); |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse n2AutoControl(DeviceAutoControlRequest request) { |
| | | |
| | | return null; |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "终端柜体未接入当前控制"); |
| | | } |
| | | } |