Browse Source

完善路牌

chenyi406 3 years ago
parent
commit
045c7a0f22

+ 7 - 0
src/main/java/com/zy/bms/common/Constant.java

@@ -8,6 +8,11 @@ package com.zy.bms.common;
  */
 public class Constant {
 
+    public static class DEVICE_TYPE {
+        public static final String LP = "lupai";
+        public static final String UBI = "gnss";
+    }
+
     public static class WX {
         /**
          * 微信小程序登录相关配置
@@ -56,6 +61,8 @@ public class Constant {
         public static final String LP_HEARTBEAT = "70";
         //路牌 - 同步更新路牌本地设置
         public static final String LP_SET_LOCAL_INFO = "71";
+        //路牌 - 设备心跳包 位置信息
+        public static final String LP_HEARTBEAT_LOCATION = "72";
 
     }
 

+ 4 - 4
src/main/java/com/zy/bms/controller/lp/LpWxController.java

@@ -1,6 +1,7 @@
 package com.zy.bms.controller.lp;
 
 import com.zy.bms.common.ServerResponse;
+import com.zy.bms.controller.BaseController;
 import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.service.lp.ILpInfoStateService;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -19,7 +20,7 @@ import javax.annotation.Resource;
  */
 @RestController
 @RequestMapping("/bms/api/lpapp")
-public class LpWxController {
+public class LpWxController extends BaseController {
     @Resource
     private ILpInfoStateService lpInfoStateService;
 
@@ -27,11 +28,10 @@ public class LpWxController {
      * 查询所有设备
      */
     @GetMapping("list.do")
-    public ServerResponse getListByUserId() {
-        return ServerResponse.success(lpInfoStateService.list());
+    public ServerResponse listByUserId() {
+        return ServerResponse.success(lpInfoStateService.listByUserId(userId()));
     }
 
-
     /**
      * 更新唤醒时间和阈值
      */

+ 12 - 0
src/main/java/com/zy/bms/entity/lp/LpInfoState.java

@@ -114,4 +114,16 @@ public class LpInfoState implements Serializable {
      */
     private LocalDateTime updateTime;
 
+    /**
+     * 更新位置标记
+     */
+    private Integer updateLocationFlag;
+
+    /**
+     * 获取在线状态
+     */
+    public Integer getStatus() {
+        if (updateTime == null || wakeInt == null) return 0;
+        return updateTime.compareTo(LocalDateTime.now().minusMinutes((int) (wakeInt.floatValue() * 60) + 3));
+    }
 }

+ 44 - 12
src/main/java/com/zy/bms/handler/LpHandlers.java

@@ -4,6 +4,7 @@ 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.pojo.dto.WakeAndThreshLpDTO;
 import com.zy.bms.service.IPostMqttMsgService;
 import com.zy.bms.service.lp.ILpInfoStateService;
 import lombok.extern.slf4j.Slf4j;
@@ -30,28 +31,59 @@ public class LpHandlers {
     /**
      * 基本信息上传
      */
-    public void deviceInfoHandler(String topic, MqttDTO mqttDTO) {
+    public void deviceInfoHandler(String openNum, MqttDTO mqttDTO) {
         try {
-            String openNum = Constant.splitOpenNum(topic);
             LpInfoState dbDevice = lpInfoStateService.getByOpenNum(openNum);
             LpInfoState device = TransformEntity.tsf2LpInfoState(openNum, mqttDTO);
-            //若数据库中不存在设备信息直接插入
-            if (dbDevice == null) {
+            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.setWakeAndThreshLp(openNum, wakeInt, thresh);
-            }
+            setWakeAndThreshLp(openNum, dbDevice, device);
             //更新数据库设备状态信息
             lpInfoStateService.update(device, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
         } catch (Exception e) {
             log.error("路牌上传基本信息异常", e);
         }
     }
+
+    /**
+     * 定位信息上传
+     */
+    public void deviceLocationHandler(String openNum, MqttDTO mqttDTO) {
+        try {
+            LpInfoState device = TransformEntity.tsf2LpInfoState(openNum, mqttDTO);
+            // 更新数据库设备状态信息
+            lpInfoStateService.update(device, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
+        } catch (Exception e) {
+            log.error("路牌上传位置信息异常", e);
+        }
+    }
+
+    /**
+     * 设置 唤醒时间和阈值
+     *
+     * @param openNum 设备码
+     * @param local   本地设置参数
+     * @param remote  远程设置参数
+     */
+    private void setWakeAndThreshLp(String openNum, LpInfoState local, LpInfoState remote) {
+        WakeAndThreshLpDTO dto = new WakeAndThreshLpDTO();
+        dto.setOpenNum(openNum);
+        //如果远程设备的(唤醒时间,阈值)与数据库的数据不一致,则发送消息更新设备的设置数据与数据库保持一致
+        if (!local.getWakeInt().equals(remote.getWakeInt()) || !local.getThresh().equals(remote.getThresh())) {
+            dto.setWakeInt(local.getWakeInt());
+            dto.setThresh(local.getThresh());
+            //以数据库为主,不需要更新这两个字段,故设置为null
+            remote.setWakeInt(null);
+            remote.setThresh(null);
+        }
+        //是否需要定位
+        dto.setGetLocation(local.getUpdateLocationFlag() == 1 ? Constant.STATUS.TRUE : Constant.STATUS.FALSE);
+        //是否需要更新数据
+        dto.setSetf((dto.getWakeInt() != null || dto.getThresh() != null) ? Constant.STATUS.TRUE : Constant.STATUS.FALSE);
+        postMqttMsgService.setWakeAndThreshLp(dto);
+        //不管有没有更新,都设置为不需要在更新位置信息了
+        remote.setUpdateLocationFlag(0);
+    }
 }

+ 8 - 0
src/main/java/com/zy/bms/handler/MqttCallbackHandler.java

@@ -66,6 +66,10 @@ public class MqttCallbackHandler {
                 lpHandlers.deviceInfoHandler(openNum, mqttDTO);
                 break;
             }
+            case Constant.M.LP_HEARTBEAT_LOCATION: {// 路牌-设备位置信息
+                lpHandlers.deviceLocationHandler(openNum, mqttDTO);
+                break;
+            }
         }
         // 3. 转发消息至网页
         webSocketHandler.massMessage(openNum, payload);
@@ -91,6 +95,10 @@ public class MqttCallbackHandler {
         }
         // mqtt服务器注册用户
         RabbitMQApi.register(device.getUsername(), device.getPassword());
+        //路牌设备需要注册_lp结尾
+        if (device.getType().equals(Constant.DEVICE_TYPE.LP)) {
+            RabbitMQApi.register(device.getUsername() + "_lp", "123456");
+        }
         // 返回处理结果
         postMqttMsgService.handleRegister(device);
     }

+ 2 - 2
src/main/java/com/zy/bms/handler/TransformEntity.java

@@ -76,9 +76,9 @@ public class TransformEntity {
     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.setWakeInt(dto.getDouble("sleept") == null ? null : BigDecimal.valueOf(dto.getDouble("sleept")).setScale(2, BigDecimal.ROUND_HALF_UP));
         result.setThresh(dto.getInt("thresh"));
-        result.setDataType(dto.getString("datetype"));
+        result.setDataType(dto.getString("datatype"));
         result.setBattery(dto.getString("batterynum"));
         result.setS4g(dto.getString("s4g"));
         result.setX(dto.getString("x"));

+ 9 - 0
src/main/java/com/zy/bms/mapper/lp/LpInfoStateMapper.java

@@ -10,6 +10,8 @@ import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 /**
  * 设备-路牌 Mapper 接口
  *
@@ -27,6 +29,13 @@ public interface LpInfoStateMapper extends BaseMapper<LpInfoState> {
      */
     IPage<LpDeviceListVo> listPage(Page<LpDeviceListVo> page, @Param("io") DeviceIO io);
 
+    /**
+     * 通过用户ID 查询设备集合
+     *
+     * @param userId 用户ID
+     */
+    List<LpInfoState> listByUserId(@Param("userId") Integer userId);
+
     /**
      * 更新唤醒时间和阈值
      *

+ 37 - 0
src/main/java/com/zy/bms/pojo/dto/WakeAndThreshLpDTO.java

@@ -0,0 +1,37 @@
+package com.zy.bms.pojo.dto;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+/**
+ * @author chen_yi
+ * Create on 2021/8/22
+ */
+@Data
+public class WakeAndThreshLpDTO {
+    /**
+     * 设备码
+     */
+    private String openNum;
+
+    /**
+     * 唤醒时间
+     */
+    private BigDecimal wakeInt;
+
+    /**
+     * 阈值
+     */
+    private Integer thresh;
+
+    /**
+     * 是否获取位置 true or false
+     */
+    private String getLocation;
+
+    /**
+     * 是否需要设置参数 true or false
+     */
+    private String setf;
+}

+ 0 - 33
src/main/java/com/zy/bms/schedule/DeviceStatusSchedule.java

@@ -1,33 +0,0 @@
-package com.zy.bms.schedule;
-
-import com.zy.bms.service.ubi.IUbiInfoStateService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.annotation.EnableAsync;
-import org.springframework.scheduling.annotation.Scheduled;
-
-/**
- * 定时更新设备在线状态
- *
- * @author yang xiao kun
- * create on 2021/5/14
- */
-@EnableAsync
-@Configuration
-//@EnableScheduling
-public class DeviceStatusSchedule {
-
-    @Autowired
-    private IUbiInfoStateService deviceDynamicService;
-
-    /**
-     * 更新频率 5分钟
-     * 定时更新设备在线状态
-     */
-    @Async
-    @Scheduled(cron = "0 0/5 * * * ?")
-    public void updateDeviceStatus() {
-        deviceDynamicService.updateStatus();
-    }
-}

+ 2 - 6
src/main/java/com/zy/bms/service/IPostMqttMsgService.java

@@ -1,7 +1,7 @@
 package com.zy.bms.service;
 
-
 import com.zy.bms.entity.DeviceBase;
+import com.zy.bms.pojo.dto.WakeAndThreshLpDTO;
 import com.zy.bms.pojo.io.set.SetIO;
 
 import java.math.BigDecimal;
@@ -32,12 +32,8 @@ public interface IPostMqttMsgService {
     /**
      * 路牌
      * 更新唤醒时间和阈值
-     *
-     * @param openNum 设备码
-     * @param wakeInt 唤醒时间
-     * @param thresh  阈值
      */
-    void setWakeAndThreshLp(String openNum, BigDecimal wakeInt, Integer thresh);
+    void setWakeAndThreshLp(WakeAndThreshLpDTO dto);
 
     /**
      * 普适性

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

@@ -5,6 +5,7 @@ import com.zy.bms.config.mqtt.MqttGateway;
 import com.zy.bms.entity.DeviceBase;
 import com.zy.bms.handler.WebSocketHandler;
 import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.pojo.dto.WakeAndThreshLpDTO;
 import com.zy.bms.pojo.io.set.*;
 import com.zy.bms.service.IPostMqttMsgService;
 import org.springframework.stereotype.Service;
@@ -56,10 +57,13 @@ public class PostMqttMsgServiceImpl implements IPostMqttMsgService {
     }
 
     @Override
-    public void setWakeAndThreshLp(String openNum, BigDecimal wakeInt, Integer thresh) {
+    public void setWakeAndThreshLp(WakeAndThreshLpDTO dto) {
         MqttDTO mqttDTO = new MqttDTO(Constant.M.LP_SET_LOCAL_INFO);
-        mqttDTO.put("setf", "true").put("wakeint", wakeInt).put("thresh", thresh);
-        sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT_LP + openNum, mqttDTO.json());
+        mqttDTO.put("setf", dto.getSetf())
+                .put("wakeint", dto.getWakeInt())
+                .put("thresh", dto.getThresh()).
+                put("getlocation", dto.getGetLocation());
+        sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT_LP + dto.getOpenNum(), mqttDTO.json());
     }
 
     @Override

+ 9 - 1
src/main/java/com/zy/bms/service/lp/ILpInfoStateService.java

@@ -6,6 +6,8 @@ import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.pojo.io.DeviceIO;
 import com.zy.bms.pojo.vo.LpDeviceListVo;
 
+import java.util.List;
+
 /**
  * 设备-路牌 服务类
  *
@@ -21,6 +23,13 @@ public interface ILpInfoStateService extends IService<LpInfoState> {
      */
     IPage<LpDeviceListVo> listPage(DeviceIO io);
 
+    /**
+     * 通过用户ID 查询用户设备
+     *
+     * @param userId 用户ID
+     */
+    List<LpInfoState> listByUserId(Integer userId);
+
     /**
      * 通过设备ID获取设备信息
      *
@@ -28,7 +37,6 @@ public interface ILpInfoStateService extends IService<LpInfoState> {
      */
     LpInfoState getByOpenNum(String openNum);
 
-
     /**
      * 更新唤醒时间和阈值
      *

+ 7 - 5
src/main/java/com/zy/bms/service/lp/impl/LpInfoStateServiceImpl.java

@@ -13,6 +13,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 /**
  * <p>
@@ -34,6 +35,11 @@ public class LpInfoStateServiceImpl extends ServiceImpl<LpInfoStateMapper, LpInf
         return baseMapper.listPage(page, io);
     }
 
+    @Override
+    public List<LpInfoState> listByUserId(Integer userId) {
+        return baseMapper.listByUserId(userId);
+    }
+
     @Override
     public LpInfoState getByOpenNum(String openNum) {
         return baseMapper.selectOne(new QueryWrapper<LpInfoState>().eq("open_num", openNum));
@@ -41,10 +47,6 @@ public class LpInfoStateServiceImpl extends ServiceImpl<LpInfoStateMapper, LpInf
 
     @Override
     public boolean updateWakeAndThresh(LpInfoState entity) {
-        if (baseMapper.updateWakeAndThresh(entity) > 0) {
-            postMqttMsgService.setWakeAndThreshLp(entity.getOpenNum(), entity.getWakeInt(), entity.getThresh());
-            return true;
-        }
-        return false;
+        return baseMapper.updateWakeAndThresh(entity) > 0;
     }
 }

+ 1 - 1
src/main/resources/application-prod.yml

@@ -1,5 +1,5 @@
 server:
-  port: 9001 # Key product method
+  port: 9002 # Key product method
 spring:
   redis:
     host: localhost

+ 1 - 1
src/main/resources/application.yml

@@ -1,3 +1,3 @@
 spring:
   profiles:
-    active: dev
+    active: prod

+ 1 - 1
src/main/resources/mapper/ItemMapper.xml

@@ -11,7 +11,7 @@
                 AND name LIKE CONCAT("%",#{io.name},"%")
             </if>
             <if test="io.status != null and io.status > -1">
-                AND (status = #{io.status}
+                AND (`status` = #{io.status}
             </if>
         </where>
         ORDER BY create_time DESC

+ 11 - 3
src/main/resources/mapper/LpInfoStateMapper.xml

@@ -25,11 +25,19 @@
         </if>
     </select>
 
+    <select id="listByUserId" resultType="com.zy.bms.entity.lp.LpInfoState">
+        SELECT t2.*
+        FROM device_base t1
+                 LEFT JOIN lp_info_state t2 ON t1.open_num = t2.open_num
+        WHERE t1.group_id IN (SELECT group_id FROM user_privilege WHERE user_id = #{userId})
+    </select>
+
     <update id="updateWakeAndThresh">
         UPDATE
-        lp_info_state
-        SET wake_int = #{entity.wakeInt},
-        thresh  = #{entity.thresh}
+            lp_info_state
+        SET wake_int             = #{entity.wakeInt},
+            thresh               = #{entity.thresh},
+            update_location_flag = #{entity.updateLocationFlag}
         WHERE open_num = #{entity.openNum}
     </update>
 </mapper>