Bladeren bron

实现消防设备发送短信预警功能

zlm 7 maanden geleden
bovenliggende
commit
4f9631a991

+ 16 - 0
firefighting-pressure-service/pom.xml

@@ -24,6 +24,22 @@
             <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.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter-test</artifactId>
+            <version>3.0.5</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 15 - 2
firefighting-pressure-service/src/main/java/com/zksy/pressure/PressureApplication.java

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

+ 17 - 0
firefighting-pressure-service/src/main/java/com/zksy/pressure/domain/Enum/FirefightingDeviceCodeEnum.java

@@ -0,0 +1,17 @@
+package com.zksy.pressure.domain.Enum;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 设备编码枚举(新增)
+ */
+@Getter
+@AllArgsConstructor
+public enum FirefightingDeviceCodeEnum {
+    //firefighting
+    FIREFIGHTING_DEVICE("1302100007", "消防设备"); // 对应数据库中的设备编码
+
+    private final String code;  // 设备编码
+    private final String name;  // 设备名称
+}

+ 18 - 0
firefighting-pressure-service/src/main/java/com/zksy/pressure/domain/Enum/FirefightingWarningCodeEnum.java

@@ -0,0 +1,18 @@
+package com.zksy.pressure.domain.Enum;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 预警编码枚举(替代原WarningTypeEnum)
+ */
+@Getter
+@AllArgsConstructor
+public enum FirefightingWarningCodeEnum {
+
+    PRESSURE("WARN-PRESSURE","消防压力过高预警",50.0);
+
+    private final String code;       // 预警编码(对应数据库)
+    private final String name;       // 预警名称
+    private final double defaultVal; // 默认阈值(数据库未配置时使用)
+}

+ 85 - 0
firefighting-pressure-service/src/main/java/com/zksy/pressure/utils/MessageHandler.java

@@ -1,9 +1,16 @@
 package com.zksy.pressure.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.pressure.domain.Enum.FirefightingDeviceCodeEnum;
+import com.zksy.pressure.domain.Enum.FirefightingWarningCodeEnum;
 import com.zksy.pressure.domain.FirefightingPressure;
 import com.zksy.pressure.service.FirefightingPressureService;
+import com.zksy.utils.DevicePhoneFetchUtil;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandler;
@@ -15,6 +22,11 @@ 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.List;
+import java.util.Map;
+
 @ChannelHandler.Sharable
 @Slf4j
 @Component
@@ -22,6 +34,12 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
 	private static Logger logger = LoggerFactory.getLogger(MessageHandler.class);
 	private final FirefightingPressureService firefightingPressureService;
 	@Autowired
+	private WarningThresholdService service;
+	@Autowired
+	private SmsUtil smsUtil;
+	@Autowired
+	private DevicePhoneFetchUtil devicePhoneFetchUtil;
+	@Autowired
 	public MessageHandler(FirefightingPressureService firefightingPressureService) {
 		this.firefightingPressureService = firefightingPressureService;
 	}
@@ -59,6 +77,8 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
 				//String addressCode = resultData.getAddressCode();
 				//DeviceOfflineCheckTask.deviceLastReceiveTimeMap.put(addressCode, new Date());
 				firefightingPressureService.save(resultData);
+
+				checkIfSmsAlertNeeded(resultData);
 			}
 		} catch (InvalidMessageException e) {
 			logger.error("数据入库失败: {}", e.getMessage());
@@ -115,4 +135,69 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
 			logger.error("发送数据失败: {}", e.getMessage());
 		}
 	}
+	//检查是否会触发短信预警
+	public void checkIfSmsAlertNeeded(FirefightingPressure resultData){
+		try {
+			//用遥测站地址作为设备的设备编号
+			String deviceId = resultData.getTelemeteringStation();
+			//1、获取到压力阈值
+			Double warningValue = null;
+			//1.1获取到设备编码
+			String deviceWarningCode = FirefightingDeviceCodeEnum.FIREFIGHTING_DEVICE.getCode();
+			FirefightingWarningCodeEnum targetWarningType=FirefightingWarningCodeEnum.PRESSURE;
+
+			String warningCode = targetWarningType.getCode();
+			String warningMsg = targetWarningType.getName();
+			try {
+				//1.2根据设备编码获取压力阈值
+				WarningThreshold threshold = service.getWarningThresholdByDeviceAndCode(deviceWarningCode, warningCode);
+				warningValue = threshold.getWarningValue();
+			} catch (Exception e) {
+				log.error("查询预警阈值失败");
+			}
+			//1.2获取到实际压力值
+			Double pressureValue = resultData.getPressureValue();
+			boolean isOverThreshold = pressureValue > warningValue;
+			//1.3超出了阈值则发送短信,设备编号这里不是很清楚
+			if(isOverThreshold){
+				sendMessage(deviceId,warningMsg,resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD14()==1){
+				sendMessage(deviceId,"压力变幅报警",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD13()==1){
+				sendMessage(deviceId,"压力下下限报警",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD12()==1){
+				sendMessage(deviceId,"压力上上限报警",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD11()==1){
+				sendMessage(deviceId,"压力下线报警",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD10()==1){
+				sendMessage(deviceId,"压力上限报警",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD9()==1){
+				sendMessage(deviceId,"传感器状态",resultData.getLongitude(),resultData.getLatitude());
+			}
+			if(resultData.getD1()==1){
+				sendMessage(deviceId,"倾斜报警1",resultData.getLongitude(),resultData.getLatitude());
+			}
+		} catch (Exception e) {
+			log.error("设备报警处理失败");
+		}
+
+	}
+	//构造发送短信参数&&发送短信
+	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
firefighting-pressure-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.pressure.domain, com.zksy.common.core.domain.entity,com.zksy.system.domain
+  mapper-locations: classpath*:mapper/**/*Mapper.xml