-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathco2-monitor.js
316 lines (261 loc) · 6.8 KB
/
co2-monitor.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const os = require('os');
const usb = require('usb');
const EventEmitter = require('events').EventEmitter;
/**
* @class Co2Monitor
* @extends EventEmitter
*
* @fires Co2Monitor#data
* @fires Co2Monitor#error
* @fires Co2Monitor#co2
* @fires Co2Monitor#temperature
* @fires Co2Monitor#humidity
*
* @typedef {"connected", "disconnected", "co2", "temperature", "humidity", "data", "error"} eventsDef
*/
class Co2Monitor extends EventEmitter {
constructor() {
super();
this.co2Device = null;
this.co2Interface = null;
this.co2Endpoint = null;
this.idVendor = 0x04D9;
this.idProduct = 0xA052;
this.response = new Response();
this.buf = Buffer.from([
0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96
]);
process.on('SIGINT', () => {
this.disconnect(function (cb) {
if (cb) process.exit();
});
});
}
/**
* @param {eventsDef} args
*/
on(...args) {
super.on(...args);
}
connect(callback) {
this.co2Device = usb.findByIds(this.idVendor, this.idProduct);
if (!this.co2Device) {
let error = new Error('Device not found');
this.emitError(error);
return callback(error);
}
try {
this.co2Device.open();
this.co2Interface = this.co2Device.interfaces[0];
if (!this.co2Interface) {
let error = new Error('Interface not found');
this.emitError(error);
return callback(error);
}
if (os.platform() === 'linux') {
if (this.co2Interface.isKernelDriverActive()) {
this.co2Interface.detachKernelDriver();
}
}
let emitError = this.emitError.bind(this);
this.co2Device.controlTransfer(0x21, 0x09, 0x0300, 0x00, this.buf, function (error, data) {
if (error) {
emitError(error);
return callback(error);
}
});
this.co2Interface.claim();
this.co2Endpoint = this.co2Interface.endpoints[0];
/**
* @event Co2Monitor#connected
*/
this.emit("connected", this.co2Device);
return callback();
} catch (error) {
this.emitError(error);
return callback(error);
}
}
disconnect(callback) {
try {
if (!this.co2Endpoint) {
console.error("No endpoint to close");
return callback();
}
this.co2Endpoint.stopPoll(() => {
this.co2Interface.release(true, (error) => {
if (error) {
this.emitError(error);
}
if (os.platform() === 'linux') {
this.co2Interface.attachKernelDriver();
}
this.co2Device.close();
/**
* @event Co2Monitor#disconnected
*/
this.emit("disconnected");
return callback();
});
});
} catch (error) {
this.emitError(error);
return callback(error);
}
}
startTransfer(callback) {
this.co2Endpoint.transfer(8, (error) => {
if (error) {
this.emitError(error);
return callback(error);
}
this.co2Endpoint.startPoll(8, 64);
this.co2Endpoint.on('data', (data) => {
// Skip decryption for newer CO2 sensors.
if (data[4] !== 0x0d) {
data = Co2Monitor._decrypt(this.buf, data);
}
let op = data[0];
let value = data[1] << 8 | data[2];
switch (op) {
case 0x41:
// Humidity
this.response.humidity = new HumidityResponse(value);
/**
* @event Co2Monitor#humidity
* @type {HumidityResponse}
*/
this.emit("humidity", this.response.humidity);
break;
case 0x42:
// Temperature
this.response.temperature = new TemperatureResponse(value);
/**
* @event Co2Monitor#temperature
* @type {TemperatureResponse}
*/
this.emit("temperature", this.response.temperature);
break;
case 0x50:
// Co2
this.response.co2 = new Co2Response(value);
/**
* @event Co2Monitor#co2
* @type {Co2Response}
*/
this.emit("co2", this.response.co2);
break;
default:
break;
}
if (this.response.co2 !== null && this.response.temperature !== null && this.response.humidity !== null) {
/**
* @event Co2Monitor#data
* @type {Response}
*/
this.emit("data", this.response);
}
})
this.co2Endpoint.on("error", (error) => {
this.emitError(error);
return callback(error);
});
return callback();
});
}
/**
* Get current temperature in Celsius
* @returns {TemperatureResponse}
*/
get temperature() {
return this.response.temperature;
}
/**
* Get current co2 level
* @returns {Co2Response}
*/
get co2() {
return this.response.co2;
}
/**
* Get current humidity level
* @returns {HumidityResponse}
*/
get humidity() {
return this.response.humidity;
}
static _decrypt(buf, data) {
const cstate = [0x48, 0x74, 0x65, 0x6D, 0x70, 0x39, 0x39, 0x65];
const shuffle = [2, 4, 0, 7, 1, 6, 5, 3];
let i;
let phase1 = [];
for (i = 0; i < shuffle.length; i++) {
phase1[shuffle[i]] = data[i];
}
let phase2 = [];
for (i = 0; i < cstate.length; i++) {
phase2[i] = phase1[i] ^ buf[i];
}
let phase3 = [];
for (i = 0; i < cstate.length; i++) {
phase3[i] = ((phase2[i] >> 3) | (phase2[(i - 1 + 8) % 8] << 5)) & 0xff;
}
let out = [];
for (i = 0; i < cstate.length; i++) {
let ctmp = ((cstate[i] >> 4) | (cstate[i] << 4)) & 0xff;
out[i] = ((0x100 + phase3[i] - ctmp) & 0xff);
}
return out;
}
emitError(error) {
/**
* @event Co2Monitor#error
* @type {Error}
*/
this.emit("error", error);
}
}
class Response {
constructor() {
this.co2 = null;
this.temperature = null;
this.humidity = null;
}
toString() {
return JSON.stringify(this);
}
}
class TemperatureResponse {
constructor(temperature) {
this.value = parseFloat((temperature / 16.0 - 273.15).toFixed(2));
this.type = "float";
this.unit = "degree celsius";
this.symbol = "°C";
}
toString() {
return JSON.stringify(this);
}
}
class HumidityResponse {
constructor(humidity) {
this.value = parseFloat((humidity / 100).toFixed(2));
this.type = "float";
this.unit = "relative humidity";
this.symbol = "% rh";
}
toString() {
return JSON.stringify(this);
}
}
class Co2Response {
constructor(co2) {
this.value = co2;
this.type = "int";
this.unit = "parts per million";
this.symbol = "ppm";
}
toString() {
return JSON.stringify(this);
}
}
module.exports = Co2Monitor;