-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathx10.js
346 lines (317 loc) · 8.12 KB
/
x10.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"use strict";
/**
* X10 protocol objects
* @author Keith W. Shaw
*/
/**
* Module Dependencies
*/
var _ = require("underscore");
// The housecodes and device codes range from A to P and 1 to 16 respectively although they do not follow a binary sequence. The encoding format for these codes is as follows:
var deviceCodes = [0x6, 0xE, 0x2, 0xA, 0x1, 0x9, 0x5, 0xD, 0x7, 0xF, 0x3, 0xB, 0x0, 0x8, 0x4, 0xC];
/**
* object to convert X-10 house codes from numbers to strings and back
*/
function HouseCode(houseCode) {
this.raw = null;
switch (typeof houseCode) {
case "string":
if (houseCode !== "" && houseCode.length === 1) {
var index = houseCode.charCodeAt(0) - 65;
if ((index >= 0) && (index <= (deviceCodes.length - 1))) {
this.raw = deviceCodes[index];
} else {
throw new Error("House code string out of range.");
}
} else {
throw new Error("Invalid house code.");
}
break;
default:
if (deviceCodes.indexOf(houseCode) !== -1) {
this.raw = houseCode;
} else {
throw new Error("House code number out of range.");
}
break;
}
return this.raw;
}
HouseCode.prototype.toString = function(base) {
if (base !== undefined && base !== null) {
return this.raw.toString(base);
} else {
return String.fromCharCode(deviceCodes.indexOf(this.raw) + 65);
}
};
HouseCode.prototype.valueOf = function() {
return this.raw;
};
/**
* object to convert X-10 unit codes from numbers to strings and back
*/
function UnitCode(unitCode) {
switch (typeof unitCode) {
case "string":
unitCode = parseInt(unitCode, 10);
if (unitCode || unitCode === 0) {
var index = unitCode - 1;
if ((index >= 0) && (index <= (deviceCodes.length - 1))) {
this.raw = deviceCodes[index];
} else {
throw new Error("Unit code string out of range.");
}
} else {
throw new Error("Invalid unit code.");
}
break;
default:
if (deviceCodes.indexOf(unitCode) !== -1) {
this.raw = unitCode;
} else {
throw new Error("Unit code number out of range.");
}
break;
}
return this.raw;
}
UnitCode.prototype.toString = function(base) {
if (base !== undefined && base !== null) {
return this.raw.toString(base);
} else {
// always return two characters by padding with zero
return ("0" + (deviceCodes.indexOf(this.raw) + 1)).substr(-2, 2);
}
};
UnitCode.prototype.valueOf = function() {
return this.raw;
};
/**
* object to convert X-10 addresses from numbers to strings and back
*/
function Address(address) {
this.houseCode = null;
this.unitCode = null;
try {
switch (typeof address) {
case "object":
if (_.isArray(address) && address.length === 2) {
this.houseCode = new HouseCode(address[0]);
this.unitCode = new UnitCode(address[1]);
} else {
if (address.houseCode !== undefined && address.houseCode !== null) {
this.houseCode = new HouseCode(address.houseCode);
}
if (address.unitCode !== undefined && address.unitCode !== null) {
this.unitCode = new UnitCode(address.unitCode);
}
}
break;
case "string":
try {
address = JSON.parse(address);
if (address.hasOwnProperty("houseCode") && address.houseCode !== null) {
this.houseCode = new HouseCode(address.houseCode);
}
if (address.hasOwnProperty("unitCode") && address.unitCode !== null) {
this.unitCode = new UnitCode(address.unitCode);
}
} catch (e) {
this.houseCode = new HouseCode((address.toUpperCase().match(/[A-P]+/)||[null])[0]);
this.unitCode = new UnitCode((address.match(/\d+/)||[null])[0]);
}
break;
case "number":
this.houseCode = new HouseCode(address>>>4);
this.unitCode = new UnitCode(address&0x0F);
break;
default:
throw new Error("Invalid address.");
break;
}
} catch (e) {
throw new Error('Address out of range.');
}
}
Address.prototype.toString = function(base) {
if (base !== undefined && base !== null) {
return this.toNumber().toString(base);
} else {
return this.houseCode.toString() + this.unitCode.toString().replace(/^0/, "");
}
};
Address.prototype.toNumber = function() {
return this.houseCode << 4 | this.unitCode;
};
Address.prototype.toArray = function() {
return [ this.houseCode.valueOf(), this.unitCode.valueOf() ];
};
Address.prototype.toObject = function() {
return { "houseCode": this.houseCode.valueOf(), "unitCode": this.unitCode.valueOf() };
};
Address.prototype.toJSON = function(base) {
return JSON.stringify(
{ "houseCode": this.houseCode.toString(base), "unitCode": this.unitCode.toString(base) }
);
};
Address.prototype.valueOf = function () {
return this.toNumber();
};
var functionCodes = [
{
"code": 0,
"func": "allUnitsOff",
"function": "All units off",
"description": "Switch off all devices with the house code indicated in the message",
"bidirectional": false
},
{
"code": 1,
"func": "allLightsOn",
"function": "All lights on",
"description": "Switches on all lighting devices (with the ability to control brightness)",
"bidirectional": false
},
{
"code": 2,
"func": "on",
"function": "On",
"description": "Switches on a device",
"bidirectional": false
},
{
"code": 3,
"func": "off",
"function": "Off",
"description": "Switches off a device",
"bidirectional": false
},
{
"code": 4,
"func": "dim",
"function": "Dim",
"description": "Reduces the light intensity",
"bidirectional": false
},
{
"code": 5,
"func": "bright",
"function": "Bright",
"description": "Increases the light intensity",
"bidirectional": false
},
{
"code": 6,
"func": "allLightsOff",
"function": "All lights off",
"description": "Switches off all lighting devices",
"bidirectional": false
},
{
"code": 7,
"func": "extendedCode",
"function": "Extended code",
"description": "Extension code",
"bidirectional": true
},
{
"code": 8,
"func": "hailRequest",
"function": "Hail request",
"description": "Requests a response from the device(s) with the house code indicated in the message",
"bidirectional": true
},
{
"code": 9,
"func": "Hail acknowledge",
"function": "hailAcknowledge",
"description": "Response to the previous command",
"bidirectional": true
},
{
"code": 10,
"funct": "preSetDim1",
"function": "Pre-set dim 1",
"description": "Allows the selection of two predefined levels of light intensity",
"bidirectional": true
},
{
"code": 11,
"func": "preSetDim2",
"function": "Pre-set dim 2",
"description": "Allows the selection of two predefined levels of light intensity",
"bidirectional": true
},
{
"code": 12,
"func": "extendedDataTransfer",
"function": "Extended Data Transfer",
"description": "For meter read & DSM",
"bidirectional": true
},
{
"code": 13,
"func": "statusIsOn",
"function": "Status is on",
"description": "Response to the Status Request indicating that the device is switched on",
"bidirectional": true
},
{
"code": 14,
"func": "statusIsOff",
"function": "Status is off",
"description": "Response indicating that the device is switched off",
"bidirectional": true
},
{
"code": 15,
"func": "statusRequest",
"function": "Status request",
"description": "Request requiring the status of a device",
"bidirectional": true
}
];
var funcCodes = _.pluck(functionCodes, "func");
function Command(command) {
this.functionCode = null;
switch (typeof command) {
case "object":
if (command instanceof Array) {
this.functionCode = command[0];
} else {
if (command.functionCode !== undefined && command.functionCode !== null) {
this.functionCode = command.functionCode;
}
}
break;
case "number":
this.functionCode = command;
break;
case "string":
try {
command = JSON.parse(command);
if (command.hasOwnProperty("functionCode")) {
this.functionCode = command.functionCode;
}
} catch (e) {
this.functionCode = funcCodes.indexOf(command);
}
break;
}
if (this.functionCode !== null) {
if (typeof this.functionCode === "string") {
if (this.functionCode !== "") {
this.functionCode = funcCodes.indexOf(this.functionCode);
}
}
}
}
Command.prototype.toObject = function () {
return { "functionCode": this.functionCode };
};
module.exports = {
HouseCode: HouseCode,
UnitCode: UnitCode,
Address: Address,
Command: Command
};