| | |
| | | |
| | | 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) { |
| | | 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 ModbusTCPMaster get(String chargePointId) { |
| | | ModbusTCPMaster modbusTcp = CHARGE_CACHE.get(chargePointId); |
| | | if (modbusTcp != null) { |
| | | // 获取或创建设备锁 |
| | | 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 ModbusTCPMaster(funcSta.getIp(), funcSta.getPort(),true); |
| | | CHARGE_CACHE.put(chargePointId, modbusTcp); |
| | | return modbusTcp; |
| | | |
| | | } |
| | | |
| | | |