forked from aslansky/max-control
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·277 lines (245 loc) · 8.83 KB
/
index.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
var EventEmitter = require('events').EventEmitter;
var net = require('net');
var fs = require('fs');
var util = require('util');
util.inherits(MaxCube, EventEmitter);
function padLeft(nr, n, str){
return Array(n-String(nr).length+1).join(str||'0')+nr;
}
function MaxCube(ip, port, heartbeatInterval) {
this.ip = ip;
this.port = port;
this.interval = heartbeatInterval || 20000;
this.isConnected = false;
this.busy = false;
this.callback = null;
this.dutyCycle = 0;
this.memorySlots = 0;
this.rooms = [];
this.devices = {};
this.deviceCount = 0;
this.client = new net.Socket();
this.client.on('data', this.onData.bind(this));
this.connect();
}
MaxCube.prototype.connect = function () {
if (!this.isConnected) {
this.client.connect(this.port, this.ip, function() {
//console.log('Connected');
this.isConnected = true;
this.emit('connected');
}.bind(this));
} else {
this.send('l:\r\n');
}
setTimeout(this.connect.bind(this), this.interval);
};
MaxCube.prototype.send = function (message) {
if (!this.busy) {
//console.log('Sending command: ' + message.substr(0,1));
this.busy = true;
this.client.write(message);
}
};
MaxCube.prototype.onData = function (data) {
data = data.toString('utf-8');
data = data.split('\r\n');
data.forEach(function (line) {
if (line.length > 0) {
var commandType = line.substr(0, 1);
var payload = line.substring(2);
//console.log('Data received: ' + commandType);
var dataObj = this.parseCommand(commandType, payload);
//console.log(dataObj);
}
}.bind(this));
this.busy = false;
this.emit('update', this.devices);
};
MaxCube.prototype.getDeviceType = function (deviceId) {
var type = null;
switch (deviceId) {
case 0:
type = 'Cube';
break;
case 1:
type = 'Heating Thermostat';
break;
case 2:
type = 'Heating Thermostat Plus';
break;
case 3:
type = 'Wall mounted Thermostat';
break;
case 4:
type = 'Shutter Contact';
break;
case 5:
type = 'Push Button';
break;
default:
type = 'unknown';
break;
}
return type;
};
MaxCube.prototype.parseCommand = function (type, payload) {
var data = null;
switch (type) {
case 'H':
data = this.parseCommandHello(payload);
break;
case 'M':
data = this.parseCommandMetadata(payload);
break;
case 'C':
data = this.parseCommandDeviceConfiguration(payload);
break;
case 'L':
data = this.parseCommandDeviceList(payload);
break;
case 'S':
data = this.parseCommandSendDevice(payload);
break;
default:
console.log('Unknown command type: ' + type);
break;
}
return data;
};
MaxCube.prototype.parseCommandHello = function (payload) {
var payloadArr = payload.split(",");
var dataObj = {
serial: payloadArr[0],
address: payloadArr[1],
firmware: payloadArr[2],
connectionId: payloadArr[4],
dutyCycle: parseInt(payloadArr[5], 16),
freeMemorySlots: parseInt(payloadArr[6], 16),
date: 2000 + parseInt(payloadArr[7].substr(0, 2), 16) + '-' + parseInt(payloadArr[7].substr(2, 2), 16) + '-' + parseInt(payloadArr[7].substr(4, 2), 16),
time: parseInt(payloadArr[8].substr(0, 2), 16) + ':' + parseInt(payloadArr[8].substr(2, 2), 16) ,
stateTime: payloadArr[9],
ntpCounter: payloadArr[10],
};
this.dutyCycle = dataObj.duty_cycle;
this.memorySlots = dataObj.free_memory_slots;
return dataObj;
};
MaxCube.prototype.parseCommandDeviceConfiguration = function (payload) {
var payloadArr = payload.split(",");
var decodedPayload = new Buffer(payloadArr[1], 'base64');
var address = decodedPayload.slice(1, 4).toString('hex');
var devicetype = this.getDeviceType(parseInt(decodedPayload[4].toString(10)));
if (devicetype == 'Heating Thermostat' && this.devices[address]) {
this.devices[address].comfortTemperature = parseInt(decodedPayload[18].toString(10)) / 2;
this.devices[address].ecoTemperature = parseInt(decodedPayload[19].toString(10)) / 2;
this.devices[address].maxTemperature = parseInt(decodedPayload[20].toString(10)) / 2;
this.devices[address].minTemperature = parseInt(decodedPayload[21].toString(10)) / 2;
this.devices[address].temperatureOffset = parseInt(decodedPayload[22].toString(10)) / 2;
this.devices[address].windowOpenTemperature = parseInt(decodedPayload[23].toString(10)) / 2;
return this.devices[address];
}
return null;
};
MaxCube.prototype.parseCommandMetadata = function (payload) {
var payloadArr = payload.split(",");
var decodedPayload = new Buffer(payloadArr[2], 'base64');
var room_count = parseInt(decodedPayload[2].toString(10));
var currentIndex = 3;
// parse rooms
for (var i = 0; i < room_count; i++) {
var roomData = {};
roomData.roomId = parseInt(decodedPayload[currentIndex].toString(10));
var room_length = parseInt(decodedPayload[currentIndex + 1].toString(10));
roomData.name = String.fromCharCode.apply(null, decodedPayload.slice(currentIndex + 2, currentIndex + 2 + room_length));
roomData.groupAddress = decodedPayload.slice(currentIndex + 2 + room_length, currentIndex + room_length + 5).toString('hex');
this.rooms.push(roomData);
currentIndex = currentIndex + room_length + 5;
}
if (currentIndex < decodedPayload.length) {
this.deviceCount = parseInt(decodedPayload[currentIndex].toString(10));
for (var j = 0; j < this.deviceCount; j++) {
var deviceData = {};
deviceData.type = this.getDeviceType(parseInt(decodedPayload[currentIndex + 1].toString(10)));
deviceData.address = decodedPayload.slice(currentIndex + 2, currentIndex + 5).toString('hex');
deviceData.serial = decodedPayload.slice(currentIndex + 5, currentIndex + 13).toString();
device_length = parseInt(decodedPayload[currentIndex + 15].toString(10));
deviceData.name = decodedPayload.slice(currentIndex + 16, currentIndex + 16 + device_length).toString('utf-8');
deviceData.roomId = parseInt(decodedPayload[currentIndex + 16 + device_length].toString(10));
this.devices[deviceData.address] = deviceData;
currentIndex = currentIndex + 16 + device_length;
}
}
return {
rooms: this.rooms,
devices: this.devices
};
};
MaxCube.prototype.parseCommandDeviceList = function (payload) {
var decodedPayload = new Buffer(payload, 'base64');
var currentIndex = 1;
for(var i = 1; i <= this.deviceCount; i++) {
var data = '';
var length = parseInt(decodedPayload[currentIndex - 1].toString());
var address = decodedPayload.slice(currentIndex, currentIndex + 3).toString('hex');
if (this.devices[address] && this.devices[address].type === 'Heating Thermostat') {
this.devices[address].valve = decodedPayload[currentIndex + 6];
this.devices[address].setpoint = parseInt(decodedPayload[currentIndex + 7].toString(10)) / 2;
data = padLeft(decodedPayload[currentIndex + 5].toString(2), 8);
this.devices[address].battery = parseInt(data.substr(0, 1)) ? 'low' : 'ok';
switch (data.substr(6, 2)) {
case '00': this.devices[address].mode = "auto"; break;
case '01': this.devices[address].mode = "manu"; break;
case '10': this.devices[address].mode = "vacation"; break;
case '11': this.devices[address].mode = "boost"; break;
}
}
else if (this.devices[address] && this.devices[address].type === 'Shutter Contact') {
data = padLeft(decodedPayload[currentIndex + 5].toString(2), 8);
this.devices[address].state = parseInt(data.substr(6, 1)) ? 'open' : 'closed';
this.devices[address].battery = parseInt(data.substr(0, 1)) ? 'low' : 'ok';
}
currentIndex = currentIndex + length + 1;
}
return this.devices;
};
MaxCube.prototype.parseCommandSendDevice = function (payload) {
var payloadArr = payload.split(",");
var dataObj = {
accepted: payloadArr[1] == '1',
duty_cycle: parseInt(payloadArr[0], 16),
free_memory_slots: parseInt(payloadArr[2], 16)
};
return dataObj;
};
MaxCube.prototype.setTemperature = function (rfAdress, mode, temperature) {
var reqTempHex, reqTempBinary;
if (!this.isConnected) {
//console.log('Not connected');
return;
}
// 00 = Auto weekprog (no temp is needed, just make the whole byte 00)
// 01 = Permanent
// 10 = Temporarily
var modeBin;
switch (mode) {
case 'auto':
modeBin = '00';
break;
case 'manu':
modeBin = '01';
break;
case 'boost':
modeBin = '11';
break;
default:
console.log('Unknown mode: ' + mode);
return false;
}
reqTempBinary = modeBin + ("000000" + (temperature * 2).toString(2)).substr(-6);
reqTempHex = parseInt(reqTempBinary, 2).toString(16);
var payload = new Buffer('000440000000' + rfAdress + '01' + reqTempHex, 'hex').toString('base64');
var data = 's:' + payload + '\r\n';
this.send(data);
};
module.exports = MaxCube;