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);
|
}
|
}
|
|
|
}
|