-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (67 loc) · 1.85 KB
/
index.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
'use strict';
require('dotenv').config();
var readline = require('readline-sync');
const dgram = require('dgram');
const coap = require('coap');
const parse = require('coap-packet/').parse;
const ipv = 'udp' + process.env.IPV;
var server, client;
var transport = 'raw';
var observe_response;
const start = () => {
transport = process.argv[2];
switch (transport) {
case 'raw':
client = dgram.createSocket(ipv);
server = dgram.createSocket(ipv);
client.bind(process.env.D2C_PORT); // send telemetry on 41234
server.bind(process.env.C2D_PORT); //
server.on('listening', function() {
var address = server.address();
console.log(`UDP DEVICE listening on ${address.address}: ${address.port}`);
});
server.on('message', function(message, remote) {
console.log(`${remote.address}:${remote.port} - ${message}`);
});
streamData();
break;
case 'coap':
server = coap.createServer({
type: ipv,
});
server.on('request', function(req, res) {
console.log(req.headers);
if (req.headers['Observe'] === 0) {
observe_response = res;
streamData();
} else {
//sendData();
res.end('ok');
}
});
server.listen(() => {
console.log('coap server started on port 5683');
});
break;
default:
console.log('start with command line option raw or coap');
break;
}
};
const streamData = () => {
let payload = JSON.stringify({
temperature: Math.random() * (14 - 12) + 12,
});
var interval = setInterval(function() {
console.log('tick')
if (transport === 'raw') {
client.send(payload, 0, payload.length, process.env.D2C_PORT, process.env.GW_HOST, function(err, bytes) {
if (err) throw err;
console.log(`${JSON.stringify(payload)} sent to ${process.env.GW_HOST}:${process.env.D2C_PORT}`);
});
} else {
observe_response.write(payload + '\n')
}
}, process.env.TIMEOUT);
};
start();