forked from amg0/ALTUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
J_ALTUI_multibox.js
1018 lines (975 loc) · 37.1 KB
/
J_ALTUI_multibox.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//# sourceURL=J_ALTUI_multibox.js
// "use strict";
// http://192.168.1.16:3480/data_request?id=lr_ALTUI_Handler&command=home
// This program is free software: you can redistribute it and/or modify
// it under the condition that it is for private or home useage and
// this whole comment is reproduced in the source code file.
// Commercial utilisation is not authorized without the appropriate
// written agreement from amg0 / alexis . mermet @ gmail . com
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
var MultiBox = ( function( window, undefined ) {
var _recorder = null;
var _devicetypesDB = {};
_devicetypesDB[0] = {};
var _controllers = [
// { ip:'' , controller:null }, // no IP = primary box on which we opened the web page
// { ip:'192.168.1.5', controller:null } // no IP = primary box on which we opened the web page
];
function _controllerOf(altuiid) {
var elems = altuiid.split("-");
return { controller:parseInt(elems[0]) , id:elems[1] };
};
function _isAltuiid(devid) {
if (typeof (devid) == "number")
return false;
var elems = devid.split("-");
return (elems.length>1);
};
function _makeAltuiid(ctrlid, devid) {
return "{0}-{1}".format(ctrlid,devid);
};
function _isControllerFeature(altuiid,required_feature) {
var info = MultiBox.controllerOf(altuiid)
var c = _controllers[ info.controller ]
return (required_feature==null ) || ( $.isFunction(c.controller[required_feature])==true )
};
function _getControllers(required_feature) {
var arr = _controllers.filter( function(c) {
return (required_feature==null ) || ( $.isFunction(c.controller[required_feature])==true )
})
return $.map(arr,function(c) { return { ip:c.ip , name:c.name, box_info:c.controller.getBoxFullInfo(), controller: c.controller } });
};
function _initDB(devicetypes) {
$.extend(true,_devicetypesDB[0],devicetypes); // data received initially comes from ctrl 0
return this;
};
function _getALTUITypesDB() {
return _devicetypesDB[0];
}
function _getDeviceTypesDB(controllerid) {
var id = controllerid || 0;
return _devicetypesDB[controllerid];
}
function _addDeviceType(controllerid,devtype, obj) {
var id = controllerid || 0;
if (_devicetypesDB[id][devtype]==null) {
_devicetypesDB[id][devtype]={};
};
return $.extend(true,_devicetypesDB[id][devtype],obj);
};
function _updateDeviceTypeUPnpDB( controllerid, devtype, Dfilename ) {
var id = controllerid || 0;
if (_devicetypesDB[id][devtype]==null)
_devicetypesDB[id][devtype]={};
// only try to load if not loaded or in the process of loading it
if (_devicetypesDB[id][devtype].Dfilename == undefined) {
_devicetypesDB[id][devtype].Dfilename = Dfilename;
// get it into the cache ( or get it from the cache )
FileDB.getFileContent(id, Dfilename , function( xmlstr , jqXHR ) {
try {
var doc = jqXHR ? ((jqXHR.responseXML != undefined) ? jqXHR.responseXML : $.parseXML( xmlstr )) : $.parseXML( xmlstr );
var xml = $( doc );
var imp = xml.find("implementationFile");
_devicetypesDB[id][devtype].Ifilename= imp.text();
_devicetypesDB[id][devtype].Services = [];
var serviceIDs = xml.find("serviceId");
var Sfilenames = xml.find("SCPDURL");
xml.find("serviceId").each( function (index,value) {
// get all services files name, but do not get content, will be fetched on demand
_devicetypesDB[id][devtype].Services.push({
ServiceId : $(value).text(),
SFilename : $(Sfilenames[index]).text(),
Actions : []
});
});
}
catch(e) {
console.log("error in xml parsing, Dfile:"+Dfilename);
console.log("xmlstr"+xmlstr);
}
});
}
};
function _updateDeviceTypeUIDB(controllerid, devtype, ui_definitions) {
if (_devicetypesDB[controllerid]==null) {
_devicetypesDB[controllerid]={};
}
if (_devicetypesDB[controllerid]['json']==null) {
_devicetypesDB[controllerid]['json']={};
}
if (_devicetypesDB[controllerid][devtype]==null) {
_devicetypesDB[controllerid][devtype]={};
};
if (ui_definitions.device_json && (_devicetypesDB[controllerid]['json'][ui_definitions.device_json]==undefined)) {
_devicetypesDB[controllerid]['json'][ui_definitions.device_json]= { ui_static_data : ui_definitions }
}
_devicetypesDB[controllerid][devtype].ui_static_data = ui_definitions;
};
function _getDeviceStaticData(device) {
var elems = device.altuiid.split("-");
function _getDeviceStaticDataByType(device) {
if (_devicetypesDB[elems[0]][device.device_type] == undefined) {
AltuiDebug.debug("_getDeviceStaticData({0}) does not find static data".format(device.altuiid));
return null;
}
return _devicetypesDB[elems[0]][device.device_type].ui_static_data
}
if ((device==null)||(device.device_type==null))
return null;
if (device.device_json) {
if (_devicetypesDB[elems[0]]['json'] && _devicetypesDB[elems[0]]['json'][device.device_json]!=undefined) {
// AltuiDebug.debug("_getDeviceStaticData({0}) found static data by json:{1}".format(device.altuiid,device.device_json));
return _devicetypesDB[elems[0]]['json'][device.device_json].ui_static_data;
}
}
return _getDeviceStaticDataByType(device)
}
function _getAllEvents(name) {
return $.map( _controllers , function(o,i) {return name+"_"+i } );
};
function _initEngine(extraController,firstuserdata, maincontrollertype) {
function _AllLoaded(eventname) {
// case "on_ui_userDataLoaded":
// case "on_ui_userDataFirstLoaded":
// console.log("All loaded : ",eventname);
EventBus.publishEvent(eventname);
};
// initialize controller 0 right away, no need to wait
maincontrollertype = maincontrollertype || "V";
var newcontroller = {
ip:'',
type:maincontrollertype,
name:'Main',
controller:null
};
switch(newcontroller.type) {
case "A":
newcontroller.controller = new AltuiBox(0,''); // create the main controller
break;
case "V":
default:
newcontroller.controller = new VeraBox(0,''); // create the main controller
}
_controllers.push(newcontroller); // default controller
// add the extra controllers
if (extraController.trim().length>0) {
$.each(extraController.split(','), function(idx,ctrlinfo) {
ctrlinfo = ctrlinfo;
var splits = ctrlinfo.trim().split("-");
var newcontroller = {
ip:splits[0],
name:splits[0],
type:splits[1] || 'V',
controller:null
}
switch (newcontroller.type) {
case 'A':
newcontroller.controller = new AltuiBox(1+idx,newcontroller.ip);
break;
case 'V':
default:
newcontroller.controller = new VeraBox(1+idx,newcontroller.ip);
}
_controllers.push(newcontroller);
// init device type DB for that controller
if (_devicetypesDB[idx]==null)
_devicetypesDB[idx]={};
});
}
if (g_ALTUI.g_MachineLearning == '1') {
_controllers.push( {
ip:'',
type:"Z", // machine learning controller
name:'Machine Learning',
controller:new LearnBox(_controllers.length)
});
}
// prepare to wait for proper initialization
// EventBus.waitForAll( "on_ui_userDataFirstLoaded", _getAllEvents("on_ui_userDataFirstLoaded"), this, _AllLoaded );
EventBus.waitForAll("on_ui_userDataLoaded", _getAllEvents("on_ui_userDataLoaded"), this, _AllLoaded );
// now start the engine
$.each(_controllers, function(idx,box) {
box.controller.initEngine( (idx==0) ? firstuserdata : null ); // will raise("on_ui_userDataFirstLoaded_"+_uniqID) ("on_ui_userDataLoaded_"+_uniqID)
});
};
function _saveEngine() {
$.each(_controllers, function(idx,box) {
box.controller.saveEngine();
});
return;
};
function _clearEngine() {
$.each(_controllers, function(idx,box) {
box.controller.clearEngine();
});
return;
};
function _getUrlHead(altuiid) {
var elems = altuiid.split("-");
return _controllers[elems[0]].controller.getUrlHead();
};
function _getIpAddr(altuiid) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getIpAddr();
};
function _isUI5(controller) {
if (controller==0)
return (_devicetypesDB[0]["info"].ui7Check == "false" ); // we were told by Lua plugin
return _controllers[controller].controller.isUI5();
};
function _getDataProviders(cbfunc) {
return _controllers[0].controller.getDataProviders(cbfunc);
};
function _initializeJsonp(controller) {
return _controllers[controller].controller.initializeJsonp();
};
function _RequestBackup(controller, cbfunc) {
return _controllers[controller].controller.RequestBackup(cbfunc);
}
function _initializeSysinfo(controller) {
return _controllers[controller].controller.initializeSysinfo();
}
function _setHouseMode(newmode,cbfunc) {
return _controllers[0].controller.setHouseMode(newmode,cbfunc);
};
function _getHouseModeSwitchDelay() {
return _controllers[0].controller.getHouseModeSwitchDelay();
};
function _getRooms( func , filterfunc, endfunc) {
var dfd = $.Deferred();
var arr=[];
var answers=0;
$.each(_controllers, function( i,c) {
c.controller.getRooms(
function( idx, room) {
var index = arr.length;
arr.push(room);
if ($.isFunction(func))
(func)(index,room);
} ,
filterfunc,
function(rooms) {
answers++;
if (answers == _controllers.length) {
var result = arr.sort(altuiSortByName);
if ($.isFunction(endfunc))
(endfunc)(result);
dfd.resolve(result);
}
}
);
});
return dfd.promise();
};
function _getRoomsSync() {
var arr=[];
$.each(_controllers, function( i,c) {
arr = arr.concat(c.controller.getRoomsSync( ));
});
return arr.sort(altuiSortByName);
};
function _getRoomByID( controllerid, roomid ) {
return _controllers[controllerid].controller.getRoomByID( roomid );
};
function _getRoomByAltuiID( altuiid ) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[ elems[0] ].controller.getRoomByID( elems[1] );
};
function _getUsers(func , filterfunc, endfunc) {
var arr=[];
var answers=0;
$.each(_controllers, function( i,c) {
c.controller.getUsers(
function( idx, user) {
var index = arr.length;
arr.push(user);
if ($.isFunction(func))
(func)(index,user);
} ,
filterfunc,
function(users) {
answers++;
if (answers == _controllers.length) {
if ($.isFunction(endfunc))
(endfunc)(arr.sort(altuiSortByName2));
}
}
);
});
return _getUsersSync();
};
function _getUsersSync(controllerid) {
var arr=[];
if (controllerid!=null)
arr = arr.concat(_controllers[controllerid].controller.getUsersSync( ));
else
$.each(_controllers, function( i,c) {
arr = arr.concat(c.controller.getUsersSync( ));
});
return arr.sort(altuiSortByName2);
};
function _getUserByID(controllerid, userid) {
return _controllers[controllerid].controller.getUserByID( userid );
};
function _getMainUser() {
var usrs = _controllers[0].controller.getUsersSync();
if ( (usrs!=null) && (usrs.length>=1) )
return usrs[0];
var user = MyLocalStorage.getSettings('MainUser')
if (user==null) {
user= uuidv4(); //joint.util.uuid();
MyLocalStorage.setSettings('MainUser',user);
}
return {Name:user};
};
function _deleteRoom(room) {
var elems = room.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.deleteRoom(elems[1]);
};
function _createRoom(controllerid, name, cbfunc ) {
return _controllers[controllerid].controller.createRoom(name, cbfunc);
};
function _renameRoom(room, name) {
var elems = room.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.renameRoom(elems[1],name);
};
function _createDevice( controllerid, param , cbfunc ) {
var id = controllerid || 0;
return _controllers[id].controller.createDevice( param , function(newid) {
(cbfunc)("{0}-{1}".format(id,newid));
});
};
function _renameDevice( device, newname, roomid ) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.renameDevice( device, newname, roomid);
};
function _deleteDevice(device) {
// delete watches
$.each(["VariablesToSend","VariablesToWatch"], function(idx,whichwatch) {
$.each( MultiBox.getWatches(whichwatch,function(w) { return (w.deviceid == device.altuiid) }), function(i,watch) {
MultiBox.delWatch(watch);
});
});
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.deleteDevice(elems[1]);
};
function _getDevices( func , filterfunc, endfunc, required_feature ) {
var arr=[];
var answers = 0;
var ctrls = (required_feature==null) ? _controllers : _getControllers(required_feature) // only controllers which can create devices
$.each(ctrls, function( i,c) {
c.controller.getDevices( func , filterfunc, function(devices){
arr = arr.concat(devices);
answers++;
if ((answers == ctrls.length) && ($.isFunction(endfunc)) ){
(endfunc)( arr );
};
});
});
return arr;
};
function _getDevicesSync() {
var arr=[];
$.each(_controllers, function( i,c) {
arr = arr.concat(c.controller.getDevicesSync());
});
return arr;
};
function _getDeviceBatteryLevel(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getDeviceBatteryLevel(device);
};
function _getDeviceByAltuiID( devid ) {
var elems = devid.split("-");
return (_controllers[elems[0]]==undefined) ? null :_controllers[ elems[0] ].controller.getDeviceByID( elems[1] );
};
function _getDeviceByID( controllerid , devid ) {
return (_controllers[controllerid]==undefined) ? null : _controllers[controllerid].controller.getDeviceByID( devid );
};
function _getDeviceByAltID( controllerid, parentdevid , altid ) {
var id = controllerid || 0;
return _controllers[id].controller.getDeviceByAltID( parentdevid , altid );
};
function _getDeviceByType(controllerid,str,opt_parents_arr) {
var id = controllerid || 0;
return _controllers[id].controller.getDeviceByType(str,opt_parents_arr);
};
function _getDeviceActions(device,cbfunc) {
if (device==null) {
cbfunc([]);
return [];
}
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getDeviceActions(device,cbfunc);
};
function _getDeviceService(device,ServiceId,cbfunc) {
var dfd = $.Deferred();
MultiBox.getDeviceActions(device,function( services ) {
var found = null;
$.each(services, function(idx,service) {
if (service.ServiceId == ServiceId) {
found = service;
return false;
}
})
if ($.isFunction(cbfunc))
(cbfunc)(found);
dfd.resolve( { device:device, service:found } );
})
return dfd.promise();
};
function _getDeviceEvents(device) {
if (device==null) return [];
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getDeviceEvents(device);
};
function _getDeviceDependants(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getDeviceDependants(device);
};
function _getDeviceVariableHistory( device, varidx, cbfunc) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getDeviceVariableHistory( device, varidx, cbfunc);
};
function _delWatch( w ) {
w = $.extend({sceneid:-1, luaexpr:'true', xml:'', provider:''},w);
return _controllers['0'].controller.delWatch( w )
};
function _addWatch( w ) {
w = $.extend({sceneid:-1, luaexpr:'true', xml:'', provider:''},w);
return _controllers['0'].controller.addWatch( w )
};
function _getWatches(whichwatches,filterfunc) {
if ((whichwatches!="VariablesToWatch") && (whichwatches!="VariablesToSend"))
return null;
return _controllers['0'].controller.getWatches( whichwatches,filterfunc );
};
function _getWatchesHistory(cbfunc) {
return _controllers['0'].controller.getWatchesHistory( cbfunc );
};
function _getStatesByAltuiID(altuiid) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getStates( elems[1] );
};
function _getStateByID( altuiid, id ) {
id = parseInt(id);
var found = null;
var states = _getStatesByAltuiID(altuiid)
$.each(states, function( idx, state) {
if (state.id==id) {
found = state;
return false;
}
})
return found;
}
function _getStates( device ) {
if (device==null) return null;
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getStates( elems[1] );
};
function _getStatus( device, service, variable ) {
if (device==null) return null;
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getStatus( elems[1], service, variable );
};
function _setStatus( device, service, variable, value, dynamic ) {
if (_recorder!=null ) {
_recorder.record( {type:'variable_set', device:device.altuiid, service:service, value:value } );
return;
}
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.setStatus( elems[1], service, variable, value, dynamic );
};
function _getJobStatus( controllerid, jobid , cbfunc )
{
return (_controllers[controllerid]==undefined) ? null : _controllers[controllerid].controller.getJobStatus( jobid, cbfunc );
};
function _runAction(device, service, action, params,cbfunc) {
if (_recorder!=null ) {
_recorder.record( {type:'action', device:device.altuiid, service:service, action:action, params:params } );
return;
}
var elems = device.altuiid.split("-");
EventBus.publishEvent("on_deviceAction",device.altuiid, service, action, params)
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.runAction( elems[1], service, action, params, cbfunc )
};
function _runActionByAltuiID(altuiid, service, action, params,cbfunc) {
if (_recorder!=null ) {
_recorder.record( {type:'action', device:altuiid, service:service, action:action, params:params } );
return;
}
var elems = altuiid.split("-");
EventBus.publishEvent("on_deviceAction",altuiid, service, action, params)
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.runAction(elems[1], service, action, params,cbfunc);
// return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getUPnPHelper().UPnPAction(elems[1], service, action, params,cbfunc);
};
function _setAttr(device, attribute, value,cbfunc) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.setAttr(elems[1], attribute, value,cbfunc);
};
function _isDeviceZwave(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.isDeviceZwave(device);
};
function _isDeviceZigbee(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.isDeviceZigbee(device);
};
function _isDeviceBT(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.isDeviceBT(device);
};
function _updateNeighbors(device) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.updateNeighbors(elems[1]);
};
function _modifyDevice(device,cb) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.modifyDevice(elems[1]);
}
function _setColor(device,hex) {
var elems = device.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.setColor(elems[1],hex);
}
function _getCategories( cbfunc, filterfunc, endfunc ) {
var dfd = $.Deferred();
var arr=[];
var answers = 0;
$.each(_controllers, function( idx,c) {
var index = idx;
c.controller.getCategories(
function (idx,cat) {
var index = arr.length;
if ($.inArray(cat.name,$.map(arr,function(e){ return e.name }))==-1)
arr.push(cat);
},
filterfunc,
function (categories) {
answers++;
if (answers == _controllers.length) {
var arr2 = arr.sort(altuiSortByName);
if ($.isFunction(cbfunc))
$.each(arr2, cbfunc);
if ($.isFunction(endfunc))
(endfunc)(arr2);
dfd.resolve(categories);
}
}
);
});
return dfd.promise();
};
function _getCategoryTitle(catnum) {
return _controllers[0].controller.getCategoryTitle(catnum); //returns (found !=undefined) ? found : '';
};
function _evaluateConditions(device,conditions) {
var elems = device.altuiid.split("-");
var cat = device.category_num || 0
var subcat = device.subcategory_num || 0
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.evaluateConditions(elems[1],cat,subcat,conditions);
};
function _getWeatherSettings() {
return _controllers[0].controller.getWeatherSettings();
};
function _reloadEngine(controllerid) {
var id = controllerid || 0;
return _controllers[id].controller.reloadEngine();
};
function _reboot(controllerid) {
var id = controllerid || 0;
return _controllers[id].controller.reboot();
};
function _deleteScene(scene) {
var elems = scene.altuiid.split("-");
// delete watches
$.each( MultiBox.getWatches( "VariablesToWatch",
function(w) { return (elems[0]==0) && (w.sceneid == elems[1]) }
),
function(i,watch) {
MultiBox.delWatch(watch);
}
);
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.deleteScene(elems[1]);
};
function _getNewSceneID(controllerid) {
var id = controllerid || 0;
var newid= _controllers[id].controller.getNewSceneID();
return {
id: newid,
altuiid: "{0}-{1}".format(controllerid,newid)
};
};
function _getScenes( func , filterfunc, endfunc ) {
var arr=[];
var answers = 0;
$.each(_controllers, function( i,c) {
c.controller.getScenes( func , filterfunc, function(scenes) {
arr = arr.concat(scenes);
answers++
if ((answers==_controllers.length) && ($.isFunction(endfunc))) {
(endfunc)(arr);
}
});
});
return arr;
};
function _getScenesSync() {
var arr=[];
$.each(_controllers, function( i,c) {
arr = arr.concat(c.controller.getScenesSync());
});
return arr;
};
function _getSceneByID(controllerid,sceneid) {
return (_controllers[controllerid]==undefined) ? null : _controllers[controllerid].controller.getSceneByID(sceneid)
};
function _getSceneByAltuiID(altuiid) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getSceneByID(elems[1])
};
function _getSceneHistory( scene, cbfunc) {
var elems = scene.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getSceneHistory( elems[1], cbfunc);
};
function _editScene(altuiid,scenejson,cbfunc) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.editScene(elems[1],scenejson,cbfunc);
};
function _renameScene(scene,newname) {
var elems = scene.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.renameScene(elems[1],newname);
};
function _runScene(scene) {
if (_recorder!=null ) {
_recorder.record( {type:'scene', altuiid:scene.altuiid } );
return;
}
var elems = scene.altuiid.split("-");
EventBus.publishEvent("on_sceneRun",scene.altuiid)
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.runScene(elems[1]);
};
function _runSceneByAltuiID(altuiid) {
if (_recorder!=null ) {
_recorder.record( {type:'scene', altuiid:altuiid } );
return;
}
var elems = altuiid.split("-");
EventBus.publishEvent("on_sceneRun",altuiid)
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.runScene(elems[1]);
};
function _setSceneMonitorMode(scene,mode,cbfunc) {
var elems = scene.altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.setSceneMonitorMode( elems[1], mode, cbfunc);
};
function _getWorkflowStatus(cbfunc) {
return _controllers[0].controller.getWorkflowStatus(cbfunc);
};
function _getWorkflowHistory(altuiid, cbfunc) {
return _controllers[0].controller.getWorkflowHistory(altuiid, cbfunc);
};
function _getWorkflows(cbfunc) {
return _controllers[0].controller.getWorkflows(cbfunc);
};
function _getCustomPages(cbfunc) {
return _controllers[0].controller.getCustomPages(cbfunc);
}
function _isWorkflowEnabled() {
return _controllers[0].controller.isWorkflowEnabled();
};
function _runLua(controllerid, code, cbfunc) {
var id = controllerid || 0;
return _controllers[id].controller.runLua(code, cbfunc);
};
function _getLuaStartup(controllerid) {
var id = controllerid || 0;
return _controllers[id].controller.getLuaStartup();
};
function _setStartupCode(controllerid,code) {
var id = controllerid || 0;
if (id==0)
return _controllers[id].controller.setStartupCode(code);
else {
var dfd = $.Deferred();
dfd.reject();
return dfd.promise();
}
};
function _saveChangeCaches( controllerid,msgidx ) {
var id = controllerid || 0;
return _controllers[id].controller.saveChangeCaches( msgidx );
};
function _updateChangeCache( controllerid,target ) {
var id = controllerid || 0;
return _controllers[id].controller.updateChangeCache( target );
};
function _saveData( key, name, data , cbfunc) {
return _controllers[0].controller.saveData( key, name, data , cbfunc );
};
function _getPlugins( func , endfunc ) {
var arr=[];
$.each(_getControllers("getPlugins"), function( i,c) {
arr = arr.concat(c.controller.getPlugins( func , null ));
});
if ($.isFunction(endfunc))
(endfunc)( arr );
return arr;
};
function _deletePlugin( altuiid, pluginid, cbfunc) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getUPnPHelper().UPnPDeletePlugin(pluginid,cbfunc);
};
function _updatePluginFromStore(plugin, cbfunc) {
return _controllers[0].controller.getUPnPHelper().UPnPUpdatePlugin2(plugin,cbfunc);
};
function _updatePlugin( altuiid, pluginid, cbfunc) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getUPnPHelper().UPnPUpdatePlugin(pluginid,cbfunc);
};
function _updatePluginVersion( altuiid, pluginid, ver, cbfunc) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.getUPnPHelper().UPnPUpdatePluginVersion(pluginid,ver,cbfunc);
};
function _modifyPlugin(altuiid,pluginid,changes,cbfunc) {
var elems = altuiid.split("-");
return (_controllers[elems[0]]==undefined) ? null : _controllers[elems[0]].controller.modifyPlugin(pluginid,changes,cbfunc);
};
function _getFileUrl(controllerid, filename ) {
var id = controllerid || 0;
return _controllers[id].controller.getFileUrl( filename);
};
function _getFileContent(controllerid, filename , cbfunc) {
var id = controllerid || 0;
return _controllers[id].controller.getFileContent( filename, cbfunc);
};
function _osCommand(controllerid, cmd, bSilent, cbfunc) {
var id = controllerid || 0;
return _controllers[id].controller.osCommand(cmd,bSilent,cbfunc);
};
function _getPower(cbfunc) {
var lines=[];
var ctrls = _getControllers("getPower");
var todo = ctrls.length;
$.each(ctrls, function( idx,c) {
var idx = idx;
c.controller.getPower(function(data) {
if (data != "No devices")
$.each(data.split('\n'), function(i,line) {
if (line.length>0)
lines.push( idx+"-"+line );
});
todo--;
if ((todo==0) && ($.isFunction(cbfunc)))
(cbfunc)(lines.join('\n'));
});
});
};
function _resetPollCounters() {
var dfd = $.Deferred();
var ctrls = _getControllers("resetPollCounters");
var todo = ctrls.length;
$.each(ctrls, function(i,c) {
c.controller.resetPollCounters(function() {
todo--;
if (todo==0)
dfd.resolve();
});
});
return dfd.promise();
};
function _isUserDataCached(controllerid) {
var id = controllerid || 0;
return _controllers[id].controller.isUserDataCached();
};
function _getIconPath( controllerid, iconname ) {
var id = controllerid || 0;
return _controllers[id].controller.getIconPath( iconname );
};
function _getIcon( controllerid, imgpath , cbfunc ) {
var id = controllerid || 0;
return _controllers[id].controller.getIcon( imgpath , cbfunc );
};
function _triggerAltUIUpgrade(newversion,newtracnum) {
return _controllers[0].controller.triggerAltUIUpgrade(newversion,newtracnum);
};
function Recorder( callback ) {
this._log = [ ];
this._callback = callback;
this.getLog = function() {
return this._log;
}
this.record = function( action ) {
this._log.push(action);
if ($.isFunction(this._callback))
(this._callback)(action)
}
};
function _isRecording() {
return _recorder != null;
};
function _startRecorder( callback ) {
if (_recorder == null )
_recorder = new Recorder( callback );
};
function _stopRecorder() {
if (_recorder == null )
return;
var log=_recorder.getLog();
_recorder = null;
return log;
};
return {
//---------------------------------------------------------
// PUBLIC functions
//---------------------------------------------------------
//static info per device type
initDB : _initDB, // (devicetypes)
initEngine : _initEngine,
reloadEngine : _reloadEngine,
reboot : _reboot,
saveEngine : _saveEngine, //()
clearEngine : _clearEngine, //()
// controller selection
controllerOf : _controllerOf, //(deviceid)
makeAltuiid : _makeAltuiid,
isAltuiid : _isAltuiid,
getControllers : _getControllers,
isControllerFeature: _isControllerFeature,
// Device Type DB
getALTUITypesDB : _getALTUITypesDB, // no param
getDeviceTypesDB : _getDeviceTypesDB, // ( controllerid )
addDeviceType : _addDeviceType, // (devtype, obj) update devitetype plugin function calls ( from Lua )
updateDeviceTypeUPnpDB : _updateDeviceTypeUPnpDB, //( controllerid, devtype, Dfilename ) update devicetype UPNP information ( from D_xx S_xx files )
updateDeviceTypeUIDB : _updateDeviceTypeUIDB, //( controllerid, devtype, ui_definitions) update devicetype UI static infos ( from user_data )
getDeviceStaticData : _getDeviceStaticData, //(device)
// Access & Modes
isRemoteAccess : function() { return window.location.href.indexOf("mios.com")!=-1; /*return true;*/ },
getBoxInfo : function( ctrlid ) { return _controllers[ctrlid || 0].controller.getBoxInfo(); },
getBoxFullInfo : function( ctrlid ) { return _controllers[ctrlid || 0].controller.getBoxFullInfo(); },
getHouseMode : function(cb) { return _controllers[0].controller.getHouseMode(cb); }, // (cbfunc)
setHouseMode : _setHouseMode, // (newmode,cbfunc)
getHouseModeSwitchDelay : _getHouseModeSwitchDelay,
RequestBackup: _RequestBackup,
// Rooms
getRooms : _getRooms, // in the future getRooms could cache the information and only call _getRooms when needed
getRoomsSync : _getRoomsSync, //()
deleteRoom : _deleteRoom, //(room)
createRoom : _createRoom, //(controllerid, name, cbfunc )
renameRoom : _renameRoom, //(controllerid, room, name) returns a promise
getRoomByID : _getRoomByID, //( roomid )
getRoomByAltuiID:_getRoomByAltuiID, //(altuiid)
// Users
getUsers : _getUsers,
getUsersSync : _getUsersSync,
getUserByID : _getUserByID,
getMainUser : _getMainUser,
// Devices
createDevice : _createDevice, // ( param , cbfunc )
deleteDevice : _deleteDevice, // id
renameDevice : _renameDevice, // (device, newname )
getDevices : _getDevices, // ( func , filterfunc, endfunc )
getDevicesSync : _getDevicesSync, // ()
getDeviceByAltuiID : _getDeviceByAltuiID, // ( devid )
getDeviceByType : _getDeviceByType, // ( str )
getDeviceByID : _getDeviceByID, // ( controller, devid )
getDeviceByAltID : _getDeviceByAltID, // ( parentdevid , altid )
getDeviceActions : _getDeviceActions, // (device,cbfunc)
getDeviceEvents : _getDeviceEvents, // (device)
getDeviceDependants : _getDeviceDependants, // (device)
getDeviceBatteryLevel : _getDeviceBatteryLevel, // ( device )
getDeviceVariableHistory : _getDeviceVariableHistory,//( device, varidx, cbfunc)
getDeviceService : _getDeviceService, // device, serviceId
addWatch : _addWatch, // ( service, variable, deviceid, sceneid, expression, xml, provider, params)
delWatch : _delWatch, // ( service, variable, deviceid, sceneid, expression, xml, provider, params )getWatches(whichwatches)
getWatches : _getWatches, // (whichwatches,filterfunc)
getWatchesHistory : _getWatchesHistory, // (cbfunc)
evaluateConditions : _evaluateConditions, // ( device,devsubcat,conditions ) evaluate a device condition table ( AND between conditions )
getStates : _getStates, // ( device )
getStatesByAltuiID : _getStatesByAltuiID, // (altuiid)
getStateByID : _getStateByID, // ( device, id ) // return idx of state object in states array , by ID
getStatus : _getStatus, // ( device, service, variable )
setStatus : _setStatus, // ( device, service, variable, value, dynamic )
getJobStatus : _getJobStatus, // ( jobid , cbfunc )
setAttr : _setAttr, // ( device, attribute, value,function(result) )
runAction : _runAction, // (device, service, action, params,cbfunc);
runActionByAltuiID : _runActionByAltuiID, // (altuiid, service, action, params,cbfunc)
isDeviceZwave : _isDeviceZwave, // (device)
isDeviceZigbee : _isDeviceZigbee, // (device)
isDeviceBT : _isDeviceBT, // (device)
updateNeighbors : _updateNeighbors, // (device)
modifyDevice : _modifyDevice,
//Alias
setOnOff : function ( altuiid, onoff) {
MultiBox.runActionByAltuiID( altuiid, 'urn:upnp-org:serviceId:SwitchPower1', 'SetTarget', {'newTargetValue':onoff} );
},
setArm : function ( altuiid, armed) {
this.runActionByAltuiID( altuiid, 'urn:micasaverde-com:serviceId:SecuritySensor1', 'SetArmed', {'newArmedValue':armed} );
},
setDoorLock : function ( altuiid, armed) {
this.runActionByAltuiID( altuiid, 'urn:micasaverde-com:serviceId:DoorLock1', 'SetTarget', {'newTargetValue':armed} );
},
setColor : _setColor,
// Categories
getCategoryTitle : _getCategoryTitle, // ( catnum )
getCategories : _getCategories, // ( cbfunc, filterfunc, endfunc )
// Scenes
deleteScene : _deleteScene, //id
getNewSceneID : _getNewSceneID, //()
getScenes : _getScenes, //( func , filterfunc, endfunc ) {
getSceneByID : _getSceneByID, //(sceneid) {
getSceneByAltuiID : _getSceneByAltuiID, // (altuiid)
getSceneHistory : _getSceneHistory, //( id, cbfunc) {
getScenesSync : _getScenesSync, //()
editScene : _editScene, //(altuiid,scenejson, function(result)
renameScene : _renameScene, // (scene, newname)
runScene : _runScene, //(id)
runSceneByAltuiID : _runSceneByAltuiID,
setSceneMonitorMode : _setSceneMonitorMode,
// workflows
getWorkflows : _getWorkflows,
getWorkflowStatus : _getWorkflowStatus,
getWorkflowHistory : _getWorkflowHistory,
isWorkflowEnabled : _isWorkflowEnabled,
// pages
getCustomPages : _getCustomPages,
// Plugins
getPlugins : _getPlugins, //( func , endfunc )
deletePlugin : _deletePlugin, //(id,function(result)
updatePluginFromStore : _updatePluginFromStore,
updatePlugin : _updatePlugin, //(id,function(result)
updatePluginVersion : _updatePluginVersion, //(id,ver,function(result)
modifyPlugin : _modifyPlugin,
// Misc
getUrlHead : _getUrlHead, // ()
getIpAddr : _getIpAddr, // ()
isUI5 : _isUI5, // (controller)
initializeJsonp : _initializeJsonp, // (controller)
initializeSysinfo : _initializeSysinfo, // (controller)
getDataProviders : _getDataProviders, // (controller)
getWeatherSettings : _getWeatherSettings, // ()
runLua : _runLua, //(code, cbfunc)
getLuaStartup : _getLuaStartup, //()
setStartupCode : _setStartupCode, //(code)
saveChangeCaches : _saveChangeCaches, //( msgidx )
updateChangeCache : _updateChangeCache, //( target )
saveData : _saveData, //( name, data , cbfunc)
getFileUrl : _getFileUrl, //(filename)
getFileContent : _getFileContent, //(Dfilename , function( xmlstr , jqXHR )
osCommand : _osCommand, //(cmd,cbfunc)