-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.js
109 lines (85 loc) · 2.86 KB
/
report.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
const noble = require('@abandonware/noble');
const DEBUG = false;
var args = process.argv.slice(2);
if (args.length < 1) {
console.error('Usage: node report.js <IAM-T1 MAC Address> <Location>');
process.exit(1);
}
const DEVICE_MAC = args[0];
const DEVICE_LOC = args[1] || 'unspecified';
console.log(`Looking for device with address ${DEVICE_MAC}...`);
const printIt = () => {
console.log(new Date());
};
DEBUG && printIt();
DEBUG && setTimeout(printIt, 60000);
// Data parsing from: https://github.com/esphome/esphome-devices/pull/601/files
const getCO2 = (data) => {
return (data[9] << 8) | data[10];
}
const getTemp = (data) => {
const isNegative = data[4] & 0xF;
const temp = (data[5] << 8) | data[6];
if (isNegative === 1) {
return -(temp) / 10.0;
} else {
return (temp) / 10.0;
}
};
const getHumidity = (data) => {
return ((data[7] << 8) | data[8]) / 10.0;
};
const getPressure = (data) => {
return (data[11] << 8) | data[12];
};
const reportReadings = async (data) => {
console.log('CO2: ', getCO2(data));
console.log('TEMP: ', getTemp(data));
console.log('HUMIDITY: ', getHumidity(data));
console.log('PRESS', getPressure(data));
};
noble.on('stateChange', async (state) => {
if (state === 'poweredOn') {
await noble.startScanningAsync([], false);
} else {
await noble.stopScanning();
}
});
noble.on('discover', async (peripheral) => {
DEBUG && console.debug('discovered: ', peripheral.address);
if (peripheral.address.toLowerCase() === DEVICE_MAC.toLowerCase()) {
console.log(`Found device with address ${DEVICE_MAC}`);
peripheral.on('disconnect', () => {
process.exit(0);
});
try {
await noble.stopScanning();
await peripheral.connectAsync();
const services = await peripheral.discoverServicesAsync([]);
for (const service of services) {
const characteristics = await service.discoverCharacteristicsAsync([]);
for (const characteristic of characteristics) {
if (characteristic.uuid === 'ffe4') {
console.log('Listening for data from characteristice ffe4');
await characteristic.notifyAsync(true);
characteristic.on('data', (data) => {
reportReadings(data);
});
}
}
}
} catch (err) {
console.log(`Error connecting to device at ${DEVICE_MAC}`);
console.log(err);
}
}
});
process.on('SIGINT', function () {
noble.stopScanning(() => process.exit());
});
process.on('SIGQUIT', function () {
noble.stopScanning(() => process.exit());
});
process.on('SIGTERM', function () {
noble.stopScanning(() => process.exit());
});