|
@@ -2,6 +2,7 @@ package com.jiayue.biz.conf;
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
|
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
import com.jiayue.biz.domain.EnvironmentalData;
|
|
import com.jiayue.biz.domain.EnvironmentalData;
|
|
import com.jiayue.biz.domain.PointAttribute;
|
|
import com.jiayue.biz.domain.PointAttribute;
|
|
import com.jiayue.biz.domain.TunnelInfo;
|
|
import com.jiayue.biz.domain.TunnelInfo;
|
|
@@ -55,23 +56,41 @@ public class Channel {
|
|
|
|
|
|
private HashMap<String, ModbusTcpMasterBuilder> masterMap;
|
|
private HashMap<String, ModbusTcpMasterBuilder> masterMap;
|
|
|
|
|
|
|
|
+
|
|
@Bean
|
|
@Bean
|
|
-// @Scheduled(fixedDelay = 60000 * 5)
|
|
|
|
public void timingTunnel() {
|
|
public void timingTunnel() {
|
|
List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
|
|
List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
|
|
for (TunnelInfo tunnelInfo : tunnelInfoList) {
|
|
for (TunnelInfo tunnelInfo : tunnelInfoList) {
|
|
- ModbusTcpMasterBuilder master = start(tunnelInfo);
|
|
|
|
- masterMap.put(tunnelInfo.getStationId(), master);
|
|
|
|
|
|
+ if(StrUtil.isNotBlank(tunnelInfo.getStatus()) && tunnelInfo.getStatus().equals("1")){
|
|
|
|
+ startTunnel(tunnelInfo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ @Scheduled(fixedDelay = 60000 * 5)
|
|
|
|
+ public void lastTimingTunnel() {
|
|
|
|
+ List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
|
|
|
|
+ for (TunnelInfo tunnelInfo : tunnelInfoList) {
|
|
|
|
+ if(StrUtil.isBlank(tunnelInfo.getStatus()) || tunnelInfo.getStatus().equals("0")){
|
|
|
|
+ startTunnel(tunnelInfo);
|
|
|
|
+ tunnelInfo.setStatus("1");
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+ tunnelInfoService.updateBatchById(tunnelInfoList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void startTunnel(TunnelInfo tunnelInfo) {
|
|
|
|
+ ModbusTcpMasterBuilder master = start(tunnelInfo);
|
|
|
|
+ masterMap.put(tunnelInfo.getWeatherLookNo(), master);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
//创建线程
|
|
//创建线程
|
|
public ModbusTcpMasterBuilder start(TunnelInfo tunnelInfo) {
|
|
public ModbusTcpMasterBuilder start(TunnelInfo tunnelInfo) {
|
|
//创建通道后 对通道map 进行 采集数据
|
|
//创建通道后 对通道map 进行 采集数据
|
|
//获取场站id 形成map
|
|
//获取场站id 形成map
|
|
ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
|
|
ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
|
|
- master.getConfigInfoMap().put("stationId", tunnelInfo.getStationId());
|
|
|
|
|
|
+ master.getConfigInfoMap().put("weatherLookNo", tunnelInfo.getWeatherLookNo());
|
|
//create 会阻塞线程
|
|
//create 会阻塞线程
|
|
calculatorThread.execute(master::create);
|
|
calculatorThread.execute(master::create);
|
|
master.isConnected();
|
|
master.isConnected();
|
|
@@ -79,10 +98,10 @@ public class Channel {
|
|
}
|
|
}
|
|
|
|
|
|
//关闭通道移除连接
|
|
//关闭通道移除连接
|
|
- public void stop(String stationId) {
|
|
|
|
- if (masterMap.get(stationId).isConnected()) {
|
|
|
|
- masterMap.get(stationId).stop();
|
|
|
|
- masterMap.remove(stationId);
|
|
|
|
|
|
+ public void stop(String weatherLookNo) {
|
|
|
|
+ if (masterMap.get(weatherLookNo).isConnected()) {
|
|
|
|
+ masterMap.get(weatherLookNo).stop();
|
|
|
|
+ masterMap.remove(weatherLookNo);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -93,19 +112,19 @@ public class Channel {
|
|
DateTime dateTime = DateUtil.beginOfMinute(new Date());
|
|
DateTime dateTime = DateUtil.beginOfMinute(new Date());
|
|
for (Map.Entry<String, ModbusTcpMasterBuilder> masterBuilderEntry : masterMap.entrySet()) {
|
|
for (Map.Entry<String, ModbusTcpMasterBuilder> masterBuilderEntry : masterMap.entrySet()) {
|
|
//获取通道map中的key
|
|
//获取通道map中的key
|
|
- String stationId = masterBuilderEntry.getKey();
|
|
|
|
|
|
+ String weatherLookNo = masterBuilderEntry.getKey();
|
|
//根据通道key获取ip端口号等数据
|
|
//根据通道key获取ip端口号等数据
|
|
- List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getStationId().equals(stationId)).collect(Collectors.toList());
|
|
|
|
|
|
+ List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
|
|
TunnelInfo tunnelInfo;
|
|
TunnelInfo tunnelInfo;
|
|
if (!tunnelist.isEmpty()) {
|
|
if (!tunnelist.isEmpty()) {
|
|
tunnelInfo = tunnelist.get(0);
|
|
tunnelInfo = tunnelist.get(0);
|
|
} else {
|
|
} else {
|
|
//如果此通道不存在 关闭通道
|
|
//如果此通道不存在 关闭通道
|
|
- this.stop(stationId);
|
|
|
|
|
|
+ this.stop(weatherLookNo);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
//根据通道key获取点表数据
|
|
//根据通道key获取点表数据
|
|
- List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getStationId().equals(stationId)).collect(Collectors.toList());
|
|
|
|
|
|
+ List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
|
|
//拿到所有点位
|
|
//拿到所有点位
|
|
List<Integer> points = pointAttributeList.stream().map(PointAttribute::getPoint).collect(Collectors.toList());
|
|
List<Integer> points = pointAttributeList.stream().map(PointAttribute::getPoint).collect(Collectors.toList());
|
|
Map<Integer, ModbusDataTypeEnum> pointTypeMap = new HashMap<>();
|
|
Map<Integer, ModbusDataTypeEnum> pointTypeMap = new HashMap<>();
|
|
@@ -146,10 +165,16 @@ public class Channel {
|
|
|
|
|
|
}
|
|
}
|
|
} catch (ModbusException e) {
|
|
} catch (ModbusException e) {
|
|
|
|
+ //出异常 停止 并且 设置状态
|
|
|
|
+ for (TunnelInfo info : tunnelist) {
|
|
|
|
+ stop(info.getWeatherLookNo());
|
|
|
|
+ info.setStatus("NO");
|
|
|
|
+ }
|
|
|
|
+ tunnelInfoService.updateBatchById(tunnelist);
|
|
log.info(e.getMsg());
|
|
log.info(e.getMsg());
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
- environmentalDataService.insertOne(packageData(weatherLookList.get(0).getWeatherLookNo(), pointMap,dateTime));
|
|
|
|
|
|
+ environmentalDataService.insertOne(packageData(weatherLookList.get(0).getWeatherLookNo(), pointMap, dateTime));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -164,7 +189,7 @@ public class Channel {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public EnvironmentalData packageData(String weatherNo, HashMap<String, BigDecimal> pointMap,DateTime dateTime) {
|
|
|
|
|
|
+ public EnvironmentalData packageData(String weatherNo, HashMap<String, BigDecimal> pointMap, DateTime dateTime) {
|
|
EnvironmentalData environmentalData = new EnvironmentalData();
|
|
EnvironmentalData environmentalData = new EnvironmentalData();
|
|
environmentalData.setTs(new Timestamp(dateTime.getTime()));
|
|
environmentalData.setTs(new Timestamp(dateTime.getTime()));
|
|
environmentalData.setWeatherLookNo(weatherNo);
|
|
environmentalData.setWeatherLookNo(weatherNo);
|