-
Notifications
You must be signed in to change notification settings - Fork 2
/
cbus.js
329 lines (300 loc) · 15.4 KB
/
cbus.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
// [baseJoin56],[baseJoin202],[bPercent],[selectedSystem],[broadcastSystem],[actionselectors]
// version 2.0
// ***SCRIPT STARTS HERE***
/*======================================================================\
| CommandFusion CBus Control JS Script |
|-----------------------------------------------------------------------|
| WARNING! Any changes made to this script WILL be overwritten whenever |
| new commands are generated. |
\======================================================================*/
var cbus = {
/*======================================================================\
| namespace vars |
\======================================================================*/
baseJoin56: [baseJoin56],
baseJoin202: [baseJoin202],
bPercent: [bPercent],
/*======================================================================\
| action selectors |
\======================================================================*/
[actionselectors]
/*======================================================================\
| utility functions |
\======================================================================*/
levS: function (lev255) { return this.bPercent ? Math.ceil(lev255 / 2.55) + "%" : lev255; },
levD: function (lev) { return lev == 0 ? 0 : 1; },
hex2dec: function (hex) { parseInt(hex, 16); },
dec2hexPair: function (dec) {
var sOut = parseInt(dec).toString(16);
if (sOut.length < 2) { sOut = "0" + sOut; }
return sOut.toString().toUpperCase();
},
/*======================================================================\
| setup: Initialise script |
\======================================================================*/
setup: function () {
CF.watch(CF.FeedbackMatchedEvent, "[selectedSystem]", "cbus network traffic", cbus.networkTraffic);
CF.watch(CF.FeedbackMatchedEvent, "[broadcastSystem]", "cbus broadcast traffic", cbus.broadcastTraffic);
},
/*======================================================================\
| ramp: Get join, and send commands, update other via broadcast |
\======================================================================*/
ramp: function (app, grp) {
CF.getJoin("a" + (this["baseJoin" + app] + grp), function (join, value, tokens) {
var joinIndex = parseInt(join.substr(1));
var lvl = Math.ceil(parseInt(value) / 65535 * 255);
CF.setJoins([
{ "join": "d" + joinIndex, "value": cbus.levD(lvl) },
{ "join": "s" + joinIndex, "value": cbus.levS(lvl) },
//Analog join simulated locally, so don't need to set that.
//{ "join": "a" + (this["baseJoin" + app] + grp), "value": newState * 65535 },
]);
//Send command to cbus system
var cmd = "\\05" + cbus.dec2hexPair(app) + "0002" + cbus.dec2hexPair(grp) + cbus.dec2hexPair(lvl);
CF.send("[selectedSystem]", cmd + cbus.getChecksum(cmd) + "\x0D");
//Send custom syntax to broadcast system to update any listening CF devices
CF.send("[broadcastSystem]", "cbus:" + cbus.dec2hexPair(app) + ":" + cbus.dec2hexPair(grp) + ":" + cbus.dec2hexPair(lvl) + ";");
});
},
/*======================================================================\
| toggle: Get join, and send commands, update other via broadcast |
\======================================================================*/
toggle: function (app, grp) {
CF.getJoin("a" + (this["baseJoin" + app] + grp), function (join, value, tokens) {
var cmd;
var joinIndex = parseInt(join.substr(1));
var newState = 1 - Math.ceil(parseInt(value) / 65535);
CF.setJoins([
{ "join": "d" + joinIndex, "value": newState },
{ "join": "s" + joinIndex, "value": cbus.levS(newState * 255) },
{ "join": "a" + joinIndex, "value": newState * 65535 },
]);
cmd = "\\05" + cbus.dec2hexPair(app) + "00" + cbus.dec2hexPair((newState * 120) + 1) + cbus.dec2hexPair(grp);
//Send command to cbus system
CF.send("[selectedSystem]", cmd + cbus.getChecksum(cmd) + "\x0D");
//Send custom syntax to broadcast system to update any listening CF devices
CF.send("[broadcastSystem]", "cbus:" + cbus.dec2hexPair(app) + ":" + cbus.dec2hexPair(grp) + ":" + cbus.dec2hexPair(newState * 255) + ";");
});
},
/*======================================================================\
| scene: Set digital join for selected scene, clear any set joins for |
| other action selectors (scenes) in this trigger group. |
\======================================================================*/
scene: function (app, group, scene) {
var tg = this["triggerGroup" + group];
for (var as in tg) {
var on = 0;
if (tg[as] == scene) { on = 1; }
CF.setJoin("d" + (this["baseJoin" + app] + group) + cbus.alz(tg[as],3), on);
}
//Send command to cbus system
var cmd = "\\05" + cbus.dec2hexPair(app) + "0002" + cbus.dec2hexPair(group) + cbus.dec2hexPair(scene);
CF.send("[selectedSystem]", cmd + cbus.getChecksum(cmd) + "\x0D");
//Send custom syntax to broadcast system to update any listening CF devices
CF.send("[broadcastSystem]", "cbus:" + cbus.dec2hexPair(app) + ":" + cbus.dec2hexPair(group) + ":" + cbus.dec2hexPair(scene) + ";");
},
/*======================================================================\
| networkTraffic: Triggered by feedback for anything (".*") sent to the |
| CBus system |
\======================================================================*/
networkTraffic: function (itemName, matchedString) { cbus.doNetworkTraffic(matchedString); },
doNetworkTraffic: function (sIn) {
sIn = sIn.replace(/(\r\n)/g, "");
var len = sIn.length - 2;
if (sIn.substr(len).toUpperCase() == cbus.getChecksum(sIn.substr(0, len))) {
sIn = sIn.substr(0, len);
if (!(sIn.match("86FEFE00F[79]07") == null)) {
cbus.netStatusResponse(sIn);
} else if (!(sIn.match("^05..38*") == null)) {
cbus.netMsg56(sIn);
} else if (!(sIn.match("^05..CA*") == null)) {
cbus.netMsg202(sIn);
}
} else {
CF.log("CBus Checksum Error: Expected '" + cbus.getChecksum(sIn.substr(0, len)) + "', received '" + sIn.substr(len)) + "'";
}
},
/*======================================================================\
| netMsg202: Process messages from application 202 (hex:CA) |
\======================================================================*/
netMsg202: function (sIn) {
//05FDCA0105010FB0
var grp, lvl;
var i = 2;
var unit = parseInt(sIn.substr(i, 2), 16); i += 2;
var app = parseInt(sIn.substr(i, 2), 16); i += 2;
i += (parseInt(sIn.substr(i, 2), 16) + 1) * 2;
while (i < sIn.length) {
if (sIn.substr(i, 2) == "01") {
//min
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = 0;
i += 4;
} else if (sIn.substr(i, 2) == "79") {
//max
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = 255;
i += 4;
} else if ("7A,72,6A,62,5A,52,4A,42,3A,32,2A,22,1A,12,0A,02".indexOf(sIn.substr(i, 2)) >= 0) {
//actionselector
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = parseInt(sIn.substr(i + 4, 2), 16);
i += 6;
} else if (sIn.substr(i, 2) == "09") {
//led off
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = -1;
i += 4;
} else {
//abort on anything else
i = sIn.length;
}
var tg = this["triggerGroup" + grp];
for (var as in tg) {
var on = 0;
if (tg[as] == lvl) { on = 1; }
CF.setJoin("d" + (this["baseJoin" + app] + grp) + cbus.alz(tg[as],3), on);
}
}
},
/*======================================================================\
| netMsg56: Process messages from application 56 (hex:38) |
\======================================================================*/
netMsg56: function (sIn) {
//05FD380105010FB0
var grp, lvl;
var i = 2;
var unit = parseInt(sIn.substr(i, 2), 16); i += 2;
var app = parseInt(sIn.substr(i, 2), 16); i += 2;
i += (parseInt(sIn.substr(i, 2), 16) + 1) * 2;
while (i < sIn.length) {
if (sIn.substr(i, 2) == "01") {
//off
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = 0;
i += 4;
} else if (sIn.substr(i, 2) == "79") {
//on
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = 255;
i += 4;
} else if ("7A,72,6A,62,5A,52,4A,42,3A,32,2A,22,1A,12,0A,02".indexOf(sIn.substr(i, 2)) >= 0) {
//ramp
grp = parseInt(sIn.substr(i + 2, 2), 16);
lvl = parseInt(sIn.substr(i + 4, 2), 16);
i += 6;
} else {
//abort on anything else
i = sIn.length;
}
CF.setJoins([
{ "join": "d" + (this["baseJoin" + app] + grp), "value": cbus.levD(lvl) },
{ "join": "s" + (this["baseJoin" + app] + grp), "value": cbus.levS(lvl) },
{ "join": "a" + (this["baseJoin" + app] + grp), "value": lvl * (65535 / 255) },
]);
}
},
/*======================================================================\
| netStatusResponse: Process messages received from a level mmi |
\======================================================================*/
netStatusResponse: function (sIn) {
//trim header
sIn = sIn.substr(12);
//get the application, then trim it off
var app = parseInt(sIn.substr(0, 2), 16);
sIn = sIn.substr(2);
//get first group, then trim it off the start
var grp = parseInt(sIn.substr(0, 2), 16);
sIn = sIn.substr(2);
//lookup code to decode level - kudos to Florent (fpillet)
var nibbles = "AA A9 A6 A5 9A 99 96 95 6A 69 66 65 5A 59 56 55 ";
for (var i = 0; i < sIn.length; i += 4) {
var lvl = nibbles.indexOf(sIn.substr(i, 2) + " ") / 3 + ((nibbles.indexOf(sIn.substr(i + 2, 2) + " ") / 3) << 4);
if (lvl >= 0) {
CF.setJoins([
{ "join": "d" + (this["baseJoin" + app] + grp), "value": cbus.levD(lvl) },
{ "join": "s" + (this["baseJoin" + app] + grp), "value": cbus.levS(lvl) },
{ "join": "a" + (this["baseJoin" + app] + grp), "value": lvl * (65535 / 255) },
]);
}
grp++;
}
},
/*======================================================================\
// broadcastTraffic: Triggered by feedback for anything sent to the |
// broadcast system starting with "cbus:" |
\======================================================================*/
broadcastTraffic: function (itemName, matchedString) { cbus.doBroadcastTraffic(matchedString); },
doBroadcastTraffic: function (matchedString) {
if (matchedString.substr(0, 5) == "cbus:") {
var app = parseInt(matchedString.substr(5, 2), 16);
var grp = parseInt(matchedString.substr(8, 2), 16);
var lvl = parseInt(matchedString.substr(11, 2), 16);
if ( app == 56 ) {
CF.setJoins([
{ "join": "d" + (this["baseJoin" + app] + grp), "value": cbus.levD(lvl) },
{ "join": "s" + (this["baseJoin" + app] + grp), "value": cbus.levS(lvl) },
{ "join": "a" + (this["baseJoin" + app] + grp), "value": lvl * (65535 / 255) },
]);
} else if ( app == 202 ) {
var tg = this["triggerGroup" + grp];
for (var as in tg) {
var on = 0;
if (tg[as] == lvl) { on = 1; }
CF.setJoin("d" + (this["baseJoin" + app] + grp) + cbus.alz(tg[as],3), on);
}
}
}
},
/*======================================================================\
| getChecksum: Calculates and returns checksum in uppercase as per CBus |
| serial documentation |
\======================================================================*/
getChecksum: function (sInput) {
if (sInput.substring(0, 1) == "\\") { sInput = sInput.substring(1); }
if (sInput.length % 2) { return 0; }
var i = 0;
var iSum = 0;
var iLimit = sInput.length / 2;
for (i = 0; i < iLimit; i++) {
iSum = iSum + parseInt(sInput.substr(i * 2, 2), 16);
}
iSum %= 256; // modulo 256
iSum = 256 - iSum; // 2"s complement
var sRet = cbus.alz((iSum).toString(16).toUpperCase(),2);
return sRet.substr(sRet.length-2,2);
},
/*======================================================================\
| format: Provides printf-type multi string replacemetns |
| eg. format("test{1}{0}",2,3)); would produce "test32" |
\======================================================================*/
format: function (str) {
for (i = 1; i < arguments.length; i++) {
str = str.replace("{" + (i - 1) + "}", arguments[i]);
}
return str;
},
/*======================================================================\
| alz: Adds the specified number of leading zeros and returns str |
\======================================================================*/
alz: function (str,len) {
while (str.toString().length < len){ str = "0" + str; }
return str.toString();
},
/*======================================================================\
| log: Only allow logging calls when CF is in debug mode - better |
| performance in release mode this way. |
\======================================================================*/
log: function(msg) {
if (CF.debug) {
CF.log(msg);
}
}
};
/*======================================================================\
| iViewer Entry Function |
\======================================================================*/
CF.modules.push({name:"cbus", setup:cbus.setup});
/*======================================================================\
| END OF FILE |
\======================================================================*/