Browse Source

优化代码结构

yangxiaokun 3 years ago
parent
commit
91f55b8e4b

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

@@ -87,11 +87,11 @@ public class Constant {
         //设备注册时server -> device主题,仅设备注册使用
         public static final String REGISTER_CLIENT = "$regdrx2";
         // device -> server 通讯主题
-        public static final String DEVICE_SERVER = "$dtx2/IMEI";
+        public static final String DEVICE_SERVER = "$dtx2/#";
         // server -> device 通讯主题
         public static final String DEVICE_CLIENT = "$drx2/IMEI";
         // device -> server 通讯主题
-        public static final String DEVICE_SERVER_LP = "$dtxlp/";
+        public static final String DEVICE_SERVER_LP = "$dtxlp/#";
         // server -> device 通讯主题
         public static final String DEVICE_CLIENT_LP = "$drxlp/IMEI";
     }

+ 1 - 1
src/main/java/com/zy/bms/config/interceptor/WxAppAuthInterceptor.java

@@ -7,7 +7,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 /**
- * 普适型微信小程序
+ * 微信小程序
  * 登录权限验证拦截器
  *
  * @author chenyi

+ 2 - 1
src/main/java/com/zy/bms/config/mqtt/MqttConsumerCfg.java

@@ -1,5 +1,6 @@
 package com.zy.bms.config.mqtt;
 
+import com.zy.bms.common.Constant;
 import com.zy.bms.handler.MqttCallbackHandler;
 import com.zy.bms.utils.RandomCode;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -32,7 +33,7 @@ public class MqttConsumerCfg {
     private MqttPahoClientFactory mqttClientFactory;
 
     //默认监听主题
-    private final String[] defaultTopic = new String[]{"$regdtx2", "$dtx2/#", "$dtxlp/#"};
+    private final String[] defaultTopic = new String[]{Constant.TOPIC.REGISTER_SERVER, Constant.TOPIC.DEVICE_SERVER, Constant.TOPIC.DEVICE_SERVER_LP};
 
     /**
      * MQTT 消息订阅绑定(消费者)

+ 2 - 2
src/main/java/com/zy/bms/config/mqtt/MqttGateway.java

@@ -3,12 +3,12 @@ package com.zy.bms.config.mqtt;
 import org.springframework.integration.annotation.MessagingGateway;
 import org.springframework.integration.mqtt.support.MqttHeaders;
 import org.springframework.messaging.handler.annotation.Header;
-import org.springframework.stereotype.Service;
+import org.springframework.stereotype.Component;
 
 /**
  * 消息推送接口
  */
