调整DEVICE的字段和缓存,增加支持根据ID直接获取设备信息,优化MODBUS-TCP协议 实现
| | |
| | | */ |
| | | List<Device> getCacheDeviceBySerId(String companyId, String serId); |
| | | |
| | | |
| | | /** |
| | | * æ ¹æ®åæºä¿¡æ¯è·åè®¾å¤ |
| | | * |
| | | * @param companyId |
| | | * @param id |
| | | * @return |
| | | */ |
| | | Device getCacheDeviceById(String companyId, String id); |
| | | |
| | | /** |
| | | * å·æ°ç¼å |
| | | * |
| | |
| | | package com.ld.igds.common.impl; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.ld.igds.common.CoreDeviceService; |
| | | import com.ld.igds.common.dto.DepotSerData; |
| | | import com.ld.igds.common.mapper.DeviceMapper; |
| | | import com.ld.igds.constant.Constant; |
| | | import com.ld.igds.constant.DeviceStatus; |
| | | |
| | | import com.ld.igds.constant.DeviceType; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | |
| | | @Override |
| | | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| | | @Override |
| | | public void setCacheAllDevice(List<Device> listAll, String companyId) { |
| | | // é¦å
æç
§åæºåç»ï¼ç¶ååæ´æ°ç¼åã |
| | | Map<String, List<Device>> map = listAll.stream().collect(Collectors.groupingBy(Device::getSerId)); |
| | | |
| | | if (null == map || map.isEmpty()) { |
| | | log.error("åå
¸ä¿¡æ¯ï¼ææè®¾å¤æç
§åæºåç»ä¿åç¼åå¤±è´¥ï¼æ²¡æåç»æå=={}", companyId); |
| | | return; |
| | | } |
| | | if (null == listAll || listAll.isEmpty()) return; |
| | | |
| | | for (String serId : map.keySet()) { |
| | | updateCacheDeviceBySerId(map.get(serId), companyId, serId); |
| | | } |
| | | //æç
§åæºåç±» |
| | | Map<String, List<Device>> mapSer = new HashMap<>(); |
| | | |
| | | this.setCacheDepotDeviceSer(listAll, companyId); |
| | | } |
| | | //ä»åº-设å¤-åæºå¯¹è±¡åç±» |
| | | Map<String, DepotSerData> mapDepot = new HashMap<>(); |
| | | |
| | | @SuppressWarnings({"unchecked", "rawtypes"}) |
| | | private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) { |
| | | // å¤çä»åºä¸åæºçå
³ç³»ç¼å |
| | | Map<String, DepotSerData> tempMap = new HashMap<>(); |
| | | String key; |
| | | for (Device item : listAll) { |
| | | key = companyId + "-" + item.getDepotId() + "-" + item.getSerId(); |
| | | if (tempMap.get(key) == null) { |
| | | tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId())); |
| | | |
| | | //æè®¾å¤æç
§åæºè¿è¡åç»ï¼åäºå§è®¾å¤æ·»å å°ç¼ååè¡¨ä¸ |
| | | String deviceKey, depotDataKey; |
| | | for (Device device : listAll) { |
| | | |
| | | //å°è£
ä»åº-设å¤-åæºå¯¹è±¡å
³ç³» |
| | | depotDataKey = companyId + "-" + device.getDepotId() + "-" + device.getSerId(); |
| | | if (mapDepot.get(depotDataKey) == null) { |
| | | mapDepot.put(depotDataKey, new DepotSerData(companyId, device.getDepotId(), device.getSerId())); |
| | | } |
| | | |
| | | |
| | | //å个设å¤è¿è¡ç¼å |
| | | deviceKey = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE, device.getId()); |
| | | redisUtil.set(deviceKey, device); |
| | | |
| | | //æç
§åæºåç±» |
| | | if (null == mapSer.get(device.getSerId())) mapSer.put(device.getSerId(), new ArrayList<Device>()); |
| | | mapSer.get(device.getSerId()).add(device); |
| | | |
| | | } |
| | | |
| | | redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values())); |
| | | //ä»¥åæºä¸ºåä½ç¼åè®¾å¤ |
| | | for (String serId : mapSer.keySet()) { |
| | | updateCacheDeviceBySerId(mapSer.get(serId), companyId, serId); |
| | | } |
| | | |
| | | |
| | | redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(mapDepot.values())); |
| | | } |
| | | |
| | | |
| | | // private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) { |
| | | // // å¤çä»åºä¸åæºçå
³ç³»ç¼å |
| | | // Map<String, DepotSerData> tempMap = new HashMap<>(); |
| | | // String key; |
| | | // for (Device item : listAll) { |
| | | // key = companyId + "-" + item.getDepotId() + "-" + item.getSerId(); |
| | | // if (tempMap.get(key) == null) { |
| | | // tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId())); |
| | | // } |
| | | // } |
| | | // |
| | | // redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values())); |
| | | // } |
| | | |
| | | @Override |
| | | public void updateCacheDeviceBySerId(List<Device> listBySer, String companyId, String serId) { |
| | |
| | | return null; |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | public Device getCacheDeviceById(String companyId, String id) { |
| | | String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE, id); |
| | | return (Device) redisUtil.get(key); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | log.debug("è§£æè®¾å¤ç¶æ--主éé={}---å
³èéé={}--è§£æåç¶æ={}", curDevice.getPassCode(), curDevice.getLink(), curDevice.getStatus()); |
| | | } |
| | | |
| | | |
| | | // æ´æ°ç¼å |
| | | this.updateCacheDeviceBySerId(listCache, companyId, serId); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<Device> getDeviceByTimId(String companyId,String timId){ |
| | | return deviceMapper.queryByTimId(companyId,timId); |
| | | public List<Device> getDeviceByTimId(String companyId, String timId) { |
| | | return deviceMapper.queryByTimId(companyId, timId); |
| | | } |
| | | } |
| | |
| | | * 设å¤å表 |
| | | */ |
| | | public static final String KEY_DEVICE_LIST = "DEVICE_LIST"; |
| | | |
| | | /** |
| | | * å个è²å·å§ |
| | | */ |
| | | public static final String KEY_DEVICE = "DEVICE"; |
| | | |
| | | /** |
| | | * 设å¤å表 |
| | | */ |
| | |
| | | TCP_BHZH_VERB_V2("TCP_BHZH_VERB_V2", "馿µ·æ§å¶ææºè½æçº¿çV2"), |
| | | TCP_BHZH_INOUT_V1("TCP_BHZH_INOUT_V1", "馿µ·æºè½åºå
¥åºæ§å¶å¨åè®®"), |
| | | TCP_BEIBO_GRAIN_V1("TCP_BEIBO_GRAIN_V1", "è´åç²®æ
åæºæå¡ç«¯åè®®"), |
| | | FZZY_OPENAPI_HTTP("FZZY_OPENAPI_HTTP", "飿£è´è¿API-HTTPåè®®"); |
| | | FZZY_OPENAPI_HTTP("FZZY_OPENAPI_HTTP", "飿£è´è¿API-HTTPåè®®"), |
| | | MODBUS_TCP("MODBUS_TCP", "Modbus-TCPåè®®"); |
| | | |
| | | |
| | | private String code; |
| | |
| | | @PropertyDef(label = "设å¤åç", description = "设å¤åçï¼æ²¡æå¯ä¸å¡«å:KW/H") |
| | | private Double power; |
| | | |
| | | @Column(name = "MODBUS_",length = 100) |
| | | @PropertyDef(label = "Modbusé
ç½®") |
| | | private String modbus; |
| | | |
| | | @Column(name = "REMARK_", length = 250) |
| | | @PropertyDef(label = "夿³¨", description = "夿³¨ä¿¡æ¯") |
| | | private String remark; |
| | |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <AutoFormElement layoutConstraint="colSpan:2"> |
| | | <Property name="name">modbus</Property> |
| | | <Property name="property">modbus</Property> |
| | | <Editor/> |
| | | </AutoFormElement> |
| | | <Label layoutConstraint="colSpan:2"> |
| | | <Property name="text">modbusè§åï¼å¼å°å-å
³å°å-åå°å-å¼å°ä½-å
³å°ä½-弿
é-å
³æ
éï¼æ å°åç¨N代æ¿</Property> |
| | | <Property name="style"> |
| | | <Property name="padding-left">100px</Property> |
| | | <Property name="color">blue</Property> |
| | | </Property> |
| | | </Label> |
| | | <AutoFormElement layoutConstraint="colSpan:2"> |
| | | <Property name="name">remark</Property> |
| | | <Property name="property">remark</Property> |
| | | <Property name="editorType">TextArea</Property> |
| | |
| | | } |
| | | |
| | | public void pageList(Page<DicArea> page, String key) throws Exception { |
| | | |
| | | String hql = " from " + DicArea.class.getName(); |
| | | |
| | | Map<String, Object> args = new HashMap<>(); |
| | | List<DicArea> list; |
| | | String countHql; |
| | | if (StringUtils.isNotEmpty(key)) { |
| | | hql += " where (name like :key1 or simple like:key2)"; |
| | |
| | | </Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="ggm"> |
| | | <Property/> |
| | | <Property></Property> |
| | | <Property name="label">å½è§ç </Property> |
| | | </PropertyDef> |
| | | </DataType> |
| | |
| | | <Property></Property> |
| | | <Property name="label">ä½ç½®</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="modbus"> |
| | | <Property/> |
| | | <Property name="label">Modbusé
ç½®</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="ext1"> |
| | | <Property/> |
| | | <Property name="label">æ©å±å段1</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="ext2"> |
| | | <Property/> |
| | | <Property name="label">æ©å±å段2</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="ext3"> |
| | | <Property/> |
| | | <Property name="label">æ©å±å段3</Property> |
| | | </PropertyDef> |
| | | <PropertyDef name="statusName"> |
| | | <Property/> |
| | | <Property name="label">å½åç¶æ</Property> |
| | | </PropertyDef> |
| | | </DataType> |
| | | <DataType name="dtDeviceSer"> |
| | | <Property name="creationType">com.ld.igds.models.DeviceSer</Property> |
| | |
| | | g.LOCATION_ AS location, |
| | | g.STATUS_ AS status, |
| | | g.POWER_ AS power, |
| | | g.MODBUS_ AS modbus, |
| | | g.LINK_ AS link, |
| | | g.EXT1_ AS ext1, |
| | | g.EXT2_ AS ext2, |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.modbus; |
| | | |
| | | import com.serotonin.modbus4j.ModbusFactory; |
| | | import com.serotonin.modbus4j.ModbusMaster; |
| | | import com.serotonin.modbus4j.exception.ModbusInitException; |
| | | import com.serotonin.modbus4j.ip.IpParameters; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.Import; |
| | | |
| | | import java.util.HashMap; |
| | | |
| | | @Configuration |
| | | @Import(com.serotonin.modbus4j.ModbusFactory.class) |
| | | public class ModbusConfig { |
| | | |
| | | |
| | | @Bean |
| | | public HashMap<String, ModbusMaster> contextModbusMap() { |
| | | return new HashMap<>(); |
| | | } |
| | | |
| | | @Autowired |
| | | private ModbusFactory modbusFactory; |
| | | |
| | | @Autowired |
| | | @Qualifier("contextModbusMap") |
| | | private HashMap<String, ModbusMaster> masterMap; |
| | | |
| | | /** |
| | | * @Description: éè¿ipè·å对åºçmodbusè¿æ¥å¨ |
| | | */ |
| | | public ModbusMaster getMaster(String ip, int port) { |
| | | ModbusMaster modbusMaster = masterMap.get(ip); |
| | | if (modbusMaster == null || !modbusMaster.isConnected()) { |
| | | setMaster(ip, port); |
| | | modbusMaster = masterMap.get(ip); |
| | | } |
| | | return modbusMaster; |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: 设置ip对åºçmodbusè¿æ¥å¨ |
| | | */ |
| | | private void setMaster(String ip, Integer port) { |
| | | ModbusMaster master; |
| | | IpParameters params = new IpParameters(); |
| | | params.setHost(ip); |
| | | params.setPort(port); |
| | | //设置为trueï¼ä¼å¯¼è´TimeoutException: request=com.serotonin.modbus4j.ip.encap.EncapMessageRequest@774dfba5", |
| | | //params.setEncapsulated(true); |
| | | master = modbusFactory.createTcpMaster(params, true);// TCP åè®® |
| | | try { |
| | | //设置è¶
æ¶æ¶é´ |
| | | master.setTimeout(3 * 1000); |
| | | //设置éè¿æ¬¡æ° |
| | | master.setRetries(3); |
| | | //åå§å |
| | | master.init(); |
| | | } catch (ModbusInitException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | masterMap.put(ip, master); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ld.igds.protocol.modbus; |
| | | |
| | | import com.serotonin.modbus4j.ModbusMaster; |
| | | import com.serotonin.modbus4j.exception.ModbusInitException; |
| | | import com.serotonin.modbus4j.exception.ModbusTransportException; |
| | | import com.serotonin.modbus4j.msg.ReadCoilsRequest; |
| | | import com.serotonin.modbus4j.msg.ReadCoilsResponse; |
| | | import com.serotonin.modbus4j.msg.ReadDiscreteInputsRequest; |
| | | import com.serotonin.modbus4j.msg.ReadDiscreteInputsResponse; |
| | | import com.serotonin.modbus4j.msg.ReadHoldingRegistersRequest; |
| | | import com.serotonin.modbus4j.msg.ReadHoldingRegistersResponse; |
| | | import com.serotonin.modbus4j.msg.ReadInputRegistersRequest; |
| | | import com.serotonin.modbus4j.msg.ReadInputRegistersResponse; |
| | | import com.serotonin.modbus4j.msg.WriteCoilRequest; |
| | | import com.serotonin.modbus4j.msg.WriteCoilResponse; |
| | | import com.serotonin.modbus4j.msg.WriteCoilsRequest; |
| | | import com.serotonin.modbus4j.msg.WriteCoilsResponse; |
| | | import com.serotonin.modbus4j.msg.WriteRegisterRequest; |
| | | import com.serotonin.modbus4j.msg.WriteRegisterResponse; |
| | | import com.serotonin.modbus4j.msg.WriteRegistersRequest; |
| | | import com.serotonin.modbus4j.msg.WriteRegistersResponse; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * @Desc: å·¥å
·ç±» |
| | | * @author: Andy |
| | | * @update-time: 2023/8/11 |
| | | */ |
| | | @Component |
| | | public class ModbusUtil { |
| | | |
| | | //仿ºé»è®¤å¼ |
| | | private Integer slaveId = 1; |
| | | |
| | | @Autowired |
| | | private ModbusConfig modbusConfig; |
| | | |
| | | |
| | | public boolean[] readCoilStatus(String ip, int port, int offset, int numberOfRegister) throws ModbusTransportException { |
| | | |
| | | ModbusMaster master = modbusConfig.getMaster(ip, port); |
| | | ReadCoilsRequest request = new ReadCoilsRequest(slaveId, offset, numberOfRegister); |
| | | ReadCoilsResponse response = (ReadCoilsResponse) master.send(request); |
| | | boolean[] booleans = response.getBooleanData(); |
| | | |
| | | return valueRegroup(numberOfRegister, booleans); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 读åå¤å´è®¾å¤è¾å
¥çå¼å
³éï¼ç¸å½äºåè½ç ï¼02H-读离æ£è¾å
¥ç¶æ |
| | | * @Param: [ip, offset, numberOfRegister] |
| | | * @return: boolean[] |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public boolean[] readInputStatus(String ip, int port, int offset, int numberOfRegister) throws ModbusTransportException { |
| | | |
| | | ModbusMaster master = modbusConfig.getMaster(ip, port); |
| | | ReadDiscreteInputsRequest request = new ReadDiscreteInputsRequest(slaveId, offset, numberOfRegister); |
| | | ReadDiscreteInputsResponse response = (ReadDiscreteInputsResponse) master.send(request); |
| | | boolean[] booleans = response.getBooleanData(); |
| | | |
| | | return valueRegroup(numberOfRegister, booleans); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 读åä¿æå¯å卿°æ®ï¼ç¸å½äºåè½ç ï¼03H-è¯»ä¿æå¯åå¨ |
| | | * @Param: [ip, offset, numberOfRegister] |
| | | * @return: short[] |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public short[] readHoldingRegister(String ip, int port, int offset, int numberOfRegister) throws ModbusTransportException { |
| | | |
| | | ModbusMaster master = modbusConfig.getMaster(ip, port); |
| | | ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, offset, numberOfRegister); |
| | | ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse) master.send(request); |
| | | return response.getShortData(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 读åå¤å´è®¾å¤è¾å
¥çæ°æ®ï¼ç¸å½äºåè½ç ï¼04H-读è¾å
¥å¯åå¨ |
| | | * @Param: [ip, offset, numberOfRegister] |
| | | * @return: short[] |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public short[] readInputRegisters(String ip, int port, int offset, int numberOfRegister) throws ModbusTransportException { |
| | | |
| | | ModbusMaster master = modbusConfig.getMaster(ip, port); |
| | | ReadInputRegistersRequest request = new ReadInputRegistersRequest(slaveId, offset, numberOfRegister); |
| | | ReadInputRegistersResponse response = (ReadInputRegistersResponse) master.send(request); |
| | | return response.getShortData(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: åå个ï¼çº¿åï¼å¼å
³éæ°æ®ï¼ç¸å½äºåè½ç ï¼05H-åå个线å |
| | | * @Param: [ip, writeOffset, writeValue] |
| | | * @return: boolean |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public boolean writeCoil(String ip, int port, int writeOffset, boolean writeValue) throws ModbusTransportException { |
| | | |
| | | ModbusMaster tcpMaster = modbusConfig.getMaster(ip, port); |
| | | WriteCoilRequest request = new WriteCoilRequest(slaveId, writeOffset, writeValue); |
| | | WriteCoilResponse response = (WriteCoilResponse) tcpMaster.send(request); |
| | | return !response.isException(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: åå¤ä¸ªå¼å
³éæ°æ®ï¼çº¿åï¼ï¼ç¸å½äºåè½ç ï¼0FH-åå¤ä¸ªçº¿å |
| | | * @Param: [ip, startOffset, data] |
| | | * @return: boolean |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public boolean writeCoils(String ip, int port, int startOffset, boolean[] data) throws ModbusTransportException { |
| | | |
| | | ModbusMaster tcpMaster = modbusConfig.getMaster(ip, port); |
| | | WriteCoilsRequest request = new WriteCoilsRequest(slaveId, startOffset, data); |
| | | WriteCoilsResponse response = (WriteCoilsResponse) tcpMaster.send(request); |
| | | return !response.isException(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: ååä¸ªä¿æå¯åå¨ï¼ç¸å½äºåè½ç ï¼06H-ååä¸ªä¿æå¯åå¨ |
| | | * @Param: [ip, writeOffset, writeValue] |
| | | * @return: boolean |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public boolean writeHoldingRegister(String ip, int port,int writeOffset, short writeValue) throws ModbusTransportException, ModbusInitException { |
| | | |
| | | ModbusMaster tcpMaster = modbusConfig.getMaster(ip, port); |
| | | WriteRegisterRequest request = new WriteRegisterRequest(slaveId, writeOffset, writeValue); |
| | | WriteRegisterResponse response = (WriteRegisterResponse) tcpMaster.send(request); |
| | | return !response.isException(); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: åå¤ä¸ªä¿æå¯åå¨ï¼ç¸å½äºåè½ç ï¼10H-åå¤ä¸ªä¿æå¯åå¨ |
| | | * @Param: [ip, startOffset, data] |
| | | * @return: boolean |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | public boolean writeHoldingRegisters(String ip,int port, int startOffset, short[] data) throws ModbusTransportException, ModbusInitException { |
| | | |
| | | ModbusMaster tcpMaster = modbusConfig.getMaster(ip, port); |
| | | WriteRegistersRequest request = new WriteRegistersRequest(slaveId, startOffset, data); |
| | | WriteRegistersResponse response = (WriteRegistersResponse) tcpMaster.send(request); |
| | | return !response.isException(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 转æ¢å·¥å
·ï¼å°Booleanè½¬æ¢æ0ï¼1 |
| | | * @Param: [numberOfBits, values] |
| | | * @return: boolean[] |
| | | * @throws: |
| | | * @Author: Ricardo.Liu |
| | | * @Date: 2020/5/8 |
| | | */ |
| | | private boolean[] valueRegroup(int numberOfBits, boolean[] values) { |
| | | boolean[] bs = new boolean[numberOfBits]; |
| | | int temp = 1; |
| | | for (boolean b : values) { |
| | | bs[temp - 1] = b; |
| | | temp++; |
| | | if (temp > numberOfBits) { |
| | | break; |
| | | } |
| | | } |
| | | return bs; |
| | | } |
| | | } |
| | |
| | | package com.ld.igds.protocol.modbus.command; |
| | | |
| | | import com.ld.igds.common.CoreDeviceService; |
| | | import com.ld.igds.constant.Constant; |
| | | import com.ld.igds.io.RemoteControlService; |
| | | import com.ld.igds.io.constant.OrderRespEnum; |
| | | import com.ld.igds.io.constant.ProtocolEnum; |
| | | import com.ld.igds.io.request.DeviceAutoControlRequest; |
| | | import com.ld.igds.io.request.DeviceControlRequest; |
| | | 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.models.Device; |
| | | import com.ld.igds.protocol.modbus.ModbusUtil; |
| | | import com.ld.igds.protocol.modbus.data.ModbusTcp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * å½ååè®®é对æ åModbus-TCPåè®® |
| | |
| | | |
| | | public static final String BEAN_ID = "modbus.remoteControlService"; |
| | | |
| | | @Resource |
| | | private CoreDeviceService deviceService; |
| | | @Resource |
| | | private ModbusUtil modbusUtil; |
| | | |
| | | @Override |
| | | public String getProtocol() { |
| | | return null; |
| | | return ProtocolEnum.MODBUS_TCP.getCode(); |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse deviceControl(DeviceControlRequest request) { |
| | | return null; |
| | | List<ExeDevice> deviceList = request.getDeviceList(); |
| | | |
| | | if (null == deviceList || deviceList.isEmpty()) { |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_ERROR.getCode(), "没æéè¦æ§è¡ç设å¤"); |
| | | } |
| | | |
| | | |
| | | try { |
| | | |
| | | |
| | | Device device; |
| | | ModbusTcp modbusTcp; |
| | | String[] addr; |
| | | String temp; |
| | | for (ExeDevice exeDevice : deviceList) { |
| | | device = deviceService.getCacheDeviceById(exeDevice.getCompanyId(), exeDevice.getId()); |
| | | |
| | | if (null == device) continue; |
| | | |
| | | if (null == device.getModbus()) continue; |
| | | |
| | | modbusTcp = new ModbusTcp(); |
| | | modbusTcp.setIp(request.getIp()); |
| | | modbusTcp.setPort(request.getPort()); |
| | | modbusTcp.setDeviceId(device.getId()); |
| | | modbusTcp.setSerId(request.getSerId()); |
| | | |
| | | //modbusè§åï¼å¼å°å-å
³å°å-åå°å-å¼å°ä½-å
³å°ä½-弿
é-å
³æ
éï¼æ å°åç¨Nä»£æ¿ |
| | | addr = device.getModbus().split("-"); |
| | | temp = addr[0]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpen(Integer.valueOf(temp)); |
| | | temp = addr[1]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrClose(Integer.valueOf(temp)); |
| | | temp = addr[2]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrStop(Integer.valueOf(temp)); |
| | | temp = addr[3]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpenEnd(Integer.valueOf(temp)); |
| | | temp = addr[4]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrCloseEnd(Integer.valueOf(temp)); |
| | | temp = addr[5]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrOpenError(Integer.valueOf(temp)); |
| | | temp = addr[6]; |
| | | if (!Constant.YN_N.equals(temp)) modbusTcp.setAddrCloseError(Integer.valueOf(temp)); |
| | | |
| | | |
| | | //æ ¹æ®è®¾å¤ç®æ ç¶æï¼è°ç¨MODBUSæ§è¡ |
| | | modbusUtil.writeCoil(modbusTcp.getIp(), modbusTcp.getPort(), modbusTcp.getAddrOpen(), true); |
| | | |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | |
| | | return new DeviceControlResponse(OrderRespEnum.ORDER_SUCCESS); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public DeviceControlResponse closeAll(DeviceControlRequest request) { |
| | |
| | | |
| | | @Override |
| | | public DeviceControlResponse airAutoControl(DeviceAutoControlRequest request) { |
| | | |
| | | |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public DeviceControlResponse n2AutoControl(DeviceAutoControlRequest request) { |
| | | |
| | | return null; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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 ModbusTcp { |
| | | |
| | | private String deviceId; |
| | | |
| | | private String serId; |
| | | |
| | | private String ip; |
| | | |
| | | private int port; |
| | | |
| | | private int addrOpen; |
| | | |
| | | private int addrClose; |
| | | |
| | | private int addrStop; |
| | | |
| | | private int addrOpenEnd; |
| | | |
| | | private int addrCloseEnd; |
| | | |
| | | private int addrOpenError; |
| | | |
| | | private int addrCloseError; |
| | | |
| | | } |