Merge remote-tracking branch 'origin/main_app权限适配' into dyf-app
This commit is contained in:
@ -0,0 +1,33 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.config;
|
||||||
|
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
||||||
|
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 14:40
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
public class MqttConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private MqttPropertiesConfig mqttPropertiesConfig;
|
||||||
|
/** 创建连接工厂 **/
|
||||||
|
@Bean
|
||||||
|
public MqttPahoClientFactory mqttPahoClientFactory(){
|
||||||
|
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
||||||
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
|
options.setCleanSession(true); //设置新会话
|
||||||
|
options.setUserName(mqttPropertiesConfig.getUsername());
|
||||||
|
options.setPassword(mqttPropertiesConfig.getPassword().toCharArray());
|
||||||
|
options.setServerURIs(new String[]{mqttPropertiesConfig.getUrl()});
|
||||||
|
factory.setConnectionOptions(options);
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.config;
|
||||||
|
|
||||||
|
import org.springframework.integration.annotation.MessagingGateway;
|
||||||
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
||||||
|
import org.springframework.messaging.handler.annotation.Header;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 17:06
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
|
||||||
|
public interface MqttGateway {
|
||||||
|
public abstract void sendMsgToMqtt(@Header(value = MqttHeaders.TOPIC) String topic, String payload);
|
||||||
|
public abstract void sendMsgToMqtt(@Header(value = MqttHeaders.TOPIC) String topic, @Header(value = MqttHeaders.QOS) int qos, String payload );
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fuyuanshen.modules.mqtt.receiver.ReceiverMessageHandler;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.integration.annotation.ServiceActivator;
|
||||||
|
import org.springframework.integration.channel.DirectChannel;
|
||||||
|
import org.springframework.integration.core.MessageProducer;
|
||||||
|
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
||||||
|
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
|
||||||
|
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
|
||||||
|
import org.springframework.messaging.MessageChannel;
|
||||||
|
import org.springframework.messaging.MessageHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 14:54
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
public class MqttInboundConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private MqttPropertiesConfig mqttPropertiesConfig;
|
||||||
|
@Autowired
|
||||||
|
private MqttPahoClientFactory mqttPahoClientFactory;
|
||||||
|
@Autowired
|
||||||
|
private ReceiverMessageHandler receiverMessageHandler;
|
||||||
|
//消息通道
|
||||||
|
@Bean
|
||||||
|
public MessageChannel messageInboundChannel(){
|
||||||
|
return new DirectChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置入站适配器
|
||||||
|
* 作用: 设置订阅主题,以及指定消息的通道 等相关属性
|
||||||
|
* */
|
||||||
|
@Bean
|
||||||
|
public MessageProducer messageProducer(){
|
||||||
|
MqttPahoMessageDrivenChannelAdapter mqttPahoMessageDrivenChannelAdapter = new MqttPahoMessageDrivenChannelAdapter(
|
||||||
|
mqttPropertiesConfig.getUrl(),
|
||||||
|
mqttPropertiesConfig.getSubClientId(),
|
||||||
|
mqttPahoClientFactory,
|
||||||
|
mqttPropertiesConfig.getSubTopic().split(",")
|
||||||
|
);
|
||||||
|
mqttPahoMessageDrivenChannelAdapter.setQos(1);
|
||||||
|
mqttPahoMessageDrivenChannelAdapter.setConverter(new DefaultPahoMessageConverter());
|
||||||
|
mqttPahoMessageDrivenChannelAdapter.setOutputChannel(messageInboundChannel());
|
||||||
|
return mqttPahoMessageDrivenChannelAdapter;
|
||||||
|
}
|
||||||
|
/** 指定处理消息来自哪个通道 */
|
||||||
|
@Bean
|
||||||
|
@ServiceActivator(inputChannel = "messageInboundChannel")
|
||||||
|
public MessageHandler messageHandler(){
|
||||||
|
return receiverMessageHandler;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.config;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.integration.annotation.ServiceActivator;
|
||||||
|
import org.springframework.integration.channel.DirectChannel;
|
||||||
|
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
||||||
|
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
||||||
|
import org.springframework.messaging.MessageChannel;
|
||||||
|
import org.springframework.messaging.MessageHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 15:46
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
|
public class MqttOutboundConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private MqttPropertiesConfig mqttPropertiesConfig;
|
||||||
|
@Autowired
|
||||||
|
private MqttPahoClientFactory mqttPahoClientFactory;
|
||||||
|
|
||||||
|
// 消息通道
|
||||||
|
@Bean
|
||||||
|
public MessageChannel mqttOutboundChannel(){
|
||||||
|
return new DirectChannel();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** 配置出站消息处理器 */
|
||||||
|
@Bean
|
||||||
|
@ServiceActivator(inputChannel = "mqttOutboundChannel") // 指定处理器针对哪个通道的消息进行处理
|
||||||
|
public MessageHandler mqttOutboundMessageHandler(){
|
||||||
|
MqttPahoMessageHandler mqttPahoMessageHandler = new MqttPahoMessageHandler(
|
||||||
|
mqttPropertiesConfig.getUrl(),
|
||||||
|
mqttPropertiesConfig.getPubClientId(),
|
||||||
|
mqttPahoClientFactory
|
||||||
|
);
|
||||||
|
mqttPahoMessageHandler.setDefaultQos(1);
|
||||||
|
mqttPahoMessageHandler.setDefaultTopic("worker/location");
|
||||||
|
mqttPahoMessageHandler.setAsync(true);
|
||||||
|
return mqttPahoMessageHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 14:32
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "mqtt")
|
||||||
|
@Component
|
||||||
|
public class MqttPropertiesConfig {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String url;
|
||||||
|
private String subClientId;
|
||||||
|
private String subTopic;
|
||||||
|
private String pubClientId;
|
||||||
|
private String pubTopic;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.publish;
|
||||||
|
|
||||||
|
import com.fuyuanshen.annotation.rest.AnonymousGetMapping;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/device")
|
||||||
|
@Slf4j
|
||||||
|
public class DeviceDataController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MqttClientTest mqttClientTest;
|
||||||
|
|
||||||
|
// @PostMapping("/{deviceId}/command")
|
||||||
|
@AnonymousGetMapping(value = "/test/command")
|
||||||
|
public ResponseEntity<String> sendCommand() {
|
||||||
|
|
||||||
|
mqttClientTest.sendMsg();
|
||||||
|
return ResponseEntity.ok("success");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.publish;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fuyuanshen.modules.mqtt.config.MqttGateway;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class MqttClientTest {
|
||||||
|
@Autowired
|
||||||
|
private MqttGateway mqttGateway;
|
||||||
|
|
||||||
|
public void sendMsg() {
|
||||||
|
mqttGateway.sendMsgToMqtt("worker/location/1", "hello mqtt spring boot");
|
||||||
|
log.info("message is send");
|
||||||
|
|
||||||
|
mqttGateway.sendMsgToMqtt("worker/alert/2", "hello mqtt spring boot2");
|
||||||
|
log.info("message is send2");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.publish;
|
||||||
|
|
||||||
|
import com.fuyuanshen.modules.mqtt.config.MqttGateway;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
||||||
|
import org.springframework.messaging.handler.annotation.Header;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 16:16
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
public class MqttMessageSender {
|
||||||
|
@Autowired
|
||||||
|
private MqttGateway mqttGateway;
|
||||||
|
public void sendMsg(@Header(value = MqttHeaders.TOPIC) String topic, String payload) {
|
||||||
|
mqttGateway.sendMsgToMqtt(topic,payload);
|
||||||
|
}
|
||||||
|
public void sendMsg(@Header(value = MqttHeaders.TOPIC) String topic, @Header(value = MqttHeaders.QOS) int qos, String payload) {
|
||||||
|
mqttGateway.sendMsgToMqtt(topic,qos,payload);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.fuyuanshen.modules.mqtt.receiver;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.messaging.Message;
|
||||||
|
import org.springframework.messaging.MessageHandler;
|
||||||
|
import org.springframework.messaging.MessageHeaders;
|
||||||
|
import org.springframework.messaging.MessagingException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: HarryLin
|
||||||
|
* @Date: 2025/3/20 15:24
|
||||||
|
* @Company: 北京红山信息科技研究院有限公司
|
||||||
|
* @Email: linyun@***.com.cn
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class ReceiverMessageHandler implements MessageHandler {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message<?> message) throws MessagingException{
|
||||||
|
Object payload = message.getPayload();
|
||||||
|
MessageHeaders headers = message.getHeaders();
|
||||||
|
String receivedTopic = Objects.requireNonNull(headers.get("mqtt_receivedTopic")).toString();
|
||||||
|
String receivedQos = Objects.requireNonNull(headers.get("mqtt_receivedQos")).toString();
|
||||||
|
String timestamp = Objects.requireNonNull(headers.get("timestamp")).toString();
|
||||||
|
log.info("MQTT payload= {} \n receivedTopic = {} \n receivedQos = {} \n timestamp = {}"
|
||||||
|
,payload,receivedTopic,receivedQos,timestamp);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.fuyuanshen.modules.security.config;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.integration.annotation.ServiceActivator;
|
||||||
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
||||||
|
import org.springframework.messaging.Message;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class DeviceDataService {
|
||||||
|
|
||||||
|
|
||||||
|
@ServiceActivator(inputChannel = "mqttInputChannel")
|
||||||
|
public void handleDeviceData(Message<?> message) {
|
||||||
|
String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
|
||||||
|
String payload = message.getPayload().toString();
|
||||||
|
|
||||||
|
// 解析设备数据
|
||||||
|
if (topic.startsWith("device/data/")) {
|
||||||
|
log.info("Received device data device/data/: {} {}", topic, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,9 +4,9 @@ spring:
|
|||||||
druid:
|
druid:
|
||||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||||
driverClassName: com.p6spy.engine.spy.P6SpyDriver
|
driverClassName: com.p6spy.engine.spy.P6SpyDriver
|
||||||
url: jdbc:p6spy:mysql://192.168.2.23:3306/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
|
url: jdbc:p6spy:mysql://120.79.224.186:3366/eladmin?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
|
||||||
username: root
|
username: root
|
||||||
password: root
|
password: 1fys@QWER..
|
||||||
# 初始连接数,建议设置为与最小空闲连接数相同
|
# 初始连接数,建议设置为与最小空闲连接数相同
|
||||||
initial-size: 20
|
initial-size: 20
|
||||||
# 最小空闲连接数,保持足够的空闲连接以应对请求
|
# 最小空闲连接数,保持足够的空闲连接以应对请求
|
||||||
@ -53,10 +53,10 @@ spring:
|
|||||||
|
|
||||||
redis:
|
redis:
|
||||||
#数据库索引
|
#数据库索引
|
||||||
database: ${REDIS_DB:2}
|
database: ${REDIS_DB:0}
|
||||||
host: ${REDIS_HOST:123.207.99.140}
|
host: ${REDIS_HOST:120.79.224.186}
|
||||||
port: ${REDIS_PORT:6379}
|
port: ${REDIS_PORT:26379}
|
||||||
password: ${REDIS_PWD:ccxx11234}
|
password: ${REDIS_PWD:1fys@QWER..}
|
||||||
#连接超时时间
|
#连接超时时间
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
# 连接池配置
|
# 连接池配置
|
||||||
@ -149,3 +149,12 @@ file:
|
|||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.fuyuanshen: debug
|
com.fuyuanshen: debug
|
||||||
|
# MQTT配置
|
||||||
|
mqtt:
|
||||||
|
username: admin
|
||||||
|
password: fys123456
|
||||||
|
url: tcp://127.0.0.1:1883
|
||||||
|
subClientId: wuLang_subClient_01
|
||||||
|
subTopic: worker/alert/#,worker/location/#
|
||||||
|
pubTopic: worker/location
|
||||||
|
pubClientId: wuLang_pubClient_01
|
8
pom.xml
8
pom.xml
@ -223,6 +223,14 @@
|
|||||||
<artifactId>commons-text</artifactId>
|
<artifactId>commons-text</artifactId>
|
||||||
<version>1.13.0</version>
|
<version>1.13.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-integration</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.integration</groupId>
|
||||||
|
<artifactId>spring-integration-mqtt</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
Reference in New Issue
Block a user