diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 00000000..7722cb09
Binary files /dev/null and b/.DS_Store differ
diff --git a/.gitignore b/.gitignore
index b921d78e..668b97f4 100755
--- a/.gitignore
+++ b/.gitignore
@@ -23,5 +23,5 @@
hs_err_pid*
.idea
target
-.iml
-*.yml
\ No newline at end of file
+*.iml
+*.yml
diff --git a/device-server/device-api/device-api.iml b/device-server/device-api/device-api.iml
deleted file mode 100644
index d18ce86d..00000000
--- a/device-server/device-api/device-api.iml
+++ /dev/null
@@ -1,55 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/device-server/mqtt-auth/mqtt-auth.iml b/device-server/mqtt-auth/mqtt-auth.iml
deleted file mode 100755
index 10abb7d1..00000000
--- a/device-server/mqtt-auth/mqtt-auth.iml
+++ /dev/null
@@ -1,70 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/dao/ProductDao.java b/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/dao/ProductDao.java
new file mode 100755
index 00000000..f1782be6
--- /dev/null
+++ b/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/dao/ProductDao.java
@@ -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);
+ }
+}
diff --git a/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/service/DeviceService.java b/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/service/DeviceService.java
index 4dd62622..fdb563ef 100755
--- a/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/service/DeviceService.java
+++ b/device-server/mqtt-auth/src/main/java/cc/iotkit/mqttauth/service/DeviceService.java
@@ -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));
diff --git a/device-server/mqtt-auth/src/test/java/SupperUser.java b/device-server/mqtt-auth/src/test/java/SupperUser.java
index 40e2f115..83f16234 100755
--- a/device-server/mqtt-auth/src/test/java/SupperUser.java
+++ b/device-server/mqtt-auth/src/test/java/SupperUser.java
@@ -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;
diff --git a/device-server/mqtt-auth/src/test/java/SysMqttAuth.java b/device-server/mqtt-auth/src/test/java/SysMqttAuth.java
index b8ae1990..9f0a73fe 100755
--- a/device-server/mqtt-auth/src/test/java/SysMqttAuth.java
+++ b/device-server/mqtt-auth/src/test/java/SysMqttAuth.java
@@ -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 {
diff --git a/device-server/mqtt-client-simulator/.DS_Store b/device-server/mqtt-client-simulator/.DS_Store
new file mode 100644
index 00000000..a3c148ab
Binary files /dev/null and b/device-server/mqtt-client-simulator/.DS_Store differ
diff --git a/device-server/mqtt-client-simulator/pom.xml b/device-server/mqtt-client-simulator/pom.xml
new file mode 100755
index 00000000..989e1f3b
--- /dev/null
+++ b/device-server/mqtt-client-simulator/pom.xml
@@ -0,0 +1,76 @@
+
+
+
+ device-server
+ cc.iotkit
+ 0.0.1-SNAPSHOT
+
+ 4.0.0
+ mqtt客户端模拟器
+
+ mqtt-client-simulator
+
+
+
+
+ org.eclipse.paho
+ org.eclipse.paho.client.mqttv3
+
+
+
+ cc.iotkit
+ common
+
+
+
+ org.slf4j
+ slf4j-api
+
+
+
+ ch.qos.logback
+ logback-core
+
+
+
+ ch.qos.logback
+ logback-classic
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-assembly-plugin
+
+
+ package
+
+ single
+
+
+
+
+ cc.iotkit.simulator.Application
+
+
+
+ jar-with-dependencies
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/Application.java b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/Application.java
new file mode 100755
index 00000000..e6bcb418
--- /dev/null
+++ b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/Application.java
@@ -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();
+ }
+}
diff --git a/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/config/Mqtt.java b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/config/Mqtt.java
new file mode 100755
index 00000000..5167bf50
--- /dev/null
+++ b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/config/Mqtt.java
@@ -0,0 +1,7 @@
+package cc.iotkit.simulator.config;
+
+public class Mqtt {
+
+ public static String broker;
+
+}
diff --git a/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Device.java b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Device.java
new file mode 100755
index 00000000..208361a9
--- /dev/null
+++ b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Device.java
@@ -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;
+}
diff --git a/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Gateway.java b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Gateway.java
new file mode 100755
index 00000000..bded1d2c
--- /dev/null
+++ b/device-server/mqtt-client-simulator/src/main/java/cc/iotkit/simulator/service/Gateway.java
@@ -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 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 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 data;
+ }
+
+}
diff --git a/device-server/mqtt-server/mqtt-server.iml b/device-server/mqtt-server/mqtt-server.iml
deleted file mode 100644
index 0aac775c..00000000
--- a/device-server/mqtt-server/mqtt-server.iml
+++ /dev/null
@@ -1,87 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/device-server/mqtt-server/src/main/java/cc/iotkit/server/config/MqttConfig.java b/device-server/mqtt-server/src/main/java/cc/iotkit/server/config/MqttConfig.java
index bdf661ed..c984a60c 100755
--- a/device-server/mqtt-server/src/main/java/cc/iotkit/server/config/MqttConfig.java
+++ b/device-server/mqtt-server/src/main/java/cc/iotkit/server/config/MqttConfig.java
@@ -35,7 +35,7 @@ public class MqttConfig {
@Value("${mqtt.url}")
private String url;
- @Value(("${spring.profiles.active}"))
+ @Value(("${spring.profiles.active:}"))
private String env;
/**
diff --git a/device-server/mqtt-server/src/main/java/cc/iotkit/server/handler/RegisterHandler.java b/device-server/mqtt-server/src/main/java/cc/iotkit/server/handler/RegisterHandler.java
index 3825ce1e..22f79da4 100755
--- a/device-server/mqtt-server/src/main/java/cc/iotkit/server/handler/RegisterHandler.java
+++ b/device-server/mqtt-server/src/main/java/cc/iotkit/server/handler/RegisterHandler.java
@@ -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 {
+public class RegisterHandler implements MqttHandler {
@Autowired
private DeviceService deviceService;
@@ -28,10 +27,10 @@ public class RegisterHandler implements MqttHandler request) {
+ public DeviceRegister handler(String topic, DeviceInfo device, Request request) {
DeviceRegister regInfo = request.getParams();
deviceService.register(device.getDeviceId(), regInfo.getProductKey(),
regInfo.getDeviceName(), regInfo.getModel());
- return Response.empty();
+ return regInfo;
}
}
diff --git a/device-server/mqtt-server/src/main/java/cc/iotkit/server/service/DeviceService.java b/device-server/mqtt-server/src/main/java/cc/iotkit/server/service/DeviceService.java
index fb9dba21..fdfc0bea 100755
--- a/device-server/mqtt-server/src/main/java/cc/iotkit/server/service/DeviceService.java
+++ b/device-server/mqtt-server/src/main/java/cc/iotkit/server/service/DeviceService.java
@@ -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;
diff --git a/device-server/pom.xml b/device-server/pom.xml
index 8ed7b0d7..e5077655 100755
--- a/device-server/pom.xml
+++ b/device-server/pom.xml
@@ -15,6 +15,7 @@
mqtt-auth
mqtt-server
device-api
+ mqtt-client-simulator
diff --git a/manager/.DS_Store b/manager/.DS_Store
index ee771938..a3c148ab 100755
Binary files a/manager/.DS_Store and b/manager/.DS_Store differ
diff --git a/manager/src/main/java/cc/iotkit/manager/controller/ProductController.java b/manager/src/main/java/cc/iotkit/manager/controller/ProductController.java
index a02cda6f..b0ea953b 100755
--- a/manager/src/main/java/cc/iotkit/manager/controller/ProductController.java
+++ b/manager/src/main/java/cc/iotkit/manager/controller/ProductController.java
@@ -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);
diff --git a/pom.xml b/pom.xml
index 751d6275..f653e772 100755
--- a/pom.xml
+++ b/pom.xml
@@ -142,6 +142,12 @@
2.10.10
+
+ org.eclipse.paho
+ org.eclipse.paho.client.mqttv3
+ 1.2.2
+
+
cc.iotkit
model
diff --git a/rule-engine/src/main/java/cc/iotkit/ruleengine/config/RuleConfiguration.java b/rule-engine/src/main/java/cc/iotkit/ruleengine/config/RuleConfiguration.java
index d2ba6e3f..8c2048a2 100755
--- a/rule-engine/src/main/java/cc/iotkit/ruleengine/config/RuleConfiguration.java
+++ b/rule-engine/src/main/java/cc/iotkit/ruleengine/config/RuleConfiguration.java
@@ -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;
diff --git a/rule-engine/src/main/java/cc/iotkit/ruleengine/scene/SceneMessageHandler.java b/rule-engine/src/main/java/cc/iotkit/ruleengine/scene/SceneMessageHandler.java
index 85b5e48b..fb7edb5a 100755
--- a/rule-engine/src/main/java/cc/iotkit/ruleengine/scene/SceneMessageHandler.java
+++ b/rule-engine/src/main/java/cc/iotkit/ruleengine/scene/SceneMessageHandler.java
@@ -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);