| | |
| | | <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"> |
| | |
| | | <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> |
| | |
| | | <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"> |
| | |
| | | <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> |
| | |
| | | } |
| | | |
| | | /** |
| | | * gasModbusPR#flushCache |
| | | * |
| | | * @return |
| | | */ |
| | | @Expose |
| | | public void flushCache() { |
| | | String companyId = ContextUtil.getCompanyId(); |
| | | modbusService.flushCacheGas(companyId); |
| | | } |
| | | |
| | | /** |
| | | * deviceModbusPR#delData |
| | | * |
| | | * @return |
| | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | @Component |
| | |
| | | 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); |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | |
| | |
| | | Session session = this.getSessionFactory().openSession(); |
| | | try { |
| | | session.delete(data); |
| | | |
| | | flushCacheGas(null); |
| | | } catch (Exception e) { |
| | | } finally { |
| | | session.flush(); |
| | |
| | | |
| | | 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; |
| | |
| | | * |
| | | * @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()) { |
| | |
| | | ipParameters.setPort(port); |
| | | modbusMaster = modbusFactory.createTcpMaster(ipParameters, true); |
| | | modbusMaster.init(); |
| | | |
| | | //åå§åä¹åçå¾
ï¼é¿å
åºç°è¿æ¥æªå建 |
| | | Thread.sleep(2000); |
| | | |
| | | masterMap.put(key, modbusMaster); |
| | | |
| | | return modbusMaster; |
| | | } |
| | | return modbusMaster; |
| | |
| | | * @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; |
| | |
| | | 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; |
| | | } |
| | |
| | | * @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); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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); |
| | | } |
| | | |
| | | |
| | |
| | | * @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(); |
| | | } |
| | | */ |
| | | |
| | | |
| | | /** |
| | |
| | | * @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(); |
| | |
| | | * @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(); |
| | |
| | | * @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(); |
| | |
| | | package com.ld.igds.protocol.modbus; |
| | | |
| | | import com.ld.igds.constant.Constant; |
| | | import com.ld.igds.util.ContextUtil; |
| | | |
| | | import java.util.HashMap; |
| | |
| | | */ |
| | | public class ServerUtil { |
| | | |
| | | |
| | | /** |
| | | * ç¨æ¥ç¼åï¼TCPè®¾å¤æ§è¡æ¶é´è®°å½key = TCPå¯ä¸æ å¿ï¼value = å½åæ§è¡æ¶é´ |
| | | */ |
| | | public static Map<String, Long> contextExeModbusTcp = new HashMap<>(); |
| | | |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |
| | |
| | | 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; |
| | | |
| | | /** |
| | | * è§£æ |
| | |
| | | log.error("---MODBUS-TCP-ç¶æè§£æå¼å¸¸{}", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è§£ææ°ä½æ£æµç»æ |
| | | * |
| | | * @param request |
| | | * @param results |
| | | */ |
| | | public void analysisGas(CheckGasRequest request, List<ModbusGasResult> results) { |
| | | } |
| | | } |
| | |
| | | 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()); |
| | | |
| | |
| | | |
| | | 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()); |
| | | |
| | |
| | | |
| | | //å
å¼çªå£ |
| | | 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.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.getCode().equals(exeDevice.getTargetStatus())) { |
| | | temp = modbusTcp.getDeviceModbus().getOpen(); |
| | | modbusTcp.setAddrExe(this.getAddrExe(temp)); |
| | | modbusTcp.setAddrExe(ServerUtil.getAddrExe(temp)); |
| | | |
| | | doWriteExe(modbusTcp, 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)); |
| | | 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 = 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); |
| | | } |
| | | } |
| | | |
| | |
| | | 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()); |
| | | |
| | |
| | | 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); |
| | | } |
| | |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | |
| | | 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åè®® |
| | |
| | | |
| | | 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()); |
| | | //éç¥è§£æ |
| | | 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); |
| | | } |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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 + |
| | | '}'; |
| | | } |
| | | } |
| | |
| | | |
| | | |
| | | private String companyId; |
| | | |
| | | private String deviceCode; |
| | | |
| | | //ä¸å¡ç¸å
³ç¼ç |
| | | private String bizCode; |
| | | |
| | | private String serId; |
| | | |
| | |
| | | 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; |