-@Service
+@Component
 @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
 public interface MqttGateway {
     /**

+ 4 - 3
src/main/java/com/zy/bms/controller/UbiLpWxController.java

@@ -8,6 +8,7 @@ import com.zy.bms.entity.User;
 import com.zy.bms.entity.UserPrivilege;
 import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.pojo.io.LpInfoRecordIO;
+import com.zy.bms.sender.UbiSender;
 import com.zy.bms.service.*;
 import com.zy.bms.service.lp.ILpInfoRecordService;
 import com.zy.bms.service.lp.ILpInfoStateService;
@@ -40,7 +41,7 @@ public class UbiLpWxController extends BaseController {
     @Resource
     private IDeviceBaseService deviceBaseService;
     @Resource
-    private IPostMqttMsgService postMqttMsgService;
+    private UbiSender ubiSender;
     @Resource
     private ILpInfoStateService lpInfoStateService;
     @Resource
@@ -149,7 +150,7 @@ public class UbiLpWxController extends BaseController {
      */
     @GetMapping("buzzing.do")
     public ServerResponse buzzing(String openNum) {
-        postMqttMsgService.requestBuzzingUbi(openNum);
+        ubiSender.requestBuzzing(openNum);
         return ServerResponse.success();
     }
 
@@ -158,7 +159,7 @@ public class UbiLpWxController extends BaseController {
      */
     @GetMapping("heartbeat.do")
     public ServerResponse heartbeat(String openNum) {
-        postMqttMsgService.requestHeartbeatUbi(openNum);
+        ubiSender.requestHeartbeat(openNum);
         return ServerResponse.success();
     }
 

+ 0 - 2
src/main/java/com/zy/bms/controller/lp/LpDeviceController.java

@@ -1,8 +1,6 @@
 package com.zy.bms.controller.lp;
 
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.zy.bms.common.ServerResponse;
-import com.zy.bms.entity.lp.LpInfoState;
 import com.zy.bms.pojo.io.DeviceIO;
 import com.zy.bms.service.lp.ILpInfoStateService;
 import org.springframework.web.bind.annotation.GetMapping;

+ 6 - 17
src/main/java/com/zy/bms/handler/LpHandlers.java → src/main/java/com/zy/bms/handler/LpHandler.java

@@ -6,7 +6,7 @@ import com.zy.bms.entity.lp.LpInfoRecord;
 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.sender.LpSender;
 import com.zy.bms.service.lp.ILpInfoRecordService;
 import com.zy.bms.service.lp.ILpInfoStateService;
 import com.zy.bms.utils.BeanUtil;
@@ -23,14 +23,14 @@ import javax.annotation.Resource;
  */
 @Slf4j
 @Component
-public class LpHandlers {
+public class LpHandler {
 
     @Resource
     private ILpInfoStateService lpInfoStateService;
     @Resource
     private ILpInfoRecordService lpInfoRecordService;
     @Resource
-    private IPostMqttMsgService postMqttMsgService;
+    private LpSender lpSender;
 
     /**
      * 基本信息上传
@@ -52,7 +52,7 @@ public class LpHandlers {
         }
         setWakeAndThreshLp(openNum, local, remote);
         //更新数据库设备状态信息
-        updateLpInfoStateService(openNum, remote);
+        lpInfoStateService.update(remote, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
     }
 
     /**
@@ -63,7 +63,7 @@ public class LpHandlers {
         //收到定位信息,修改状态为0
         device.setUpdateLocationFlag(0);
         // 更新数据库设备状态信息
-        updateLpInfoStateService(openNum, device);
+        lpInfoStateService.update(device, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
     }
 
     /**
@@ -86,17 +86,6 @@ public class LpHandlers {
         }
         //是否需要定位
         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);
-    }
-
-    /**
-     * 更新状态信息
-     */
-    private void updateLpInfoStateService(String openNum, LpInfoState lpInfoState) {
-        //不更新设备码字段
-        lpInfoState.setOpenNum(null);
-        lpInfoStateService.update(lpInfoState, new QueryWrapper<LpInfoState>().eq("open_num", openNum));
+        lpSender.setWakeAndThreshLp(dto);
     }
 }

+ 22 - 20
src/main/java/com/zy/bms/handler/MqttCallbackHandler.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.zy.bms.common.Constant;
 import com.zy.bms.entity.*;
 import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.sender.CommonSender;
 import com.zy.bms.service.*;
 import com.zy.bms.utils.MD5Util;
 import com.zy.bms.utils.RabbitMQApi;
@@ -25,15 +26,15 @@ public class MqttCallbackHandler {
     @Resource
     private WebSocketHandler webSocketHandler;
     @Resource
-    private IPostMqttMsgService postMqttMsgService;
+    private CommonSender commonSender;
     @Resource
     private ILogsService logsService;
     @Resource
     private IDeviceBaseService deviceBaseService;
     @Resource
-    private UbiHandlers ubiHandlers;
+    private UbiHandler ubiHandler;
     @Resource
-    private LpHandlers lpHandlers;
+    private LpHandler lpHandler;
 
     /**
      * 处理消息
@@ -55,19 +56,19 @@ public class MqttCallbackHandler {
                 break;
             }
             case Constant.M.UBI_HEARTBEAT: {// 心跳包
-                ubiHandlers.heartbeatHandler(openNum, mqttDTO);
+                ubiHandler.heartbeatHandler(openNum, mqttDTO);
                 break;
             }
             case Constant.M.UBI_SET_INFO: {// 获取设备设置信息
-                ubiHandlers.deviceSetHandler(openNum, mqttDTO);
+                ubiHandler.deviceSetHandler(openNum, mqttDTO);
                 break;
             }
             case Constant.M.LP_HEARTBEAT: {// 获取设备设置信息
-                lpHandlers.deviceInfoHandler(openNum, mqttDTO);
+                lpHandler.deviceInfoHandler(openNum, mqttDTO);
                 break;
             }
             case Constant.M.LP_HEARTBEAT_LOCATION: {// 路牌-设备位置信息
-                lpHandlers.deviceLocationHandler(openNum, mqttDTO);
+                lpHandler.deviceLocationHandler(openNum, mqttDTO);
                 break;
             }
         }
@@ -81,18 +82,19 @@ public class MqttCallbackHandler {
     private void registerHandler(MqttDTO mqttDTO) {
         String openNum = mqttDTO.getString("regnum");
         // 查询数据库中是否有此设备
-        DeviceBase device = deviceBaseService.getOne(new QueryWrapper<DeviceBase>().eq("open_num", openNum));
-        // 没有该设备,保存至数据库
-        if (device == null) {
-            device = new DeviceBase();
-            device.setOpenNum(openNum);
-            device.setGroupId(mqttDTO.getString("group"));
-            device.setType(mqttDTO.getString("type"));
-            device.setUsername("IMEI" + openNum);
-            device.setPassword(MD5Util.encode(openNum));
-            device.setSn("SN" + openNum);
-            deviceBaseService.save(device);
-        }
+        DeviceBase dbData = deviceBaseService.getOne(new QueryWrapper<DeviceBase>().eq("open_num", openNum));
+        //创建新的设备
+        DeviceBase device = new DeviceBase();
+        //saveOrUpdate根据主键ID来判断
+        device.setId(dbData.getId());
+        device.setOpenNum(openNum);
+        device.setGroupId(mqttDTO.getString("group"));
+        device.setUsername(Constant.PREF.RABBIT_MQ_USERNAME + openNum);
+        device.setPassword(MD5Util.encode(openNum));
+        device.setSn("SN" + openNum);
+        device.setType(mqttDTO.getString("type"));
+        deviceBaseService.saveOrUpdate(device);
+
         // mqtt服务器注册用户
         RabbitMQApi.register(device.getUsername(), device.getPassword());
         //路牌设备需要注册_lp结尾
@@ -100,6 +102,6 @@ public class MqttCallbackHandler {
             RabbitMQApi.register(device.getUsername() + "_lp", "123456");
         }
         // 返回处理结果
-        postMqttMsgService.handleRegister(device);
+        commonSender.register(device);
     }
 }

+ 7 - 10
src/main/java/com/zy/bms/handler/UbiHandlers.java → src/main/java/com/zy/bms/handler/UbiHandler.java

@@ -1,7 +1,6 @@
 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;
@@ -27,12 +26,12 @@ import java.util.Map;
  */
 @Slf4j
 @Component
-public class UbiHandlers {
+public class UbiHandler {
 
     @Resource
     private IUbiInfoRecordService heartbeatRecordService;
     @Resource
-    private IUbiInfoStateService deviceDynamicService;
+    private IUbiInfoStateService ubiInfoStateService;
     @Resource
     private IUbiNtripService ntripSetService;
     @Resource
@@ -43,21 +42,19 @@ public class UbiHandlers {
      * 心跳协议
      */
     public void heartbeatHandler(String openNum, MqttDTO mqttDTO) {
-        UbiInfoRecord entity = TransformEntity.tsf2UbiInfoRecord(openNum, mqttDTO);
+        UbiInfoRecord record = TransformEntity.tsf2UbiInfoRecord(openNum, mqttDTO);
         //存储记录
-        heartbeatRecordService.save(entity);
+        heartbeatRecordService.save(record);
         //更新最新的记录
-        UbiInfoState ubiInfoState = BeanUtil.cast(entity, UbiInfoState.class);
+        UbiInfoState ubiInfoState = BeanUtil.cast(record, UbiInfoState.class);
         //设置更新时间
         ubiInfoState.setUpdateTime(LocalDateTime.now());
-        //设置在线状态
-        ubiInfoState.setStatus(1);
-        deviceDynamicService.saveOrUpdate(ubiInfoState,
+        ubiInfoStateService.saveOrUpdate(ubiInfoState,
                 new QueryWrapper<UbiInfoState>().eq("open_num", ubiInfoState.getOpenNum()));
     }
 
     /**
-     * 普适性-设备设置信息
+     * 设备设置信息
      */
     @SuppressWarnings("unchecked")
     public void deviceSetHandler(String openNum, MqttDTO mqttDTO) {

+ 0 - 9
src/main/java/com/zy/bms/handler/WebSocketHandler.java

@@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
 import javax.websocket.OnClose;
-import javax.websocket.OnError;
 import javax.websocket.OnOpen;
 import javax.websocket.Session;
 import javax.websocket.server.PathParam;
@@ -48,14 +47,6 @@ public class WebSocketHandler {
         webSocketMap.get(deviceId).remove(session);
     }
 
-    /**
-    * 连接错误
-    */
-    @OnError
-    public void onError(Session session, Throwable error, @PathParam("deviceId") String deviceId) {
-       webSocketMap.get(deviceId).remove(session);
-    }
-
     /**
      * 群发消息
      */

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

@@ -1,10 +1,14 @@
 package com.zy.bms.pojo.dto;
 
+import com.zy.bms.common.Constant;
 import lombok.Data;
 
 import java.math.BigDecimal;
 
 /**
+ * 路牌
+ * 设置唤醒时间和阈值
+ *
  * @author chen_yi
  * Create on 2021/8/22
  */
@@ -34,4 +38,8 @@ public class WakeAndThreshLpDTO {
      * 是否需要设置参数 true or false
      */
     private String setf;
+
+    public String getSetf() {
+        return (wakeInt != null || thresh != null) ? Constant.STATUS.TRUE : Constant.STATUS.FALSE;
+    }
 }

+ 45 - 0
src/main/java/com/zy/bms/sender/BaseSender.java

@@ -0,0 +1,45 @@
+package com.zy.bms.sender;
+
+import com.zy.bms.common.Constant;
+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.service.impl.LogsService;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * Mqtt消息发送者
+ *
+ * @author yang xiao kun
+ * create on 2021/10/26
+ */
+@Component
+public class BaseSender {
+
+    @Resource
+    private MqttGateway mqttGateway;
+    @Resource
+    private LogsService logsService;
+    @Resource
+    private WebSocketHandler webSocketHandler;
+
+    /**
+     * 发送Mqtt 消息
+     *
+     * @param topic   主题
+     * @param payload 内容
+     */
+    public void send(String topic, String payload) {
+        // 1. 发送 MQTT 协议消息
+        mqttGateway.sendMsgToMqtt(payload, topic);
+        // 2. 存储消息日志
+        logsService.saveSend(topic, payload);
+        // 3. 消息同步转发至webSocket
+        webSocketHandler.massMessage(Constant.splitOpenNum(topic), payload);
+    }
+
+
+}

+ 30 - 0
src/main/java/com/zy/bms/sender/CommonSender.java

@@ -0,0 +1,30 @@
+package com.zy.bms.sender;
+
+import com.zy.bms.common.Constant;
+import com.zy.bms.entity.DeviceBase;
+import com.zy.bms.pojo.dto.MqttDTO;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author yang xiao kun
+ * create on 2021/10/26
+ */
+@Slf4j
+@Component
+public class CommonSender extends BaseSender {
+    /**
+     * 处理设备注册请求
+     * 返回登录密码
+     *
+     * @param device 设备信息
+     */
+    public void register(DeviceBase device) {
+        MqttDTO mqttDTO = new MqttDTO(Constant.M.HANDLE_REGISTER);
+        mqttDTO.put("regnum", device.getOpenNum())
+                .put("password", device.getPassword())
+                .put("group", device.getGroupId())
+                .put("sn", device.getSn());
+        send(Constant.TOPIC.REGISTER_CLIENT, mqttDTO.json());
+    }
+}

+ 32 - 0
src/main/java/com/zy/bms/sender/LpSender.java

@@ -0,0 +1,32 @@
+package com.zy.bms.sender;
+
+import com.zy.bms.common.Constant;
+import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.pojo.dto.WakeAndThreshLpDTO;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+
+/**
+ * 路牌
+ * 发送 MQTT 消息
+ *
+ * @author yang xiao kun
+ * create on 2021/10/26
+ */
+@Slf4j
+@Component
+public class LpSender extends BaseSender {
+
+    /**
+     * 设置唤醒时间和阈值
+     */
+    public void setWakeAndThreshLp(WakeAndThreshLpDTO dto) {
+        MqttDTO mqttDTO = new MqttDTO(Constant.M.LP_SET_LOCAL_INFO);
+        mqttDTO.put("setf", dto.getSetf())
+                .put("wakeint", dto.getWakeInt())
+                .put("thresh", dto.getThresh()).
+                put("getlocation", dto.getGetLocation());
+        send(Constant.TOPIC.DEVICE_CLIENT_LP + dto.getOpenNum(), mqttDTO.json());
+    }
+}

+ 104 - 0
src/main/java/com/zy/bms/sender/UbiSender.java

@@ -0,0 +1,104 @@
+package com.zy.bms.sender;
+
+import com.zy.bms.common.Constant;
+import com.zy.bms.pojo.dto.MqttDTO;
+import com.zy.bms.pojo.io.set.AlarmIO;
+import com.zy.bms.pojo.io.set.NtripIO;
+import com.zy.bms.pojo.io.set.SetIO;
+import com.zy.bms.pojo.io.set.TcpIO;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+/**
+ * 普适型
+ * 发送 MQTT 消息
+ *
+ * @author yang xiao kun
+ * create on 2021/10/26
+ */
+@Slf4j
+@Component
+public class UbiSender extends BaseSender {
+
+    public void set(SetIO io) {
+        //发送消息
+        send(Constant.TOPIC.DEVICE_CLIENT + io.getOpenNum(), getMqttDTO(io).json());
+    }
+
+    /**
+     * 请求设备远程设置
+     *
+     * @param openNum 设备码
+     */
+    public void requestRemoteSet(String openNum) {
+        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_GET_SET_INFO);
+        mqttDTO.put("getset", "getset");
+        send(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
+    }
+
+
+    /**
+     * 请求设备立即发送心跳包
+     *
+     * @param openNum 设备码
+     */
+    public void requestHeartbeat(String openNum) {
+        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_REQUEST_HEARTBEAT);
+        mqttDTO.put("fun", "gethearbeat");
+        send(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
+    }
+
+    /**
+     * 请求设备蜂鸣器
+     *
+     * @param openNum 设备码
+     */
+    public void requestBuzzing(String openNum) {
+        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_REQUEST_HEARTBEAT);
+        mqttDTO.put("fun", "buzzerring");
+        send(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
+    }
+
+    /**
+     * 组装需要发送的模板消息
+     *
+     * @param io 设置信息
+     */
+    private MqttDTO getMqttDTO(SetIO io) {
+        MqttDTO result = new MqttDTO(io.getM());
+        switch (io.getM()) {
+            //普适性 - ntripServer设置
+            case Constant.M.UBI_NTRIP_SERVER: {
+                NtripIO tmp = (NtripIO) io;
+                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
+                        .put("port", tmp.getPort()).put("mountpoint", tmp.getMountPoint())
+                        .put("password", tmp.getPassword());
+                break;
+            }
+            //普适性 - ntripClient设置
+            case Constant.M.UBI_NTRIP_CLIENT: {
+                NtripIO tmp = (NtripIO) io;
+                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
+                        .put("port", tmp.getPort()).put("mountpoint", tmp.getMountPoint())
+                        .put("password", tmp.getPassword()).put("user", tmp.getUser());
+                break;
+            }
+            //普适性 - tcp设置
+            case Constant.M.UBI_TCP: {
+                TcpIO tmp = (TcpIO) io;
+                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
+                        .put("port", tmp.getPort()).put("type", tmp.getType());
+                break;
+            }
+            //普适性 - 远程控制设备报警
+            case Constant.M.UBI_CONTROL_ALARM: {
+                AlarmIO tmp = (AlarmIO) io;
+                result.put("duration", tmp.getDuration()).put("freq", tmp.getFreq())
+                        .put("times", tmp.getTimes());
+                break;
+            }
+        }
+        return result;
+    }
+
+}

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

@@ -1,59 +0,0 @@
-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;
-
-/**
- * 向设备发送协议消息 服务类
- *
- * @author yangxiaokun
- * @since 2021-06-03
- */
-public interface IPostMqttMsgService {
-
-    /**
-     * 发送设置指令
-     *
-     * @param io 设置参数
-     */
-    void set(SetIO io);
-
-    /**
-     * 处理设备注册请求
-     * 返回登录密码
-     *
-     * @param device 设备信息
-     */
-    void handleRegister(DeviceBase device);
-
-    /**
-     * 路牌
-     * 更新唤醒时间和阈值
-     */
-    void setWakeAndThreshLp(WakeAndThreshLpDTO dto);
-
-    /**
-     * 普适性
-     * 获取设备本地设置
-     *
-     * @param openNum 设备码
-     */
-    void requestLocalSetUbi(String openNum);
-
-    /**
-     * 普适性
-     * 服务器主动请求获取心跳包
-     *
-     * @param openNum 设备码
-     */
-    void requestHeartbeatUbi(String openNum);
-
-    /**
-     * 普适性
-     * 服务器主动发送请求蜂鸣器
-     *
-     * @param openNum 设备码
-     */
-    void requestBuzzingUbi(String openNum);
-}

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

@@ -1,139 +0,0 @@
-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.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;
-
-import javax.annotation.Resource;
-
-/**
- * 向设备发送协议消息 服务类
- *
- * @author yangxiaokun
- * @since 2021-06-03
- */
-@Service
-public class PostMqttMsgServiceImpl implements IPostMqttMsgService {
-
-    @Resource
-    private MqttGateway mqttGateway;
-    @Resource
-    private LogsService mqttLogService;
-    @Resource
-    private WebSocketHandler webSocketHandler;
-
-    @Override
-    public void set(SetIO io) {
-        //主题
-        String topic = Constant.TOPIC.DEVICE_CLIENT + io.getOpenNum();
-        //消息
-        String payload = getMqttDTO(io).json();
-        //发送消息
-        sendMqttMessage(topic, payload);
-    }
-
-    @Override
-    public void handleRegister(DeviceBase device) {
-        MqttDTO mqttDTO = new MqttDTO(Constant.M.HANDLE_REGISTER);
-        mqttDTO.put("regnum", device.getOpenNum())
-                .put("password", device.getPassword())
-                .put("group", device.getGroupId())
-                .put("sn", device.getSn());
-        sendMqttMessage(Constant.TOPIC.REGISTER_CLIENT, mqttDTO.json());
-    }
-
-    @Override
-    public void requestLocalSetUbi(String openNum) {
-        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_GET_SET_INFO);
-        mqttDTO.put("getset", "getset");
-        sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
-    }
-
-    @Override
-    public void setWakeAndThreshLp(WakeAndThreshLpDTO dto) {
-        MqttDTO mqttDTO = new MqttDTO(Constant.M.LP_SET_LOCAL_INFO);
-        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
-    public void requestHeartbeatUbi(String openNum) {
-        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_REQUEST_HEARTBEAT);
-        mqttDTO.put("fun", "gethearbeat");
-        sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
-    }
-
-    @Override
-    public void requestBuzzingUbi(String openNum) {
-        MqttDTO mqttDTO = new MqttDTO(Constant.M.UBI_REQUEST_HEARTBEAT);
-        mqttDTO.put("fun", "buzzerring");
-        sendMqttMessage(Constant.TOPIC.DEVICE_CLIENT + openNum, mqttDTO.json());
-    }
-
-    /**
-     * 发送Mqtt 消息
-     *
-     * @param topic   主题
-     * @param payload 内容
-     */
-    private void sendMqttMessage(String topic, String payload) {
-        // 1. 发送 MQTT 协议消息
-        mqttGateway.sendMsgToMqtt(payload, topic);
-        // 2. 存储消息日志
-        mqttLogService.saveSend(topic, payload);
-        // 3. 消息同步转发至webSocket
-        webSocketHandler.massMessage(Constant.splitOpenNum(topic), payload);
-    }
-
-    /**
-     * 组装需要发送的模板消息
-     *
-     * @param io 设置信息
-     */
-    private MqttDTO getMqttDTO(SetIO io) {
-        MqttDTO result = new MqttDTO(io.getM());
-        switch (io.getM()) {
-            //普适性 - ntripServer设置
-            case Constant.M.UBI_NTRIP_SERVER: {
-                NtripIO tmp = (NtripIO) io;
-                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
-                        .put("port", tmp.getPort()).put("mountpoint", tmp.getMountPoint())
-                        .put("password", tmp.getPassword());
-                break;
-            }
-            //普适性 - ntripClient设置
-            case Constant.M.UBI_NTRIP_CLIENT: {
-                NtripIO tmp = (NtripIO) io;
-                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
-                        .put("port", tmp.getPort()).put("mountpoint", tmp.getMountPoint())
-                        .put("password", tmp.getPassword()).put("user", tmp.getUser());
-                break;
-            }
-            //普适性 - tcp设置
-            case Constant.M.UBI_TCP: {
-                TcpIO tmp = (TcpIO) io;
-                result.put("en", tmp.getOnOff() == 0 ? "off" : "on").put("host", tmp.getHost())
-                        .put("port", tmp.getPort()).put("type", tmp.getType());
-                break;
-            }
-            //普适性 - 远程控制设备报警
-            case Constant.M.UBI_CONTROL_ALARM: {
-                AlarmIO tmp = (AlarmIO) io;
-                result.put("duration", tmp.getDuration()).put("freq", tmp.getFreq())
-                        .put("times", tmp.getTimes());
-                break;
-            }
-        }
-        return result;
-    }
-
-}