| | |
| | | package com.zy.acs.manager.core.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.github.xingshuangs.iot.protocol.modbus.service.ModbusRtuOverTcp; |
| | | import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp; |
| | | import com.github.xingshuangs.iot.utils.HexUtil; |
| | | import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.manager.entity.FuncSta; |
| | | import com.zy.acs.manager.manager.enums.FuncStaType; |
| | |
| | | @Autowired |
| | | private FuncStaService funcStaService; |
| | | |
| | | private final Map<String, ModbusRtuOverTcp> CHARGE_CACHE = new ConcurrentHashMap<>(); |
| | | private final Map<String, ModbusTCPMaster> CHARGE_CACHE = new ConcurrentHashMap<>(); |
| | | |
| | | private final Map<String, Object> deviceLocks = new ConcurrentHashMap<>(); |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | |
| | | |
| | | |
| | | public void add(FuncSta funcSta) { |
| | | ModbusRtuOverTcp modbusTcp = new ModbusRtuOverTcp(funcSta.getIp(), funcSta.getPort()); |
| | | modbusTcp.setComCallback((tag, bytes) -> log.info("%s[%d] %s%n", tag, bytes.length, HexUtil.toHexString(bytes))); |
| | | ModbusTCPMaster modbusTcp = new ModbusTCPMaster(funcSta.getIp(), funcSta.getPort(), true); |
| | | try { |
| | | modbusTcp.connect(); |
| | | } catch (Exception e) { |
| | | log.info("连接失败"); |
| | | } |
| | | CHARGE_CACHE.put(funcSta.getUuid(), modbusTcp); |
| | | } |
| | | |
| | | public void remove(String chargePointId) { |
| | | CHARGE_CACHE.remove(chargePointId); |
| | | ModbusTCPMaster master = CHARGE_CACHE.remove(chargePointId); |
| | | if (master != null && master.isConnected()) { |
| | | master.disconnect(); |
| | | } |
| | | } |
| | | |
| | | public ModbusRtuOverTcp get(String chargePointId) { |
| | | ModbusRtuOverTcp modbusTcp = CHARGE_CACHE.get(chargePointId); |
| | | if (modbusTcp != null) { |
| | | public ModbusTCPMaster get(String chargePointId) { |
| | | // 获取或创建设备锁 |
| | | Object lock = deviceLocks.computeIfAbsent(chargePointId, k -> new Object()); |
| | | synchronized (lock) { |
| | | ModbusTCPMaster modbusTcp = CHARGE_CACHE.get(chargePointId); |
| | | if (modbusTcp != null) { |
| | | return modbusTcp; |
| | | } |
| | | FuncSta funcSta = funcStaService.getOne(new LambdaQueryWrapper<FuncSta>().eq(FuncSta::getUuid, chargePointId)); |
| | | modbusTcp = new ModbusTCPMaster(funcSta.getIp(), funcSta.getPort(),true); |
| | | try { |
| | | modbusTcp.connect(); |
| | | } catch (Exception e) { |
| | | log.info("连接失败"); |
| | | return null; |
| | | } |
| | | CHARGE_CACHE.put(chargePointId, modbusTcp); |
| | | return modbusTcp; |
| | | } |
| | | FuncSta funcSta = funcStaService.getOne(new LambdaQueryWrapper<FuncSta>().eq(FuncSta::getUuid, chargePointId)); |
| | | modbusTcp = new ModbusRtuOverTcp(funcSta.getIp(), funcSta.getPort()); |
| | | modbusTcp.setComCallback((tag, bytes) -> log.info("%s[%d] %s%n", tag, bytes.length, HexUtil.toHexString(bytes))); |
| | | CHARGE_CACHE.put(chargePointId, modbusTcp); |
| | | return modbusTcp; |
| | | |
| | | } |
| | | |
| | | |