forked from aallan/node-owlintuition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owl.js
222 lines (176 loc) · 6.18 KB
/
owl.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
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var dgram = require('dgram');
var xml = require('xml2json');
var OWL = function( ) {
EventEmitter.call(this);
var multicastsocket = null;
var udpkey = null;
var owlsocket = null;
var OWL_HOST = null;
var OWL_PORT = null;
}
util.inherits(OWL, EventEmitter);
OWL.prototype.configure = function ( ip, key ) {
var self = this;
self.OWL_HOST = ip;
self.OWL_PORT = 5100;
self.udpkey = key;
self.owlsocket = dgram.createSocket('udp4');
self.owlsocket.on("listening", function() {
});
self.owlsocket.on("message", function (message, remote) {
//console.log("The packet came back: " + message + "," + util.inspect(remote, {"depth": null}));
var string = message.toString('utf-8');
string = string.trim();
var array = string.split(',');
var status = array.shift();
var result = null;
if ( status === "OK") {
result = { 'status':status,'result':string.substring(3) };
} else if ( status === "ERROR" ) {
result = { 'status':status,'result':string.substring(6) };
}
self.emit( 'control', result );
});
}
// Control ---------------------------------------------------------------------------
// version()
// Retrieves the version information of the device.
OWL.prototype.version = function ( ) {
var self = this;
var message = new Buffer("GET,VERSION,"+self.udpkey);
self.owlsocket.send( message, 0, message.length, self.OWL_PORT, self.OWL_HOST, function(err, bytes) {
if( err ) {
self.emit( 'error', err );
}
})
}
// uptime()
// Retrieves the run time of the device in days, hours, minutes and seconds.
OWL.prototype.uptime = function ( ) {
var self = this;
var message = new Buffer("GET,UPTIME,"+self.udpkey);
self.owlsocket.send( message, 0, message.length, self.OWL_PORT, self.OWL_HOST, function(err, bytes) {
if( err ) {
self.emit( 'error', err );
}
})
}
// mac()
// Returns the MAC ID of this network owl.
OWL.prototype.mac = function ( ) {
var self = this;
var message = new Buffer("GET,MAC,"+self.udpkey);
self.owlsocket.send( message, 0, message.length, self.OWL_PORT, self.OWL_HOST, function(err, bytes) {
if( err ) {
self.emit( 'error', err );
}
})
}
// device()
// Manages internal device list. Allows user to add, delete and view device details.
OWL.prototype.device = function ( ) {
var self = this;
var message = new Buffer("GET,DEVICE,ALL,"+self.udpkey);
self.owlsocket.send( message, 0, message.length, self.OWL_PORT, self.OWL_HOST, function(err, bytes) {
if( err ) {
self.emit( 'error', err );
}
})
}
// boost()
// Boosts the heating temperature. Toggles the boost on/off.
OWL.prototype.boost = function ( setting ) {
var self = this;
var string = setting;
if ( string === "ON" || string === "OFF ") {
var message = new Buffer("BOOST,"+string+","+self.udpkey);
console.log( "buffer = " + message );
self.owlsocket.send( message, 0, message.length, self.OWL_PORT, self.OWL_HOST, function(err, bytes) {
if( err ) {
self.emit( 'error', err );
}
})
} else {
self.emit( 'error', new Error("Unknown command "+string+" sent to BOOST command" ));
}
}
// Monitoring ---------------------------------------------------------------------------
OWL.prototype.monitor = function ( ) {
var LOCAL_BROADCAST_HOST = '224.192.32.19';
var LOCAL_BROADCAST_PORT = 22600;
var self = this;
self.multicastsocket = dgram.createSocket('udp4');
self.multicastsocket.bind(LOCAL_BROADCAST_PORT, function() {
self.multicastsocket.addMembership(LOCAL_BROADCAST_HOST);
});
self.multicastsocket.on("listening", function() {
self.emit('connect');
});
self.multicastsocket.on("message", function(msg, rinfo) {
var json = xml.toJson( msg );
var buff = JSON.parse( json );
//console.log( util.inspect( buff , {depth: null}));
if ( buff.electricity ) {
var channel0, channel1, channel2 = null;
buff.electricity.chan.forEach(function(entry) {
if( entry.id === 0 ) {
channel0 = [{'current':entry.curr.$t,'units':entry.curr.units},
{'day':entry.day.$t,'units':entry.day.units}];
}
if( entry.id === 1 ) {
channel1 = [{'current':entry.curr.$t,'units':entry.curr.units},
{'day':entry.day.$t,'units':entry.day.units}];
}
if( entry.id === 2 ) {
channel2 = [{'current':entry.curr.$t,'units':entry.curr.units},
{'day':entry.day.$t,'units':entry.day.units}];
}
});
var electricity = { 'id':buff.electricity.id,
'signal':buff.electricity.signal,
'battery':buff.electricity.battery.level,
'channels':{'0':channel0,'1':channel1,'2':channel2} };
self.emit( 'electricity', JSON.stringify(electricity) );
} else if ( buff.heating ) {
if(buff.heating.zones && buff.heating.zones.zone)
{
var heating = { 'id':buff.heating.id,
'signal':buff.heating.zones.zone.signal,
'battery':buff.heating.zones.zone.battery.level,
'temperature':buff.heating.zones.zone.temperature };
self.emit( 'heating', JSON.stringify(heating) );
}
else
{
var heating = { 'id':buff.heating.id,
'signal':buff.heating.signal,
'battery':buff.heating.battery.level,
'temperature':buff.heating.temperature };
self.emit( 'heating', JSON.stringify(heating) );
}
} else if ( buff.weather ) {
self.emit( 'weather', JSON.stringify(buff.weather) );
} else if ( buff.solar ) {
var solar = { 'id':buff.solar.id,
'current':[{'generating':buff.solar.current.generating.$t,'units':buff.solar.current.generating.units},
{'exporting':buff.solar.current.exporting.$t, 'units':buff.solar.current.exporting.units}],
'day':[{'generated':buff.solar.day.generated.$t,'units':buff.solar.day.generated.units},
{'exported':buff.solar.day.exported.$t, 'units':buff.solar.day.exported.units}] };
self.emit( 'solar', JSON.stringify(solar) );
} else {
self.emit( 'error', new Error("Unknown message of received. XML = " + msg + ", JSON = " + json ));
}
});
}
OWL.prototype.stop = function ( ) {
if( this.multicastsocket ) {
this.multicastsocket.close();
}
if( this.owlsocket ) {
this.owlsocket.close();
}
this.emit('disconnect');
}
module.exports = OWL;