增加mqtt客户端模拟器
parent
c23bc07fda
commit
a2e0fecb39
|
@ -23,5 +23,5 @@
|
|||
hs_err_pid*
|
||||
.idea
|
||||
target
|
||||
.iml
|
||||
*.yml
|
||||
*.iml
|
||||
*.yml
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.cloud:spring-cloud-openfeign-core:3.1.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.github.openfeign.form:feign-form-spring:3.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.github.openfeign.form:feign-form:3.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.4" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-io:commons-io:2.11.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
|
||||
<orderEntry type="module" module-name="model" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-mongodb:3.3.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.1" level="project" />
|
||||
<orderEntry type="module" module-name="common" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.9.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.2" level="project" />
|
||||
</component>
|
||||
</module>
|
|
@ -1,70 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-mongodb:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-mongodb:3.3.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.ben-manes.caffeine:caffeine:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:2.10.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.9.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.57" level="project" />
|
||||
<orderEntry type="library" name="Maven: joda-time:joda-time:2.10.10" level="project" />
|
||||
<orderEntry type="module" module-name="common" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.1" level="project" />
|
||||
<orderEntry type="module" module-name="model" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.1" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:2.2" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,23 @@
|
|||
package cc.iotkit.mqttauth.dao;
|
||||
|
||||
import cc.iotkit.model.product.Product;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||
|
||||
@Repository
|
||||
public class ProductDao {
|
||||
|
||||
@Resource
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
public Product getProduct(String pk) {
|
||||
Query query = query(where("code").is(pk));
|
||||
return mongoTemplate.findOne(query, Product.class);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
package cc.iotkit.mqttauth.service;
|
||||
|
||||
import cc.iotkit.common.exception.BizException;
|
||||
import cc.iotkit.common.utils.JsonUtil;
|
||||
import cc.iotkit.model.device.DeviceInfo;
|
||||
import cc.iotkit.model.product.Product;
|
||||
import cc.iotkit.mqttauth.dao.DeviceDao;
|
||||
import cc.iotkit.mqttauth.dao.ProductDao;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -15,16 +18,28 @@ public class DeviceService {
|
|||
|
||||
@Autowired
|
||||
private DeviceDao deviceDao;
|
||||
@Autowired
|
||||
private ProductDao productDao;
|
||||
|
||||
public DeviceInfo register(DeviceInfo device) {
|
||||
DeviceInfo deviceInfo = deviceDao.getByPkAndDn(device.getProductKey(), device.getDeviceName());
|
||||
|
||||
String pk = device.getProductKey();
|
||||
Product product = productDao.getProduct(pk);
|
||||
if (product == null) {
|
||||
throw new BizException("Product does not exist");
|
||||
}
|
||||
String uid = product.getUid();
|
||||
|
||||
DeviceInfo deviceInfo = deviceDao.getByPkAndDn(pk, device.getDeviceName());
|
||||
if (deviceInfo != null) {
|
||||
device.setDeviceId(deviceInfo.getDeviceId());
|
||||
device.setUid(uid);
|
||||
deviceDao.updateDevice(device);
|
||||
log.info("device register update:{}", JsonUtil.toJsonString(device));
|
||||
return device;
|
||||
}
|
||||
|
||||
device.setUid(uid);
|
||||
device.setDeviceId(newDeviceId(device.getDeviceName()));
|
||||
deviceDao.addDevice(device);
|
||||
log.info("device registered:{}", JsonUtil.toJsonString(device));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import cc.iotkit.common.Constants;
|
||||
import cc.iotkit.common.utils.CodecUtil;
|
||||
import cc.iotkit.mqttauth.config.Constants;
|
||||
import cc.iotkit.mqttauth.controller.MqttAuthController;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import cc.iotkit.common.Constants;
|
||||
import cc.iotkit.common.utils.CodecUtil;
|
||||
import cc.iotkit.mqttauth.config.Constants;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SysMqttAuth {
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,76 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>device-server</artifactId>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>mqtt客户端模拟器</description>
|
||||
|
||||
<artifactId>mqtt-client-simulator</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>cc.iotkit.simulator.Application</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package cc.iotkit.simulator;
|
||||
|
||||
import cc.iotkit.simulator.config.Mqtt;
|
||||
import cc.iotkit.simulator.service.Gateway;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Mqtt.broker = args[0];
|
||||
|
||||
Gateway gateway = new Gateway("hbtgIA0SuVw9lxjB", "AA:BB:CC:DD:22");
|
||||
gateway.addSubDevice("Rf4QSjbm65X45753", "ABC12300002", "S01");
|
||||
gateway.addSubDevice("Rf4QSjbm65X45753", "ABC12300003", "S01");
|
||||
gateway.addSubDevice("hdX3PCMcFrCYpesJ", "ABD12300001", "F01");
|
||||
gateway.addSubDevice("hdX3PCMcFrCYpesJ", "ABD12300002", "F01");
|
||||
gateway.start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package cc.iotkit.simulator.config;
|
||||
|
||||
public class Mqtt {
|
||||
|
||||
public static String broker;
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package cc.iotkit.simulator.service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Device {
|
||||
|
||||
protected String productKey;
|
||||
|
||||
protected String deviceName;
|
||||
|
||||
private String model;
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
package cc.iotkit.simulator.service;
|
||||
|
||||
import cc.iotkit.common.Constants;
|
||||
import cc.iotkit.common.utils.JsonUtil;
|
||||
import cc.iotkit.simulator.config.Mqtt;
|
||||
import lombok.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class Gateway extends Device {
|
||||
|
||||
private MqttClient client;
|
||||
|
||||
private List<Device> subDevices = new ArrayList<>();
|
||||
|
||||
public Gateway(String productKey, String deviceName) {
|
||||
super(productKey, deviceName, "GW01");
|
||||
}
|
||||
|
||||
public void start() {
|
||||
String broker = Mqtt.broker;
|
||||
|
||||
String clientId = String.format("%s_%s", productKey, deviceName);
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try {
|
||||
MqttClient client = new MqttClient(broker, clientId, persistence);
|
||||
|
||||
// MQTT 连接选项
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setUserName(this.deviceName);
|
||||
connOpts.setPassword(DigestUtils.md5Hex(Constants.MQTT_SECRET + clientId).toCharArray());
|
||||
// 保留会话
|
||||
connOpts.setCleanSession(true);
|
||||
|
||||
// 设置回调
|
||||
client.setCallback(new OnMessageCallback(client));
|
||||
|
||||
// 建立连接
|
||||
log.info("Connecting to broker:{} ", broker);
|
||||
client.connect(connOpts);
|
||||
|
||||
log.info("Connected");
|
||||
// 订阅
|
||||
String topic = String.format("/sys/%s/%s/c/#", productKey, deviceName);
|
||||
log.info("subscribe topic:{}", topic);
|
||||
IMqttToken mqttToken = client.subscribeWithResponse(topic);
|
||||
if (mqttToken.isComplete()) {
|
||||
//注册子设备
|
||||
for (Device subDevice : subDevices) {
|
||||
Request request = new Request();
|
||||
request.setId(UUID.randomUUID().toString());
|
||||
request.setParams(subDevice);
|
||||
client.publish(String.format("/sys/%s/%s/s/register", productKey, deviceName),
|
||||
new MqttMessage(JsonUtil.toJsonString(request).getBytes())
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
log.error("connect mqtt-broker error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSubDevice(String productKey, String deviceName, String model) {
|
||||
subDevices.add(new Device(productKey, deviceName, model));
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class OnMessageCallback implements MqttCallback {
|
||||
|
||||
private MqttClient client;
|
||||
|
||||
public OnMessageCallback(MqttClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void connectionLost(Throwable cause) {
|
||||
log.info("连接断开");
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void messageArrived(String topic, MqttMessage message) {
|
||||
log.info("接收消息,topic:{},payload:{}", topic,
|
||||
new String(message.getPayload()));
|
||||
|
||||
if (topic.endsWith("register_reply")) {
|
||||
String payload = new String(message.getPayload());
|
||||
Response response = JsonUtil.parse(payload, Response.class);
|
||||
//子设备注册成功
|
||||
if (response.code == 0) {
|
||||
Map<String, Object> data = response.getData();
|
||||
//订阅子设备消息
|
||||
String subTopic = String.format("/sys/%s/%s/c/#",
|
||||
data.get("productKey"), data.get("deviceName"));
|
||||
log.info("subscribe topic:{}", subTopic);
|
||||
client.subscribe(subTopic);
|
||||
}
|
||||
}
|
||||
|
||||
if (topic.endsWith("_reply")) {
|
||||
return;
|
||||
}
|
||||
String payload = new String(message.getPayload());
|
||||
Request request = JsonUtil.parse(payload, Request.class);
|
||||
|
||||
Response response = new Response(request.getId(), 0, new HashMap<>());
|
||||
client.publish(topic.replace("/c/", "/s/") + "_reply",
|
||||
new MqttMessage(JsonUtil.toJsonString(response).getBytes()));
|
||||
|
||||
//属性设置后上报属性
|
||||
String setTopic = "/c/service/property/set";
|
||||
if (topic.endsWith(setTopic)) {
|
||||
request.setId(UUID.randomUUID().toString());
|
||||
client.publish(topic.replace(setTopic, "/s/event/property/post"),
|
||||
new MqttMessage(JsonUtil.toJsonString(request).getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
public void deliveryComplete(IMqttDeliveryToken token) {
|
||||
log.info("deliveryComplete,topic:{},result:{}", token.getTopics(), token.isComplete());
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Request {
|
||||
|
||||
private String id;
|
||||
|
||||
private Object params;
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Response {
|
||||
|
||||
private String id;
|
||||
|
||||
private int code;
|
||||
|
||||
private Map<String, Object> data;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-mongodb:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.9" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-sync:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:bson:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:4.4.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-mongodb:3.3.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.6.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.56" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-integration:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.integration:spring-integration-core:5.5.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-messaging:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.retry:spring-retry:1.3.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.4.13" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.integration:spring-integration-mqtt:5.5.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-cache:2.6.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.cloud:spring-cloud-commons:3.1.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-crypto:5.6.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.github.ben-manes.caffeine:caffeine:2.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:2.10.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.3.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.9.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.2" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.14" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-io:commons-io:2.11.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.google.guava:guava:20.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.57" level="project" />
|
||||
<orderEntry type="library" name="Maven: joda-time:joda-time:2.10.10" level="project" />
|
||||
<orderEntry type="module" module-name="model" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.1" level="project" />
|
||||
<orderEntry type="module" module-name="common" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.1" level="project" />
|
||||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.1" level="project" />
|
||||
<orderEntry type="module" module-name="device-api" />
|
||||
<orderEntry type="library" name="Maven: org.springframework.cloud:spring-cloud-openfeign-core:3.1.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.github.openfeign.form:feign-form-spring:3.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: io.github.openfeign.form:feign-form:3.8.0" level="project" />
|
||||
<orderEntry type="library" name="Maven: commons-fileupload:commons-fileupload:1.4" level="project" />
|
||||
</component>
|
||||
</module>
|
|
@ -35,7 +35,7 @@ public class MqttConfig {
|
|||
@Value("${mqtt.url}")
|
||||
private String url;
|
||||
|
||||
@Value(("${spring.profiles.active}"))
|
||||
@Value(("${spring.profiles.active:}"))
|
||||
private String env;
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,14 +4,13 @@ import cc.iotkit.common.utils.JsonUtil;
|
|||
import cc.iotkit.model.device.DeviceInfo;
|
||||
import cc.iotkit.model.device.message.DeviceRegister;
|
||||
import cc.iotkit.model.mq.Request;
|
||||
import cc.iotkit.model.mq.Response;
|
||||
import cc.iotkit.server.service.DeviceService;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class RegisterHandler implements MqttHandler<DeviceRegister, Response.Empty> {
|
||||
public class RegisterHandler implements MqttHandler<DeviceRegister, DeviceRegister> {
|
||||
|
||||
@Autowired
|
||||
private DeviceService deviceService;
|
||||
|
@ -28,10 +27,10 @@ public class RegisterHandler implements MqttHandler<DeviceRegister, Response.Emp
|
|||
}
|
||||
|
||||
@Override
|
||||
public Response.Empty handler(String topic, DeviceInfo device, Request<DeviceRegister> request) {
|
||||
public DeviceRegister handler(String topic, DeviceInfo device, Request<DeviceRegister> request) {
|
||||
DeviceRegister regInfo = request.getParams();
|
||||
deviceService.register(device.getDeviceId(), regInfo.getProductKey(),
|
||||
regInfo.getDeviceName(), regInfo.getModel());
|
||||
return Response.empty();
|
||||
return regInfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cc.iotkit.server.service;
|
||||
|
||||
import cc.iotkit.common.exception.BizException;
|
||||
import cc.iotkit.common.exception.NotFoundException;
|
||||
import cc.iotkit.common.exception.OfflineException;
|
||||
import cc.iotkit.common.utils.DeviceUtil;
|
||||
|
@ -57,10 +58,17 @@ public class DeviceService implements IDeviceManager, IDeviceService {
|
|||
device.setDeviceName(deviceName);
|
||||
device.setModel(model);
|
||||
|
||||
DeviceInfo parentDevice = deviceDao.getByDeviceId(parentId);
|
||||
if (parentDevice == null) {
|
||||
throw new BizException("Parent device does not exist");
|
||||
}
|
||||
String uid = parentDevice.getUid();
|
||||
|
||||
DeviceInfo deviceInfo = deviceDao.getByPkAndDn(productKey, deviceName);
|
||||
if (deviceInfo != null) {
|
||||
device.setId(deviceInfo.getId());
|
||||
device.setDeviceId(deviceInfo.getDeviceId());
|
||||
device.setUid(uid);
|
||||
deviceDao.updateDevice(device);
|
||||
log.info("device register update:{}", JsonUtil.toJsonString(device));
|
||||
return deviceInfo;
|
||||
|
@ -70,6 +78,7 @@ public class DeviceService implements IDeviceManager, IDeviceService {
|
|||
|
||||
device.setId(deviceId);
|
||||
device.setDeviceId(deviceId);
|
||||
device.setUid(uid);
|
||||
deviceDao.addDevice(device);
|
||||
log.info("device registered:{}", JsonUtil.toJsonString(device));
|
||||
return device;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
<module>mqtt-auth</module>
|
||||
<module>mqtt-server</module>
|
||||
<module>device-api</module>
|
||||
<module>mqtt-client-simulator</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
|
Binary file not shown.
|
@ -87,14 +87,14 @@ public class ProductController {
|
|||
return categoryRepository.findAll();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('iot_admin')")
|
||||
@PreAuthorize("hasRole('iot_admin')")
|
||||
@PostMapping("/saveCategory")
|
||||
public void saveCategory(Category cate) {
|
||||
cate.setCreateAt(System.currentTimeMillis());
|
||||
categoryRepository.save(cate);
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('iot_admin')")
|
||||
@PreAuthorize("hasRole('iot_admin')")
|
||||
@PostMapping("/delCategory")
|
||||
public void delCategory(String id) {
|
||||
categoryRepository.deleteById(id);
|
||||
|
|
6
pom.xml
6
pom.xml
|
@ -142,6 +142,12 @@
|
|||
<version>2.10.10</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cc.iotkit</groupId>
|
||||
<artifactId>model</artifactId>
|
||||
|
|
|
@ -30,7 +30,7 @@ public class RuleConfiguration {
|
|||
@Value("${mqtt.url}")
|
||||
private String url;
|
||||
|
||||
@Value(("${spring.profiles.active}"))
|
||||
@Value(("${spring.profiles.active:}"))
|
||||
private String env;
|
||||
|
||||
private MqttPahoMessageDrivenChannelAdapter adapter;
|
||||
|
|
|
@ -58,7 +58,7 @@ public class SceneMessageHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
Map<?, ?> params = new HashMap<>();
|
||||
Map<?, ?> params;
|
||||
//匹配到topic,取消息内容
|
||||
if (topic.endsWith("_reply")) {
|
||||
ResponseMsg response = JsonUtil.parse(message, ResponseMsg.class);
|
||||
|
|
Loading…
Reference in New Issue