1
zhang
1 天以前 b78572fb09a2c63398e8d87bd19d5d3f92f5aa58
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.zy.acs.conveyor.core.service;
 
import com.zy.acs.common.utils.News;
import com.zy.acs.conveyor.core.model.StaProtocol;
import com.zy.acs.conveyor.core.properties.DevpSlave;
import com.zy.acs.conveyor.core.properties.SlaveProperties;
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 StationService {
 
    @Autowired
    private SlaveProperties slaveProperties;
 
 
    private Map<Integer, Map<Integer, StaProtocol>> STATION_CACHE = new ConcurrentHashMap<>();
 
 
    @PostConstruct
    public void init() {
        News.info("初始化站点缓存...................................................");
        List<DevpSlave> devpSlaves = slaveProperties.getDevp();
        for (DevpSlave devp : devpSlaves) {
            List<Integer> staNos = devp.getStaNos();
            for (Integer staNo : staNos) {
                STATION_CACHE.computeIfAbsent(devp.getId(), k -> new ConcurrentHashMap<>());
                Map<Integer, StaProtocol> stationMap = STATION_CACHE.get(devp.getId());
                StaProtocol staProtocol = new StaProtocol();
                staProtocol.setPlcId(devp.getId());
                staProtocol.setSiteId(staNo);
                staProtocol.setWorkNo(0);
                stationMap.put(staNo, staProtocol);
            }
        }
    }
 
 
    public void remove(Integer plcId) {
        STATION_CACHE.remove(plcId);
    }
 
    public void remove(Integer plcId, Integer staNo) {
        Map<Integer, StaProtocol> stationMap = STATION_CACHE.get(plcId);
        if (stationMap != null) {
            stationMap.remove(staNo);
        }
    }
 
    public StaProtocol get(Integer plcId, Integer staNo) {
        Map<Integer, StaProtocol> stationMap = STATION_CACHE.get(plcId);
        if (stationMap != null) {
            return stationMap.get(staNo);
        }
        return null;
    }
 
    public Map<Integer, StaProtocol> getStationMap(Integer plcId) {
        return STATION_CACHE.get(plcId);
    }
 
    public Map<Integer, Map<Integer, StaProtocol>> getAllStations(Integer devpId) {
        return STATION_CACHE;
    }
 
 
    public void updateStaProtocol(StaProtocol staProtocol) {
        Map<Integer, StaProtocol> stationMap = STATION_CACHE.get(staProtocol.getPlcId());
        if (stationMap != null) {
            stationMap.put(staProtocol.getSiteId(), staProtocol);
        }
    }
 
 
}