forked from MajorDOOM/node-rfxcomdecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfxcom.js
88 lines (82 loc) · 2.35 KB
/
rfxcom.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
var SerialPort = require("serialport").SerialPort;
var EventEmitter = require('events').EventEmitter;
var Decoder = require('./rfxcomDecoder.js');
var util = require('util');
var data = [];
function rfxcom() {
var self = this;
self._cmnd = 0;
self.rfxtrxParser = function() {
return function(emitter, buffer) {
data.push.apply(data, buffer);
while (data.length >= (data[0] + 1)) {
emitter.emit("data", data.slice(0, data[0] + 1));
data = data.slice(data[0] + 1);
if (data[0]<5) {
data = [];
break;
}
}
};
};
rfxcom.prototype.reset = function(ms) {
self.emit("status","reset");
return self.sendMessage(0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
};
rfxcom.prototype.flush = function() {
self.emit("status","flush");
self.serialport.flush(function(error) {});
data = [];
};
rfxcom.prototype.getStatus = function() {
self.emit("status","status");
return self.sendMessage(0, 0, [2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
};
rfxcom.prototype.initialize = function(ms) {
self.reset();
setInterval(function() {
self.flush();
self.getStatus();
clearInterval(this);},500);
};
rfxcom.prototype.getCmndNumber = function() {
if (self._cmnd > 256) self._cmnd = 0;
return self._cmnd++;
};
rfxcom.prototype.readMessage = function(data) {
var deco=Decoder.decode(data);
self.emit("data", deco);
};
rfxcom.prototype.sendMessage = function(type, subtype, extra) {
cmdId = self.getCmndNumber();
var buffertmp = [extra.length + 3, type, subtype, cmdId];
buffertmp=buffertmp.concat(extra);
self.serialport.write(buffertmp);
return cmdId;
};
rfxcom.prototype.open = function (serialport) {
self.serialport = new SerialPort(serialport, {
baudrate: 38400,
parser: self.rfxtrxParser()
},false);
self.serialport.on("open", function() {
self.emit("open");
self.initialize();
});
self.serialport.on("data", function(data) {
self.emit("raw", data);
self.readMessage(data);
});
self.serialport.on("error", function(data) {
self.emit("error",data);
});
self.serialport.on("end", function() {
self.emit("end");
});
setInterval(function() {
self.serialport.open();
clearInterval(this);},500);
};
}
util.inherits(rfxcom, EventEmitter);
module.exports = new rfxcom();