Bladeren bron

增加路牌接口

chenyi406 3 jaren geleden
bovenliggende
commit
568d334bc9

+ 0 - 2
src/main/java/com/zy/omp/OMPApplication.java

@@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
 public class OMPApplication {
-
     public static void main(String[] args) {
         SpringApplication.run(OMPApplication.class, args);
     }
-
 }

+ 9 - 7
src/main/java/com/zy/omp/config/mqtt/MqttCallbackHandler.java

@@ -12,6 +12,7 @@ import com.zy.omp.websocket.WebSocketServer;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 
 /**
@@ -49,7 +50,6 @@ public class MqttCallbackHandler {
      * @param payload 消息内容
      */
     void handle(String topic, String payload) {
-        System.out.println(topic + " -- " + payload);
         //存储日志-接收
         mqttLogService.saveLog(topic, payload, 0);
         //消息返回JSON转Object
@@ -182,8 +182,10 @@ public class MqttCallbackHandler {
             String num = topic.split("IMEI")[1];
             //如果远程设备的信息与数据库的信息不一致,则发送消息更新远程信息
             DeviceLp dbDevice = deviceLpService.getByNum(num);
-            if (!dbDevice.getWakeInt().equals(msg.getInt("wakeint"))
-                    || !dbDevice.getThresh().equals(msg.getDouble("thresh"))) {
+            System.out.println(dbDevice.getWakeInt());
+            Double dbWakeInt = dbDevice.getWakeInt() == null ? null : dbDevice.getWakeInt().setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
+            Double wakeInt = msg.getDouble("sleept") == null ? null : new BigDecimal(msg.getDouble("sleept")).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
+            if (dbWakeInt == null || !dbWakeInt.equals(wakeInt) || !dbDevice.getThresh().equals(msg.getInt("thresh"))) {
                 setService.updateDevice_LP(num, dbDevice.getWakeInt(), dbDevice.getThresh());
             }
             DeviceLp deviceLp = new DeviceLp();
@@ -196,15 +198,15 @@ public class MqttCallbackHandler {
             deviceLp.setAngley(msg.getString("angley"));
             deviceLp.setAnglez(msg.getString("anglez"));
             deviceLp.setTemp(msg.getString("temp"));
-            deviceLp.setLng(msg.getString("lng"));
+            deviceLp.setLon(msg.getString("lon"));
             deviceLp.setLat(msg.getString("lat"));
             deviceLp.setUpdateTime(LocalDateTime.now());
             //转换后的坐标
-            String[] gcj = CoordTransformUtil.wgs84toGcj02(deviceLp.getLng(), deviceLp.getLat());
-            deviceLp.setLngGcj(gcj[0]);
+            String[] gcj = CoordTransformUtil.wgs84toGcj02(deviceLp.getLon(), deviceLp.getLat());
+            deviceLp.setLonGcj(gcj[0]);
             deviceLp.setLatGcj(gcj[1]);
             // 逆地理位置解析
-            deviceLp.setSite(GaoDeApiUtil.regeo(deviceLp.getLngGcj(), deviceLp.getLatGcj()));
+            deviceLp.setSite(GaoDeApiUtil.regeo(deviceLp.getLonGcj(), deviceLp.getLatGcj()));
             // 保存位置信息
             deviceLpService.updateByNum(deviceLp, num);
         } catch (Exception e) {

+ 26 - 4
src/main/java/com/zy/omp/controller/wx/DeviceLpController.java

@@ -1,19 +1,41 @@
 package com.zy.omp.controller.wx;
 
-
+import com.zy.omp.common.ServerResponse;
+import com.zy.omp.model.DeviceLp;
+import com.zy.omp.service.DeviceLpService;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.annotation.Resource;
+
 /**
- * <p>
  * 设备-路牌 前端控制器
- * </p>
  *
  * @author chenyi
  * @since 2021-06-12
  */
 @RestController
-@RequestMapping("/builder/device-lp")
+@RequestMapping("/omp/api/wx/device/lp")
 public class DeviceLpController {
+    @Resource
+    private DeviceLpService deviceLpService;
+
+    /**
+     * 查询所有设备
+     */
+    @GetMapping("list.do")
+    public ServerResponse getListByUserId() {
+        return ServerResponse.createBySuccess(deviceLpService.list());
+    }
 
+    /**
+     * 更新设备设置
+     */
+    @PostMapping("update.do")
+    public ServerResponse update(DeviceLp deviceLp) {
+        deviceLpService.updateParam(deviceLp);
+        return ServerResponse.createBySuccess();
+    }
 }

+ 5 - 1
src/main/java/com/zy/omp/mapper/DeviceLpMapper.java

@@ -3,6 +3,7 @@ package com.zy.omp.mapper;
 import com.zy.omp.model.DeviceLp;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
 
 /**
@@ -16,5 +17,8 @@ import org.springframework.stereotype.Repository;
 @Mapper
 @Repository
 public interface DeviceLpMapper extends BaseMapper<DeviceLp> {
-
+    /**
+     * 更新可设置参数
+     */
+    void updateParam(@Param("entity") DeviceLp deviceLp);
 }

+ 5 - 9
src/main/java/com/zy/omp/model/DeviceLp.java

@@ -1,6 +1,7 @@
 package com.zy.omp.model;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 
 import com.baomidou.mybatisplus.annotation.TableField;
@@ -41,11 +42,6 @@ public class DeviceLp implements Serializable {
      */
     private String password;
 
-    /**
-     * 状态
-     */
-    private Integer status;
-
     /**
      * 设备组
      */
@@ -60,12 +56,12 @@ public class DeviceLp implements Serializable {
     /**
      * 定时唤醒时间
      */
-    private Integer wakeInt;
+    private BigDecimal wakeInt;
 
     /**
      * 阈值
      */
-    private Double thresh;
+    private Integer thresh;
 
     /**
      * 电量
@@ -97,7 +93,7 @@ public class DeviceLp implements Serializable {
     /**
      * 经度
      */
-    private String lng;
+    private String lon;
 
     /**
      * 纬度
@@ -107,7 +103,7 @@ public class DeviceLp implements Serializable {
     /**
      * 经度(gcj)
      */
-    private String lngGcj;
+    private String lonGcj;
 
     /**
      * 纬度(gcj)

+ 12 - 0
src/main/java/com/zy/omp/service/DeviceLpService.java

@@ -7,6 +7,8 @@ import com.zy.omp.mapper.DeviceLpMapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+
 /**
  * <p>
  * 设备-路牌 服务实现类
@@ -17,6 +19,8 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class DeviceLpService extends ServiceImpl<DeviceLpMapper, DeviceLp> {
+    @Resource
+    private SetService setService;
 
     /**
      * 通过设备 num 获取设备信息
@@ -35,4 +39,12 @@ public class DeviceLpService extends ServiceImpl<DeviceLpMapper, DeviceLp> {
         UpdateWrapper<DeviceLp> updateWrapper = new UpdateWrapper<DeviceLp>().eq("num", num);
         baseMapper.update(entity, updateWrapper);
     }
+
+    /**
+     * 更新可设置参数
+     */
+    public void updateParam(DeviceLp entity) {
+        setService.updateDevice_LP(entity.getNum(), entity.getWakeInt(), entity.getThresh());
+        baseMapper.updateParam(entity);
+    }
 }

+ 4 - 3
src/main/java/com/zy/omp/service/SetService.java

@@ -13,6 +13,7 @@ import com.zy.omp.websocket.WebSocketServer;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -146,7 +147,7 @@ public class SetService {
         Map<String, Object> data = new HashMap<>();
         data.put("regnum", device.getNum());
         data.put("sn", "SN" + device.getNum());
-        data.put("password", "123456");
+        data.put("password", device.getPassword());
         data.put("group", device.getGroup());
         msgDto.setData(data);
         mqttGateway.sendMsgToMqtt(msgDto.toJson(), Constant.TOPIC_REGISTER_CLIENT_LP);
@@ -158,13 +159,13 @@ public class SetService {
      * 服务器返回授权码
      * 路牌
      */
-    public void updateDevice_LP(String num, Integer wakeInt, Double thresh) {
+    public void updateDevice_LP(String num, BigDecimal wakeInt, Integer thresh) {
         //发送模板消息给设备
         MqttMsgDto msgDto = new MqttMsgDto();
         msgDto.setM(Constant.M_CODE_UPDATE_DEVICE_INFO_LP);
         Map<String, Object> data = new HashMap<>();
         data.put("setf", "true");
-        data.put("wakeint", wakeInt);
+        data.put("wakeint", wakeInt.doubleValue());
         data.put("thresh", thresh);
         msgDto.setData(data);
         mqttGateway.sendMsgToMqtt(msgDto.toJson(), Constant.TOPIC_DEVICE_CLIENT_LP + num);

+ 17 - 15
src/main/java/com/zy/omp/utils/CoordTransformUtil.java

@@ -16,7 +16,7 @@ public class CoordTransformUtil {
     private static final double PI = 3.1415926535897932384626;
     private static final double a = 6378245.0;
     private static final double ee = 0.00669342162296594323;
-    private static final DecimalFormat decimalFormat = new DecimalFormat("#.000000");//保留七位
+    private static final DecimalFormat decimalFormat = new DecimalFormat("#.######");//保留七位
 
     /**
      * WGS84转GCj02
@@ -25,19 +25,9 @@ public class CoordTransformUtil {
      * @param latStr 纬度 dd.dddd
      */
     public static String[] wgs84toGcj02(String lngStr, String latStr) {
-        double lat = Double.parseDouble(latStr);
-        double lng = Double.parseDouble(lngStr);
-
-        double dLat = transformLat(lng - 105.0, lat - 35.0);
-        double dLng = transformLng(lng - 105.0, lat - 35.0);
-        double radLat = lat / 180.0 * PI;
-        double magic = Math.sin(radLat);
-        magic = 1 - ee * magic * magic;
-        double sqrtMagic = Math.sqrt(magic);
-        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
-        dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
-        double mgLat = lat + dLat;
-        double mgLng = lng + dLng;
+        double[] mgLatAndLag = common(lngStr, latStr);
+        double mgLat = mgLatAndLag[0];
+        double mgLng = mgLatAndLag[1];
         return new String[]{decimalFormat.format(mgLng), decimalFormat.format(mgLat)};
     }
 
@@ -48,6 +38,18 @@ public class CoordTransformUtil {
      * @param latStr 纬度 dd.dddd
      */
     public static String[] gcj02toWgs84(String lngStr, String latStr) {
+        double lat = Double.parseDouble(latStr);
+        double lng = Double.parseDouble(lngStr);
+        double[] mgLatAndLag = common(lngStr, latStr);
+        double mgLat = mgLatAndLag[0];
+        double mgLng = mgLatAndLag[1];
+        return new String[]{decimalFormat.format(lng * 2 - mgLng), decimalFormat.format(lat * 2 - mgLat)};
+    }
+
+    /**
+     * wgs84toGcj02 和 gcj02toWgs84 公共计算模块
+     */
+    private static double[] common(String lngStr, String latStr) {
         double lat = Double.parseDouble(latStr);
         double lng = Double.parseDouble(lngStr);
         double dLat = transformLat(lng - 105.0, lat - 35.0);
@@ -60,7 +62,7 @@ public class CoordTransformUtil {
         dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
         double mgLat = lat + dLat;
         double mgLng = lng + dLng;
-        return new String[]{decimalFormat.format(lng * 2 - mgLng), decimalFormat.format(lat * 2 - mgLat)};
+        return new double[]{mgLat, mgLng};
     }
 
     /**

+ 7 - 1
src/main/resources/mapper/DeviceLpMapper.xml

@@ -1,5 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zy.omp.mapper.DeviceLpMapper">
-
+    <update id="updateParam">
+        UPDATE
+            device_lp
+        SET wakeInt = #{entity.wakeInt},
+            thresh  = #{entity.thresh}
+        WHERE num = #{entity.num}
+    </update>
 </mapper>