Browse Source

增加路牌处理

yangxiaokun 3 years ago
parent
commit
0aa0e3bf16

+ 9 - 4
src/main/java/com/zy/bms/entity/lp/LpInfoState.java

@@ -1,5 +1,6 @@
 package com.zy.bms.entity.lp;
 
+import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
 import lombok.Data;
@@ -69,22 +70,26 @@ public class LpInfoState implements Serializable {
     /**
      * 经度
      */
-    private String lon;
+    @JSONField(format = "#.######")
+    private Double lon;
 
     /**
      * 纬度
      */
-    private String lat;
+    @JSONField(format = "#.######")
+    private Double lat;
 
     /**
      * 经度(Gcj)
      */
-    private String lonGcj;
+    @JSONField(format = "#.######")
+    private Double lonGcj;
 
     /**
      * 纬度(Gcj)
      */
-    private String latGcj;
+    @JSONField(format = "#.######")
+    private Double latGcj;
 
     /**
      * 格式化位置

+ 57 - 0
src/main/java/com/zy/bms/handler/LpHandlers.java

@@ -0,0 +1,57 @@
+package com.zy.bms.handler;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.zy.bms.common.Constant;
+import com.zy.bms.entity.lp.LpInfoState;
+import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.service.IPostMqttMsgService;
+import com.zy.bms.service.lp.ILpInfoStateService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+
+/**
+ * 路牌 设备消息处理
+ *
+ * @author yang xiao kun
+ * create on 2021/7/26
+ */
+@Slf4j
+@Component
+public class LpHandlers {
+
+    @Resource
+    private ILpInfoStateService lpInfoStateService;
+    @Resource
+    private IPostMqttMsgService postMqttMsgService;
+
+    /**
+     * 基本信息上传
+     */
+    public void deviceInfoHandler(String topic, MqttDTO mqttDTO) {
+        try {
+            String openNum = Constant.splitOpenNum(topic);
+            LpInfoState dbDevice = lpInfoStateService.getByOpenNum(openNum);
+            LpInfoState device = TransformEntity.tsf2LpInfoState(openNum, mqttDTO);
+            //若数据库中不存在设备信息直接插入
+            if (dbDevice == null) {
+                lpInfoStateService.save(device);
+                return;
+            }
+            BigDecimal wakeInt = BigDecimal.valueOf(mqttDTO.getDouble("sleept")).setScale(2, BigDecimal.ROUND_HALF_UP);
+            Integer thresh = mqttDTO.getInt("thresh");
+            BigDecimal dbWakeInt = dbDevice.getWakeInt().setScale(2, BigDecimal.ROUND_HALF_UP);
+            //如果远程设备的(唤醒时间,阈值)与数据库的数据不一致,则发送消息更新设备的设置数据与数据库保持一致
+            if ((dbWakeInt != null && !dbWakeInt.equals(wakeInt)) ||
+                    (dbDevice.getThresh() != null && !dbDevice.getThresh().equals(thresh))) {
+                postMqttMsgService.updateDeviceInfoLp(openNum, wakeInt, thresh);
+            }
+            //更新数据库设备状态信息
+            lpInfoStateService.update(device, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
+        } catch (Exception e) {
+            log.error("路牌上传基本信息异常", e);
+        }
+    }
+}

+ 3 - 121
src/main/java/com/zy/bms/handler/MqttCallbackHandler.java

@@ -2,29 +2,15 @@ package com.zy.bms.handler;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.zy.bms.common.Constant;
-import com.zy.bms.common.exception.ApiRuntimeException;
 import com.zy.bms.entity.*;
-import com.zy.bms.entity.ubi.UbiNtrip;
-import com.zy.bms.entity.ubi.UbiInfoRecord;
-import com.zy.bms.entity.ubi.UbiInfoState;
-import com.zy.bms.entity.ubi.UbiTcp;
 import com.zy.bms.pojo.dto.MqttDTO;
 import com.zy.bms.service.*;
-import com.zy.bms.service.lp.ILpInfoStateService;
-import com.zy.bms.service.ubi.IUbiInfoRecordService;
-import com.zy.bms.service.ubi.IUbiInfoStateService;
-import com.zy.bms.service.ubi.IUbiNtripService;
-import com.zy.bms.service.ubi.IUbiTcpService;
-import com.zy.bms.utils.BeanUtil;
 import com.zy.bms.utils.MD5Util;
 import com.zy.bms.utils.RabbitMQApi;
-import com.zy.bms.utils.TransformEntity;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
-import java.time.LocalDateTime;
-import java.util.Map;
 
 /**
  * MQTT 消息返回处理类
@@ -45,15 +31,7 @@ public class MqttCallbackHandler {
     @Resource
     private IDeviceBaseService deviceStaticService;
     @Resource
-    private IUbiInfoRecordService heartbeatRecordService;
-    @Resource
-    private IUbiInfoStateService deviceDynamicService;
-    @Resource
-    private IUbiNtripService ntripSetService;
-    @Resource
-    private IUbiTcpService tcpSetService;
-    @Resource
-    private ILpInfoStateService lpInfoStateService;
+    private UbiHandlers ubiHandlers;
 
     /**
      * 处理消息
@@ -83,12 +61,12 @@ public class MqttCallbackHandler {
             }
             // 心跳包
             case Constant.M.UBI_HEARTBEAT: {
-                heartbeatHandler(openNum, mqttDTO);
+                ubiHandlers.heartbeatHandler(openNum, mqttDTO);
                 break;
             }
             // 获取设备设置信息
             case Constant.M.UBI_SET_INFO: {
-                deviceSetHandler(openNum, mqttDTO);
+                ubiHandlers.deviceSetHandler(openNum, mqttDTO);
                 break;
             }
         }
@@ -121,100 +99,4 @@ public class MqttCallbackHandler {
         // 返回处理结果
         postMqttMsgService.handleRegister(device);
     }
-
-    /**
-     * 心跳协议
-     */
-    private void heartbeatHandler(String openNum, MqttDTO mqttDTO) {
-        try {
-            UbiInfoRecord entity = TransformEntity.tsf2UbiInfoRecord(openNum, mqttDTO);
-            //存储记录
-            heartbeatRecordService.save(entity);
-            //更新最新的记录
-            UbiInfoState ubiInfoState = BeanUtil.cast(entity, UbiInfoState.class);
-            //设置更新时间
-            ubiInfoState.setUpdateTime(LocalDateTime.now());
-            //设置在线状态
-            ubiInfoState.setStatus(1);
-            deviceDynamicService.saveOrUpdate(ubiInfoState,
-                    new QueryWrapper<UbiInfoState>().eq("open_num", ubiInfoState.getOpenNum()));
-        } catch (Exception e) {
-            log.error("解析设备心跳消息异常", e);
-            throw new ApiRuntimeException("处理设备上传位置定位信息出错");
-        }
-    }
-
-    /**
-     * 普适性-设备设置信息
-     */
-    @SuppressWarnings("unchecked")
-    private void deviceSetHandler(String openNum, MqttDTO mqttDTO) {
-        // ntrip数据链路设置
-        UbiNtrip client = TransformEntity.tsf2UbiNtrip((Map<String, String>) mqttDTO.getObject("ntripclient"));
-        UbiNtrip server = TransformEntity.tsf2UbiNtrip((Map<String, String>) mqttDTO.getObject("ntripserver"));
-        ntripSetService.saveOrUpdateNtripSet(client, openNum, IUbiNtripService.CLIENT);
-        ntripSetService.saveOrUpdateNtripSet(server, openNum, IUbiNtripService.SERVER);
-        // tcp数据链路设置
-        UbiTcp link1 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink1"));
-        UbiTcp link2 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink2"));
-        UbiTcp link3 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink3"));
-        tcpSetService.saveOrUpdateTcpSet(link1, openNum, IUbiTcpService.LINK_1);
-        tcpSetService.saveOrUpdateTcpSet(link2, openNum, IUbiTcpService.LINK_2);
-        tcpSetService.saveOrUpdateTcpSet(link3, openNum, IUbiTcpService.LINK_3);
-    }
-
-//    /**
-//     * 路牌-基本信息上传
-//     */
-//    private void LP_deviceInfoHandler(String topic, MqttDTO mqttDTO) {
-//        try {
-//            String openNum = Constant.splitOpenNum(topic);
-//            LpInfoState dbDevice = lpInfoStateService.getByOpenNum(openNum);
-//            if (dbDevice == null) {
-//                log.error("设备需要手动添加至数据库:" + openNum);
-//                return;
-//            }
-//            LpInfoState deviceLp = new LpInfoState();
-//            Double wakeInt = mqttDTO.getDouble("sleept");
-//            Integer thresh = mqttDTO.getInt("thresh");
-//            //若数据库中没有设置唤醒时间和阈值则直接同步设备数据
-//            if (dbDevice.getThresh() == null || dbDevice.getWakeInt() == null) {
-//                deviceLp.setWakeInt(BigDecimal.valueOf(wakeInt));
-//                deviceLp.setThresh(thresh);
-//            } else {
-//                //如果远程设备的(唤醒时间,阈值)与数据库的数据不一致,则发送消息更新设备的设置数据与数据库保持一致
-//                if (!dbDevice.getWakeInt().equals(wakeInt) || !dbDevice.getThresh().equals(thresh)) {
-//                    setService.updateDevice_LP(num, dbDevice.getWakeInt(), dbDevice.getThresh());
-//                }
-//            }
-//            deviceLp.setDataType(msg.getString("datetype"));
-//            deviceLp.setBattery(msg.getString("batterynum"));
-//            deviceLp.setS4g(msg.getString("s4g"));
-//            deviceLp.setX(msg.getString("x"));
-//            deviceLp.setY(msg.getString("y"));
-//            deviceLp.setZ(msg.getString("z"));
-//            deviceLp.setAnglex(msg.getString("anglex"));
-//            deviceLp.setAngley(msg.getString("angley"));
-//            deviceLp.setAnglez(msg.getString("anglez"));
-//            deviceLp.setTemp(msg.getString("temp"));
-//            deviceLp.setUpdateTime(LocalDateTime.now());
-//            //临时解决方案
-//            if (msg.getString("lon") != null) {
-//                deviceLp.setMode(msg.getInt("mode"));
-//                deviceLp.setLon(msg.getString("lon"));
-//                deviceLp.setLat(msg.getString("lat"));
-//                //转换后的坐标
-//                String[] gcj = CoordTransformUtil.wgs84toGcj02(deviceLp.getLon(), deviceLp.getLat());
-//                deviceLp.setLonGcj(gcj[0]);
-//                deviceLp.setLatGcj(gcj[1]);
-//                // 逆地理位置解析
-//                deviceLp.setSite(GaoDeApiUtil.regeo(deviceLp.getLonGcj(), deviceLp.getLatGcj()));
-//            }
-//            // 保存基本信息
-//            deviceLpService.updateByNum(deviceLp, num);
-//        } catch (Exception e) {
-//            log.error("路牌上传基本信息异常", e);
-//        }
-//    }
-
 }

+ 44 - 4
src/main/java/com/zy/bms/utils/TransformEntity.java → src/main/java/com/zy/bms/handler/TransformEntity.java

@@ -1,12 +1,16 @@
-package com.zy.bms.utils;
+package com.zy.bms.handler;
 
 import com.zy.bms.common.Constant;
+import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.entity.ubi.UbiInfoRecord;
 import com.zy.bms.entity.ubi.UbiNtrip;
 import com.zy.bms.entity.ubi.UbiTcp;
 import com.zy.bms.pojo.dto.MqttDTO;
 import com.zy.bms.utils.CoordTransformUtil;
+import com.zy.bms.utils.GaoDeApiUtil;
 
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
 import java.util.Map;
 
 /**
@@ -18,7 +22,7 @@ import java.util.Map;
 public class TransformEntity {
 
     /**
-     * 转换为 普适性 - 心跳包记录
+     * 普适性 - 心跳包记录
      */
     public static UbiInfoRecord tsf2UbiInfoRecord(String openNum, MqttDTO dto) {
         UbiInfoRecord result = new UbiInfoRecord();
@@ -39,7 +43,7 @@ public class TransformEntity {
     }
 
     /**
-     * 转换为 普适性 - 数据链路设置信息
+     * 普适性 - 数据链路设置信息
      */
     public static UbiNtrip tsf2UbiNtrip(Map<String, String> map) {
         UbiNtrip result = new UbiNtrip();
@@ -54,7 +58,7 @@ public class TransformEntity {
     }
 
     /**
-     * 转换为 普适性 - TCP链路设置
+     * 普适性 - TCP链路设置
      */
     public static UbiTcp tsf2UbiTcp(Map<String, String> map) {
         UbiTcp result = new UbiTcp();
@@ -65,4 +69,40 @@ public class TransformEntity {
         result.setStatus(map.get("linkF") != null && map.get("linkF").equals(Constant.STATUS.TRUE) ? 1 : 0);
         return result;
     }
+
+    /**
+     * 路牌 - 状态信息
+     */
+    public static LpInfoState tsf2LpInfoState(String openNum, MqttDTO dto) {
+        LpInfoState result = new LpInfoState();
+        result.setOpenNum(openNum);
+        result.setWakeInt(BigDecimal.valueOf(dto.getDouble("sleept")).setScale(2, BigDecimal.ROUND_HALF_UP));
+        result.setThresh(dto.getInt("thresh"));
+        result.setDataType(dto.getString("datetype"));
+        result.setBattery(dto.getString("batterynum"));
+        result.setS4g(dto.getString("s4g"));
+        result.setX(dto.getString("x"));
+        result.setY(dto.getString("y"));
+        result.setZ(dto.getString("z"));
+        result.setAnglex(dto.getString("anglex"));
+        result.setAngley(dto.getString("angley"));
+        result.setAnglez(dto.getString("anglez"));
+        result.setTemp(dto.getString("temp"));
+        result.setUpdateTime(LocalDateTime.now());
+        //经纬度
+        Double lon = dto.getDouble("lon");
+        Double lat = dto.getDouble("lat");
+        if (lon != null && lat != null) {
+            result.setMode(dto.getInt("mode"));
+            result.setLon(lon);
+            result.setLat(lat);
+            //转换后的坐标
+            double[] gcj = CoordTransformUtil.wgs84toGcj02(lon, lat);
+            result.setLonGcj(gcj[0]);
+            result.setLatGcj(gcj[1]);
+            // 逆地理位置解析
+            result.setSite(GaoDeApiUtil.regeo(lon.toString(), lat.toString()));
+        }
+        return result;
+    }
 }

+ 82 - 0
src/main/java/com/zy/bms/handler/UbiHandlers.java

@@ -0,0 +1,82 @@
+package com.zy.bms.handler;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.zy.bms.common.exception.ApiRuntimeException;
+import com.zy.bms.entity.ubi.UbiInfoRecord;
+import com.zy.bms.entity.ubi.UbiInfoState;
+import com.zy.bms.entity.ubi.UbiNtrip;
+import com.zy.bms.entity.ubi.UbiTcp;
+import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.service.ubi.IUbiInfoRecordService;
+import com.zy.bms.service.ubi.IUbiInfoStateService;
+import com.zy.bms.service.ubi.IUbiNtripService;
+import com.zy.bms.service.ubi.IUbiTcpService;
+import com.zy.bms.utils.BeanUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.util.Map;
+
+/**
+ * 普适性 设备消息处理
+ *
+ * @author yang xiao kun
+ * create on 2021/7/26
+ */
+@Slf4j
+@Component
+public class UbiHandlers {
+
+    @Resource
+    private IUbiInfoRecordService heartbeatRecordService;
+    @Resource
+    private IUbiInfoStateService deviceDynamicService;
+    @Resource
+    private IUbiNtripService ntripSetService;
+    @Resource
+    private IUbiTcpService tcpSetService;
+
+
+    /**
+     * 心跳协议
+     */
+    public void heartbeatHandler(String openNum, MqttDTO mqttDTO) {
+        try {
+            UbiInfoRecord entity = TransformEntity.tsf2UbiInfoRecord(openNum, mqttDTO);
+            //存储记录
+            heartbeatRecordService.save(entity);
+            //更新最新的记录
+            UbiInfoState ubiInfoState = BeanUtil.cast(entity, UbiInfoState.class);
+            //设置更新时间
+            ubiInfoState.setUpdateTime(LocalDateTime.now());
+            //设置在线状态
+            ubiInfoState.setStatus(1);
+            deviceDynamicService.saveOrUpdate(ubiInfoState,
+                    new QueryWrapper<UbiInfoState>().eq("open_num", ubiInfoState.getOpenNum()));
+        } catch (Exception e) {
+            log.error("解析设备心跳消息异常", e);
+            throw new ApiRuntimeException("处理设备上传位置定位信息出错");
+        }
+    }
+
+    /**
+     * 普适性-设备设置信息
+     */
+    @SuppressWarnings("unchecked")
+    public void deviceSetHandler(String openNum, MqttDTO mqttDTO) {
+        // ntrip数据链路设置
+        UbiNtrip client = TransformEntity.tsf2UbiNtrip((Map<String, String>) mqttDTO.getObject("ntripclient"));
+        UbiNtrip server = TransformEntity.tsf2UbiNtrip((Map<String, String>) mqttDTO.getObject("ntripserver"));
+        ntripSetService.saveOrUpdateNtripSet(client, openNum, IUbiNtripService.CLIENT);
+        ntripSetService.saveOrUpdateNtripSet(server, openNum, IUbiNtripService.SERVER);
+        // tcp数据链路设置
+        UbiTcp link1 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink1"));
+        UbiTcp link2 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink2"));
+        UbiTcp link3 = TransformEntity.tsf2UbiTcp((Map<String, String>) mqttDTO.getObject("tcplink3"));
+        tcpSetService.saveOrUpdateTcpSet(link1, openNum, IUbiTcpService.LINK_1);
+        tcpSetService.saveOrUpdateTcpSet(link2, openNum, IUbiTcpService.LINK_2);
+        tcpSetService.saveOrUpdateTcpSet(link3, openNum, IUbiTcpService.LINK_3);
+    }
+}

+ 11 - 0
src/main/java/com/zy/bms/service/IPostMqttMsgService.java

@@ -4,6 +4,8 @@ package com.zy.bms.service;
 import com.zy.bms.entity.DeviceBase;
 import com.zy.bms.pojo.io.set.SetIO;
 
+import java.math.BigDecimal;
+
 /**
  * 向设备发送协议消息 服务类
  *
@@ -33,4 +35,13 @@ public interface IPostMqttMsgService {
      * @param openNum 设备码
      */
     void getDeviceLocalSet(String openNum);
+
+    /**
+     * 更新路牌的设置信息
+     *
+     * @param openNum 设备码
+     * @param wakeInt 唤醒时间
+     * @param thresh  阈值
+     */
+    void updateDeviceInfoLp(String openNum, BigDecimal wakeInt, Integer thresh);
 }

+ 7 - 0
src/main/java/com/zy/bms/service/impl/PostMqttMsgServiceImpl.java

@@ -3,6 +3,7 @@ package com.zy.bms.service.impl;
 import com.zy.bms.common.Constant;
 import com.zy.bms.config.mqtt.MqttGateway;
 import com.zy.bms.entity.DeviceBase;
+import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.handler.WebSocketHandler;
 import com.zy.bms.pojo.dto.MqttDTO;
 import com.zy.bms.pojo.io.set.AlarmIO;
@@ -13,6 +14,7 @@ import com.zy.bms.service.IPostMqttMsgService;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
 
 /**
  * 向设备发送协议消息 服务类
@@ -57,6 +59,11 @@ public class PostMqttMsgServiceImpl implements IPostMqttMsgService {
         sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
     }
 
+    @Override
+    public void updateDeviceInfoLp(String openNum, BigDecimal wakeInt, Integer thresh) {
+
+    }
+
     /**
      * 发送Mqtt 消息
      *

+ 0 - 1
src/main/java/com/zy/bms/utils/CoordTransformUtil.java

@@ -22,7 +22,6 @@ public class CoordTransformUtil {
      * @param lat 纬度 dd.dddd
      */
     public static double[] wgs84toGcj02(double lng, double lat) {
-
         double dLat = transformLat(lng - 105.0, lat - 35.0);
         double dLng = transformLng(lng - 105.0, lat - 35.0);
         double radLat = lat / 180.0 * PI;