From 664bb4d0b7799328667ffc47e39da711a77ddbfa Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期一, 28 八月 2023 12:05:33 +0800 Subject: [PATCH] 更新MODBUS-TCP协议,调整配置2 --- igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java | 13 + igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteControlServiceImpl.java | 51 ++--- igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java | 80 +++++++-- igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java | 44 +--- igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusGasResult.java | 31 +++ igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java | 142 +++++++++++++++++ igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusTcp.java | 9 igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbus.view.xml | 26 +-- igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbusPR.java | 11 - igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java | 19 + 10 files changed, 307 insertions(+), 119 deletions(-) diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbus.view.xml b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbus.view.xml index 50f26ee..12ec7d6 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbus.view.xml +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbus.view.xml @@ -118,11 +118,11 @@ <DataType name="dtGas"> <Property name="creationType">com.ld.igds.models.GasModbus</Property> <PropertyDef name="id"> - <Property/> + <Property></Property> <Property name="label">ID</Property> </PropertyDef> <PropertyDef name="depotId"> - <Property/> + <Property></Property> <Property name="label">鎵�灞炰粨搴�</Property> </PropertyDef> <PropertyDef name="passcode"> @@ -130,27 +130,27 @@ <Property name="label">閫氶亾</Property> </PropertyDef> <PropertyDef name="co2"> - <Property/> + <Property></Property> <Property name="label">浜屾哀鍖栫⒊</Property> </PropertyDef> <PropertyDef name="co2Fun"> - <Property/> + <Property></Property> <Property name="label">鍔熻兘鍙�</Property> </PropertyDef> <PropertyDef name="o2"> - <Property/> + <Property></Property> <Property name="label">姘ф皵</Property> </PropertyDef> <PropertyDef name="o2Fun"> - <Property/> + <Property></Property> <Property name="label">鍔熻兘鍙�</Property> </PropertyDef> <PropertyDef name="ph3"> - <Property/> + <Property></Property> <Property name="label">纾峰寲姘�</Property> </PropertyDef> <PropertyDef name="ph3Fun"> - <Property/> + <Property></Property> <Property name="label">鍔熻兘鍙�</Property> </PropertyDef> </DataType> @@ -199,12 +199,6 @@ <Property name="caption">鍒犻櫎閫氶亾</Property> <Property name="exClassName">btn-warn</Property> <Property name="iconClass">fa fa-minus</Property> - </Button> - <Button layoutConstraint="left"> - <Property name="caption">鍒锋柊缂撳瓨</Property> - <Property name="exClassName">btn-default</Property> - <Property name="iconClass">fa fa-refresh</Property> - <Property name="action">ajaxFlushCache</Property> </Button> </Container> <Container layout="regionPadding:10" layoutConstraint="center"> @@ -364,10 +358,6 @@ <AjaxAction id="ajaxDel"> <Property name="service">gasModbusPR#delData</Property> <Property name="confirmMessage">纭畾瑕佸垹闄や箞锛�</Property> - </AjaxAction> - <AjaxAction id="ajaxFlushCache"> - <Property name="service">gasModbusPR#flushCache</Property> - <Property name="successMessage">缂撳瓨鏇存柊鎴愬姛</Property> </AjaxAction> </View> </ViewConfig> diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbusPR.java b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbusPR.java index 266c5ac..05d89ff 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbusPR.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/GasModbusPR.java @@ -70,17 +70,6 @@ } /** - * gasModbusPR#flushCache - * - * @return - */ - @Expose - public void flushCache() { - String companyId = ContextUtil.getCompanyId(); - modbusService.flushCacheGas(companyId); - } - - /** * deviceModbusPR#delData * * @return diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java index 7652715..0f1ff64 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/modbus/service/HModbusService.java @@ -18,7 +18,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; @Component @@ -49,13 +48,26 @@ for (DeviceModbus hibDevice : listAll) { entityDevice = new DeviceModbus(); BeanUtils.copyProperties(hibDevice, entityDevice); - key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode()); + + if (null == hibDevice.getBizType()) hibDevice.setBizType(BizType.AREATION.getCode()); + if (BizType.AREATION.getCode().equals(hibDevice.getBizType())) { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode()); + } else { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, hibDevice.getDeviceCode(), hibDevice.getBizType()); + } redisUtil.set(key, entityDevice); } } public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode) { + return getCacheDeviceModbus(companyId, deviceCode, null); + } + + public DeviceModbus getCacheDeviceModbus(String companyId, String deviceCode, String bizType) { String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode); + if (null != bizType && !BizType.AREATION.equals(bizType)) { + key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_DEVICE_CACHE, deviceCode, bizType); + } return (DeviceModbus) redisUtil.get(key); } @@ -194,32 +206,6 @@ session.update(gas); } } - flushCacheGas(null); - } - - public void flushCacheGas(String companyId) { - - List<GasModbus> listAll = this.listGasModBus(null); - - if (null == listAll || listAll.isEmpty()) return; - - if (null == companyId) companyId = ContextUtil.getDefaultCompanyId(); - - - Map<String, List<GasModbus>> collect = listAll.stream().collect(Collectors.groupingBy(GasModbus::getDepotId)); - - String key; - for (String depotId : collect.keySet()) { - key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_GAS_CACHE_LIST, depotId); - redisUtil.set(key, key); - } - } - - - public List<GasModbus> getCacheGasModbus(String companyId, String depotId) { - if (null == companyId) companyId = ContextUtil.getDefaultCompanyId(); - String key = RedisConst.buildKey(companyId, ModbusConstant.MODBUS_GAS_CACHE_LIST, depotId); - return (List<GasModbus>) redisUtil.get(key); } @@ -227,8 +213,6 @@ Session session = this.getSessionFactory().openSession(); try { session.delete(data); - - flushCacheGas(null); } catch (Exception e) { } finally { session.flush(); diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java index b6395c0..56a5d85 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ModbusUtil2.java @@ -2,6 +2,7 @@ import com.serotonin.modbus4j.ModbusFactory; import com.serotonin.modbus4j.ModbusMaster; +import com.serotonin.modbus4j.code.DataType; import com.serotonin.modbus4j.exception.ErrorResponseException; import com.serotonin.modbus4j.exception.ModbusInitException; import com.serotonin.modbus4j.exception.ModbusTransportException; @@ -44,7 +45,7 @@ * * @return */ - public static ModbusMaster getMaster(String ip, int port) throws ModbusInitException { + public static ModbusMaster getMaster(String ip, int port) throws ModbusInitException, InterruptedException { String key = ip; ModbusMaster modbusMaster = masterMap.get(key); if (modbusMaster == null || !modbusMaster.isConnected()) { @@ -53,7 +54,12 @@ ipParameters.setPort(port); modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); modbusMaster.init(); + + //鍒濆鍖栦箣鍚庣瓑寰咃紝閬垮厤鍑虹幇杩炴帴鏈垱寤� + Thread.sleep(2000); + masterMap.put(key, modbusMaster); + return modbusMaster; } return modbusMaster; @@ -69,7 +75,7 @@ * @throws ModbusTransportException * @throws ErrorResponseException */ - public static Boolean readCoilStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException { + public static Boolean readCoilStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { BaseLocator<Boolean> coilStatus = BaseLocator.coilStatus(slaveId, offset); Boolean res = getMaster(ip, port).getValue(coilStatus); return res; @@ -87,7 +93,7 @@ public static Boolean readInputStatus(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { BaseLocator<Boolean> inputStatus = BaseLocator.inputStatus(slaveId, offset); - Thread.sleep(3000); + // Thread.sleep(3000); Boolean res = getMaster(ip, port).getValue(inputStatus); return res; } @@ -102,10 +108,15 @@ * @throws ModbusTransportException * @throws ErrorResponseException */ - public static Number readHoldingRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { + public static Number readHoldingRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { BaseLocator<Number> holdingRegister = BaseLocator.holdingRegister(slaveId, offset, dataType); Number value = getMaster(ip, port).getValue(holdingRegister); return value; + } + + public static Number readHoldingRegister(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { + int dataType = DataType.TWO_BYTE_INT_SIGNED; + return readHoldingRegister(ip, port, offset, dataType); } /** @@ -118,10 +129,24 @@ * @throws ModbusTransportException * @throws ErrorResponseException */ - public static Number readInputRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException { + public static Number readInputRegister(String ip, int port, int offset, int dataType) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { BaseLocator<Number> inputRegister = BaseLocator.inputRegister(slaveId, offset, dataType); Number value = getMaster(ip, port).getValue(inputRegister); return value; + } + + /** + * 璇昏緭鍏ュ瘎瀛樺櫒鏁版嵁 0x04 + * + * @param offset + * @return + * @throws ModbusInitException + * @throws ModbusTransportException + * @throws ErrorResponseException + */ + public static Number readInputRegister(String ip, int port, int offset) throws ModbusInitException, ModbusTransportException, ErrorResponseException, InterruptedException { + int dataType = DataType.TWO_BYTE_INT_SIGNED; + return readInputRegister(ip, port, offset, dataType); } @@ -135,21 +160,38 @@ * @throws ModbusInitException */ public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException, InterruptedException { -// boolean coilValue = status; -// WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue); -// WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); -// return !coilResponse.isException(); - WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, status); - Thread.sleep(3000); WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); - - //鍙戦�佹竻闄ゅ懡浠� - WriteCoilRequest coilRequest1 = new WriteCoilRequest(slaveId, offset, !status); - getMaster(ip, port).send(coilRequest1); - return !coilResponse.isException(); } + + + /** + * 鍐欑嚎鍦堝紑鍏崇姸鎬佹暟鎹� 0x05 + * + * @param offset + * @param status + * @return + * @throws ModbusTransportException + * @throws ModbusInitException + + public static Boolean writeCoilStatus(String ip, int port, int offset, boolean status) throws ModbusTransportException, ModbusInitException, InterruptedException { + // boolean coilValue = status; + // WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, coilValue); + // WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); + // return !coilResponse.isException(); + + WriteCoilRequest coilRequest = new WriteCoilRequest(slaveId, offset, status); + // Thread.sleep(3000); + WriteCoilResponse coilResponse = (WriteCoilResponse) getMaster(ip, port).send(coilRequest); + + //鍙戦�佹竻闄ゅ懡浠� + WriteCoilRequest coilRequest1 = new WriteCoilRequest(slaveId, offset, !status); + getMaster(ip, port).send(coilRequest1); + + return !coilResponse.isException(); + } + */ /** @@ -161,7 +203,7 @@ * @throws ModbusTransportException * @throws ModbusInitException */ - public static Boolean writeRegister(String ip, int port, int offset, int value) throws ModbusTransportException, ModbusInitException { + public static Boolean writeRegister(String ip, int port, int offset, int value) throws ModbusTransportException, ModbusInitException, InterruptedException { WriteRegisterRequest registerRequest = new WriteRegisterRequest(slaveId, offset, value); WriteRegisterResponse registerResponse = (WriteRegisterResponse) getMaster(ip, port).send(registerRequest); return !registerResponse.isException(); @@ -177,7 +219,7 @@ * @throws ModbusTransportException * @throws ModbusInitException */ - public static Boolean writeCoils(String ip, int port, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException { + public static Boolean writeCoils(String ip, int port, int offset, boolean[] booleans) throws ModbusTransportException, ModbusInitException, InterruptedException { WriteCoilsRequest writeCoils = new WriteCoilsRequest(slaveId, offset, booleans); WriteCoilsResponse coilsResponse = (WriteCoilsResponse) getMaster(ip, port).send(writeCoils); return !coilsResponse.isException(); @@ -193,7 +235,7 @@ * @throws ModbusTransportException * @throws ModbusInitException */ - public static Boolean writeRegisters(String ip, int port, int offset, short[] nums) throws ModbusTransportException, ModbusInitException { + public static Boolean writeRegisters(String ip, int port, int offset, short[] nums) throws ModbusTransportException, ModbusInitException, InterruptedException { WriteRegistersRequest writeRegisters = new WriteRegistersRequest(slaveId, offset, nums); WriteRegistersResponse registersResponse = (WriteRegistersResponse) getMaster(ip, port).send(writeRegisters); return !registersResponse.isException(); diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java index b31119c..84352e8 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/ServerUtil.java @@ -1,5 +1,6 @@ package com.ld.igds.protocol.modbus; +import com.ld.igds.constant.Constant; import com.ld.igds.util.ContextUtil; import java.util.HashMap; @@ -10,11 +11,6 @@ */ public class ServerUtil { - - /** - * 鐢ㄦ潵缂撳瓨锛孴CP璁惧鎵ц鏃堕棿璁板綍key = TCP鍞竴鏍囧織锛寁alue = 褰撳墠鎵ц鏃堕棿 - */ - public static Map<String, Long> contextExeModbusTcp = new HashMap<>(); /** @@ -38,4 +34,17 @@ return contextStatusMap; } + + + + + + public static 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; + } + } diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java index 07211d3..e069867 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/AnalysisService.java @@ -3,13 +3,16 @@ import com.ld.igds.common.CoreDeviceService; import com.ld.igds.io.constant.OrderRespEnum; import com.ld.igds.io.notify.NotifyWebInvoker; +import com.ld.igds.io.request.CheckGasRequest; import com.ld.igds.io.request.DeviceControlRequest; import com.ld.igds.protocol.modbus.ServerUtil; +import com.ld.igds.protocol.modbus.data.ModbusGasResult; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.List; /** * 瑙f瀽 @@ -46,4 +49,14 @@ log.error("---MODBUS-TCP-鐘舵�佽В鏋愬紓甯竰}", e); } } + + + /** + * 瑙f瀽姘斾綋妫�娴嬬粨鏋� + * + * @param request + * @param results + */ + public void analysisGas(CheckGasRequest request, List<ModbusGasResult> results) { + } } diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteControlServiceImpl.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteControlServiceImpl.java index 9d6730e..2fe10f4 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteControlServiceImpl.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteControlServiceImpl.java @@ -74,7 +74,7 @@ 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()); @@ -89,7 +89,7 @@ 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; @@ -101,7 +101,7 @@ 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()); @@ -111,7 +111,7 @@ //鍏堝紑绐楀彛 temp = modbusTcp.getDeviceModbus().getOpen(); - modbusTcp.setAddrExe(this.getAddrExe(temp)); + modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); doWriteExe(modbusTcp, DeviceStatus.OPEN.getCode()); @@ -120,7 +120,7 @@ //鍐嶅紑椋庢満 temp = modbusTcpLink.getDeviceModbus().getOpen(); - modbusTcpLink.setAddrExe(this.getAddrExe(temp)); + modbusTcpLink.setAddrExe(ServerUtil.getAddrExe(temp)); doWriteExe(modbusTcpLink, DeviceStatus.OPEN.getCode()); } @@ -128,7 +128,7 @@ //濡傛灉鍏抽鏈� 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()); } @@ -137,7 +137,7 @@ //濡傛灉寮�绐� 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()); } @@ -147,14 +147,14 @@ //鍏堝叧椋庢満 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)); + modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); doWriteExe(modbusTcp, DeviceStatus.CLOSE.getCode()); } @@ -168,19 +168,14 @@ 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 = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); if (exeResult) { - log.debug("--------鍐欏叆鍊�---{}-{}-{}", modbusTcp.getSerId(), modbusTcp.getDeviceCode(), targetStatus); - 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); } } @@ -214,7 +209,7 @@ 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()); @@ -232,10 +227,10 @@ 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())); doReadStatus(modbusTcpLink); } @@ -260,22 +255,22 @@ if (Constant.YN_N.equals(modbusTcp.getDeviceModbus().getOpen())) return; - int addrExe = getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd()); + int addrExe = ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getOpenEnd()); boolean result = ModbusUtil2.readInputStatus(modbusTcp.getIp(), modbusTcp.getPort(), addrExe); - log.debug("---------璇诲紑鍒颁綅鐘舵��------{}-{}-{}", modbusTcp.getDeviceCode(), addrExe, result); + log.debug("---------璇诲紑鍒颁綅鐘舵��------{}-{}-{}", modbusTcp.getBizCode(), addrExe, result); if (result) { - ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), DeviceStatus.OPEN.getCode()); + ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getBizCode(), DeviceStatus.OPEN.getCode()); } - addrExe = getAddrExe(modbusTcp.getDeviceModbus().getCloseEnd()); + addrExe = ServerUtil.getAddrExe(modbusTcp.getDeviceModbus().getCloseEnd()); result = ModbusUtil2.readInputStatus(modbusTcp.getIp(), modbusTcp.getPort(), addrExe); - log.debug("---------璇诲叧鍒颁綅鐘舵��------{}-{}-{}", modbusTcp.getDeviceCode(), addrExe, result); + log.debug("---------璇诲叧鍒颁綅鐘舵��------{}-{}-{}", modbusTcp.getBizCode(), addrExe, result); if (result) { - ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getDeviceCode(), DeviceStatus.CLOSE.getCode()); + ServerUtil.add2StatusMap(modbusTcp.getCompanyId(), modbusTcp.getSerId(), modbusTcp.getBizCode(), DeviceStatus.CLOSE.getCode()); } } diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java index 8862faa..f475b24 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/command/RemoteGasServiceImpl.java @@ -1,10 +1,26 @@ package com.ld.igds.protocol.modbus.command; +import com.ld.igds.constant.BizType; import com.ld.igds.io.RemoteGasService; +import com.ld.igds.io.constant.OrderRespEnum; +import com.ld.igds.io.constant.ProtocolEnum; import com.ld.igds.io.request.CheckGasRequest; +import com.ld.igds.io.response.DeviceControlResponse; import com.ld.igds.io.response.GasResponse; +import com.ld.igds.modbus.service.HModbusService; +import com.ld.igds.models.DeviceModbus; +import com.ld.igds.models.GasModbus; +import com.ld.igds.protocol.modbus.ModbusUtil2; +import com.ld.igds.protocol.modbus.ServerUtil; +import com.ld.igds.protocol.modbus.data.ModbusGasResult; +import com.ld.igds.protocol.modbus.data.ModbusTcp; import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; /** * 褰撳墠鍗忚閽堝鏍囧噯Modbus-TCP鍗忚 @@ -19,23 +35,141 @@ public static final String BEAN_ID = "modbus.remoteGasService"; + @Resource + private HModbusService modbusService; + @Resource + private AnalysisService analysisService; + @Override public String getProtocol() { - return null; + return ProtocolEnum.TCP_MODBUS.getCode(); } @Override public GasResponse checkGas(CheckGasRequest request) { - return null; + String passCode = request.getDepotId(); + + try { + //鑾峰彇闇�瑕佹墽琛岀殑璁惧閰嶇疆 + DeviceModbus deviceModbus = modbusService.getCacheDeviceModbus(request.getCompanyId(), passCode, BizType.GAS.getCode()); + + ModbusTcp modbusTcp = new ModbusTcp(deviceModbus); + modbusTcp.setIp(request.getIp()); + modbusTcp.setPort(request.getPort()); + modbusTcp.setBizCode(passCode); + modbusTcp.setSerId(request.getSerId()); + modbusTcp.setCompanyId(request.getCompanyId()); + + modbusTcp.setAddrExe(ServerUtil.getAddrExe(deviceModbus.getOpen())); + + boolean exeResult = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); + if (exeResult) { + //璋冪敤寮傛鑾峰彇缁撴灉 + progressGasResult(request, modbusTcp); + log.debug("--------鍚姩娴嬫皵---{}-{}", modbusTcp.getSerId(), modbusTcp.getBizCode()); + } + + } catch (Exception e) { + return new GasResponse(OrderRespEnum.ORDER_ERROR.getCode(), "鍚庡彴鎵ц寮傚父锛�" + e.getLocalizedMessage()); + } + + return new GasResponse(OrderRespEnum.ORDER_SUCCESS); + } + + /** + * 寮傛鎵ц杩涘害鏌ヨ + * + * @param modbusTcp + */ + @Async + public void progressGasResult(CheckGasRequest request, ModbusTcp modbusTcp) { + + log.debug("--------------寮傛鎵ц姘斾綋妫�娴嬭杩囪幏鍙�-------------"); + + try { + List<GasModbus> list = modbusService.listGasModBus(modbusTcp.getBizCode()); + + if (null == list || list.isEmpty()) { + log.error("------------鏇存柊姘斾綋妫�娴嬬粨鏋滆繘搴﹀け璐ワ紝娌℃湁鑾峰彇鍒版娴嬮�氶亾閰嶇疆淇℃伅-----{}", modbusTcp.getBizCode()); + return; + } + + //鏍规嵁閫氶亾鏁伴噺绾跨▼绛夊緟锛屾瘡涓�氶亾绛夊緟50绉� + Thread.sleep(list.size() * 50 * 1000); + log.debug("--------------绛夊緟鏃堕棿瀹屾垚锛屽紑濮嬭幏鍙栨娴嬬粨鏋�-------------{}", modbusTcp.getBizCode()); + + ModbusTcp modbusTcpTemp = modbusTcp; + List<ModbusGasResult> results = new ArrayList<>(); + ModbusGasResult gasResult; + Number check; + for (GasModbus gasModbus : list) { + gasResult = new ModbusGasResult(); + gasResult.setPasscode(gasModbus.getPasscode()); + + //浜屾哀鍖栫⒊ + modbusTcpTemp.setAddrExe(ServerUtil.getAddrExe(gasModbus.getCo2())); + check = ModbusUtil2.readHoldingRegister(modbusTcpTemp.getIp(), modbusTcpTemp.getPort(), modbusTcpTemp.getAddrExe()); + gasResult.setCo2(check); + + //姘ф皵 + modbusTcpTemp.setAddrExe(ServerUtil.getAddrExe(gasModbus.getO2())); + check = ModbusUtil2.readHoldingRegister(modbusTcpTemp.getIp(), modbusTcpTemp.getPort(), modbusTcpTemp.getAddrExe()); + gasResult.setO2(check); + + //纾峰寲姘� + modbusTcpTemp.setAddrExe(ServerUtil.getAddrExe(gasModbus.getPh3())); + check = ModbusUtil2.readHoldingRegister(modbusTcpTemp.getIp(), modbusTcpTemp.getPort(), modbusTcpTemp.getAddrExe()); + gasResult.setPh3(check); + + results.add(gasResult); + } + + + log.debug("----鑾峰彇鐨勬皵浣撴娴嬬粨鏋�---{}--{}", request.getDepotId(), results.toString()); + //閫氱煡瑙f瀽 + analysisService.analysisGas(request, results); + + } catch (Exception e) { + log.error("------------鏇存柊姘斾綋妫�娴嬬粨鏋滆繘搴﹀け璐ワ紝鎵ц寮傚父-----{}--{}", modbusTcp.getBizCode(), e.getMessage()); + } + } @Override public GasResponse checkGasByPoint(CheckGasRequest request) { - return null; + return new GasResponse(OrderRespEnum.ORDER_ERROR.getCode(), "褰撳墠鎺у埗鏌滀笉鏀寔閫夋嫨鐐逛綅閲囬泦"); } @Override public GasResponse stopCheckGas(CheckGasRequest request) { - return null; + String passCode = request.getDepotId(); + + try { + //鑾峰彇闇�瑕佹墽琛岀殑璁惧閰嶇疆 + DeviceModbus deviceModbus = modbusService.getCacheDeviceModbus(request.getCompanyId(), passCode, BizType.GAS.getCode()); + + ModbusTcp modbusTcp = new ModbusTcp(deviceModbus); + modbusTcp.setIp(request.getIp()); + modbusTcp.setPort(request.getPort()); + modbusTcp.setBizCode(passCode); + modbusTcp.setSerId(request.getSerId()); + modbusTcp.setCompanyId(request.getCompanyId()); + + modbusTcp.setAddrExe(ServerUtil.getAddrExe(deviceModbus.getStop())); + + boolean exeResult = ModbusUtil2.writeCoilStatus(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrExe(), true); + if (exeResult) { + log.debug("--------鍏抽棴娴嬫皵---{}-{}", modbusTcp.getSerId(), modbusTcp.getBizCode()); + + //閫氱煡寮傛缁撴潫缁撴灉閲囬泦 + } + + } catch (Exception e) { + return new GasResponse(OrderRespEnum.ORDER_ERROR.getCode(), "鍚庡彴鎵ц寮傚父锛�" + e.getLocalizedMessage()); + } + + return new GasResponse(OrderRespEnum.ORDER_SUCCESS); } + + } diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusGasResult.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusGasResult.java new file mode 100644 index 0000000..5285f72 --- /dev/null +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusGasResult.java @@ -0,0 +1,31 @@ +package com.ld.igds.protocol.modbus.data; + +import lombok.Data; + +/** + * @Desc: 鐢ㄤ簬灏佽ModBUsTCP鐨勪俊鎭� + * @author: andy.jia + * @update-time: 2023/8/12 15:29 + */ +@Data +public class ModbusGasResult { + + private int passcode; + + private Number co2; + + private Number o2; + + private Number ph3; + + + @Override + public String toString() { + return "GasResult{" + + "passcode=" + passcode + + ", co2=" + co2 + + ", o2=" + o2 + + ", ph3=" + ph3 + + '}'; + } +} diff --git a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusTcp.java b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusTcp.java index c69d629..f743c9d 100644 --- a/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusTcp.java +++ b/igds-protocol-modbus/src/main/java/com/ld/igds/protocol/modbus/data/ModbusTcp.java @@ -13,8 +13,9 @@ private String companyId; - - private String deviceCode; + + //涓氬姟鐩稿叧缂栫爜 + private String bizCode; private String serId; @@ -34,9 +35,9 @@ this.deviceModbus = deviceModbus; } - public ModbusTcp(String companyId, String deviceCode, String serId, String ip, int port, DeviceModbus deviceModbus) { + public ModbusTcp(String companyId, String bizCode, String serId, String ip, int port, DeviceModbus deviceModbus) { this.companyId = companyId; - this.deviceCode = deviceCode; + this.bizCode = bizCode; this.serId = serId; this.ip = ip; this.port = port; -- Gitblit v1.9.3