-
Notifications
You must be signed in to change notification settings - Fork 4
/
ComfoairStream.js
59 lines (49 loc) · 1.51 KB
/
ComfoairStream.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
'use strict';
const Duplex = require('stream').Duplex;
const { SerialPort } = require('serialport');
const ProtocolParser = require('./ProtocolParser');
const ProtocolFramer = require('./ProtocolFramer');
class ComfoairStream extends Duplex {
constructor(options, cb) {
super({
objectMode: true
});
this.port = new SerialPort({
path: options.port,
baudRate: options.baud || 9600
}, cb);
this.port.on('open', () => {
this.emit('open');
});
this.port.on('close', () => {
this.emit('close');
});
this.port.on('error', (err) => {
this.emit('error', err);
});
// set up pipe for parsing messages from comfoair
const protocolParser = new ProtocolParser({
passAcks: options.passAcks || true
});
this.parser = this.port.pipe(protocolParser);
this.parser.on('data', (chunk) => {
this.emit('data', chunk);
});
// set up pipe for framing messages to comfoair
this.framer = new ProtocolFramer();
this.framer.on('error', (err) => {
this.emit('error', err);
});
this.framer.pipe(this.port);
}
close(callback) {
this.port.close(callback);
}
_read(size) {
return this.parser.read(size);
}
_write(chunk, encoding, callback) {
return this.framer.write(chunk, encoding, callback);
}
}
module.exports = ComfoairStream;