소스 검색

去掉mqtt

hanqingsong 3 년 전
부모
커밋
ecdcb27794

+ 0 - 23
pom.xml

@@ -50,21 +50,11 @@
50 50
         <dependency>
51 51
             <groupId>org.projectlombok</groupId>
52 52
             <artifactId>lombok</artifactId>
53
-            <!--<scope>provided</scope>-->
54
-        </dependency>
55
-        <!--mqtt相关依赖-->
56
-        <dependency>
57
-            <groupId>org.springframework.integration</groupId>
58
-            <artifactId>spring-integration-stream</artifactId>
59 53
         </dependency>
60 54
         <dependency>
61 55
             <groupId>org.springframework.boot</groupId>
62 56
             <artifactId>spring-boot-starter-integration</artifactId>
63 57
         </dependency>
64
-        <dependency>
65
-            <groupId>org.springframework.integration</groupId>
66
-            <artifactId>spring-integration-mqtt</artifactId>
67
-        </dependency>
68 58
         <!-- 引入本地依赖 start -->
69 59
         <dependency>
70 60
             <groupId>com.sun.jna</groupId>
@@ -104,18 +94,5 @@
104 94
                 <artifactId>spring-boot-maven-plugin</artifactId>
105 95
             </plugin>
106 96
         </plugins>
107
-        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
108
-        <!--<resources>
109
-            <resource>
110
-                <directory>src/main/java</directory>
111
-                <includes>
112
-                    <include>**/*.properties</include>
113
-                    <include>**/*.xml</include>
114
-                </includes>
115
-            </resource>
116
-            <resource>
117
-                <directory>src/main/resources</directory>
118
-            </resource>
119
-        </resources>-->
120 97
     </build>
121 98
 </project>

+ 0 - 68
src/main/java/com/unissoft/config/MqttBaseConfig.java

@@ -1,68 +0,0 @@
1
-package com.unissoft.config;
2
-
3
-import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
4
-import org.springframework.beans.factory.annotation.Value;
5
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6
-import org.springframework.context.annotation.Bean;
7
-import org.springframework.context.annotation.Configuration;
8
-import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
9
-import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
10
-
11
-/**
12
- * @author qingsong.han
13
- * @description: 配置Mqtt公用部分, 主要是提供Mqtt客户端Bean, mqtt消息处理配置
14
- * @create 2022-06-14 11:10
15
- */
16
-@Configuration
17
-@SuppressWarnings("all")
18
-@ConditionalOnProperty(value = "mqtt.enabled", havingValue = "true")
19
-public class MqttBaseConfig {
20
-    /**
21
-     * 出站-生产者
22
-     */
23
-    public static final String CHANNEL_NAME_OUT = "mqttOutputChannel";
24
-
25
-    @Value("${mqtt.serverURIs}")
26
-    private String mqttServerURIs;
27
-    @Value("${mqtt.username}")
28
-    private String mqttUserName;
29
-    @Value("${mqtt.password}")
30
-    private String mqttPwd;
31
-    @Value("${mqtt.keepAliveInterval}")
32
-    private Integer keepAliveInterval;
33
-    @Value("${mqtt.cleanSession}")
34
-    private Boolean cleanSession;
35
-
36
-    // 客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息
37
-    /*private static final byte[] WILL_DATA;
38
-
39
-    static {
40
-        WILL_DATA = "offline".getBytes();
41
-    }*/
42
-
43
-
44
-    /**
45
-     * 创建设置MQTT Broker连接属性,如果使用SSL验证,也在这里设置(构造一个默认的mqtt客户端操作bean)
46
-     *
47
-     * @return MqttPahoClientFactory
48
-     */
49
-    @Bean
50
-    public MqttPahoClientFactory factory() {
51
-        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
52
-        MqttConnectOptions options = new MqttConnectOptions();
53
-        //如果配置了用户密码这里需要对应设置
54
-        options.setUserName(mqttUserName);
55
-        options.setPassword(mqttPwd.toCharArray());
56
-        String[] array = mqttServerURIs.split(",");
57
-        options.setServerURIs(array);
58
-        // 设为false,客户端掉线后 服务器端不会清除session,当重连后可以接收之前订阅主题的消息。当客户端上线后会接受到它离线的这段时间的消息
59
-        options.setCleanSession(cleanSession);
60
-        // 设置会话心跳时间 单位为秒
61
-        options.setKeepAliveInterval(keepAliveInterval);
62
-        // 设置"遗嘱"消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的"遗嘱"消息
63
-//        options.setWill("willTopic", WILL_DATA, 2, false);
64
-        factory.setConnectionOptions(options);
65
-        return factory;
66
-    }
67
-
68
-}

