-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
4868 lines (4035 loc) · 180 KB
/
main.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
/* jshint -W061 */ // no "eval" warnings
/* jslint node: true */
"use strict";
var herm=false;
// always required: utils
var utils = require('@iobroker/adapter-core');
var ip = require("ip");
var dgram = require('dgram');
var os = require('os');
var http = require('http');
var fs = require('fs');
// create the adapter object
var adapter; // = utils.Adapter('hausbus_de');
var DEFAULT_UDP_PORT = 5855;
var BROADCAST_SEND_IP = "192.255.255.255";
var BROADCAST_RECEIVE_IP = "0.0.0.0";
var CLASS_ID_CONTROLLER = 0;
var CLASS_ID_TASTER = 16;
var CLASS_ID_DIMMER = 17;
var CLASS_ID_ROLLLADEN = 18;
var CLASS_ID_SCHALTER = 19;
var CLASS_ID_LOGICAL_BUTTON = 20;
var CLASS_ID_LED = 21;
var CLASS_ID_RGB_DIMMER = 22;
var CLASS_ID_TEMPERATURSENSOR = 32;
var CLASS_ID_IR_SENSOR = 33;
var CLASS_ID_FEUCHTESENSOR = 34;
var CLASS_ID_HELLIGKEITSSENSOR = 39;
var CLASS_ID_ETHERNET = 162;
var MODUL_ID_32_IO=11;
var MODUL_ID_16_RELAIS_V2=10;
var MODUL_ID_16_RELAIS_V1=1;
var MODUL_ID_24_UP_IO=4;
var MODUL_ID_4_DIMMER=12;
var MODUL_ID_6_TASTER=3;
var MODUL_ID_4_TASTER=7;
var MODUL_ID_2_TASTER=8;
var MODUL_ID_1_TASTER=9;
var MODUL_ID_8_DIMMER=6;
var MODUL_ID_8_ROLLO=2;
var MODUL_ID_8_RELAIS=5;
var MODUL_ID_LAN_BRIDGE=15;
var MODUL_ID_RGB_DIMMER=16;
var MODULES = {}; // Alle Haus-Bus Module
var CLASSES = {}; // Alle Haus-Bus Klassen
var FIRMWARE_IDS = {}; // Firmwaretypen
var INSTANCES = {}; // Alle Haus-Bus Instanzen
var CONFIG_BITS = {}; // Konfigurations Bitmasken
var MY_DEVICE_ID = 12223;
var DATA_START = 15;
var CONTROLLER_RESEACH_DEVICES = "research_devices";
var CONTROLLER_ADMIN_FUNCTION = "admin_function";
var CONTROLLER_CHECK_FIRMWARE_UPDATES = "check_firmware_updates";
var CONTROLLER_FKT_STATE_ONLINE = "online";
var CONTROLLER_FKT_VERSION = "version";
var CONTROLLER_FKT_RESET = "reset";
var CONTROLLER_CFG_NEWEST_FIRMWARE = "newest_available_firmware";
var CONTROLLER_CFG_UPDATE_FIRMWARE = "update_firmware";
var ETHERNET_FKT_IP = "ip";
var ETHERNET_CFG_FIXED_IP_DHCP = "fixed_ip_or_dhcp";
var SCHALTER_FKT_ON_OFF = "on_off";
var SCHALTER_FKT_ON_DURATION_DELAY = "on_duration_delay";
var SCHALTER_FKT_OFF_DELAY = "off_delay";
var LED_FKT_ON_OFF = "on_off";
var LED_FKT_OFF_DELAY = "off_delay";
var LED_FKT_ON_BRIGHTNESS_DURATION_ONDELAY = "on_brightness_duration_ondelay";
var LED_FKT_BLINK_BRIGHTNESS_OFFTIME_ONTIME_QUANTITY = "blink_brightness_offtime_ontime_quantity";
var LED_FKT_MIN_BRIGHTNESS = "min_brightness";
var LED_CFG_INVERTED = "inverted";
var LED_CFG_TIMEBASE = "timebase";
var TEMP_FKT_TEMPERATUR = "temperature";
var TEMP_FKT_TEMPERATURE_STATE = "temperature_state";
var TEMP_CFG_LOWER_THRESHOLD = "lower_threshold";
var TEMP_CFG_UPPER_THRESHOLD = "upper_threshold";
var TEMP_CFG_MIN_REPORT_TIME = "min_report_time";
var TEMP_CFG_MAX_REPORT_TIME = "max_report_time";
var TEMP_CFG_REPORT_TIME_MULTIPLIER = "report_time_multiplier";
var TEMP_CFG_HYSTERESIS = "hysteresis";
var TEMP_CFG_CALIBRATION = "calibration";
var BRIGHT_FKT_BRIGHTNESS = "brightness";
var BRIGHT_FKT_BRIGHTNESS_STATE = "brightness_state";
var BRIGHT_CFG_LOWER_THRESHOLD = "lower_threshold";
var BRIGHT_CFG_UPPER_THRESHOLD = "upper_threshold";
var BRIGHT_CFG_MIN_REPORT_TIME = "min_report_time";
var BRIGHT_CFG_MAX_REPORT_TIME = "max_report_time";
var BRIGHT_CFG_REPORT_TIME_MULTIPLIER = "report_time_multiplier";
var BRIGHT_CFG_HYSTERESIS = "hysteresis";
var BRIGHT_CFG_CALIBRATION = "calibration";
var HUMIDITY_FKT_HUMIDITY = "humidity";
var HUMIDITY_FKT_HUMIDITY_STATE = "humidity_state";
var HUMIDITY_CFG_LOWER_THRESHOLD = "lower_threshold";
var HUMIDITY_CFG_UPPER_THRESHOLD = "upper_threshold";
var HUMIDITY_CFG_MIN_REPORT_TIME = "min_report_time";
var HUMIDITY_CFG_MAX_REPORT_TIME = "max_report_time";
var HUMIDITY_CFG_REPORT_TIME_MULTIPLIER = "report_time_multiplier";
var HUMIDITY_CFG_HYSTERESIS = "hysteresis";
var HUMIDITY_CFG_CALIBRATION = "calibration";
var TASTER_FKT_ENABLE_DISABLE_EVENTS = "enable_disable_events";
var TASTER_FKT_DISABLE_EVENTS_TIMEOUT = "disable_events_timeout";
var TASTER_FKT_PRESSED = "event_pressed";
var TASTER_FKT_RELEASED = "event_released";
var TASTER_FKT_CLICKED = "event_clicked";
var TASTER_FKT_DOUBLE_CLICKED = "event_double_clicked";
var TASTER_FKT_HOLD_START = "event_hold_start";
var TASTER_FKT_HOLD_END = "event_hold_end";
var TASTER_FKT_STATE_CLOSED_OPEN = "state_closed_open";
var TASTER_CFG_HOLD_TIMEOUT = "hold_timeout";
var TASTER_CFG_DOUBLE_CLICK_TIMEOUT = "double_click_timeout";
var TASTER_CFG_INVERTED = "inverted";
var TASTER_CFG_LED_FEEDBACK = "led_feedback";
var TASTER_CFG_EVENT_PRESSED_ENABLED = "event_pressed_enabled";
var TASTER_CFG_EVENT_RELEASED_ENABLED = "event_released_enabled";
var TASTER_CFG_EVENT_CLICKED_ENABLED = "event_clicked_enabled";
var TASTER_CFG_EVENT_DOUBLE_CLICKED_ENABLED = "event_double_clicked_enabled";
var TASTER_CFG_EVENT_HOLD_START_ENABLED = "event_hold_start_enabled";
var TASTER_CFG_EVENT_HOLD_END_ENABLED = "event_hold_end_enabled";
var LOGICAL_BUTTON_FKT_ON_OFF = "on_off";
var LOGICAL_BUTTON_ON_BRIGHTNESS_DURATION = "on_brightness";
var LOGICAL_BUTTON_FKT_BLINK_BRIGHTNESS_OFFTIME_ONTIME_QUANTITY = "blink_brightness_offtime_ontime_quantity";
var LOGICAL_BUTTON_FKT_MIN_BRIGHTNESS = "min_brightness";
var IR_SENSOR_FKT_COMMAND = "command_code";
var ROLLO_FKT_START = "start";
var ROLLO_FKT_STOP = "stop";
var ROLLO_FKT_TOGGLE = "toggle";
var ROLLO_FKT_UPDATE_POSITION = "update_position";
var ROLLO_FKT_MOVEMENT_STATUS = "movement_status";
var ROLLO_FKT_POSITION = "actual_position";
var ROLLO_CFG_OPEN_TIME = "open_time";
var ROLLO_CFG_CLOSE_TIME = "close_time";
var ROLLO_CFG_INVERT = "invert_channels";
var ROLLO_CFG_SET_POSITION = "set_position";
var ROLLO_MOVEMENT_STOPPED = "STOPPED";
var ROLLO_MOVEMENT_UP = "UP";
var ROLLO_MOVEMENT_DOWN = "DOWN";
var DIMMER_FKT_START = "start";
var DIMMER_FKT_STOP = "stop";
var DIMMER_FKT_TOGGLE = "toggle";
var DIMMER_FKT_BRIGHTNESS = "brightness";
var DIMMER_FKT_BRIGHTNESS_DURATION = "brightness_duration";
var DIMMER_FKT_DIMMING_STATUS = "dimming_status";
var DIMMER_CFG_MODE = "dimmer_mode";
var DIMMER_MODE_LEADING_EDGE = "LEADING_EDGE";
var DIMMER_MODE_TRAILING_EDGE = "TRAILING_EDGE";
var DIMMER_MODE_SWITCH_ONLY = "SWITCH_ONLY";
var DIMMER_CFG_FADING_TIME = "fading_time";
var DIMMER_CFG_DIMMING_TIME = "dimming_time";
var DIMMER_CFG_DIMMING_RANGE_START = "dimming_range_start";
var DIMMER_CFG_DIMMING_RANGE_END = "dimming_range_end";
var DIMMER_DIMMING_IDLE = "IDLE";
var DIMMER_DIMMING_UP = "DIMMING_UP";
var DIMMER_DIMMING_DOWN = "DIMMING_DOWN";
var RGB_DIMMER_FKT_COLOR = "color";
var RGB_DIMMER_FKT_COLOR_DURATION = "color_duration";
//var RGB_DIMMER_FKT_DIMMING_STATUS = "dimming_status";
var RGB_DIMMER_CFG_FADING_TIME = "fading_time";
var RGB_DIMMER_DIMMING_IDLE = "IDLE";
var RGB_DIMMER_DIMMING_UP = "DIMMING_UP";
var RGB_DIMMER_DIMMING_DOWN = "DIMMING_DOWN";
var FIRMWARE_ID_AR8 = "AR8";
var FIRMWARE_ID_MS6 = "MS6";
var FIRMWARE_ID_SD6 = "SD6";
var FIRMWARE_ID_SD485 = "SD485";
var FIRMWARE_ID_SONOFF = "SONOFF";
var FIRMWARE_ID_S0_Reader = "S0_Reader";
var FIRMWARE_ID_ESP = "ESP";
var FIRMWARE_ID_HBC = "HBC";
var FIRMWARE_ID_ESP32 = "ESP32";
var CHANNEL_CONFIG = "Konfiguration";
var udpSocket;
var sendDelayTimer;
var sendQueue = [];
var queueControl = [];
var messageCounter=0;
var checkAliveControllerDeviceId; // Controller der gerade im checkAlive ist
var checkAliveTimer; // Timer der regelmäßig alle Controller anpingt
var checkAliveTimeoutTimer; // Timer der einen Controller nach einem checkAlive Ping auf offline setzt
var myObjectId = getObjectId(MY_DEVICE_ID, CLASS_ID_CONTROLLER, 1);
var ioBrokerStates = {}; // Alle an IO Broker veröffentlichte States
var firmwareTypes = {};
var moduleTypes = {};
var moduleVersions = {};
var onlineVersions = {};
var objectIds = {};
var stateTypes = {};
var configurations = {};
var pongCallback = {};
function startAdapter(options)
{
options = options || {};
Object.assign(options, {name: 'hausbusde'});
adapter = new utils.Adapter(options);
// unloading
adapter.on('unload', function (callback)
{
try
{
if (checkAliveTimeoutTimer) clearTimeout(checkAliveTimeoutTimer);
if (checkAliveTimer) clearTimeout(checkAliveTimer);
if (sendDelayTimer) clearInterval(sendDelayTimer);
if (udpSocket) udpSocket.close();
}
catch (e)
{
warn('Error during unload: ' + e);
}
callback();
});
// startup
adapter.on('ready', function ()
{
main();
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state)
{
// Warning: state can be null if it was deleted!
if (!id || !state) return;
//debug('stateChange ' + id + ' ' + state+" ack = "+state.ack);
//stateChange hausbusde.0.1136.CONTROLLER.1.online
if (id.startsWith(adapter.namespace + '.'))
{
var newValue = state.val;
var stateName = getStateFromIoBrokerId(id);
if (stateName!="")
{
debug('stateChange ' + id + ' - ' + stateName+": "+ioBrokerStates[id]+" -> "+newValue);
ioBrokerStates[id]=newValue;
}
else warn("Unbekannter Name "+id);
if (state.ack) return;
// Hier Methodenaufrufe
aFunctionCall(id, newValue);
}
});
return adapter;
}
function main()
{
initModulesClassesInstances();
// Socket um Broadcast zu empfangen
udpSocket = dgram.createSocket('udp4');
udpSocket.on('listening', function ()
{
udpSocket.setBroadcast(true);
udpSocket.setMulticastLoopback(true);
var address = udpSocket.address();
debug('UDP broadcast server listening on ' + address.address + ":" + address.port);
});
udpSocket.on('message', handleIncomingMessage);
udpSocket.bind(DEFAULT_UDP_PORT, BROADCAST_RECEIVE_IP);
var parts = ip.address().split(".");
var mask="";
const ifaces = require('os').networkInterfaces();
Object.keys(ifaces).forEach(dev =>
{
ifaces[dev].filter(details =>
{
if (details.family === 'IPv4' && details.internal === false)
{
mask = details.netmask;
return;
}
});
});
/*if (mask=="255.255.0.0") BROADCAST_SEND_IP = parts[0]+"."+parts[1]+".255.255";
else if (mask=="255.0.0.0") BROADCAST_SEND_IP = parts[0]+".255.255.255";
else */
BROADCAST_SEND_IP = parts[0]+"."+parts[1]+"."+parts[2]+".255";
debug( "BROADCAST_SEND_IP: "+BROADCAST_SEND_IP );
adapter.getStates('*', function (err, obj)
{
if (err) error('Error reading states: ' + err);
else
{
if (obj)
{
for (var key in obj)
{
if (! obj.hasOwnProperty(key)) continue;
if (obj[key] !== null)
{
// hausbusde.0.1000.online - object(7): { val: boolean: tru
ioBrokerStates[key] = obj[key].val;
}
else debug ("property with no value: "+key);
}
}
adapter.subscribeStates(adapter.namespace+".*");
adapter.setObjectNotExists(adapter.namespace+"."+CONTROLLER_RESEACH_DEVICES,{type: 'state',common: {name: CONTROLLER_RESEACH_DEVICES,type: "boolean",role: "button"},native: {}});
adapter.setObjectNotExists(adapter.namespace+"."+CONTROLLER_ADMIN_FUNCTION,{type: 'state',common: {name: CONTROLLER_ADMIN_FUNCTION,type: "string",role: "switch"},native: {}});
searchAllDevices();
//setTimeout(readFirmwareVersions, 10000);
checkAliveTimer = setTimeout(checkAlive, 20000); // alle 20 Sekunden pingen wir einen Controller an
}
});
}
function readFirmwareVersions()
{
require('dns').resolve('www.haus-bus.de', function(err) {
if (err) debug("no internet connection");
else
{
debug("internet connection is available");
for (var firmwareId in FIRMWARE_IDS)
{
var firmwareName = FIRMWARE_IDS[firmwareId];
if (typeof firmwareName!="string" || firmwareName.length<2)
continue;
if (firmwareName == FIRMWARE_ID_S0_Reader)
continue;
readFirmwareVersionFor(firmwareId, firmwareName);
}
}
});
}
function readFirmwareVersionFor(firmwareId, firmwareName)
{
var options =
{
host: 'www.haus-bus.de',
path: '/'+firmwareName+'.chk'
};
http.request(options, function(response)
{
var str = '';
response.on('data', function (chunk) {str += chunk;});
response.on('end', function ()
{
var parts = str.split("-");
if (typeof onlineVersions[firmwareId]=="undefined") onlineVersions[firmwareId]={};
onlineVersions[firmwareId].version = (""+parts[0]).trim();
onlineVersions[firmwareId].date = (""+parts[1]).trim();
info("Online version "+firmwareName+": "+str);
for (var deviceId in firmwareTypes)
{
var myFirmwareId = FIRMWARE_IDS[firmwareTypes[deviceId]];
if (myFirmwareId == firmwareId)
{
var myId = getIoBrokerId(deviceId,CLASS_ID_CONTROLLER,1,CONTROLLER_CFG_NEWEST_FIRMWARE, CHANNEL_CONFIG);
setStateIoBroker(myId, firmwareName+" "+str, CHANNEL_CONFIG);
}
}
});
}).end();
}
function checkAlive()
{
var stateSize = Object.keys(ioBrokerStates).length;
if (stateSize!=0)
{
var newCheckControllerDeviceId;
var newCheckControllerIoBrokerId;
// Wenn wir bereits einen Controller geprüft haben, suchen wir den nächsten in der Liste
// Oder springen wieder an den Anfang, wenn wir dahinter keinen finden
if (checkAliveControllerDeviceId)
{
debug("checkAliveControllerDeviceId is "+checkAliveControllerDeviceId);
var found=false;
for (var id in ioBrokerStates)
{
if (!id.includes("."+CONTROLLER_FKT_STATE_ONLINE)) continue;
var actDeviceId = getDeviceIdFromIoBrokerId(id);
// Wir haben einen Controller nach dem zuletzt geprüften gefunden
if (found)
{
if (actDeviceId!=checkAliveControllerDeviceId)
{
newCheckControllerDeviceId = actDeviceId;
newCheckControllerIoBrokerId = id;
break;
}
}
else if (actDeviceId==checkAliveControllerDeviceId)
found=true;
}
}
// Sonst nehmen wir wieder den ersten in der Liste
if (!newCheckControllerDeviceId)
{
for (var id in ioBrokerStates)
{
if (!id.includes("."+CONTROLLER_FKT_STATE_ONLINE)) continue;
var actDeviceId = getDeviceIdFromIoBrokerId(id);
newCheckControllerDeviceId=actDeviceId;
newCheckControllerIoBrokerId = id;
break;
}
}
if (newCheckControllerDeviceId)
{
hwControllerPing(getObjectId(newCheckControllerDeviceId, CLASS_ID_CONTROLLER,1));
checkAliveTimeoutTimer = setTimeout(function () {checkAliveTimeoutOccured(newCheckControllerIoBrokerId)}, 3000); // offline nach 3 Sekunden
checkAliveControllerDeviceId = newCheckControllerDeviceId;
}
}
checkAliveTimer = setTimeout(checkAlive, 30000); // alle 30 Sekunden pingen wir einen Controller an
}
function checkAliveTimeoutOccured(ioBrokerId)
{
debug("checkAliveTimeoutOccured "+ioBrokerId);
setStateIoBroker(ioBrokerId, false);
}
function checkAliveOk(deviceId)
{
if (checkAliveTimeoutTimer && checkAliveControllerDeviceId ==deviceId)
{
debug("checkAlive ok for "+deviceId);
clearInterval(checkAliveTimeoutTimer);
}
}
function sendUdpDatagram(message)
{
sendQueue.push(message);
// Wenn die Queue leer ist, senden wir sofort, ansonsten übernimmt das der Timer
if (!sendDelayTimer)
sendNextQueueDatagram();
}
function sendNextQueueDatagram()
{
// Wenn die Queue leer ist, löschen wir den Timer
if (sendQueue.length === 0)
{
clearInterval(sendDelayTimer);
sendDelayTimer = null;
return;
}
var message = sendQueue.shift();
if (udpSocket)
{
udpSocket.send(message, 0, message.length, DEFAULT_UDP_PORT, BROADCAST_SEND_IP, function (err, bytes)
{
if (err)
{
error('UDP send error for ' + BROADCAST_SEND_IP + ':' + DEFAULT_UDP_PORT + ': ' + err);
return;
}
debug('Sent "' + message + '" to ' + BROADCAST_SEND_IP + ':' + DEFAULT_UDP_PORT);
});
}
// wir schauen wie viele Nachrichten, wir innerhalb der letzten 100ms versendet haben
var now = Date.now();
var min = now-100;
queueControl.forEach(function(item, index, object)
{
if (item<min) object.shift();
});
// Altes Interval löschen, um es anschließend auf einen neuen Wert zu setzen
clearInterval(sendDelayTimer);
// Bei mehr als 3 Nachrichten, machen wir eine 50ms Pause, sonst nur 10 ms
if (queueControl.length>3)
{
debug("controling bus speed");
sendDelayTimer = setInterval(sendNextQueueDatagram, 50);
}
else sendDelayTimer = setInterval(sendNextQueueDatagram, 10);
queueControl.push(Date.now());
}
function setStateIoBroker(id, value, forceUpdate=false)
{
if (typeof ioBrokerStates[id]!="undefined" && (""+ioBrokerStates[id]!=""+value || forceUpdate))
{
var type = stateTypes[id];
debug("setState "+id+" "+ioBrokerStates[id]+" -> "+value+" - forceUpdate = "+forceUpdate+" - "+type);
ioBrokerStates[id] = value;
if (type=="number") value=Number(value);
else if (type=="boolean") value=Boolean(value);
adapter.setState(id, {val: value, ack: true});
}
//else warn("übersprungen: setState "+id+" "+ioBrokerStates[id]+" -> "+value);
}
// handle incomming messages
function handleIncomingMessage(message, remote)
{
debug('UDP datagram from ' + remote.address + ':' + remote.port + ': length = '+message.length);
try
{
if (message.length<3) return;
// Bytewurst loggen
//debug(bytesToDebugString(message,0,message.length));
// Falscher Header ?
if (message[0]!=0xef || message[1]!=0xef)
{
debug('Non Haus-Bus header');
return;
}
// message[2] = Kontroll Byte
// message[3] = Messagecounter
var sender = bytesToDword(message,4);
var receiver = bytesToDword(message,8);
var dataLength = bytesToWord(message, 12)-1;
var functionId = message[14];
var deviceIdSender = getDeviceId(sender);
if (herm && deviceIdSender!=21336 && deviceIdSender!=1136 && deviceIdSender!=1247 && deviceIdSender!=1271) return; // test
var classIdSender = getClassId(sender);
// sender: 74453970, receiver: 801046529, functionId: 129, dataLength: 3, classIdSender: 19
debug('sender: '+sender+", receiver: "+receiver+", functionId: "+functionId+", dataLength: "+dataLength+", classIdSender: "+classIdSender);
var senderDeviceId = getDeviceId(sender);
checkAliveOk(senderDeviceId);
setStateIoBroker(getIoBrokerId(senderDeviceId,CLASS_ID_CONTROLLER,1,CONTROLLER_FKT_STATE_ONLINE), true);
if (classIdSender == CLASS_ID_CONTROLLER)
{
if (functionId==128) hwControllerReceivedModuleId(sender, receiver, message, dataLength);
else if (functionId==129) hwControllerReceivedRemoteObjects(sender, receiver, message, dataLength);
else if (functionId==202) hwControllerReceivedEvStarted(sender);
else if (functionId==131) hwControllerReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==199) hwControllerReceivedPong(sender);
}
else if (classIdSender == CLASS_ID_FEUCHTESENSOR)
{
if (functionId==128) hwFeuchteSensorReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwFeuchteSensorReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwFeuchteSensorReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_HELLIGKEITSSENSOR)
{
if (functionId==128) hwHelligkeitssensorReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwHelligkeitssensorReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwHelligkeitssensorReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_LED)
{
if (functionId==128) hwLedReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwLedReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwLedReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_LOGICAL_BUTTON)
{
if (functionId==129) hwLogicalButtonReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwLogicalButtonReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_SCHALTER)
{
if (functionId==128) hwSchalterReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwSchalterReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwSchalterReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_TASTER)
{
if (functionId==128) hwTasterReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwTasterReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwTasterReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_ROLLLADEN)
{
if (functionId==128) hwRolloReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwRolloReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwRolloReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_DIMMER)
{
if (functionId==128) hwDimmerReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwDimmerReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwDimmerReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_RGB_DIMMER)
{
if (functionId==128) hwRgbDimmerReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwRgbDimmerReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwRgbDimmerReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_IR_SENSOR)
{
if (functionId>=200 && functionId<300) hwIrSensorReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_TEMPERATURSENSOR)
{
if (functionId==128) hwTemperatursensorReceivedConfiguration(sender, receiver, message, dataLength);
else if (functionId==129) hwTemperatursensorReceivedStatus(sender, receiver, message, dataLength);
else if (functionId>=200 && functionId<300) hwTemperatursensorReceivedEvents(sender, receiver, functionId, message, dataLength);
}
else if (classIdSender == CLASS_ID_ETHERNET)
{
if (functionId==129) hwEthernetReceivedIp(sender, receiver, message, dataLength);
else if (functionId==128) hwEthernetReceivedConfiguration(sender, receiver, message, dataLength);
}
}
catch (e)
{
error('error handling incoming udp message: ' + e+"\n"+e.stack);
}
}
function searchAllDevices()
{
info("Searching all Haus-Bus.de devices");
hwControllerGetModuleId(getObjectId(0,CLASS_ID_CONTROLLER,1));
}
function adminFunction(param)
{
var receiverObjectId = getObjectId(param, CLASS_ID_ETHERNET, 1)
info("adminFunction: "+param+", ReceiverID: "+receiverObjectId);
if (param>0)
{
var data = [];
var pos=0;
data[pos++]=1; // Funktion ID
data[pos++]=192;
data[pos++]=168;
data[pos++]=178;
data[pos++]=254;
data[pos++]=4;
wordToByteArray(5855, data, pos); pos+=2;
data[pos++]=0;
data[pos++]=0;
data[pos++]=0;
data[pos++]=0;
sendHausbusUdpMessage(receiverObjectId, data, myObjectId,9);
}
}
function aFunctionCall(ioBrokerId, newValue)
{
var state = getStateFromIoBrokerId(ioBrokerId);
if (state == CONTROLLER_RESEACH_DEVICES)
{
info("Call: "+CONTROLLER_RESEACH_DEVICES);
searchAllDevices();
return;
}
if (state == CONTROLLER_ADMIN_FUNCTION)
{
info("Call: "+CONTROLLER_ADMIN_FUNCTION);
var params = getMyParams(newValue, 2);
var id = params[0];
var value = params[1];
if (id==42) adminFunction(value);
return;
}
if (state == CONTROLLER_CHECK_FIRMWARE_UPDATES)
{
info("Call: "+CONTROLLER_CHECK_FIRMWARE_UPDATES);
readFirmwareVersions();
return;
}
var objectId = objectIds[ioBrokerId];
if (typeof objectId=="undefined")
{
warn("Call on unknown id "+ioBrokerId);
return;
}
var deviceId = getDeviceId(objectId);
var classId = getClassId(objectId);
// Call: object=1136.Controller.1, state=.reset, newValue = true
info("Call: state="+state+", newValue = "+newValue+" -> "+objectIdToString(objectId));
if (classId == CLASS_ID_SCHALTER)
{
if (state == SCHALTER_FKT_ON_OFF)
{
if (newValue == true) hwSchalterOn(0, 0, objectId);
else if (newValue == false) hwSchalterOff(0, objectId);
}
else if (state == SCHALTER_FKT_ON_DURATION_DELAY)
{
var params = getMyParams(newValue, 2);
var duration = params[0];
var delay = params[1];
hwSchalterOn(duration, delay, objectId);
}
else if (state == SCHALTER_FKT_OFF_DELAY) hwSchalterOff(newValue, objectId);
}
else if (classId == CLASS_ID_TASTER)
{
if (state == TASTER_FKT_ENABLE_DISABLE_EVENTS) hwTasterEnableEvents(newValue ? "TRUE":"FALSE", 0, objectId);
else if (state == TASTER_FKT_DISABLE_EVENTS_TIMEOUT) hwTasterEnableEvents("FALSE", newValue, objectId);
else if (state == TASTER_CFG_HOLD_TIMEOUT ||
state == TASTER_CFG_DOUBLE_CLICK_TIMEOUT ||
state == TASTER_CFG_INVERTED ||
state == TASTER_CFG_LED_FEEDBACK ||
state == TASTER_CFG_EVENT_PRESSED_ENABLED ||
state == TASTER_CFG_EVENT_RELEASED_ENABLED ||
state == TASTER_CFG_EVENT_CLICKED_ENABLED ||
state == TASTER_CFG_EVENT_DOUBLE_CLICKED_ENABLED ||
state == TASTER_CFG_EVENT_HOLD_START_ENABLED ||
state == TASTER_CFG_EVENT_HOLD_END_ENABLED)
hwTasterSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_ROLLLADEN)
{
if (state == ROLLO_FKT_POSITION) hwRolloMoveToPosition(newValue, objectId);
else if (state == ROLLO_FKT_START) hwRolloStart(newValue, objectId);
else if (state == ROLLO_FKT_TOGGLE) hwRolloToggle(objectId);
else if (state == ROLLO_FKT_STOP) hwRolloStop(objectId);
else if (state == ROLLO_CFG_SET_POSITION)
{
hwRolloSetPosition(newValue, objectId);
hwRolloGetStatus(objectId);
}
else if (state == ROLLO_FKT_UPDATE_POSITION) hwRolloGetStatus(objectId);
else if (state == ROLLO_CFG_CLOSE_TIME ||
state == ROLLO_CFG_OPEN_TIME ||
state == ROLLO_CFG_INVERT)
hwRolloSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_DIMMER)
{
if (state == DIMMER_FKT_BRIGHTNESS) hwDimmerSetBrightness(newValue,0, objectId);
else if (state == DIMMER_FKT_BRIGHTNESS_DURATION)
{
var params = getMyParams(newValue, 2);
var brightness = params[0];
var duration = params[1];
hwDimmerSetBrightness(brightness, duration, objectId);
}
else if (state == DIMMER_FKT_START) hwDimmerStart(newValue, objectId);
else if (state == DIMMER_FKT_STOP) hwDimmerStop(objectId);
else if (state == DIMMER_FKT_TOGGLE) hwDimmerToggle(objectId);
else if (state == DIMMER_CFG_DIMMING_RANGE_END ||
state == DIMMER_CFG_DIMMING_RANGE_START ||
state == DIMMER_CFG_DIMMING_TIME ||
state == DIMMER_CFG_FADING_TIME)
hwDimmerSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_RGB_DIMMER)
{
if (state == RGB_DIMMER_FKT_COLOR)
{
var params = getMyParams(newValue, 3);
var red = params[0];
var green = params[1];
var blue = params[2];
hwRgbDimmerSetColor(red, green, blue,0, objectId);
}
else if (state == RGB_DIMMER_FKT_COLOR_DURATION)
{
var params = getMyParams(newValue, 4);
var red = params[0];
var green = params[1];
var blue = params[2];
var duration = params[3];
hwRgbDimmerSetColor(red, green, blue,duration, objectId);
}
else if (state == RGB_DIMMER_CFG_FADING_TIME) hwRgbDimmerSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_CONTROLLER)
{
if (state == CONTROLLER_FKT_RESET) hwControllerReset(objectId);
else if (state == ETHERNET_CFG_FIXED_IP_DHCP) hwEthernetSetConfiguration(newValue, getObjectId(deviceId, CLASS_ID_ETHERNET,1));
else if (state == CONTROLLER_CFG_UPDATE_FIRMWARE)
{
if (newValue=="UPDATE") hwControllerUpdateFirmware(objectId);
else error("Invalid value. To update firmware please enter 'UPDATE'.");
}
}
else if (classId == CLASS_ID_LED)
{
if (state == LED_FKT_ON_OFF)
{
if (newValue == true) hwLedOn(100, 0, 0, objectId);
else if (newValue == false) hwLedOff(0, objectId);
}
else if (state == LED_CFG_INVERTED) hwLedSetConfiguration(state, newValue, objectId);
else if (state == LED_CFG_TIMEBASE) hwLedSetConfiguration(state, newValue, objectId);
else if (state == LED_FKT_ON_BRIGHTNESS_DURATION_ONDELAY)
{
var params = getMyParams(newValue, 3);
var brightness = params[0];
var duration = params[1];
var delay = params[2];
hwLedOn(brightness, duration, delay, objectId);
}
else if (state == LED_FKT_OFF_DELAY) hwLedOff(newValue, objectId);
else if (state == LED_FKT_MIN_BRIGHTNESS) hwLedMinBrightness(newValue, objectId);
else if (state == LED_FKT_BLINK_BRIGHTNESS_OFFTIME_ONTIME_QUANTITY)
{
var params = getMyParams(newValue, 4);
var brightness = params[0];
var offTime = params[1];
var onTime = params[2];
var quantity = params[3];
if (brightness==0) hwLedOff(0, objectId);
else hwLedBlink(brightness, offTime, onTime, quantity, objectId);
}
}
else if (classId == CLASS_ID_LOGICAL_BUTTON)
{
if (state == LOGICAL_BUTTON_FKT_ON_OFF)
{
if (newValue == true) hwLogicalButtonOn(100, 0, objectId);
else if (newValue == false) hwLogicalButtonOff(objectId);
}
else if (state == LOGICAL_BUTTON_ON_BRIGHTNESS_DURATION)
{
newValue = parseInt(newValue);
if (newValue==0) hwLogicalButtonOff(objectId);
else hwLogicalButtonOn(newValue, 0, objectId);
hwLedGetStatus(objectId);
}
else if (state == LOGICAL_BUTTON_FKT_MIN_BRIGHTNESS) hwLogicalButtonSetMinBrightness(newValue, 0, objectId);
else if (state == LOGICAL_BUTTON_FKT_BLINK_BRIGHTNESS_OFFTIME_ONTIME_QUANTITY)
{
var params = getMyParams(newValue, 4);
var brightness = params[0];
var offTime = params[1];
var onTime = params[2];
var quantity = params[3];
if (brightness==0) hwLogicalButtonOff(0, objectId);
else hwLogicalButtonBlink(brightness, offTime, onTime, quantity, objectId);
}
}
else if (classId == CLASS_ID_TEMPERATURSENSOR)
{
if (state == TEMP_CFG_CALIBRATION ||
state == TEMP_CFG_HYSTERESIS ||
state == TEMP_CFG_LOWER_THRESHOLD ||
state == TEMP_CFG_MAX_REPORT_TIME ||
state == TEMP_CFG_MIN_REPORT_TIME ||
state == TEMP_CFG_REPORT_TIME_MULTIPLIER ||
state == TEMP_CFG_UPPER_THRESHOLD)
hwTemperatursensorSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_HELLIGKEITSSENSOR)
{
if (state == BRIGHT_CFG_CALIBRATION ||
state == BRIGHT_CFG_HYSTERESIS ||
state == BRIGHT_CFG_LOWER_THRESHOLD ||
state == BRIGHT_CFG_MAX_REPORT_TIME ||
state == BRIGHT_CFG_MIN_REPORT_TIME ||
state == BRIGHT_CFG_REPORT_TIME_MULTIPLIER ||
state == BRIGHT_CFG_UPPER_THRESHOLD)
hwHelligkeitssensorSetConfiguration(state, newValue, objectId);
}
else if (classId == CLASS_ID_FEUCHTESENSOR)
{
if (state == HUMIDITY_CFG_CALIBRATION ||
state == HUMIDITY_CFG_HYSTERESIS ||
state == HUMIDITY_CFG_LOWER_THRESHOLD ||
state == HUMIDITY_CFG_MAX_REPORT_TIME ||
state == HUMIDITY_CFG_MIN_REPORT_TIME ||
state == HUMIDITY_CFG_REPORT_TIME_MULTIPLIER ||
state == HUMIDITY_CFG_UPPER_THRESHOLD)
hwFeuchteSensorSetConfiguration(state, newValue, objectId);
}
}
function getMyParams(value, nrParams)
{
var result = {};
for (var i=0;i<nrParams;i++)
{
result[i]=0;
}
if (typeof value == "string")
{
var params = value.split(",");
for (var i=0;i<nrParams;i++)
{
if (typeof params[i]!="undefined") result[i] = parseInt(params[i]);
}
}
return result;
}
function getStateFromIoBrokerId(ioBrokerId)
{
return ioBrokerId.substring(ioBrokerId.lastIndexOf(".")+1);
}
function getDeviceIdFromIoBrokerId(ioBrokerId)
{
var parts = ioBrokerId.split(".");
if (parts.length>2) return parts[2];
return 1;
}
function hwControllerReceivedEvStarted(receiverObjectId)
{
info("controller started <- "+objectIdToString(receiverObjectId));
hwControllerGetModuleId(receiverObjectId);
}
// TEMPERATURSENSOR
function hwTemperatursensorReceivedEvents(sender, receiver, functionId, message, dataLength)
{
var instanceId = getInstanceId(sender);
var deviceId = getDeviceId(sender);
var pos = DATA_START;
var state="";
if (functionId==200) state="COLD";