设备接入
# 1 设备接入流程

设备与用户的绑定流程
说明:
1.设备连接到 iot 平台后,会自动分配到 did 与 token
2.业务应用的用户,可以通过设备端拿到 did 与 token
3.业务应用的用户,通过 token 来绑定 did 与用户在 iot 平台的归属关系,进而得到该设备的设备控制权限。
# 2 websocket 设备接入
# 2.1 建立产品&物模型实例
# 2.1.1 建立网关产品

创建一个测试网关产品

# 2.1.2 建立网关模型实例

完善物模型定义

最终得到网关产品物模型实例 json,如下:
{
"description": {
"en-US": "gateway",
"zh-TW": "",
"zh-CN": "网关"
},
"services": [{
"iid": 1,
"description": {
"en-US": "device information",
"zh-TW": "",
"zh-CN": "设备信息"
},
"type": "urn:lingdong-spec:service:device-information:00000001:aciga:test-gateway-009:1",
"properties": [{
"access": ["read"],
"iid": 1,
"format": "string",
"description": {
"en-US": "manufacturer",
"zh-TW": "",
"zh-CN": "厂商"
},
"type": "urn:lingdong-spec:property:manufacturer:00000001:aciga:test-gateway-009:1"
}, {
"access": ["read"],
"iid": 2,
"format": "string",
"description": {
"en-US": "model",
"zh-TW": "",
"zh-CN": "型号"
},
"type": "urn:lingdong-spec:property:model:00000002:aciga:test-gateway-009:1"
}, {
"access": ["read", "write"],
"iid": 3,
"format": "string",
"description": {
"en-US": "name",
"zh-TW": "",
"zh-CN": "名称"
},
"type": "urn:lingdong-spec:property:name:00000003:aciga:test-gateway-009:1"
}, {
"access": ["read"],
"iid": 4,
"format": "string",
"description": {
"en-US": "current firmware version",
"zh-TW": "",
"zh-CN": "当前固件版本"
},
"type": "urn:lingdong-spec:property:current-firmware-version:00000004:aciga:test-gateway-009:1"
}, {
"access": ["read"],
"iid": 5,
"format": "string",
"description": {
"en-US": "hardware version",
"zh-TW": "",
"zh-CN": "硬件版本"
},
"type": "urn:lingdong-spec:property:hardware-version:00000005:aciga:test-gateway-009:1"
}, {
"access": ["read"],
"iid": 6,
"format": "string",
"description": {
"en-US": "serial number",
"zh-TW": "",
"zh-CN": "序列号"
},
"type": "urn:lingdong-spec:property:serial-number:00000006:aciga:test-gateway-009:1"
}]
}],
"type": "urn:lingdong-spec:device:gateway:00000003:aciga:test-gateway-009:1"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 2.1.3 建立子设备产品
创建一个一键开关产品、一个二键开关产品

完善产品信息


# 2.1.4 建立子设备物模型实例
完善定义一键开关、二键开关的物模型实例
一键开关物模型实例:

物模型 json 实例,如下:
{
"description": {
"en-US": "One-Key Switch",
"zh-CN": "一键开关"
},
"services": [{
"iid": 1,
"description": {
"en-US": "Device Information",
"zh-CN": "设备信息"
},
"type": "urn:aciga-spec:service:device-information:00000001:aciga:test-switch-009:1",
"properties": [{
"access": ["read", "notify"],
"iid": 1,
"format": "string",
"description": {
"en-US": "Firmware Revision",
"zh-CN": "固件版本"
},
"type": "urn:aciga-spec:property:firmware-revision:00000006:aciga:test-switch-009:1"
}, {
"access": ["read", "notify"],
"iid": 2,
"format": "string",
"description": {
"en-US": "Manufacturer",
"zh-CN": "厂商"
},
"type": "urn:aciga-spec:property:manufacturer:00000fa3:aciga:test-switch-009:1"
}, {
"access": ["read", "notify"],
"iid": 3,
"format": "string",
"description": {
"en-US": "Hardware Revision",
"zh-CN": "硬件版本"
},
"type": "urn:aciga-spec:property:hardware-revision:00000fa2:aciga:test-switch-009:1"
}]
}, {
"iid": 2,
"description": {
"en-US": "Heartbeat",
"zh-CN": "心跳"
},
"type": "urn:aciga-spec:service:heartbeat:000007d1:aciga:test-switch-009:1",
"properties": [{
"unit": "seconds",
"access": ["read", "notify"],
"iid": 1,
"format": "uint16",
"description": {
"en-US": "Heartbeat Intertval",
"zh-CN": "心跳间隔"
},
"value-range": [1, 86400, 1],
"type": "urn:aciga-spec:property:heartbeat-intertval:00000fca:aciga:test-switch-009:1"
}, {
"access": ["read", "notify"],
"iid": 2,
"format": "uint16",
"description": {
"en-US": "Heartbeat Timeout",
"zh-CN": "心跳超时次数"
},
"value-range": [1, 1000, 1],
"type": "urn:aciga-spec:property:heartbeat-timeout:00000fcb:aciga:test-switch-009:1"
}, {
"unit": "none",
"access": ["notify"],
"iid": 3,
"format": "int32",
"description": {
"en-US": "heart-time",
"zh-TW": "",
"zh-CN": "心跳最后上报时间"
},
"type": "urn:aciga-spec:property:heart-time:00014e36:aciga:test-switch-009:1"
}],
"events": [{
"iid": 1,
"description": {
"en-US": "device-off-line",
"zh-TW": "",
"zh-CN": "设备离线事件"
},
"arguments": [{
"piid": 3,
"repeat": [1, 1]
}],
"type": "urn:aciga-spec:event:device-off-line:00004805:aciga:test-switch-009:1"
}, {
"iid": 2,
"description": {
"en-US": "device-online",
"zh-TW": "",
"zh-CN": "设备上线"
},
"type": "urn:aciga-spec:event:device-online:000122de:aciga:test-switch-009:1"
}]
}, {
"iid": 7,
"description": {
"en-US": "Switch Status",
"zh-CN": "开关状态"
},
"type": "urn:aciga-spec:service:switch:00000002:aciga:test-switch-009:1",
"actions": [{
"iid": 1,
"description": {
"en-US": "Toggle",
"zh-CN": "状态切换"
},
"type": "urn:aciga-spec:action:toggle:000007d2:aciga:test-switch-009:1"
}],
"properties": [{
"access": ["read", "write", "notify"],
"iid": 1,
"format": "bool",
"description": {
"en-US": "on",
"zh-CN": "开关"
},
"type": "urn:aciga-spec:property:on:00000014:aciga:test-switch-009:1"
}, {
"access": ["read", "write"],
"iid": 2,
"format": "string",
"description": {
"en-US": "Name",
"zh-CN": "名称"
},
"type": "urn:aciga-spec:property:name:00000017:aciga:test-switch-009:1"
}]
}],
"type": "urn:aciga-spec:device:switch:0000000a:aciga:test-switch-009:1"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
二键开关物模型实例:

物模型 json 实例,如下:
{
"description": {
"en-US": "Two-Key Switch",
"zh-CN": "二键开关"
},
"services": [{
"iid": 1,
"description": {
"en-US": "Device Information",
"zh-CN": "设备信息"
},
"type": "urn:aciga-spec:service:device-information:00000001:aciga:test-switch-008:1",
"properties": [{
"access": ["read", "notify"],
"iid": 1,
"format": "string",
"description": {
"en-US": "Firmware Revision",
"zh-CN": "固件版本"
},
"type": "urn:aciga-spec:property:firmware-revision:00000006:aciga:test-switch-008:1"
}, {
"access": ["read", "notify"],
"iid": 2,
"format": "string",
"description": {
"en-US": "Manufacturer",
"zh-CN": "厂商"
},
"type": "urn:aciga-spec:property:manufacturer:00000fa3:aciga:test-switch-008:1"
}, {
"access": ["read", "notify"],
"iid": 3,
"format": "string",
"description": {
"en-US": "Hardware Revision",
"zh-CN": "硬件版本"
},
"type": "urn:aciga-spec:property:hardware-revision:00000fa2:aciga:test-switch-008:1"
}]
}, {
"iid": 2,
"description": {
"en-US": "Heartbeat",
"zh-CN": "心跳"
},
"type": "urn:aciga-spec:service:heartbeat:000007d1:aciga:test-switch-008:1",
"properties": [{
"unit": "seconds",
"access": ["read", "notify"],
"iid": 1,
"format": "uint16",
"description": {
"en-US": "Heartbeat Intertval",
"zh-CN": "心跳间隔"
},
"value-range": [1, 86400, 1],
"type": "urn:aciga-spec:property:heartbeat-intertval:00000fca:aciga:test-switch-008:1"
}, {
"access": ["read", "notify"],
"iid": 2,
"format": "uint16",
"description": {
"en-US": "Heartbeat Timeout",
"zh-CN": "心跳超时次数"
},
"value-range": [1, 1000, 1],
"type": "urn:aciga-spec:property:heartbeat-timeout:00000fcb:aciga:test-switch-008:1"
}, {
"unit": "",
"access": ["notify"],
"iid": 3,
"format": "int32",
"description": {
"en-US": "heart-time",
"zh-TW": "",
"zh-CN": "心跳最后上报时间"
},
"type": "urn:aciga-spec:property:heart-time:0000c3b0:aciga:test-switch-008:1"
}],
"events": [{
"iid": 1,
"description": {
"en-US": "heartbeat-timeouted",
"zh-TW": "",
"zh-CN": "心跳超时事件"
},
"arguments": [{
"piid": 3,
"repeat": [1, 1]
}],
"type": "urn:aciga-spec:event:heartbeat-timeouted:0000533c:aciga:test-switch-008:1"
}]
}, {
"iid": 7,
"description": {
"en-US": "Switch Status",
"zh-CN": "开关状态"
},
"type": "urn:aciga-spec:service:switch:00000002:aciga:test-switch-008:1",
"actions": [{
"iid": 1,
"description": {
"en-US": "Toggle",
"zh-CN": "状态切换"
},
"type": "urn:aciga-spec:action:toggle:000007d2:aciga:test-switch-008:1"
}],
"properties": [{
"access": ["read", "write", "notify"],
"iid": 1,
"format": "bool",
"description": {
"en-US": "on",
"zh-CN": "开关"
},
"type": "urn:aciga-spec:property:on:00000014:aciga:test-switch-008:1"
}]
}, {
"iid": 8,
"description": {
"en-US": "Switch Status",
"zh-CN": "开关状态"
},
"type": "urn:aciga-spec:service:switch:00000002:aciga:test-switch-008:1",
"actions": [{
"iid": 1,
"description": {
"en-US": "Toggle",
"zh-CN": "状态切换"
},
"type": "urn:aciga-spec:action:toggle:000007d2:aciga:test-switch-008:1"
}],
"properties": [{
"access": ["read", "write", "notify"],
"iid": 1,
"format": "bool",
"description": {
"en-US": "on",
"zh-CN": "开关"
},
"type": "urn:aciga-spec:property:on:00000014:aciga:test-switch-008:1"
}]
}],
"type": "urn:aciga-spec:device:switch:0000000a:aciga:test-switch-008:1"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 2.2 设备模拟器验证物模型
说明:此举是为验证物模型是否符合产品需求
创建网关模拟器


调试步骤:1.连接网络,2.填写属性模拟数据;3.点击上报

# 2.3 查看网关产品密钥
这里查询到密钥在设备客户端中将会用到,测试网关连接云端需要用密钥种子 seed,这个 seed 是 ED25519 密钥算法主要输入参数。ED25519 可以产生公私密钥对,用来做 RSA 非对称加密。

这里可以得到测试网关 seed:MC40MDg3MDM4OTU3OTcwNzQ4
# 2.4 编写设备客户端代码
# 2.4.1 设备客户端代码
根据上面创建的产品物模型实例,可以得到下面的代码实例,也存在代码仓库中
package cn.aciga.space.iot.client.ws;
import cn.aciga.space.iot.client.ws.c.Constant;
import cn.aciga.space.iot.client.ws.model.SummaryModel;
import cn.aciga.space.iot.core.common.util.MD5Utils;
import cn.aciga.space.iot.core.cryption.ed25519.Ed25519Keypair;
import cn.aciga.space.iot.core.cryption.xcpcodec.XcpFrameCodecType;
import cn.aciga.space.iot.core.spec.model.definition.urn.DeviceType;
import cn.aciga.space.iot.core.spec.model.device.Device;
import cn.aciga.space.iot.core.spec.model.notice.device.DeviceNoticeType;
import cn.aciga.space.iot.core.spec.model.notice.device.impl.DeviceChildrenAdded;
import cn.aciga.space.iot.core.spec.model.notice.device.impl.DeviceChildrenRemoved;
import cn.aciga.space.iot.core.spec.model.notice.device.impl.DeviceEventOccurred;
import cn.aciga.space.iot.core.spec.model.notice.device.impl.DevicePropertiesChanged;
import cn.aciga.space.iot.core.spec.model.operation.ActionOperation;
import cn.aciga.space.iot.core.spec.model.operation.ArgumentOperation;
import cn.aciga.space.iot.core.spec.model.operation.EventOperation;
import cn.aciga.space.iot.core.spec.model.operation.PropertyOperation;
import cn.aciga.space.iot.core.spec.model.status.Status;
import cn.aciga.space.iot.core.spec.model.xid.PropertyID;
import cn.aciga.space.iot.core.stanza.model.iq.IQ;
import cn.aciga.space.iot.core.stanza.model.iq.IQQuery;
import cn.aciga.space.iot.core.stanza.model.iq.device.control.*;
import cn.aciga.space.iot.core.stanza.model.iq.device.key.GetAccessKey;
import cn.aciga.space.iot.core.stanza.model.message.Message;
import cn.aciga.space.iot.core.stanza.model.message.device.DeviceMessage;
import cn.aciga.space.iot.core.vertx.common.XcpLTSKGetter;
import cn.aciga.space.iot.core.vertx.device.client.XcpDeviceClient;
import cn.aciga.space.iot.core.vertx.device.client.XcpDeviceClientCipher;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClientOptions;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Base64;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
public class GateWayDeviceWsClient extends AbstractVerticle {
// 服务端连接公钥
private static final String SERVER_LTPK = "x4BIz+4a60fFWBF3I+v8GABZzMMgc0d32gc7Hqmv57E=";
//设备ID,这个ID实际上是test-gateway-009-0001@14776101
private static final String SERIAL_NUMBER = "test-gateway-009-0001";
//边缘网关设备类型标识
private static final String CLIENT_GATEWAY_DEVICE_TYPE = "urn:aciga-spec:device:gateway:0000012d:aciga:test-gateway-009:1";
// 网关类型设备连接密钥种子,用来生成通信的公钥与私钥,一型一秘,需要与数据库product_instance_info表定义一致
private static final String SEED = "MC40MDg3MDM4OTU3OTcwNzQ4";
// 产品ID
private static final String PRODUCT_ID = "14776101";
// 云端服务器地址
private static final String SERVER_IP = "127.0.0.1";
// 云端服务器端口号
private static final int SERVER_PORT = 9090;
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new GateWayDeviceWsClient());
}
private static class DeviceLTSKGetter implements XcpLTSKGetter {
@Override
public Ed25519Keypair getKeypair(String deviceId) {
return new Ed25519Keypair(Base64.decode(SEED));
}
@Override
public Ed25519Keypair getKeypair(DeviceType deviceType) {
return new Ed25519Keypair(Base64.decode(SEED));
}
}
private XcpDeviceClient client;
@Override
public void start() {
XcpDeviceClientCipher cipher = XcpDeviceClientCipher.create(new DeviceType(CLIENT_GATEWAY_DEVICE_TYPE), new DeviceLTSKGetter(), Base64.decode(SERVER_LTPK));
HttpClientOptions options = new HttpClientOptions();
options.setIdleTimeoutUnit(TimeUnit.SECONDS);
options.setIdleTimeout(30);
client = XcpDeviceClient.createWs("test",
vertx,
options,
PRODUCT_ID,
SERIAL_NUMBER,
CLIENT_GATEWAY_DEVICE_TYPE,
cipher,
XcpFrameCodecType.CHACHA20_POLY1305);
client.addQueryHandler(Constant.QueryTopic.INVOKE_DEVICE_METHOD,this::handleInvokeAction);
client.addQueryHandler(Constant.QueryTopic.DEL_GATEWAY_CHILDREN,this::handleRemoveDevice);
client.addQueryHandler(Constant.QueryTopic.GET_DEVICE_PROPERTIES,this::handleGetProperties);
client.addQueryHandler(Constant.QueryTopic.SET_DEVICE_PROPERTIES,this::handleSetProperties);
client.addQueryHandler(Constant.QueryTopic.GET_GATEWAY_CHILDREN,this::handleGetChildren);
client.closeHandler(this::onClose);
client.connect(SERVER_IP, SERVER_PORT, ar -> {
if (ar.succeeded()) {
log.info("did:{},Connected to a server",client.did());
vertx.setTimer(5000,t->getDeviceToken());
vertx.setTimer(10000,t->addChildDevice());
// 定时模拟上报属性
vertx.setPeriodic(50000,t->reportDeviceProperty());
// 定时模拟上报事件
vertx.setPeriodic(90000,t->reportDeviceEvent());
} else {
ar.cause().printStackTrace();
log.info("Failed to connect to a server");
}
});
}
@Override
public void stop() {
client.disconnect();
}
public void onClose(Void v){
log.info("设备断开连接:{}",client.did());
}
/**
* 设备端获取设备绑定的token
*/
public void getDeviceToken(){
GetAccessKey.Query query = new GetAccessKey.Query(UUID.randomUUID().toString());
this.client.send(query,ar ->{
IQ iq = ar.result();
GetAccessKey.Result res = (GetAccessKey.Result)iq;
String accessKey = res.key();
log.info("device token:{}", MD5Utils.md5(accessKey));
});
}
/**
* 模拟主动上报设备属性变化
*/
public void reportDeviceProperty(){
//属性变化列表
List<PropertyOperation> operations = new ArrayList<>();
for(String key:deviceShadowMap.keySet()){
PropertyID propertyID = new PropertyID(key);
PropertyOperation operation = new PropertyOperation(propertyID);
operation.value(deviceShadowMap.get(key));
operations.add(operation);
}
DevicePropertiesChanged devicePropertiesChanged = new DevicePropertiesChanged(operations);
DeviceMessage deviceMessage = new DeviceMessage(UUID.randomUUID().toString(), DeviceNoticeType.PROPERTIES_CHANGED,devicePropertiesChanged);
// 发送到云端网关服务
client.send(deviceMessage);
}
/**
* 模拟主动上报设备事件
*/
public void reportDeviceEvent(){
// 设备心跳超时异常事件,也是在物模型实例中定义
EventOperation eventOperation = new EventOperation("test_switch_008_02",2,1,null);
// 事件通过会携带属性参数
List<ArgumentOperation> argumentOperations = new ArrayList<>();
// 最后心跳时间作为事件参数
ArgumentOperation argumentOperation = new ArgumentOperation(3, Arrays.asList(System.currentTimeMillis()));
argumentOperations.add(argumentOperation);
eventOperation.arguments(argumentOperations);
DeviceEventOccurred deviceEventOccurred = new DeviceEventOccurred(eventOperation);
DeviceMessage deviceMessage = new DeviceMessage(UUID.randomUUID().toString(), DeviceNoticeType.EVENT_OCCURRED,deviceEventOccurred);
client.send(deviceMessage);
// 一键开关设备心跳超时事件
EventOperation eventOperation2 = new EventOperation("test_switch_009_01",2,1,null);
// 事件通过会携带属性参数
List<ArgumentOperation> argumentOperations2 = new ArrayList<>();
// 最后心跳时间作为事件参数
ArgumentOperation argumentOperation2 = new ArgumentOperation(3, Arrays.asList(System.currentTimeMillis()));
argumentOperations.add(argumentOperation2);
eventOperation.arguments(argumentOperations2);
DeviceEventOccurred deviceEventOccurred2 = new DeviceEventOccurred(eventOperation2);
DeviceMessage deviceMessage2 = new DeviceMessage(UUID.randomUUID().toString(), DeviceNoticeType.EVENT_OCCURRED,deviceEventOccurred2);
client.send(deviceMessage2);
}
/**
* 处理云端的删除子设备报文
* @param iqQuery
*/
public void handleRemoveDevice(IQQuery iqQuery){
RemoveChild.Query query = (RemoveChild.Query) iqQuery;
String childDeviceId = query.childId();
// doSomething(childDeviceId)
RemoveChild.Result result = query.result();
// 发送到云端网关服务
client.send(result);
}
/**
* 处理写属性报文
* @param iqQuery
*/
public void handleSetProperties(IQQuery iqQuery){
SetProperties.Query query = (SetProperties.Query) iqQuery;
List<PropertyOperation> propertyOperations = query.properties();
for (PropertyOperation o : query.properties()) {
try {
doWriteProperty(o);
o.status(Status.COMPLETED);
} catch (Exception e) {
o.status(Status.DEVICE_OFFLINE);
o.description("写属性异常");
}
}
// 发送到云端网关服务
client.send(query.result(propertyOperations));
}
/**
* 把属性写进本地设备影子中
* @param o
*/
private void doWriteProperty(PropertyOperation o) {
String did = o.did();
int sid = o.siid();
int piid = o.iid();
Object value = o.value();
// doSomething()
deviceShadowMap.put(o.pid().toString(),value);
}
/**
* 处理action报文
* @param iqQuery
*/
public void handleInvokeAction(IQQuery iqQuery){
InvokeActions.Query query = (InvokeActions.Query) iqQuery;
List<ActionOperation> actions = query.actions();
for (ActionOperation action :actions) {
List<ArgumentOperation> actionRes= doAction(action);
action.status(Status.COMPLETED);
action.out(actionRes);
}
// 发送到云端网关服务
client.send(query.result(actions));
}
/**
* 模拟调用action
* @param action
* @return
*/
private List<ArgumentOperation> doAction(ActionOperation action){
// 获取action的参数列表
Map<Integer,ArgumentOperation> actionParams = action.in();
// doSomething(actionParams);
List<ArgumentOperation> actionRes = new ArrayList<>();
ArgumentOperation argumentOperation = new ArgumentOperation(1, Arrays.asList("ok"));
actionRes.add(argumentOperation);
return actionRes;
}
/**
* 处理读取属性报文
* @param iqQuery
*/
public void handleGetProperties(IQQuery iqQuery){
GetProperties.Query query = (GetProperties.Query) iqQuery;
List<PropertyOperation> propertyOperations = query.properties();
for (PropertyOperation operation :propertyOperations){
operation.value(getPropertyByPid(operation.pid().toString()));
operation.status(Status.COMPLETED);
}
// 发送到云端网关服务
client.send(query.result(propertyOperations));
}
private Map<String,Object> deviceShadowMap = new HashMap<String,Object>(){{
// 网关设备影子
put("test-gateway-009-0001@14776101.1.1","SV1.0.1");//当前固件版本属性
put("test-gateway-009-0001@14776101.1.2","安心加");//厂商属性
put("test-gateway-009-0001@14776101.1.3","HV1.0.1");//硬件版本属性
// 一键开关设备影子
// 设备信息服务
put("test_switch_009_01.1.1","SV1.0.1");//固件版本属性
put("test_switch_009_01.1.2","aciga");//厂商属性
put("test_switch_009_01.1.3","HV1.0.1");// 硬件版本属性
//心跳服务
put("test_switch_009_01.2.1",60);// 心跳间隔属性
put("test_switch_009_01.2.2",3);// 心跳超时次数属性
//开关服务
put("test_switch_009_01.7.1",true);// 开关属性
put("test_switch_009_01.7.2","测试开关1");// 开关属性
// 二键开关设备影子
// 设备信息服务
put("test_switch_008_02.1.1","SV1.0.1");//固件版本属性
put("test_switch_008_02.1.2","aciga");//厂商属性
put("test_switch_008_02.1.3","HV1.0.1");// 硬件版本属性
//心跳服务
put("test_switch_008_02.2.1",60);// 心跳间隔属性
put("test_switch_008_02.2.2",3);// 心跳超时次数属性
//第一键开关服务
put("test_switch_008_02.7.1",true);// 开关属性
put("test_switch_008_02.7.2","测试开关1键");// 开关属性
//第二键开关服务
put("test_switch_008_02.8.1",true);// 开关属性
put("test_switch_008_02.8.2","测试开关2键");// 开关属性
}};
/**
* 模拟查询属性值
* @param pid
* @return
*/
private Object getPropertyByPid(String pid){
return deviceShadowMap.get(pid);
}
/**
* 处理获取网关子设备报文
* @param iqQuery
*/
public void handleGetChildren( IQQuery iqQuery){
GetChildren.Query query = (GetChildren.Query) iqQuery;
String parentId =query.did();
List<Device> devices = new ArrayList<>();
for(SummaryModel summaryModel:this.getDeviceByParent(parentId)){
Device device = new Device(summaryModel.getDid(),summaryModel.getType(),summaryModel.isOnline(),summaryModel.getProtocol(),parentId,null,null);
devices.add(device);
}
GetChildren.Result result = query.result(devices);
// 发送到云端网关服务
client.send(result);
}
/**
* 模拟构建子设备信息
* @param parentId
* @return
*/
List<SummaryModel> getDeviceByParent(String parentId){
List<SummaryModel> devices = new ArrayList<>();
//一个一键开关
SummaryModel summaryModel = SummaryModel.builder()
.did("test_switch_009_01")
.type("urn:aciga-spec:device:switch:0000000a:aciga:test-switch-009:1")
.protocol("ble")
.parentId(parentId)
.online(true).build();
devices.add(summaryModel);
//一个二键开关
// SummaryModel summaryModel1 = SummaryModel.builder()
// .did("test_switch_008_02")
// .type("urn:aciga-spec:device:switch:0000000a:aciga:test-switch-008:1")
// .protocol("ble")
// .parentId(parentId)
// .online(true).build();
// devices.add(summaryModel1);
return devices;
}
/**
* 网关添加了一个子设备
*/
public void addChildDevice(){
String parentId = this.client.did();
DeviceChildrenAdded deviceChildrenAdded = new DeviceChildrenAdded(parentId);
List<Device> devices = new ArrayList<>();
SummaryModel summaryModel = SummaryModel.builder()
.did("test_switch_008_02")
.type("urn:aciga-spec:device:switch:0000000a:aciga:test-switch-008:1")
.protocol("ble")
.parentId(parentId)
.online(true).build();
Device device = new Device(summaryModel.getDid(),summaryModel.getType(),summaryModel.isOnline(),summaryModel.getProtocol(),parentId,null,null);
devices.add(device);
deviceChildrenAdded.children(devices);
DeviceMessage deviceMessage = new DeviceMessage(UUID.randomUUID().toString(), DeviceNoticeType.CHILDREN_ADDED,deviceChildrenAdded);
this.client.send(deviceMessage);
}
/**
* 删除一个网关子设备
*/
public void removeChildDevice(){
DeviceChildrenRemoved deviceChildrenRemoved = new DeviceChildrenRemoved(this.client.did());
List<String> removeDidList = Arrays.asList("did1");
deviceChildrenRemoved.children(removeDidList);
DeviceMessage deviceMessage = new DeviceMessage(UUID.randomUUID().toString(), DeviceNoticeType.CHILDREN_REMOVED,deviceChildrenRemoved);
this.client.send(deviceMessage);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
其中的连接信息需要根据具体情况进行配置

正确配置后,即可启动设备客户端。

这个部分设备客户端与云端握手部分日志,代表与云端握手成功
# 2.4.2 得到设备 token

设备端会打印设备用来在云端绑定设备归属者的 token,有了这个 token 就可以实现在云端控制设备。
token:225ac8c92941e21fc66b4964e6ec8b3f
Did: test-gateway-009-0001@14776101mailto:test-gateway-009-0001@14776101
# 2.5 设备调试
# 2.5.1 通过 token 绑定设备
使用 2.4.2 得到的 did 与 token 进行设备绑定

绑定网关后,网关下的子设备会跟随网关自动绑定到当前用户下

# 2.5.2 调试设备
绑定完设备后,就可以对设备进行调试操作了
操作网关:


操作子设备一键开关:




操作子设备二键开关



上面的操作的指令都会直接传递到设备客户端中,可以通过打印的日志观察:

# 2.6 物模型实例上线
当完成设备调试后,就可以对物模型进行上线操作,物模型实例上线以后,原则上是不允许修改了
调试人员调试完成后,可以申请上线

最终由产品经理进行上线审批