+ 0 - 53
src/main/java/com/unissoft/config/MqttOutConfig.java

@@ -1,53 +0,0 @@
1
-package com.unissoft.config;
2
-
3
-import org.springframework.beans.factory.annotation.Value;
4
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5
-import org.springframework.context.annotation.Bean;
6
-import org.springframework.context.annotation.Configuration;
7
-import org.springframework.integration.annotation.ServiceActivator;
8
-import org.springframework.integration.channel.DirectChannel;
9
-import org.springframework.integration.config.EnableIntegration;
10
-import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
11
-import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
12
-import org.springframework.messaging.MessageChannel;
13
-import org.springframework.messaging.MessageHandler;
14
-
15
-/**
16
- * @author qingsong.han
17
- * @description: 配置出站消息通道及相关处理Bean(生产发布消息)
18
- * @create 2022-03-01 11:10
19
- */
20
-@EnableIntegration
21
-@Configuration
22
-@ConditionalOnProperty(value = "mqtt.enabled", havingValue = "true")
23
-public class MqttOutConfig {
24
-    @Value("${mqtt.clientId}")
25
-    private String clientId;
26
-    @Value("${mqtt.qos}")
27
-    private int qos;
28
-    /**
29
-     * mqtt消息出站通道,用于发送出站消息
30
-     *
31
-     * @return
32
-     */
33
-    @Bean
34
-    public MessageChannel mqttOutputChannel() {
35
-        return new DirectChannel();
36
-    }
37
-
38
-    /**
39
-     * mqtt消息出站通道默认配置,用于向外发出mqtt消息,当前案例中发到了同一个mqtt服务器
40
-     *
41
-     * @return
42
-     */
43
-    @Bean
44
-    @ServiceActivator(inputChannel = MqttBaseConfig.CHANNEL_NAME_OUT)
45
-    public MessageHandler mqttOutbound(MqttPahoClientFactory factory) {
46
-        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, factory);
47
-        messageHandler.setAsync(true);
48
-        messageHandler.setDefaultQos(qos);
49
-        messageHandler.setDefaultTopic("mqtt_default_topic"); // 默认主题不能为空
50
-        return messageHandler;
51
-    }
52
-
53
-}

+ 0 - 55
src/main/java/com/unissoft/mqtt/MqttMessageSender.java

@@ -1,55 +0,0 @@
1
-package com.unissoft.mqtt;
2
-
3
-import com.unissoft.config.MqttBaseConfig;
4
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5
-import org.springframework.integration.annotation.MessagingGateway;
6
-import org.springframework.integration.mqtt.support.MqttHeaders;
7
-import org.springframework.messaging.handler.annotation.Header;
8
-import org.springframework.stereotype.Component;
9
-
10
-/**
11
- * @author qingsong.han
12
- * @description: mqtt消息发送工具,指定通过某个通道发出消息
13
- * @create 2022-06-14 11:29
14
- */
15
-@Component
16
-@ConditionalOnProperty(value = "mqtt.enabled", havingValue = "true")
17
-@MessagingGateway(defaultRequestChannel = MqttBaseConfig.CHANNEL_NAME_OUT)
18
-public interface MqttMessageSender {
19
-    /**
20
-     * 发送信息
21
-     *
22
-     * @param payload 发送的消息
23
-     */
24
-    void sendToMqtt(String payload);
25
-
26
-    /**
27
-     * 指定主题发送消息
28
-     *
29
-     * @param topic   主题
30
-     * @param payload 消息主体
31
-     */
32
-    void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, String payload);
33
-
34
-    /**
35
-     * 指定主题、qos发送消息
36
-     *
37
-     * @param topic   主题
38
-     * @param qos     对消息处理的几种机制。
39
-     *                0 表示的是订阅者没收到消息不会再次发送,消息会丢失。
40
-     *                1 表示的是会尝试重试,一直到接收到消息,但这种情况可能导致订阅者收到多次重复消息。
41
-     *                2 多了一次去重的动作,确保订阅者收到的消息有一次。
42
-     * @param payload 消息主体
43
-     */
44
-    void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);
45
-
46
-    /**
47
-     * 指定topic进行消息发送
48
-     *
49
-     * @param topic   自定义
50
-     * @param qos     (0,1,2)
51
-     * @param payload byte[]
52
-     */
53
-    void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, byte[] payload);
54
-
55
-}

+ 5 - 10
src/main/java/com/unissoft/mqtt/MqttTestSendController.java

@@ -1,11 +1,9 @@
1 1
 package com.unissoft.mqtt;
2 2
 
