| | |
| | | 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.HDeviceModbusService; |
| | | import com.ld.igds.modbus.service.HModbusService; |
| | | import com.ld.igds.models.Device; |
| | | import com.ld.igds.models.DeviceModbus; |
| | | import com.ld.igds.protocol.modbus.ModbusUtil; |
| | | 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; |
| | |
| | | @Resource |
| | | private CoreDeviceService coreDeviceService; |
| | | @Resource |
| | | private HDeviceModbusService deviceModbusService; |
| | | private HModbusService deviceModbusService; |
| | | @Resource |
| | | private AnalysisService analysisService; |
| | | @Resource |
| | | private ModbusUtil modbusUtil; |
| | | private CoreSerService coreSerService; |
| | | |
| | | |
| | | @Override |
| | |
| | | modbusTcp = new ModbusTcp(deviceModbus); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceCode(exeDevice.getPassCode() + ""); |
| | | modbusTcp.setBizCode(exeDevice.getPassCode() + ""); |
| | | modbusTcp.setSerId(request.getSerId()); |
| | | modbusTcp.setCompanyId(request.getCompanyId()); |
| | | |
| | |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | } |
| | | |
| | | //环流风机-关 |
| | | if (DeviceStatus.F_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getStop(); |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(exeDevice.getLink())) { |
| | | //环流风机-开 |
| | | if (DeviceStatus.F_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | } |
| | | |
| | | if (StringUtils.isBlank(exeDevice.getLink()) || "null".equals(exeDevice.getLink())) { |
| | | //根据设备目标状态,调用MODBUS执行 |
| | | modbusTcp.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | doWriteExe(modbusTcp, exeDevice.getTargetStatus()); |
| | | |
| | | continue; |
| | |
| | | modbusTcpLink = new ModbusTcp(deviceModbus); |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setDeviceCode(exeDevice.getLink()); |
| | | 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()); |
| | | } |
| | | |
| | | //如果是开风机 |
| | | if (DeviceStatus.F_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | |
| | | //先开窗口 |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, DeviceStatus.OPEN.getCode()); |
| | | |
| | |
| | | |
| | | //再开风机 |
| | | temp = modbusTcpLink.getDeviceModbus().getOpen(); |
| | | modbusTcpLink.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcpLink, DeviceStatus.OPEN.getCode()); |
| | | } |
| | | |
| | | //如果关风机 |
| | | if (DeviceStatus.F_CLOSE.equals(exeDevice.getTargetStatus())) { |
| | | if (DeviceStatus.F_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcpLink.getDeviceModbus().getStop(); |
| | | modbusTcpLink.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | | |
| | | |
| | | //如果开窗 |
| | | if (DeviceStatus.W_OPEN.equals(exeDevice.getTargetStatus())) { |
| | | if (DeviceStatus.W_OPEN.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, exeDevice.getTargetStatus()); |
| | | } |
| | | |
| | | //如果是关窗户 |
| | | if (DeviceStatus.W_CLOSE.equals(exeDevice.getTargetStatus())) { |
| | | if (DeviceStatus.W_CLOSE.getCode().equals(exeDevice.getTargetStatus())) { |
| | | |
| | | //先关风机 |
| | | temp = modbusTcpLink.getDeviceModbus().getStop(); |
| | | modbusTcpLink.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | doWriteExe(modbusTcpLink, DeviceStatus.CLOSE.getCode()); |
| | | |
| | | Thread.sleep(300); |
| | | |
| | | //在关窗户 |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(this.getAddrExe(temp)); |
| | | temp = modbusTcp.getDeviceModbus().getClose(); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, DeviceStatus.CLOSE.getCode()); |
| | | } |
| | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); |
| | | } |
| | | |
| | | private int getAddrExe(String temp) { |
| | | if (Constant.YN_N.equals(temp)) return 65535; |
| | | if (temp.length() > 4) { |
| | | temp = temp.substring(temp.length() - 4); |
| | | } |
| | | return Integer.valueOf(temp) - 1; |
| | | } |
| | | |
| | | private void doWriteExe(ModbusTcp modbusTcp, String targetStatus) throws Exception { |
| | | boolean exeResult = modbusUtil.writeValue05(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); |
| | | boolean exeResult = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); |
| | | if (exeResult) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), targetStatus); |
| | | //根据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); |
| | | } |
| | | } |
| | | |
| | |
| | | if (null == list || list.isEmpty()) { |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "未获取到设备列表信息"); |
| | | } |
| | | |
| | | DeviceModbus deviceModbus; |
| | | ModbusTcp modbusTcp; |
| | | ModbusTcp modbusTcpLink; |
| | | int i = 1; |
| | | for (Device device : list) { |
| | | |
| | | deviceModbus = deviceModbusService.getCacheDeviceModbus(request.getCompanyId(), device.getPassCode() + ""); |
| | | |
| | | if (null == deviceModbus) { |
| | |
| | | modbusTcp = new ModbusTcp(deviceModbus); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceCode(device.getPassCode() + ""); |
| | | modbusTcp.setBizCode(device.getPassCode() + ""); |
| | | modbusTcp.setSerId(device.getSerId()); |
| | | modbusTcp.setCompanyId(device.getCompanyId()); |
| | | |
| | | // modbusTcp.setAddrExe(this.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd())); |
| | | doReadStatus(modbusTcp); |
| | | |
| | | modbusTcp.setAddrExe(1024); |
| | | doReadExe(modbusTcp); |
| | | |
| | | if (i == 1) break; |
| | | |
| | | if (StringUtils.isEmpty(device.getLink())) continue; |
| | | if (StringUtils.isBlank(device.getLink()) || "null".equals(device.getLink())) continue; |
| | | |
| | | Thread.sleep(300); |
| | | |
| | |
| | | modbusTcpLink.setIp(request.getIp()); |
| | | modbusTcpLink.setPort(request.getPort()); |
| | | modbusTcpLink.setSerId(device.getSerId()); |
| | | modbusTcpLink.setDeviceCode(device.getLink()); |
| | | modbusTcpLink.setBizCode(device.getLink()); |
| | | modbusTcpLink.setCompanyId(device.getCompanyId()); |
| | | |
| | | modbusTcpLink.setAddrExe(this.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd())); |
| | | modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd())); |
| | | |
| | | doReadExe(modbusTcpLink); |
| | | doReadStatus(modbusTcpLink); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | |
| | | * @param modbusTcp |
| | | * @throws Exception |
| | | */ |
| | | private void doReadExe(ModbusTcp modbusTcp) throws Exception { |
| | | private void doReadStatus(ModbusTcp modbusTcp) throws Exception { |
| | | |
| | | if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getOpen())) return; |
| | | |
| | | boolean[] result = modbusUtil.readStatus02(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), 2); |
| | | log.debug("---------读取状态------{}--{}", modbusTcp.getDeviceCode(), result.toString()); |
| | | if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getOpenEnd())) return; |
| | | int addrExe = ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd()); |
| | | |
| | | if (result[0]) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), DeviceStatus.OPEN.getCode()); |
| | | 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 (result[1]) { |
| | | ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), DeviceStatus.CLOSE.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()); |
| | | } |
| | | } |
| | | |