| | |
| | | package com.ld.igds.protocol.modbus.command; |
| | | |
| | | import com.ld.igds.common.CoreDeviceService; |
| | | import com.ld.igds.common.CoreSerService; |
| | | import com.ld.igds.constant.Constant; |
| | | import com.ld.igds.constant.DeviceStatus; |
| | | import com.ld.igds.io.RemoteControlService; |
| | |
| | | import com.ld.igds.io.request.ExeDevice; |
| | | import com.ld.igds.io.request.TempControlRequest; |
| | | import com.ld.igds.io.response.DeviceControlResponse; |
| | | import com.ld.igds.modbus.service.HModbusService; |
| | | import com.ld.igds.models.Device; |
| | | import com.ld.igds.protocol.modbus.ModbusUtil; |
| | | import com.ld.igds.models.DeviceModbus; |
| | | import com.ld.igds.models.DeviceSer; |
| | | import com.ld.igds.protocol.modbus.ModbusUtil2; |
| | | import com.ld.igds.protocol.modbus.ServerUtil; |
| | | import com.ld.igds.protocol.modbus.data.ModbusTcp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | public static final String BEAN_ID = "modbus.remoteControlService"; |
| | | |
| | | @Resource |
| | | private CoreDeviceService deviceService; |
| | | private CoreDeviceService coreDeviceService; |
| | | @Resource |
| | | private ModbusUtil modbusUtil; |
| | | private HModbusService deviceModbusService; |
| | | @Resource |
| | | private AnalysisService analysisService; |
| | | @Resource |
| | | private CoreSerService coreSerService; |
| | | |
| | | |
| | | @Override |
| | | public String getProtocol() { |
| | | return ProtocolEnum.MODBUS_TCP.getCode(); |
| | | return ProtocolEnum.TCP_MODBUS.getCode(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "没有需要执行的设备"); |
| | | } |
| | | |
| | | |
| | | try { |
| | | |
| | | |
| | | Device device; |
| | | DeviceModbus deviceModbus; |
| | | ModbusTcp modbusTcp; |
| | | String[] addr; |
| | | String temp; |
| | | int addrExe = 65535; |
| | | ModbusTcp modbusTcpLink = null; |
| | | String temp = Constant.YN_N; |
| | | for (ExeDevice exeDevice : deviceList) { |
| | | device = deviceService.getCacheDeviceById(exeDevice.getCompanyId(), exeDevice.getId()); |
| | | deviceModbus = deviceModbusService.getCacheDeviceModbus(request.getCompanyId(), exeDevice.getPassCode() + ""); |
| | | |
| | | if (null == device) continue; |
| | | if (null == deviceModbus) { |
| | | log.error("当前设备---{}{},为获取到配置的MODBUS信息", exeDevice.getSerId(), exeDevice.getName()); |
| | | continue; |
| | | } |
| | | |
| | | if (null == device.getModbus()) continue; |
| | | |
| | | modbusTcp = new ModbusTcp(); |
| | | modbusTcp = new ModbusTcp(deviceModbus); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceId(device.getId()); |
| | | modbusTcp.setBizCode(exeDevice.getPassCode() + ""); |
| | | modbusTcp.setSerId(request.getSerId()); |
| | | |
| | | //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); |
| | | |
| | | modbusTcp.setCompanyId(request.getCompanyId()); |
| | | |
| | | //根据设备目标目标状态,选择需要执行的通道 |
| | | if (DeviceStatus.CLOSE.getCode().equals(device.getTargetStatus())) { |
| | | addrExe = Integer.valueOf(modbusTcp.getAddrClose()); |
| | | if (DeviceStatus.CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getClose(); |
| | | } |
| | | |
| | | if(DeviceStatus.OPEN.getCode().equals(device.getTargetStatus())){ |
| | | addrExe = Integer.valueOf(modbusTcp.getAddrOpen()); |
| | | if (DeviceStatus.OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | } |
| | | |
| | | //环流风机-关 |
| | | if (DeviceStatus.F_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getStop(); |
| | | } |
| | | |
| | | //环流风机-开 |
| | | if (DeviceStatus.F_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | } |
| | | |
| | | if (StringUtils.isBlank(exeDevice.getLink()) || "null".equals(exeDevice.getLink())) { |
| | | //根据设备目标状态,调用MODBUS执行 |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | doWriteExe(modbusTcp, exeDevice.getTargetStatus()); |
| | | |
| | | continue; |
| | | } |
| | | |
| | | |
| | | //TODO 其他状态待定 |
| | | deviceModbus = deviceModbusService.getCacheDeviceModbus(request.getCompanyId(), exeDevice.getLink()); |
| | | if (null == deviceModbus) continue; |
| | | modbusTcpLink = new ModbusTcp(deviceModbus); |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setBizCode(exeDevice.getLink()); |
| | | modbusTcpLink.setSerId(request.getSerId()); |
| | | modbusTcpLink.setCompanyId(request.getCompanyId()); |
| | | if(StringUtils.isNotEmpty(deviceModbus.getSerId())){ |
| | | //若Modbus设备配置分机,则使用所配置分机的IP、端口 |
| | | DeviceSer deviceSer = coreSerService.getCacheSer(request.getCompanyId(), deviceModbus.getSerId()); |
| | | modbusTcpLink.setIp(deviceSer.getIp()); |
| | | modbusTcpLink.setPort(deviceSer.getPort()); |
| | | } |
| | | |
| | | //根据设备目标状态,调用MODBUS执行 |
| | | modbusUtil.writeCoil(modbusTcp.getIp(), modbusTcp.getPort(),addrExe, true); |
| | | //如果是开风机 |
| | | if (DeviceStatus.F_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | |
| | | //先开窗口 |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, DeviceStatus.OPEN.getCode()); |
| | | |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | //再开风机 |
| | | temp = modbusTcpLink.getDeviceModbus().getOpen(); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcpLink, DeviceStatus.OPEN.getCode()); |
| | | } |
| | | |
| | | //如果关风机 |
| | | if (DeviceStatus.F_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcpLink.getDeviceModbus().getStop(); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | | |
| | | |
| | | //如果开窗 |
| | | if (DeviceStatus.W_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, exeDevice.getTargetStatus()); |
| | | } |
| | | |
| | | //如果是关窗户 |
| | | if (DeviceStatus.W_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | |
| | | //先关风机 |
| | | temp = modbusTcpLink.getDeviceModbus().getStop(); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | doWriteExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | //在关窗户 |
| | | temp = modbusTcp.getDeviceModbus().getClose(); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(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 doWriteExe(ModbusTcp modbusTcp, String targetStatus) throws Exception { |
| | | boolean exeResult = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); |
| | | if (exeResult) { |
| | | //根据Modbus-TCP,将写过的值,恢复原来的值 |
| | | ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), false); |
| | | |
| | | log.debug("--------写入值---{}-{}-{}", modbusTcp.getSerId(), modbusTcp.getBizCode(), targetStatus); |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getBizCode(), 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 = coreDeviceService.getCacheDeviceBySerId(request.getCompanyId(), request.getSerId()); |
| | | |
| | | if (null == list || list.isEmpty()) { |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "未获取到设备列表信息"); |
| | | } |
| | | DeviceModbus deviceModbus; |
| | | ModbusTcp modbusTcp; |
| | | ModbusTcp modbusTcpLink; |
| | | for (Device device : list) { |
| | | deviceModbus = deviceModbusService.getCacheDeviceModbus(request.getCompanyId(), device.getPassCode() + ""); |
| | | |
| | | if (null == deviceModbus) { |
| | | log.error("当前设备---{}{},为获取到配置的MODBUS信息", device.getSerId(), device.getName()); |
| | | continue; |
| | | } |
| | | |
| | | if (Constant.YN_N.equals(deviceModbus.getOpen())) continue; |
| | | |
| | | modbusTcp = new ModbusTcp(deviceModbus); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setBizCode(device.getPassCode() + ""); |
| | | modbusTcp.setSerId(device.getSerId()); |
| | | modbusTcp.setCompanyId(device.getCompanyId()); |
| | | |
| | | doReadStatus(modbusTcp); |
| | | |
| | | if (StringUtils.isBlank(device.getLink()) || "null".equals(device.getLink())) continue; |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | deviceModbus = deviceModbusService.getCacheDeviceModbus(request.getCompanyId(), device.getLink()); |
| | | |
| | | if (null == deviceModbus) continue; |
| | | |
| | | modbusTcpLink = new ModbusTcp(deviceModbus); |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setSerId(device.getSerId()); |
| | | modbusTcpLink.setBizCode(device.getLink()); |
| | | modbusTcpLink.setCompanyId(device.getCompanyId()); |
| | | |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd())); |
| | | |
| | | doReadStatus(modbusTcpLink); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("调用MODBUS-TCP执行异常:{}", e); |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "后台执行异常:" + e.getLocalizedMessage()); |
| | | } |
| | | |
| | | analysisService.analysisDevice(request, 3000); |
| | | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); |
| | | } |
| | | |
| | | /** |
| | | * 读取状态,同时读取开状态和关闭状态 |
| | | * |
| | | * @param modbusTcp |
| | | * @throws Exception |
| | | */ |
| | | private void doReadStatus(ModbusTcp modbusTcp) throws Exception { |
| | | |
| | | if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getOpen())) return; |
| | | |
| | | if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getOpenEnd())) return; |
| | | int addrExe = ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd()); |
| | | |
| | | boolean result = ModbusUtil2.readInputStatus(modbusTcp.getIp(), modbusTcp.getPort(), addrExe); |
| | | log.debug("---------读开到位状态------{}-{}-{}", modbusTcp.getBizCode(), addrExe, result); |
| | | |
| | | if (result) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getBizCode(), DeviceStatus.OPEN.getCode()); |
| | | } |
| | | |
| | | if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getCloseEnd())) return; |
| | | addrExe = ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getCloseEnd()); |
| | | result = ModbusUtil2.readInputStatus(modbusTcp.getIp(), modbusTcp.getPort(), addrExe); |
| | | log.debug("---------读关到位状态------{}-{}-{}", modbusTcp.getBizCode(), addrExe, result); |
| | | |
| | | if (result) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getBizCode(), 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(), "终端柜体未接入当前控制"); |
| | | } |
| | | } |