-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.js
147 lines (127 loc) · 3.86 KB
/
node.js
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
147
const coap = require('coap');
coap.registerOption('111', Buffer, String);
const reqParams = {
host: 'localhost',
port: 5683
};
const obsReqParams = {
observe: true,
...reqParams
};
const ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN';
const DEVICE_ID = 'coap-test-node';
const COMMAND = 'coap-test-node-command';
const COMMAND_UPDATED_STATUS = 'updated';
const NOTIFICATION = 'coap-test-node-notification';
coap.request(obsReqParams).on('response', resStream => {
let commandsSubscribed = false;
resStream.on('data', data => {
const msg = JSON.parse(data.toString());
if (msg.id) {
reqParams.headers = {
111: msg.id
};
auth(ACCESS_TOKEN);
}
if (isSuccess(msg)) {
if (msg.action === 'authenticate') {
createDevice(DEVICE_ID);
} else if (msg.action === 'device/save') {
subscribeCommandInsert(DEVICE_ID);
subscribeCommandUpdate(DEVICE_ID);
subscribeNotification(DEVICE_ID);
} else if (msg.action === 'command/subscribe' && !commandsSubscribed) {
console.log('Subscribed for commands');
commandsSubscribed = true;
sendCommand(DEVICE_ID, COMMAND);
} else if (msg.action === 'notification/subscribe') {
console.log('Subscribed for notifications');
sendNotification(DEVICE_ID, NOTIFICATION);
} else if (msg.action === 'command/insert') {
updateCommand(DEVICE_ID, msg.command.id, COMMAND_UPDATED_STATUS);
}
} else {
handleSubscription(msg);
}
});
}).end();
function isSuccess(msg) {
return msg.status === 'success';
}
function auth(accessToken) {
coap.request(reqParams).end(JSON.stringify({
action: 'authenticate',
token: accessToken
}));
}
function createDevice(deviceId) {
coap.request(reqParams).end(JSON.stringify({
action: 'device/save',
device: {
name: deviceId
},
deviceId
}));
}
function subscribeCommandInsert(deviceId) {
coap.request(reqParams).end(JSON.stringify({
action: 'command/subscribe',
deviceId
}));
}
function subscribeCommandUpdate(deviceId) {
coap.request(reqParams).end(JSON.stringify({
action: 'command/subscribe',
returnUpdatedCommands: true,
deviceId
}));
}
function subscribeNotification(deviceId) {
coap.request(reqParams).end(JSON.stringify({
action: 'notification/subscribe',
deviceId
}));
}
function sendCommand(deviceId, commandName) {
coap.request(reqParams).end(JSON.stringify({
action: 'command/insert',
deviceId,
command: {
command: commandName
}
}));
}
function updateCommand(deviceId, commandId, commandStatus) {
coap.request(reqParams).end(JSON.stringify({
action: 'command/update',
deviceId,
commandId,
command: {
status: commandStatus
}
}));
}
function sendNotification(deviceId, notificationName) {
coap.request(reqParams).end(JSON.stringify({
action: 'notification/insert',
deviceId,
notification: {
notification: notificationName
}
}));
}
function handleSubscription(msg) {
if (typeof msg.status === 'undefined') {
if (msg.action === 'command/insert') {
console.log('---COMMAND-INSERTED---');
console.log(msg.command);
} else if (msg.action === 'command/update') {
console.log('---COMMAND-UPDATED---');
console.log(msg.command);
} else if (msg.action === 'notification/insert') {
console.log('---NOTIFICATION---');
console.log(msg.notification);
}
console.log('\n');
}
}