连接MQTT,接收车辆的数据
parent
411a454c1b
commit
e9a19e94e9
|
@ -7,6 +7,7 @@ import com.muyu.common.core.web.page.TableDataInfo;
|
|||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||
import com.muyu.enterprise.domain.WarnLogs;
|
||||
import com.muyu.enterprise.service.IWarnLogsService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -23,6 +24,7 @@ import java.util.List;
|
|||
*/
|
||||
@RestController
|
||||
@RequestMapping("/logs")
|
||||
@Tag(name = "车辆预警日志",description = "用来记录车辆预警的日志")
|
||||
public class WarnLogsController extends BaseController
|
||||
{
|
||||
@Resource
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.muyu.common.core.web.page.TableDataInfo;
|
|||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||
import com.muyu.enterprise.domain.WarnRule;
|
||||
import com.muyu.enterprise.service.IWarnRuleService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -24,6 +25,7 @@ import java.util.List;
|
|||
*/
|
||||
@RestController
|
||||
@RequestMapping("/rule")
|
||||
@Tag(name = "车辆预警规则",description = "用来客户添加或者修改规则")
|
||||
public class WarnRuleController extends BaseController
|
||||
{
|
||||
@Resource
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.muyu.common.core.web.page.TableDataInfo;
|
|||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||
import com.muyu.enterprise.domain.resp.WarnStrategy;
|
||||
import com.muyu.enterprise.service.IWarnStrategyService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -23,6 +24,7 @@ import java.util.List;
|
|||
*/
|
||||
@RestController
|
||||
@RequestMapping("/strategy")
|
||||
@Tag(name = "故障策略",description = "故障策略对应车辆类型用来客户选择")
|
||||
public class WarnStrategyController extends BaseController
|
||||
{
|
||||
@Resource
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
<version>3.6.3</version>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>cloud-modules-vehicle-gateway</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-modules-vehicle-gateway系统模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataSource -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataScope -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datascope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common Log -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-log</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 接口模块 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,57 @@
|
|||
package com.muyu;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
/**
|
||||
* @ClassName demo
|
||||
* @Description 描述
|
||||
* @Author YiBo.Liu
|
||||
* @Date 2024/9/27 22:27
|
||||
*/
|
||||
public class demo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String topic = "vehicle";
|
||||
String content = "Message from MqttPublishSample";
|
||||
int qos = 2;
|
||||
String broker = "tcp://106.15.136.7:1883";
|
||||
String clientId = "JavaSample";
|
||||
|
||||
try {
|
||||
// 第三个参数为空,默认持久化策略
|
||||
MqttClient sampleClient = new MqttClient(broker, clientId);
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
System.out.println("Connecting to broker: "+broker);
|
||||
sampleClient.connect(connOpts);
|
||||
sampleClient.subscribe(topic,0);
|
||||
sampleClient.setCallback(new MqttCallback() {
|
||||
// 连接丢失
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
|
||||
}
|
||||
// 连接成功
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
System.out.println(new String(mqttMessage.getPayload()));
|
||||
}
|
||||
// 接收信息
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
} catch(MqttException me) {
|
||||
System.out.println("reason "+me.getReasonCode());
|
||||
System.out.println("msg "+me.getMessage());
|
||||
System.out.println("loc "+me.getLocalizedMessage());
|
||||
System.out.println("cause "+me.getCause());
|
||||
System.out.println("excep "+me);
|
||||
me.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package com.muyu.vehicleGateway.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @ClassName VehicleGatewayController
|
||||
* @Description 描述
|
||||
* @Author YiBo.Liu
|
||||
* @Date 2024/9/27 20:02
|
||||
*/
|
||||
@RequestMapping("/vehicleGateway")
|
||||
@RestController
|
||||
@Tag(name = "车辆网关",description = "车辆网关管理负载中心")
|
||||
public class VehicleGatewayController {
|
||||
|
||||
|
||||
@PostMapping("/MqttPublishSample")
|
||||
public static void MqttPublishSample(){
|
||||
//定义MQTT主题
|
||||
String topic = "vehicle";
|
||||
//定义要发布的消息内容
|
||||
String content = "Message from MqttPublishSample";
|
||||
//定义消息服务质量(qos)等级
|
||||
int qos = 2;
|
||||
//定义MQTT代理服务器地址
|
||||
String broker = "tcp://127.0.0.1:1883";
|
||||
//定义客户端标识符
|
||||
String clientId = "JavaSample";
|
||||
//创建MemoryPersistence对象,用于消息的内存持久性
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try{
|
||||
//创建MqttClient实例,连接到MQTT代理服务器
|
||||
MqttClient sampleClient = new MqttClient(broker,clientId);
|
||||
|
||||
//创建MqttConnectOptions对象,用于配置连接选项
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
//设置CleanSession为true,标识每个连接都是一个全新的会话
|
||||
connOpts.setCleanSession(true);
|
||||
//打印连接信息
|
||||
System.out.println("Connecting to broker: " + broker);
|
||||
|
||||
sampleClient.connect(connOpts);
|
||||
sampleClient.subscribe(topic,0);
|
||||
sampleClient.setCallback(new MqttCallback() {
|
||||
|
||||
//连接报错
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
|
||||
}
|
||||
//接收到消息
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
System.out.println(new String(mqttMessage.getPayload()));
|
||||
}
|
||||
//消费完成
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
} catch(MqttException me) {
|
||||
System.out.println("reason "+me.getReasonCode());
|
||||
System.out.println("msg "+me.getMessage());
|
||||
System.out.println("loc "+me.getLocalizedMessage());
|
||||
System.out.println("cause "+me.getCause());
|
||||
System.out.println("excep "+me);
|
||||
me.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -14,6 +14,7 @@
|
|||
<module>cloud-modules-file</module>
|
||||
<module>cloud-modules-enterprise</module>
|
||||
<module>cloud-weixin-mp</module>
|
||||
<module>cloud-modules-vehicle-gateway</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
|
|
Loading…
Reference in New Issue