-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRF64Adapter.js
51 lines (48 loc) · 1.51 KB
/
MTRF64Adapter.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
/**
* @copyright Pavel Tsytovich, 2019
*
* Implement MTRF64 Adapter interface
*/
"use strict"
const MTRF64Command = require('./MTRF64Command');
const RemoteControlNooliteDevice = require('./AbstractRemoteControl');
class MTRF64Adapter {
/**
* Constructor class
* @param {object} port - Serial port object for send data to serial port
* @param {function} onSend - Callback after send packet. Internal use only
* @param {function} onReceive - Callback after receive packet. Internal use only
* @param {object} parser - Serial port object for read data from serial port
*/
constructor(port,onSend, onReceive,parser) {
this.port = port;
this.onSend = onSend;
this.onReceive = onReceive;
this._parser = parser ? parser : port;
}
/**
* Send MTRF64Command to serial port
* @param {MTRF64Command} command
*/
send(command) {
if(!command || !(command instanceof MTRF64Command))
throw Error('Bad type of parameter');
this.port.write(command.buildPacket(),() => {
if(this.onSend)
this.onSend(command);
});
}
/**
* Open Adapter for receive packet. Do not use this method directly
*/
listen() {
var self = this;
this._parser.on('data',(data) => {
const command = new MTRF64Command(data);
if(self.onReceive)
self.onReceive(command);
});
}
}
MTRF64Adapter.Command = {"Bind":9}
module.exports = MTRF64Adapter;