Kaynağa Gözat

实现可燃气体设备发送短信预警功能

zlm 7 ay önce
ebeveyn
işleme
ea5c69bd58

+ 10 - 0
flammable-gas-service/pom.xml

@@ -24,6 +24,16 @@
             <artifactId>zk-common</artifactId>
             <version>1.0.0</version>
         </dependency>
+        <dependency>
+            <groupId>com.zksy</groupId>
+            <artifactId>zk-api-service</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.zksy</groupId>
+            <artifactId>zksy-system</artifactId>
+            <version>3.9.0</version>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 15 - 2
flammable-gas-service/src/main/java/com/zksy/gas/GasApplication.java

@@ -5,8 +5,21 @@ import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-@MapperScan(basePackages = "com.zksy.gas.mapper")
-@SpringBootApplication(scanBasePackages = {"com.zksy.gas","com.zksy.api"})
+//@MapperScan(basePackages = "com.zksy.gas.mapper")
+//@SpringBootApplication(scanBasePackages = {"com.zksy.gas","com.zksy.api"})
+@MapperScan({
+        "com.zksy.gas.mapper",
+        "com.zksy.base.mapper",
+        "com.zksy.system.mapper"
+})
+@SpringBootApplication(scanBasePackages = {
+        "com.zksy.gas",
+        "com.zksy.api",
+        "com.zksy.base.service",
+        "com.zksy.system.service",
+        "com.zksy.common",
+        "com.zksy.utils"
+})
 public class GasApplication {
     public static void main(String[] args) {
         SpringApplication.run(GasApplication.class, args);

+ 17 - 0
flammable-gas-service/src/main/java/com/zksy/gas/domain/Enum/GasDeviceCodeEnum.java

@@ -0,0 +1,17 @@
+package com.zksy.gas.domain.Enum;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 设备编码枚举(新增)
+ */
+@Getter
+@AllArgsConstructor
+public enum GasDeviceCodeEnum {
+    //firefighting
+    FIREFIGHTING_DEVICE("xxxxxxx", "可燃气体监测仪"); // 对应数据库中的设备编码
+
+    private final String code;  // 设备编码
+    private final String name;  // 设备名称
+}

+ 20 - 0
flammable-gas-service/src/main/java/com/zksy/gas/domain/Enum/GasWarningCodeEnum.java

@@ -0,0 +1,20 @@
+package com.zksy.gas.domain.Enum;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 预警编码枚举(替代原WarningTypeEnum)
+ */
+@Getter
+@AllArgsConstructor
+public enum GasWarningCodeEnum {
+
+    TEMPERATRUE("WARN-GAS-TEMPERATRUE","可燃气体温度预警",50.0),
+    HUMIDITY("WARN-GAS-HUMIDITY","可燃气体湿度预警",50.0),
+    GASCONCENTRATION("WARN-GAS-CONCENTRATION","可燃气体浓度预警",50.0);
+
+    private final String code;       // 预警编码(对应数据库)
+    private final String name;       // 预警名称
+    private final double defaultVal; // 默认阈值(数据库未配置时使用)
+}

+ 129 - 0
flammable-gas-service/src/main/java/com/zksy/gas/utils/MessageHandler.java

@@ -1,9 +1,16 @@
 package com.zksy.gas.utils;
 
 import cn.hutool.core.lang.UUID;
+import com.alibaba.fastjson.JSONObject;
+import com.zksy.api.domain.WarningThreshold;
+import com.zksy.api.service.WarningThresholdService;
+import com.zksy.api.utils.SmsUtil;
 import com.zksy.common.exception.InvalidMessageException;
+import com.zksy.gas.domain.Enum.GasDeviceCodeEnum;
+import com.zksy.gas.domain.Enum.GasWarningCodeEnum;
 import com.zksy.gas.domain.GasMonitorData;
 import com.zksy.gas.service.GasMonitorDataService;
+import com.zksy.utils.DevicePhoneFetchUtil;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandler;
@@ -11,13 +18,17 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.handler.timeout.ReadTimeoutException;
 import io.netty.util.ReferenceCountUtil;
+import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.math.BigDecimal;
 import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
 
 @ChannelHandler.Sharable
 @Slf4j
@@ -25,6 +36,13 @@ import java.util.Arrays;
 public class MessageHandler extends ChannelInboundHandlerAdapter {
 	private static Logger logger = LoggerFactory.getLogger(MessageHandler.class);
 	private final GasMonitorDataService service;
+	@Autowired
+	private WarningThresholdService warningThresholdService;
+	@Autowired
+	private SmsUtil smsUtil;
+	@Autowired
+	private DevicePhoneFetchUtil devicePhoneFetchUtil;
+
 	@Autowired
 	public MessageHandler(GasMonitorDataService firefightingPressureService) {
 		this.service = firefightingPressureService;
@@ -92,7 +110,9 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
 			com.zksy.gas.domain.GasMonitorData resultData = DataParser.parseMessage(msgBytes);
 			resultData.setId(java.util.UUID.randomUUID().toString());
 			service.save(resultData);
+			checkIfSmsAlertNeeded(resultData);
 			logger.info("数据解析入库成功: {}", resultData);
+
 		} catch (InvalidMessageException e) {
 			logger.error("数据校验失败: {}", e.getMessage());
 			ctx.writeAndFlush(Unpooled.copiedBuffer("数据校验失败".getBytes()));
@@ -153,4 +173,113 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
 			logger.error("发送数据失败: {}", e.getMessage());
 		}
 	}
+	public void checkIfSmsAlertNeeded(GasMonitorData resultData){
+
+		//用MAC地址作为设备的设备编号
+		String deviceId = resultData.getMacAddress();
+
+		//获取温度阈值
+		String deviceWarningCode = GasDeviceCodeEnum.FIREFIGHTING_DEVICE.getCode();
+		GasWarningCodeEnum temTargetWarningType=GasWarningCodeEnum.TEMPERATRUE;
+		String temWarningCode = temTargetWarningType.getCode();
+		String temWarningMsg = temTargetWarningType.getName();
+		Double temWarningValue = checkTemAndHum(deviceWarningCode, temWarningCode);
+		//获取湿度阈值
+		GasWarningCodeEnum humTargetWarningType=GasWarningCodeEnum.HUMIDITY;
+		String humWarningCode = humTargetWarningType.getCode();
+		String humWarningMsg = humTargetWarningType.getName();
+		Double humWarningValue = checkTemAndHum(deviceWarningCode, humWarningCode);
+		//获取气体浓度阈值
+		GasWarningCodeEnum gasTargetWarningType=GasWarningCodeEnum.GASCONCENTRATION;
+		String gasWarningCode = gasTargetWarningType.getCode();
+		String gasWarningMsg = gasTargetWarningType.getName();
+		Double gasWarningValue = checkTemAndHum(deviceWarningCode, gasWarningCode);
+
+		//判断温度
+		float temperature = resultData.getTemperature();
+		boolean temperatureIsOverThreshold = temperature > temWarningValue;
+		if(temperatureIsOverThreshold){
+			sendMessage(deviceId,temWarningMsg,resultData.getLongitude(),resultData.getLatitude());
+		}
+		//判断湿度
+		float humidity = resultData.getHumidity();
+		boolean humidityIsOverThreshold=humidity>humWarningValue;
+		if(humidityIsOverThreshold){
+			sendMessage(deviceId,humWarningMsg,resultData.getLongitude(),resultData.getLatitude());
+		}
+		//判断气体浓度
+		BigDecimal gasConcentration = resultData.getGasConcentration();
+		float actualGasConcentration=gasConcentration.floatValue();
+		boolean concentrationIsOverThreshold=actualGasConcentration>gasWarningValue;
+		if(concentrationIsOverThreshold){
+			sendMessage(deviceId,gasWarningMsg,resultData.getLongitude(),resultData.getLatitude());
+		}
+		//获取到的数据逐一进行判断
+		Map<String, Boolean> alarmBits = resultData.parseAlarmBits();
+		if(alarmBits.get("unknownAlarm")){
+			sendMessage(deviceId,"未知报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("highAlarm")){
+			sendMessage(deviceId,"高报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("overRange")){
+			sendMessage(deviceId,"超量程",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("calibrationCycle")){
+			sendMessage(deviceId,"标定周期",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("overLife")){
+			sendMessage(deviceId,"超寿命",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("fallAlarm")){
+			sendMessage(deviceId,"跌倒报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("undervoltage")){
+			sendMessage(deviceId,"欠压报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("rangeAlarm")){
+			sendMessage(deviceId,"区间报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("keyAlarm")){
+			sendMessage(deviceId,"按键报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("vibrationAlarm")){
+			sendMessage(deviceId,"震动报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("waterLevelAlarm")){
+			sendMessage(deviceId,"水位报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("powerOffAlarm")){
+			sendMessage(deviceId,"断电报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("sensorFault")){
+			sendMessage(deviceId,"传感器故障",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("overHumidity")){
+			sendMessage(deviceId,"超湿报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("overTemperature")){
+			sendMessage(deviceId,"超温报警",resultData.getLongitude(),resultData.getLatitude());
+		}
+		if(alarmBits.get("systemFault")){
+			sendMessage(deviceId,"系统故障",resultData.getLongitude(),resultData.getLatitude());
+		}
+	}
+	public Double checkTemAndHum(String deviceWarningCode,String warningCode){
+		//获取数据库中的阈值
+		WarningThreshold threshold = warningThresholdService.getWarningThresholdByDeviceAndCode(deviceWarningCode, warningCode);
+		return threshold.getWarningValue();
+	}
+	//构造发送短信参数&&发送短信
+	public void sendMessage(String deviceNo, String alarmType, BigDecimal lng, BigDecimal lat){
+		// 构造短信参数
+		JSONObject params = new JSONObject();
+		params.put("deviceNo", deviceNo);
+		params.put("alarmType", alarmType);
+		// 经纬度处理(位置)
+		params.put("location", String.format("经纬度:%.6f,%.8f", lng, lat));
+		List<String> phoneListByDeviceId = devicePhoneFetchUtil.getPhoneListByDeviceId(deviceNo);
+		Map<String, Boolean> sendResults = smsUtil.sendBatchSms(phoneListByDeviceId, params.toJSONString());
+		log.info("发送信息完成");
+	}
 }

+ 4 - 1
flammable-gas-service/src/main/resources/application-dev.yaml

@@ -14,4 +14,7 @@ spring:
       discovery:
         server-addr: 192.168.110.30:8848
       config:
-        server-addr: 192.168.110.30:8848
+        server-addr: 192.168.110.30:8848
+mybatis-plus:
+  type-aliases-package: com.zksy.gas.domain, com.zksy.common.core.domain.entity,com.zksy.system.domain
+  mapper-locations: classpath*:mapper/**/*Mapper.xml