-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
70 lines (60 loc) · 1.59 KB
/
Copy pathindex.ts
File metadata and controls
70 lines (60 loc) · 1.59 KB
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
import { Network, Device } from "@tago-io/sdk";
import * as net from "net";
const PORT = 3338;
const network = new Network({ token: "" });
function parsePayload(payload: Buffer) {
const data = [
{
variable: "serial",
value: payload.readUInt32LE(1),
},
{
variable: "timestamp",
value: payload.readUInt32LE(5),
},
{
variable: "external_supply_voltage",
value: payload.readUInt8(9) + payload.readUInt8(10) * 0.01,
},
{
variable: "supply_voltage",
value: payload.readUInt8(11) + payload.readUInt8(12) * 0.01,
},
{
variable: "battery_voltage",
value: payload.readUInt8(13) + payload.readUInt8(14) * 0.01,
},
{
variable: "temperature",
value: payload.readInt8(15) + payload.readUInt8(16) * 0.01,
},
{
variable: "gsm_level",
value: payload.readInt8(17),
},
];
return data;
}
async function dataReceived(msg: Buffer) {
const data = parsePayload(msg);
console.info("data ", data);
const serial = data.find((e) => e.variable == "serial")?.value.toString();
if (!serial) {
return console.log(`Serial not found`);
}
const token = await network.resolveToken(serial).catch(() => null);
if (!token) {
return console.log(`Token not found, serie: ${serial}`);
}
const device = new Device({ token });
device.sendData(data).then(console.log, console.log);
}
const server = net
.createServer((socket) => {
socket.on("data", dataReceived);
})
.on("error", (err) => {
throw err;
});
server.listen(PORT);
console.info("Started Server at PORT", PORT);