一、事件处理规划
1.事件规划
- 处理事件类型:CONNECT_EVENT、DISCONNECT_EVENT、ACTIVITY_EVENT、INACTIVITY_EVENT
- ThingsBoard通过规则引擎把事件路由到消息队列(RabbitMQ或Kafka、EMQX)
二、准备工作
1.RabbitMQ
队列:event-queue
2.规则引擎
2.1.创建规则链
2.2.数据整合
1
2
3
4
5
6
msg.msgType = msgType;
msg.deviceName = metadata.deviceName;
msg.deviceType = metadata.deviceType;
msg.deviceLabel = metadata.deviceLabel;
return {msg: msg, metadata: metadata, msgType: msgType};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"lastConnectTime": 1697017421141,
"lastActivityTime": 1697016501369,
"lastDisconnectTime": 1697016501383,
"lastInactivityAlarmTime": 1697017103552,
"inactivityTimeout": 600000,
"msgType": "CONNECT_EVENT",
"deviceName": "Test-event",
"deviceType": "default",
"deviceLabel": ""
}
{
"deviceLabel": "",
"deviceName": "Test-event",
"deviceType": "default",
"scope": "SERVER_SCOPE"
}
2.3.RabbitMQ
3.数据
1.CONNECT_EVENT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"lastConnectTime": 1697015317349,
"lastActivityTime": 0,
"lastDisconnectTime": 0,
"lastInactivityAlarmTime": 0,
"inactivityTimeout": 600000
}
# 元数据
{
"deviceLabel": "",
"deviceName": "Test-event",
"deviceType": "default",
"scope": "SERVER_SCOPE"
}
2.DISCONNECT_EVENT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"active": true,
"lastConnectTime": 1697015317349,
"lastActivityTime": 1697015317350,
"lastDisconnectTime": 1697015348610,
"lastInactivityAlarmTime": 0,
"inactivityTimeout": 600000
}
# 元数据
{
"deviceLabel": "",
"deviceName": "Test-event",
"deviceType": "default",
"scope": "SERVER_SCOPE"
}
3.ACTIVITY_EVENT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"active": true,
"lastConnectTime": 1697015317349,
"lastActivityTime": 1697015317350,
"lastDisconnectTime": 0,
"lastInactivityAlarmTime": 0,
"inactivityTimeout": 600000
}
# 元数据
{
"deviceLabel": "",
"deviceName": "Test-event",
"deviceType": "default",
"scope": "SERVER_SCOPE"
}
4.INACTIVITY_EVENT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"active": false,
"lastConnectTime": 1697016478869,
"lastActivityTime": 1697016501369,
"lastDisconnectTime": 1697016501383,
"lastInactivityAlarmTime": 1697017103552,
"inactivityTimeout": 600000
}
# 元数据
{
"deviceLabel": "",
"deviceName": "Test-event",
"deviceType": "default",
"scope": "SERVER_SCOPE"
}
4.服务端属性
5.MQTTX
三、事件处理
1.创建设备
2.创建规则引擎
3.配置RabbitMQ
4.测试
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
{
"lastConnectTime": 1697016478869,
"lastActivityTime": 1697016109278,
"lastDisconnectTime": 1697016109289,
"lastInactivityAlarmTime": 0,
"inactivityTimeout": 600000,
"msgType": "CONNECT_EVENT",
"deviceName": "Test-event",
"deviceType": "default",
"deviceLabel": ""
}
{
"active": true,
"lastConnectTime": 1697016478869,
"lastActivityTime": 1697016478870,
"lastDisconnectTime": 1697016501383,
"lastInactivityAlarmTime": 0,
"inactivityTimeout": 600000,
"msgType": "DISCONNECT_EVENT",
"deviceName": "Test-event",
"deviceType": "default",
"deviceLabel": ""
}
{
"active": true,
"lastConnectTime": 1697017421141,
"lastActivityTime": 1697017421142,
"lastDisconnectTime": 1697016501383,
"lastInactivityAlarmTime": 1697017103552,
"inactivityTimeout": 600000,
"msgType": "ACTIVITY_EVENT",
"deviceName": "Test-event",
"deviceType": "default",
"deviceLabel": ""
}
{
"active": false,
"lastConnectTime": 1697016478869,
"lastActivityTime": 1697016501369,
"lastDisconnectTime": 1697016501383,
"lastInactivityAlarmTime": 1697017103552,
"inactivityTimeout": 600000,
"msgType": "INACTIVITY_EVENT",
"deviceName": "Test-event",
"deviceType": "default",
"deviceLabel": ""
}