|
@@ -4,13 +4,10 @@ import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.jiayue.biz.domain.EnvironmentalData;
|
|
|
-import com.jiayue.biz.domain.PointAttribute;
|
|
|
import com.jiayue.biz.domain.TunnelInfo;
|
|
|
import com.jiayue.biz.domain.WeatherLook;
|
|
|
-import com.jiayue.biz.service.EnvironmentalDataService;
|
|
|
-import com.jiayue.biz.service.PointAttributeService;
|
|
|
-import com.jiayue.biz.service.TunnelInfoService;
|
|
|
-import com.jiayue.biz.service.WeatherLookService;
|
|
|
+import com.jiayue.biz.service.*;
|
|
|
+import com.jiayue.biz.vo.AttributeAndPointVo;
|
|
|
import com.jiayue.common.utils.DateUtil;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -23,7 +20,7 @@ import wei.yigulu.modbus.domain.datatype.IModbusDataType;
|
|
|
import wei.yigulu.modbus.domain.datatype.ModbusDataTypeEnum;
|
|
|
import wei.yigulu.modbus.domain.datatype.NumericModbusData;
|
|
|
import wei.yigulu.modbus.exceptiom.ModbusException;
|
|
|
-import wei.yigulu.modbus.netty.ModbusTcpMasterBuilder;
|
|
|
+import wei.yigulu.modbus.netty.HSModbusTcpMasterBuilder;
|
|
|
import wei.yigulu.modbus.utils.ModbusRequestDataUtils;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
@@ -42,111 +39,107 @@ public class Channel {
|
|
|
private final
|
|
|
TunnelInfoService tunnelInfoService;
|
|
|
|
|
|
- private final
|
|
|
- PointAttributeService pointAttributeService;
|
|
|
-
|
|
|
private final WeatherLookService weatherLookService;
|
|
|
|
|
|
private final EnvironmentalDataService environmentalDataService;
|
|
|
|
|
|
+ private final AttributeAndPointVoService attributeAndPointVoService;
|
|
|
+
|
|
|
|
|
|
private final ExecutorService calculatorThread = new ThreadPoolExecutor(15, 1000,
|
|
|
60L, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactoryBuilder().setNamePrefix("MyRecurringTaskThread-").build());
|
|
|
|
|
|
|
|
|
- private HashMap<String, ModbusTcpMasterBuilder> masterMap;
|
|
|
+ private HashMap<String, ModbusMaster> masterMap;
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
public void timingTunnel() {
|
|
|
List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
|
|
|
for (TunnelInfo tunnelInfo : tunnelInfoList) {
|
|
|
- if(StrUtil.isNotBlank(tunnelInfo.getStatus()) && tunnelInfo.getStatus().equals("1")){
|
|
|
+ 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");
|
|
|
- }
|
|
|
|
|
|
+ //定时重启异常通道
|
|
|
+ @Scheduled(fixedDelay = 60000)
|
|
|
+ public void restartTunnel() {
|
|
|
+ List<TunnelInfo> tunnelInfoList = tunnelInfoService.lambdaQuery().eq(TunnelInfo::getStatus, "0").list();
|
|
|
+ for (TunnelInfo tunnelInfo : tunnelInfoList) {
|
|
|
+ ModbusMaster master = start(tunnelInfo);
|
|
|
+ masterMap.put(tunnelInfo.getId(), master);
|
|
|
}
|
|
|
- tunnelInfoService.updateBatchById(tunnelInfoList);
|
|
|
}
|
|
|
|
|
|
public void startTunnel(TunnelInfo tunnelInfo) {
|
|
|
- ModbusTcpMasterBuilder master = start(tunnelInfo);
|
|
|
- masterMap.put(tunnelInfo.getWeatherLookNo(), master);
|
|
|
+ ModbusMaster master = start(tunnelInfo);
|
|
|
+ masterMap.put(tunnelInfo.getId(), master);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//创建线程
|
|
|
- public ModbusTcpMasterBuilder start(TunnelInfo tunnelInfo) {
|
|
|
+ public ModbusMaster start(TunnelInfo tunnelInfo) {
|
|
|
//创建通道后 对通道map 进行 采集数据
|
|
|
//获取场站id 形成map
|
|
|
- ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
|
|
|
- master.getConfigInfoMap().put("weatherLookNo", tunnelInfo.getWeatherLookNo());
|
|
|
+ ModbusMaster modbusMaster = new ModbusMaster(tunnelInfo.getIp(), tunnelInfo.getPort());
|
|
|
+// ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
|
|
|
+ modbusMaster.getConfigInfoMap().put("weatherLookNo", tunnelInfo.getId());
|
|
|
//create 会阻塞线程
|
|
|
- calculatorThread.execute(master::create);
|
|
|
- master.isConnected();
|
|
|
- return master;
|
|
|
+ calculatorThread.execute(modbusMaster::create);
|
|
|
+ return modbusMaster;
|
|
|
}
|
|
|
|
|
|
//关闭通道移除连接
|
|
|
public void stop(String weatherLookNo) {
|
|
|
- if (masterMap.get(weatherLookNo).isConnected()) {
|
|
|
- masterMap.get(weatherLookNo).stop();
|
|
|
- masterMap.remove(weatherLookNo);
|
|
|
- }
|
|
|
+ masterMap.get(weatherLookNo).stop();
|
|
|
+ masterMap.remove(weatherLookNo);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Scheduled(cron = "0 0/10 * * * ? ")
|
|
|
public void tunnel() {
|
|
|
List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
|
|
|
- List<PointAttribute> attributeList = pointAttributeService.list();
|
|
|
+ List<AttributeAndPointVo> attributeAndPointVoList = attributeAndPointVoService.selectEquipmentAttributeAndGatherDataPointById();
|
|
|
+ ArrayList<TunnelInfo> tunnelInfoForIdList = new ArrayList<>();
|
|
|
DateTime dateTime = DateUtil.beginOfMinute(new Date());
|
|
|
- for (Map.Entry<String, ModbusTcpMasterBuilder> masterBuilderEntry : masterMap.entrySet()) {
|
|
|
+ for (Map.Entry<String, ModbusMaster> masterBuilderEntry : masterMap.entrySet()) {
|
|
|
//获取通道map中的key
|
|
|
String weatherLookNo = masterBuilderEntry.getKey();
|
|
|
//根据通道key获取ip端口号等数据
|
|
|
- List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
|
|
|
+ List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getId().equals(weatherLookNo)).collect(Collectors.toList());
|
|
|
TunnelInfo tunnelInfo;
|
|
|
- if (!tunnelist.isEmpty()) {
|
|
|
+ if (masterBuilderEntry.getValue().isConnected()) {
|
|
|
tunnelInfo = tunnelist.get(0);
|
|
|
+ tunnelInfo.setStatus("1");
|
|
|
} else {
|
|
|
//如果此通道不存在 关闭通道
|
|
|
- this.stop(weatherLookNo);
|
|
|
+ tunnelist.get(0).setStatus("0");
|
|
|
+ tunnelInfoForIdList.add(tunnelist.get(0));
|
|
|
continue;
|
|
|
}
|
|
|
//根据通道key获取点表数据
|
|
|
- List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
|
|
|
+ List<AttributeAndPointVo> attributeAndPointVos = attributeAndPointVoList.stream().filter(g -> g.getTunnelId().equals(masterBuilderEntry.getKey())).collect(Collectors.toList());
|
|
|
+ if (attributeAndPointVos.size() == 0) {
|
|
|
+ log.info("{}此通道没有设置点位",tunnelInfo.getTunnelName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
//拿到所有点位
|
|
|
- List<Integer> points = pointAttributeList.stream().map(PointAttribute::getPoint).collect(Collectors.toList());
|
|
|
Map<Integer, ModbusDataTypeEnum> pointTypeMap = new HashMap<>();
|
|
|
- for (Integer point : points) {
|
|
|
- //点位以及点位类型存入map
|
|
|
- pointTypeMap.put(point, ModbusDataTypeEnum.valueOf(tunnelInfo.getDataFormat()));
|
|
|
- }
|
|
|
-
|
|
|
//获取连接
|
|
|
- ModbusTcpMasterBuilder master = masterBuilderEntry.getValue();
|
|
|
+ ModbusMaster master = masterBuilderEntry.getValue();
|
|
|
//获取点位信息map
|
|
|
- Map<String, Integer> stringStringBuilderMap = savePoints(pointAttributeList);
|
|
|
+ Map<String, Integer> stringStringBuilderMap = savePoints(attributeAndPointVos, pointTypeMap);
|
|
|
// 判断是否存在此塔
|
|
|
- List<WeatherLook> weatherLookList = weatherLookService.lambdaQuery().eq(WeatherLook::getWeatherLookNo, tunnelInfo.getWeatherLookNo()).list();
|
|
|
+ List<WeatherLook> weatherLookList = weatherLookService.lambdaQuery().eq(WeatherLook::getWeatherLookNo, attributeAndPointVos.get(0).getEquipmentId()).list();
|
|
|
if (weatherLookList.isEmpty()) {
|
|
|
- log.info("此环境监测仪不存在");
|
|
|
+ log.info("此{}环境监测仪不存在",attributeAndPointVos.get(0).getEquipmentId());
|
|
|
continue;
|
|
|
}
|
|
|
// 例如: key:wsAve val:数据
|
|
|
HashMap<String, BigDecimal> pointMap = new HashMap<>();
|
|
|
//分解成Obj4RequestCoil
|
|
|
- List<Obj4RequestRegister> obj4RequestRegisters = null;
|
|
|
+ List<Obj4RequestRegister> obj4RequestRegisters;
|
|
|
try {
|
|
|
obj4RequestRegisters = ModbusRequestDataUtils.splitModbusRequest(pointTypeMap, 1, FunctionCode.READ_HOLDING_REGISTERS);
|
|
|
for (Obj4RequestRegister obj4RequestRegister : obj4RequestRegisters) {
|
|
@@ -155,9 +148,9 @@ public class Channel {
|
|
|
for (Map.Entry<Integer, IModbusDataType> typeEntry : registerData.entrySet()) {
|
|
|
for (Map.Entry<String, Integer> entry : stringStringBuilderMap.entrySet()) {
|
|
|
if (typeEntry.getKey().equals(entry.getValue())) {
|
|
|
- log.info("点位:{}-----数值:{}", typeEntry.getKey(), multiplying(((NumericModbusData) typeEntry.getValue()).getValue(), pointAttributeList, typeEntry.getKey()));
|
|
|
+ log.info("点位:{}-----数值:{}", typeEntry.getKey(), multiplying(((NumericModbusData) typeEntry.getValue()).getValue(), attributeAndPointVos, typeEntry.getKey()));
|
|
|
//点位一致 置换value
|
|
|
- pointMap.put(entry.getKey(), multiplying(((NumericModbusData) typeEntry.getValue()).getValue(), pointAttributeList, typeEntry.getKey()));
|
|
|
+ pointMap.put(entry.getKey(), multiplying(((NumericModbusData) typeEntry.getValue()).getValue(), attributeAndPointVos, typeEntry.getKey()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -167,8 +160,8 @@ public class Channel {
|
|
|
} catch (ModbusException e) {
|
|
|
//出异常 停止 并且 设置状态
|
|
|
for (TunnelInfo info : tunnelist) {
|
|
|
- stop(info.getWeatherLookNo());
|
|
|
- info.setStatus("NO");
|
|
|
+ stop(info.getId());
|
|
|
+ info.setStatus("0");
|
|
|
}
|
|
|
tunnelInfoService.updateBatchById(tunnelist);
|
|
|
log.info(e.getMsg());
|
|
@@ -176,14 +169,22 @@ public class Channel {
|
|
|
}
|
|
|
environmentalDataService.insertOne(packageData(weatherLookList.get(0).getWeatherLookNo(), pointMap, dateTime));
|
|
|
}
|
|
|
+ if (tunnelInfoForIdList.size() > 0) {
|
|
|
+ for (TunnelInfo tunnelInfo : tunnelInfoForIdList) {
|
|
|
+ this.stop(tunnelInfo.getId());
|
|
|
+ log.info("{}通道异常,已关闭", tunnelInfo.getTunnelName());
|
|
|
+ }
|
|
|
+ tunnelInfoService.updateBatchById(tunnelInfoForIdList);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//倍率转换
|
|
|
- public BigDecimal multiplying(BigDecimal num, List<PointAttribute> pointAttributeList, Integer point) {
|
|
|
+ public BigDecimal multiplying(BigDecimal num, List<AttributeAndPointVo> attributeAndPointVos, Integer point) {
|
|
|
|
|
|
- List<PointAttribute> pointAttributes = pointAttributeList.stream().filter(p -> Objects.equals(p.getPoint(), point)).collect(Collectors.toList());
|
|
|
+ List<AttributeAndPointVo> pointAttributes = attributeAndPointVos.stream().filter(p -> Objects.equals(p.getPointNo(), point)).collect(Collectors.toList());
|
|
|
|
|
|
- return num.multiply(BigDecimal.valueOf(pointAttributes.get(0).getMultiplying()));
|
|
|
+ return num.multiply(BigDecimal.valueOf(pointAttributes.get(0).getMagnification()));
|
|
|
|
|
|
|
|
|
}
|
|
@@ -193,35 +194,44 @@ public class Channel {
|
|
|
EnvironmentalData environmentalData = new EnvironmentalData();
|
|
|
environmentalData.setTs(new Timestamp(dateTime.getTime()));
|
|
|
environmentalData.setWeatherLookNo(weatherNo);
|
|
|
- environmentalData.setWs(pointMap.get("ws").floatValue());
|
|
|
- environmentalData.setWd(pointMap.get("wd").floatValue());
|
|
|
- environmentalData.setT(pointMap.get("t").floatValue());
|
|
|
- environmentalData.setRh(pointMap.get("rh").floatValue());
|
|
|
- environmentalData.setPa(pointMap.get("pa").floatValue());
|
|
|
+ environmentalData.setWs(pointMap.get("ws") != null ? pointMap.get("ws").floatValue() : 0);
|
|
|
+ environmentalData.setWd(pointMap.get("wd") != null ? pointMap.get("wd").floatValue() : 0);
|
|
|
+ environmentalData.setT(pointMap.get("t") != null ? pointMap.get("t").floatValue() : 0);
|
|
|
+ environmentalData.setRh(pointMap.get("rh") != null ? pointMap.get("rh").floatValue() : 0);
|
|
|
+ environmentalData.setPa(pointMap.get("pa") != null ? pointMap.get("pa").floatValue() : 0);
|
|
|
return environmentalData;
|
|
|
}
|
|
|
|
|
|
|
|
|
//获取点表所对应数据
|
|
|
- public Map<String, Integer> savePoints(List<PointAttribute> channelPoints) {
|
|
|
- //如果数据库没有此测风塔 读取点表信息增加塔表
|
|
|
+ public Map<String, Integer> savePoints(List<AttributeAndPointVo> attributeAndPointVos, Map<Integer, ModbusDataTypeEnum> pointTypeMap) {
|
|
|
HashMap<String, Integer> dataMap = new HashMap<>();
|
|
|
- for (PointAttribute channelPoint : channelPoints) {
|
|
|
- if (channelPoint.getMeaning().contains("风速")) {
|
|
|
- dataMap.put("ws", channelPoint.getPoint());
|
|
|
- } else if (channelPoint.getMeaning().contains("风向")) {
|
|
|
- dataMap.put("wd", channelPoint.getPoint());
|
|
|
- } else if (channelPoint.getMeaning().contains("温度")) {
|
|
|
- dataMap.put("t", channelPoint.getPoint());
|
|
|
- } else if (channelPoint.getMeaning().contains("湿度")) {
|
|
|
- dataMap.put("rh", channelPoint.getPoint());
|
|
|
- } else if (channelPoint.getMeaning().contains("气压")) {
|
|
|
- dataMap.put("pa", channelPoint.getPoint());
|
|
|
- }
|
|
|
+ for (AttributeAndPointVo attributeAndPointVo : attributeAndPointVos) {
|
|
|
+ //点位以及点位类型存入map
|
|
|
+ pointTypeMap.put(attributeAndPointVo.getPointNo(), ModbusDataTypeEnum.valueOf(attributeAndPointVo.getPointType()));
|
|
|
+
|
|
|
+ dataMap.put(attributeAndPointVo.getFiledName(), attributeAndPointVo.getPointNo());
|
|
|
|
|
|
}
|
|
|
+
|
|
|
return dataMap;
|
|
|
}
|
|
|
|
|
|
+ static class ModbusMaster extends HSModbusTcpMasterBuilder {
|
|
|
|
|
|
+ public ModbusMaster(String ip, Integer port) {
|
|
|
+ super(ip, port);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connected() {
|
|
|
+ System.out.println("已连接");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void disconnected() {
|
|
|
+ System.out.println("连接异常");
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|