|
@@ -1,9 +1,16 @@
|
|
|
package com.zksy.gas.utils;
|
|
package com.zksy.gas.utils;
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.UUID;
|
|
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.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.domain.GasMonitorData;
|
|
|
import com.zksy.gas.service.GasMonitorDataService;
|
|
import com.zksy.gas.service.GasMonitorDataService;
|
|
|
|
|
+import com.zksy.utils.DevicePhoneFetchUtil;
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandler;
|
|
@@ -11,13 +18,17 @@ import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
import io.netty.handler.timeout.ReadTimeoutException;
|
|
import io.netty.handler.timeout.ReadTimeoutException;
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
|
|
+import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
@ChannelHandler.Sharable
|
|
@ChannelHandler.Sharable
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -25,6 +36,13 @@ import java.util.Arrays;
|
|
|
public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
private static Logger logger = LoggerFactory.getLogger(MessageHandler.class);
|
|
private static Logger logger = LoggerFactory.getLogger(MessageHandler.class);
|
|
|
private final GasMonitorDataService service;
|
|
private final GasMonitorDataService service;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WarningThresholdService warningThresholdService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private SmsUtil smsUtil;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private DevicePhoneFetchUtil devicePhoneFetchUtil;
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
public MessageHandler(GasMonitorDataService firefightingPressureService) {
|
|
public MessageHandler(GasMonitorDataService firefightingPressureService) {
|
|
|
this.service = firefightingPressureService;
|
|
this.service = firefightingPressureService;
|
|
@@ -92,7 +110,9 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
com.zksy.gas.domain.GasMonitorData resultData = DataParser.parseMessage(msgBytes);
|
|
com.zksy.gas.domain.GasMonitorData resultData = DataParser.parseMessage(msgBytes);
|
|
|
resultData.setId(java.util.UUID.randomUUID().toString());
|
|
resultData.setId(java.util.UUID.randomUUID().toString());
|
|
|
service.save(resultData);
|
|
service.save(resultData);
|
|
|
|
|
+ checkIfSmsAlertNeeded(resultData);
|
|
|
logger.info("数据解析入库成功: {}", resultData);
|
|
logger.info("数据解析入库成功: {}", resultData);
|
|
|
|
|
+
|
|
|
} catch (InvalidMessageException e) {
|
|
} catch (InvalidMessageException e) {
|
|
|
logger.error("数据校验失败: {}", e.getMessage());
|
|
logger.error("数据校验失败: {}", e.getMessage());
|
|
|
ctx.writeAndFlush(Unpooled.copiedBuffer("数据校验失败".getBytes()));
|
|
ctx.writeAndFlush(Unpooled.copiedBuffer("数据校验失败".getBytes()));
|
|
@@ -153,4 +173,113 @@ public class MessageHandler extends ChannelInboundHandlerAdapter {
|
|
|
logger.error("发送数据失败: {}", e.getMessage());
|
|
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("发送信息完成");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|