1
zhang
9 天以前 24f6766a113090fb97f9dd399586b61a364702ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.zy.acs.manager.core.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xingshuangs.iot.protocol.modbus.service.ModbusTcp;
import com.github.xingshuangs.iot.utils.HexUtil;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.manager.entity.FuncSta;
import com.zy.acs.manager.manager.enums.FuncStaType;
import com.zy.acs.manager.manager.enums.ProtocolType;
import com.zy.acs.manager.manager.service.FuncStaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
 
@Slf4j
@Service
public class ChargeService {
 
    @Autowired
    private FuncStaService funcStaService;
 
    private final Map<String, ModbusTcp> CHARGE_CACHE = new ConcurrentHashMap<>();
 
 
    @PostConstruct
    public void init() {
        List<FuncSta> list = funcStaService.list(new LambdaQueryWrapper<FuncSta>()
                .eq(FuncSta::getType, FuncStaType.CHARGE.toString()));
        for (FuncSta funcSta : list) {
            if (Cools.isEmpty(funcSta.getProtocol()) && funcSta.getProtocol().equalsIgnoreCase(ProtocolType.MODBUS.toString())) {
                if (!Cools.isEmpty(funcSta.getIp()) && !Cools.isEmpty(funcSta.getPort()))
                    add(funcSta);
            }
        }
    }
 
 
    public void add(FuncSta funcSta) {
        ModbusTcp modbusTcp = new ModbusTcp(funcSta.getIp(), funcSta.getPort());
        modbusTcp.setComCallback((tag, bytes) -> log.info("%s[%d] %s%n", tag, bytes.length, HexUtil.toHexString(bytes)));
        CHARGE_CACHE.put(funcSta.getUuid(), modbusTcp);
    }
 
    public void remove(String chargePointId) {
        CHARGE_CACHE.remove(chargePointId);
    }
 
    public ModbusTcp get(String chargePointId) {
        ModbusTcp modbusTcp = CHARGE_CACHE.get(chargePointId);
        if (modbusTcp != null) {
            return modbusTcp;
        }
        FuncSta funcSta = funcStaService.getOne(new LambdaQueryWrapper<FuncSta>().eq(FuncSta::getUuid, chargePointId));
        modbusTcp = new ModbusTcp(funcSta.getIp(), funcSta.getPort());
        modbusTcp.setComCallback((tag, bytes) -> log.info("%s[%d] %s%n", tag, bytes.length, HexUtil.toHexString(bytes)));
        return modbusTcp;
    }
 
 
}