-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
93 lines (85 loc) · 1.72 KB
/
simple.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
import { fail } from 'k6';
import { setTimeout } from "k6/x/timers"
import { Client } from 'k6/x/coap';
export default function() {
// Create new client and connect.
let client;
try {
client = new Client(
"coap.golioth.io:5684",
"COAP_PSK_ID",
"COAP_PSK",
// path/to/client/crt.pem,
// path/to/client/key.pem,
);
} catch (e) {
fail(e);
}
// Verify connection.
try {
let res = client.get("/hello", 10);
console.log(String.fromCharCode(...res.body));
} catch (e) {
fail(e);
}
// Send data.
try {
let res = client.post("/.s", "application/json", '{"hello": "world"}', 10);
console.log(res.code);
} catch (e) {
fail(e);
}
// Get JSON data.
try {
let res = client.get("/.u/desired", 10);
let json = JSON.parse(String.fromCharCode(...res.body));
console.log(json.sequenceNumber);
} catch (e) {
fail(e);
}
// Start RPC observation.
try {
client.observe("/.rpc", 15, (req) => {
let json;
try {
json = JSON.parse(String.fromCharCode(...req.body));
} catch (e) {
// First message is acknowledgement of
// observation.
console.log(e);
return;
}
try {
console.log(json);
client.post("/.rpc/status", "application/json", '{"id": "' + json.id + '", "statusCode": 0, "detail":"ack"}', 10);
} catch (e) {
fail(e);
}
});
} catch (e) {
fail(e);
}
// Start OTA observation.
try {
client.observe("/.u/desired", 15, (req) => {
let json;
try {
json = JSON.parse(String.fromCharCode(...req.body));
} catch (e) {
return;
}
console.log(json);
});
} catch (e) {
fail(e);
}
// Wait for observations to complete.
setTimeout(() => {
// Close connection.
try {
client.close();
} catch (e) {
fail(e);
}
}, 20000)
}