3
-import com.alibaba.fastjson.JSONObject;
4 3
 import lombok.extern.slf4j.Slf4j;
5 4
 import org.springframework.util.LinkedMultiValueMap;
6 5
 import org.springframework.util.MultiValueMap;
7 6
 import org.springframework.web.bind.annotation.GetMapping;
8
-import org.springframework.web.bind.annotation.PostMapping;
9 7
 import org.springframework.web.bind.annotation.RestController;
10 8
 import org.springframework.web.client.RestTemplate;
11 9
 
@@ -20,19 +18,16 @@ import java.util.Date;
20 18
 @Slf4j
21 19
 @RestController
22 20
 public class MqttTestSendController {
23
-
24
-    /*@Resource
25
-    private MqttMessageSender mqttMessageSender;*/
26
-    /*@Resource
27
-    private RestTemplate restTemplate;*/
21
+    @Resource
22
+    private RestTemplate restTemplate;
28 23
 
29 24
     @GetMapping("/mqttSendMsg")
30 25
     public String sendMessage() {
31
-//        mqttMessageSender.sendToMqtt("异常闯入/入侵");
32
-        /*MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
26
+        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
27
+        // DS-2CD7T27DWD-IZS(16384)/DS-7716NX-I4(4354)
33 28
         requestEntity.add("sAlarmType", 4354);
34 29
         requestEntity.add("alarmNow", new Date());
35
-        restTemplate.postForObject("http://localhost:8072/pushIntrusion", requestEntity, Object.class);*/
30
+        restTemplate.postForObject("http://localhost:8072/pushIntrusion", requestEntity, Object.class);
36 31
         return "200";
37 32
     }
38 33
 

+ 24 - 23
src/main/resources/bootstrap.yml

@@ -8,31 +8,16 @@ spring:
8 8
   jackson:
9 9
     date-format: yyyy-MM-dd HH:mm:ss
10 10
     time-zone: GMT+8
11
-# Mqtt配置
12
-mqtt:
13
-  #自定义应用开关
14
-  enabled: true
15
-#  # mqtt服务器地址,多个用逗号隔开(tcp://172.16.0.8:1883,tcp://172.16.0.9:1883)
16
-#  serverURIs: tcp://172.16.0.5:1883
17
-#  username: admin
18
-#  password: admin@123
19
-  serverURIs: tcp://192.168.50.169:1883
20
-  #mqtt服务用户名 当前未配置
21
-  username: admin
22
-  #mqtt服务用户密码 当前未配置
23
-  password: public
24
-  # 异常闯入/入侵 topic
25
-  topics: /PUSH/DS-7716NX-I41620220625CCRRK19773208WCVU/DPUT/intrusion
26
-  qos: 1
27
-  keepAliveInterval: 120
28
-  # 默认为true
29
-  cleanSession: true
30
-  # TODO 正式上线 不使用随机参数(clientId修改会导致无法收到启动前的消息)
31
-  clientId: mqtt-input-smart-grp-nof-devices-${random.value}
11
+  # dev: 开发环境,test: 测试环境,prod: 生产环境(线上)
12
+  profiles:
13
+    active: dev
14
+---
15
+spring:
16
+  profiles: dev
32 17
 # 异常闯入异常,入侵分析
33 18
 # 设备登录
34 19
 exceptionAlarm:
35
-  # 设备ip地址 DS-2CD7T27DWD-IZS(16384)/DS-7716NX-I4(4354)
20
+  # 设备ip地址 枪机: DS-2CD7T27DWD-IZS(16384)/球机: DS-7716NX-I4(4354)
36 21
   deviceIP: 192.168.0.101
37 22
   # 设备用户名
38 23
   userName: admin
@@ -41,4 +26,20 @@ exceptionAlarm:
41 26
 # 只支持post
42 27
 address:
43 28
   # 推送异常闯入/入侵信息地址
44
-  pushToIntrusion: http://localhost:8077/pushIntrusion
29
+  pushToIntrusion: http://localhost:8077/pushIntrusion
30
+---
31
+spring:
32
+  profiles: prop
33
+# 异常闯入异常,入侵分析
34
+# 设备登录
35
+exceptionAlarm:
36
+  # 设备ip地址 DS-2CD7T27DWD-IZS(16384)/DS-7716NX-I4(4354)
37
+  deviceIP: 192.168.163.21
38
+  # 设备用户名
39
+  userName: admin
40
+  # 设备密码
41
+  password: TLJK@123$%^
42
+# 只支持post
43
+address:
44
+  # 推送异常闯入/入侵信息地址
45
+  pushToIntrusion: http://10.105.101.60:8072/pushIntrusion