-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
1214 lines (1100 loc) · 37.2 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
"use strict";
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
const utils = require("@iobroker/adapter-core");
// eslint-disable-next-line no-unused-vars
const helper = require("./lib/helper");
const init = require("./lib/init");
const timers = require("./lib/timers");
const switchingOnOff = require("./lib/switchingOnOff");
const lightHandling = require("./lib/lightHandling");
const { params } = require("./lib/params");
//const { objects } = require("./lib/objects");
// Sentry error reporting, disable when testing alpha source code locally!
const disableSentry = false;
class Lightcontrol extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "lightcontrol",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("objectChange", this.onObjectChange.bind(this));
this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
this.Settings = {};
this.LightGroups = {};
this.LuxSensors = [];
this.MotionSensors = [];
this.activeStates = []; // Array of activated states for LightControl
this.ActualGenericLux = 0;
this.ActualPresence = true;
this.ActualPresenceCount = { newVal: 1, oldVal: 1 };
this.RampOnIntervalObject = {};
this.RampOffIntervalObject = {};
this.AutoOffTimeoutObject = {};
this.AutoOffNoticeTimeoutObject = {};
this.TickerIntervall = null;
this.BlinkIntervalObj = {};
this.lat = "";
this.lng = "";
this.DevMode = false;
this.processing = false;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.GlobalSettings = this.config;
this.Settings = this.config;
this.writeLog(`[ onReady ] LightGroups from Settings: ${JSON.stringify(this.Settings.LightGroups)}`);
//Create LightGroups Object from GroupNames
await init.CreateLightGroupsObject(this);
this.log.debug(JSON.stringify(this.LightGroups));
//Create all States, Devices and Channels
if (Object.keys(this.LightGroups).length !== 0) {
await init.Init(this);
await this.InitCustomStates();
await this.SetLightState();
} else {
this.writeLog(`[ onReady ] No Init because no LightGroups defined in settings`);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
timers.clearRampOnIntervals(this, null);
timers.clearRampOffIntervals(this, null);
timers.clearBlinkIntervals(this, null);
timers.clearAutoOffTimeouts(this, null);
this.clearTimeout(this.TickerIntervall);
callback();
} catch (error) {
this.errorHandling(error, "onUnload");
callback();
}
}
/**
* Is called if an object changes to ensure (de-) activation of calculation or update configuration settings
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
async onObjectChange(id, obj) {
//ToDo : Verify with test-results if debounce on object change must be implemented
try {
if (!this.processing) {
this.processing = true;
const stateID = id;
// Check if object is activated for LightControl
if (obj && obj.common) {
// Verify if custom information is available regarding LightControl
if (
obj.common.custom &&
obj.common.custom[this.namespace] &&
obj.common.custom[this.namespace].enabled
) {
//Check if its an own Lightcontrol State
if (stateID.includes(this.namespace)) {
this.writeLog(
`[ onObjectChange ] This Object-ID: "${stateID}" is not allowed, because it's an LightControl State! The settings will be deaktivated automatically!`,
"warn",
);
const stateInfo = await this.getForeignObjectAsync(stateID);
if (stateInfo?.common?.custom) {
stateInfo.common.custom[this.namespace].enabled = false;
await this.setForeignObjectAsync(stateID, stateInfo);
}
} else {
this.writeLog(
`[ onObjectChange ] Object array of LightControl activated state changed : ${JSON.stringify(
obj,
)} stored Objects : ${JSON.stringify(this.activeStates)}`,
);
// Verify if the object was already activated, if not initialize new parameter
if (!this.activeStates.includes(stateID)) {
this.writeLog(`[ onObjectChange ] Enable LightControl for : ${stateID}`, "info");
await this.buildLightGroupParameter(stateID);
if (!this.activeStates.includes(stateID)) {
this.writeLog(
`[ onObjectChange ] Cannot enable LightControl for ${stateID}, check settings and error messages`,
"warn",
);
}
} else {
this.writeLog(
`[ onObjectChange ] Updating LightControl configuration for : ${stateID}`,
);
//Cleaning LightGroups from ID and set it new
await this.deleteStateIdFromLightGroups(stateID);
await this.buildLightGroupParameter(stateID);
if (!this.activeStates.includes(stateID)) {
this.writeLog(
`[ onObjectChange ] Cannot update LightControl configuration for ${stateID}, check settings and error messages`,
"warn",
);
}
}
}
} else if (this.activeStates.includes(stateID)) {
this.activeStates = await helper.removeValue(this.activeStates, stateID);
this.writeLog(`[ onObjectChange ] Disabled LightControl for : ${stateID}`, "info");
await this.deleteStateIdFromLightGroups(stateID);
this.writeLog(
`[ onObjectChange ] Active state array after deactivation of ${stateID} : ${
this.activeStates.length === 0 ? "empty" : JSON.stringify(this.activeStates)
}`,
);
this.writeLog(
`[ onObjectChange ] LightGroups after deactivation of ${stateID} : ${JSON.stringify(
this.LightGroups,
)}`,
);
this.unsubscribeForeignStates(stateID);
}
this.processing = false;
} else {
// Object change not related to this adapter, ignoring
}
}
} catch (error) {
this.errorHandling(error, "onObjectChange");
}
}
/**
* Is called if a message is comming
*/
async onMessage(msg) {
this.writeLog(`[ onMessage ] Incomming Message from: ${JSON.stringify(msg)}`);
if (msg.callback) {
switch (msg.command) {
case "LightGroup": {
try {
const groups = [];
if (Object.keys(this.LightGroups).length !== 0) {
for (const Group in this.LightGroups) {
// iterate through all existing groups and extract group names
if (Group === "All") continue;
groups.push({ value: Group, label: Group });
}
}
this.sendTo(msg.from, msg.command, groups, msg.callback);
this.writeLog(`[ onMessage ] LightGroup => LightGroups Callback: ${JSON.stringify(groups)}.`);
} catch (error) {
this.errorHandling(error, "onMessage // case LightGroup");
}
break;
}
case "LightName": {
try {
const lightGroups = msg.message.LightGroups;
const DEFAULT_LIGHT = { value: "Example_Light", label: "Example_Light" };
this.writeLog(`[ onMessage ] LightName => getLights for Groups: ${lightGroups}.`);
const lights = [];
if (
lightGroups &&
this.LightGroups &&
Object.prototype.hasOwnProperty.call(this.LightGroups, lightGroups)
) {
const group = this.LightGroups[lightGroups];
if (group && group.lights) {
for (const light of group.lights) {
lights.push({ value: light.description, label: light.description });
this.writeLog(
`[ onMessage ] LightName => Light: ${light.description} in Group: ${lightGroups} found.`,
);
}
}
}
if (!lights.length) {
lights.push(DEFAULT_LIGHT);
}
this.sendTo(msg.from, msg.command, lights, msg.callback);
} catch (error) {
this.errorHandling(error, "onMessage // case LightName");
}
break;
}
case "id": {
try {
const value = msg.message.value;
this.writeLog(`[ onMessage ] id => Set new ID. Value = ${value}.`);
if (msg.message.value !== null) {
this.sendTo(msg.from, msg.command, value, msg.callback);
} else {
const oldID = this.config._id;
const newID = oldID + 1;
await this.extendForeignObjectAsync("system.adapter." + this.namespace, {
native: { _id: newID },
});
this.writeLog(`[ onMessage ] id => Set new ID. OldID = ${oldID}, NewID = ${newID}`);
this.sendTo(msg.from, msg.command, newID.toString(), msg.callback);
}
} catch (error) {
this.errorHandling(error, "onMessage // case id");
}
break;
}
case "checkIdForDuplicates": {
try {
this.writeLog(`[ onMessage ] checkcheckIdForDuplicates`);
this.writeLog(JSON.stringify(msg.message));
const LightGroups = msg.message.LightGroups;
if (LightGroups && LightGroups !== undefined) {
const arr = [];
for (const Group of LightGroups) {
arr.push(Group.Group);
}
this.writeLog(`[ onMessage ] checkcheckIdForDuplicates: ${arr}`);
// empty object
const map = {};
let result = false;
for (let i = 0; i < arr.length; i++) {
// check if object contains entry with this element as key
if (map[arr[i]]) {
result = true;
// terminate the loop
break;
}
// add entry in object with the element as key
map[arr[i]] = true;
}
if (!result) {
this.writeLog(`[ onMessage ] checkcheckIdForDuplicates: No duplicates.`);
this.sendTo(msg.from, msg.command, "", msg.callback);
} else {
this.writeLog(
`[ onMessage ] Define LightGroups => checkcheckIdForDuplicates: Duplicate GroupNames found.`,
"warn",
);
this.sendTo(msg.from, msg.command, "labelDuplicateGroup", msg.callback);
}
} else {
this.sendTo(msg.from, msg.command, "", msg.callback);
}
} catch (error) {
this.errorHandling(error, "onMessage // case checkIdForDuplicates");
}
break;
}
}
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
try {
const ids = id.split(".");
if (state && state.val === null) {
this.writeLog(`Null or empty value not allowed! Please set a value!`, "warn");
return;
}
if (state && state.val !== null) {
this.writeLog(`[ onStateChange ] state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (ids[0] == "lightcontrol") {
if (!state.ack) {
const NewVal = state.val;
let OldVal;
const OwnId = await helper.removeNamespace(this, id);
const { Group, Prop } = await helper.ExtractGroupAndProp(OwnId);
if (Prop === "power" && Group !== "All") {
OldVal = this.LightGroups[Group].powerOldVal = this.LightGroups[Group].powerNewVal;
this.LightGroups[Group].powerNewVal = NewVal;
}
if (Group === "All") {
await switchingOnOff.SetMasterPower(this, NewVal);
} else {
await this.Controller(Group, Prop, NewVal, OldVal, OwnId);
}
}
} else {
//Handle External States
if (state.ack || !state.ack) {
this.writeLog(`[ onStateChange ] ExternalState`);
//Check if it's a LuxSensor
if (this.LuxSensors.includes(id)) {
const groupsWithLuxSensor = Object.values(this.LightGroups).filter(
(Group) => Group.LuxSensor === id,
);
for (const Group of groupsWithLuxSensor) {
if (state.val !== Group.actualLux) {
this.writeLog(
`[ onStateChange ] It's a LuxSensor in following Group: ${Group.description} with value = ${state.val} (old value = ${Group.actualLux})`,
);
Group.actualLux = state.val;
await this.Controller(
Group.description,
"actualLux",
state.val,
Group.actualLux,
"",
);
}
}
//Check if it's a MotionSensor
} else if (this.MotionSensors.includes(id)) {
for (const Group in this.LightGroups) {
if (Group === "All") continue;
for (const Sensor of this.LightGroups[Group].sensors) {
if (Sensor.oid === id) {
this.writeLog(
`[ onStateChange ] It's a MotionSensor in following Group: ${Group}`,
);
if (state.val === Sensor.motionVal) {
//Inhalt lesen und neues Property anlegen und füllen
Sensor.isMotion = true;
this.writeLog(
`[ onStateChange ] Sensor in Group="${Group}". This isMotion="true"`,
);
} else {
Sensor.isMotion = false;
this.writeLog(
`[ onStateChange ] Sensor in Group="${Group}". This isMotion="false"`,
);
}
await this.SummarizeSensors(Group).catch((e) => this.log.error(e));
break;
}
}
}
//Check if it's Presence
} else if (this.Settings.IsPresenceDp === id) {
this.writeLog(`[ onStateChange ] It's IsPresenceDp: ${id}`);
this.ActualPresence = typeof state.val === "boolean" ? state.val : false;
await switchingOnOff.AutoOnPresenceIncrease(this).catch((e) => this.log.error(e));
//Check if it's Presence Counter
} else if (this.Settings.PresenceCountDp === id) {
this.writeLog(`[ onStateChange ] It's PresenceCountDp: ${id}`);
this.ActualPresenceCount.oldVal = this.ActualPresenceCount.newVal;
this.ActualPresenceCount.newVal = typeof state.val === "number" ? state.val : 0;
if (this.ActualPresenceCount.newVal > this.ActualPresenceCount.oldVal) {
this.writeLog(
`[ onStateChange ] PresenceCountDp value is greater than old value: ${state.val}`,
);
await switchingOnOff.AutoOnPresenceIncrease(this).catch((e) => this.log.error(e));
}
}
}
}
} else {
// The state was deleted
this.writeLog(`[ onStateChange ] state ${id} deleted`);
}
} catch (error) {
this.errorHandling(error, "onStateChange");
}
}
/**
* Init all Custom states
* @description Init all Custom states
*/
async InitCustomStates() {
try {
// Get all objects with custom configuration items
const customStateArray = await this.getObjectViewAsync("system", "custom", {});
this.writeLog(`[ InitCustomStates ] All states with custom items : ${JSON.stringify(customStateArray)}`);
// List all states with custom configuration
if (customStateArray && customStateArray.rows) {
// Verify first if result is not empty
// Loop truth all states and check if state is activated for LightControl
for (const index in customStateArray.rows) {
if (customStateArray.rows[index].value) {
// Avoid crash if object is null or empty
// Check if custom object contains data for LightControl
// @ts-ignore
if (customStateArray.rows[index].value[this.namespace]) {
this.writeLog(`[ InitCustomStates ] LightControl configuration found`);
// Simplify stateID
const stateID = customStateArray.rows[index].id;
// Check if custom object is enabled for LightControl
// @ts-ignore
if (customStateArray.rows[index].value[this.namespace].enabled) {
if (!this.activeStates.includes(stateID)) this.activeStates.push(stateID);
this.writeLog(`[ InitCustomStates ] LightControl enabled state found ${stateID}`);
} else {
this.writeLog(
`[ InitCustomStates ] LightControl configuration found but not Enabled, skipping ${stateID}`,
);
}
}
}
}
}
const totalEnabledStates = this.activeStates.length;
let totalInitiatedStates = 0;
let totalFailedStates = 0;
this.writeLog(`Found ${totalEnabledStates} LightControl enabled states`, "info");
// Initialize all discovered states
let count = 1;
for (const stateID of this.activeStates) {
this.writeLog(`[ InitCustomStates ] Initialising (${count} of ${totalEnabledStates}) "${stateID}"`);
await this.buildLightGroupParameter(stateID);
if (this.activeStates.includes(stateID)) {
totalInitiatedStates = totalInitiatedStates + 1;
this.writeLog(`Initialization of ${stateID} successfully`, "info");
} else {
this.writeLog(
`[ InitCustomStates ] Initialization of ${stateID} failed, check warn messages !`,
"warn",
);
totalFailedStates = totalFailedStates + 1;
}
count = count + 1;
}
// Subscribe on all foreign objects to detect (de)activation of LightControl enabled states
await this.subscribeForeignObjectsAsync("*");
this.writeLog(
`[ InitCustomStates ] subscribed all foreign objects to detect (de)activation of LightControl enabled states`,
);
if (totalFailedStates > 0) {
this.writeLog(
`[ InitCustomStates ] Cannot handle calculations for ${totalFailedStates} of ${totalEnabledStates} enabled states, check error messages`,
"warn",
);
}
this.writeLog(
`Successfully activated LightControl for ${totalInitiatedStates} of ${totalEnabledStates} states, will do my Job until you stop me!`,
"info",
);
} catch (error) {
this.errorHandling(error, "InitCustomStates");
}
}
/**
* Load state definitions to memory this.activeStates[stateID]
* @param {string} stateID ID of state to refresh memory values
*/
async buildLightGroupParameter(stateID) {
this.writeLog(`[ buildStateDetailsArray ] started for ${stateID}`);
try {
let stateInfo;
try {
// Load configuration as provided in object
/** @type {ioBroker.StateObject} */
stateInfo = await this.getForeignObjectAsync(stateID);
if (!stateInfo) {
this.writeLog(
`[ buildStateDetailsArray ] Can't get information for ${stateID}, state will be ignored`,
"warn",
);
this.activeStates = await helper.removeValue(this.activeStates, stateID);
this.unsubscribeForeignStates(stateID);
return;
}
} catch (error) {
this.writeLog(
`[ buildStateDetailsArray ] ${stateID} is incorrectly correctly formatted, ${JSON.stringify(
error,
)}`,
"error",
);
this.activeStates = await helper.removeValue(this.activeStates, stateID);
this.unsubscribeForeignStates(stateID);
return;
}
// Check if configuration for LightControl is present, trow error in case of issue in configuration
if (stateInfo && stateInfo.common && stateInfo.common.custom && stateInfo.common.custom[this.namespace]) {
const customData = stateInfo.common.custom[this.namespace];
const LightGroup = this.LightGroups[customData.group];
//Check if a Groupname defined
if (!customData.group) {
this.writeLog(
`[ buildStateDetailsArray ] No Group Name defined for StateID: ${stateID}. Initalisation aborted`,
"warn",
);
return;
}
//Check if a Group in LightGroups is available or deleted by user
if (!LightGroup) {
//If checkbox for removing lights and sensor setting is acitaved in instance settings
if (this.Settings.deleteUnusedConfig) {
this.writeLog(
`[ buildStateDetailsArray ] Light group "${customData.group}" was deleted by the user in the instance settings! LightControl settings will be deactivated for this StateID: ${stateID})`,
"warn",
);
this.writeLog(
`[ buildStateDetailsArray ] Object before deactivating: ${JSON.stringify(stateInfo)}`,
);
stateInfo.common.custom[this.namespace].enabled = false;
this.writeLog(
`[ buildStateDetailsArray ] Object after deactivating: ${JSON.stringify(stateInfo)}`,
);
await this.setForeignObjectAsync(stateID, stateInfo);
} else {
this.writeLog(
`[ buildStateDetailsArray ] Light group "${customData.group}" was deleted by the user in the instance settings! (StateID: ${stateID})`,
"warn",
);
}
return;
}
//const commonData = stateInfo.common;
this.writeLog(`[ buildLightGroupParameter ] customData ${JSON.stringify(customData)}`);
//Covert string to boolean and numbers
for (const key in customData) {
const val = customData[key];
if (val === "false") {
customData[key] = false;
} else if (val === "true") {
customData[key] = true;
} else if (parseFloat(val)) {
customData[key] = parseFloat(val);
}
}
//Add Id to custom data
customData.oid = stateID;
/*
CustomData Example
{
"enabled": true,
"defaultBri": "100",
"whiteModeVal": "false",
"colorModeVal": "true",
"colorType": "hex",
"defaultColor": "#FFFFFF",
"sendCt": true,
"sendSat": true,
"sendColor": true,
"sendModeswitch": true,
"useBri": true,
"type": "light",
"func": "bri",
"onVal": 1,
"offVal": 0,
"minVal": 0,
"maxVal": 100,
"unit": "s",
"motionVal": "On",
"noMotionVal": "Off",
"group": "Wohnzimmer",
"description": "Licht1"
}
*/
// Function to reduce the customData
const getSubset = (obj, ...keys) => keys.reduce((a, c) => ({ ...a, [c]: obj[c] }), {});
switch (customData.type) {
case "light": {
//Check if a Lightname is available
if (!customData.description) {
this.writeLog(
`[ buildStateDetailsArray ] No Lightname defined. Initalisiation aborted`,
"warn",
);
return;
}
if (LightGroup.lights && Array.isArray(LightGroup.lights)) {
const Lights = LightGroup.lights;
// Überprüfen, ob jedes Objekt eine description-Eigenschaft hat
const allObjectsHaveDescription = Lights.every(
(x) => x && typeof x.description === "string",
);
if (allObjectsHaveDescription) {
///Find index in Lights Array if description available
const index = Lights.findIndex((x) => x.description === customData.description);
let Light;
if (await helper.isNegative(index)) {
Light = Lights.length === 0 ? (Lights[0] = {}) : (Lights[Lights.length] = {});
} else {
Light = Lights[index];
}
// Add parameters to Light
Light.description = customData.description;
Light[customData.func] = getSubset(customData, ...params[customData.func]);
this.writeLog(
`[ buildStateDetailsArray ] Type: Light, in Group: ${
LightGroup.description
} with Lights: ${JSON.stringify(Lights)} and Light: ${JSON.stringify(
Light,
)} with Index: ${index}`,
);
} else {
this.writeLog(
`[ buildStateDetailsArray ] Any Light of Group=${LightGroup.description} has no own description. Init aborted`,
"warn",
);
}
} else {
this.errorHandling(
`Any Light has no description. Init aborted. No Index found`,
"buildStateDetailsArray",
JSON.stringify(LightGroup.lights),
);
return;
}
break;
}
case "sensor": {
this.writeLog(`[ buildStateDetailsArray ] Type: Sensor in Group ${LightGroup.description}}`);
const Sensors = LightGroup.sensors;
Sensors.push({
oid: customData.oid,
motionVal: customData.motionVal,
noMotionVal: customData.noMotionVal,
});
await init.DoAllTheMotionSensorThings(this, customData.group);
break;
}
default:
break;
}
//Push stateID after processing
if (!this.activeStates.includes(stateID)) this.activeStates.push(stateID);
this.writeLog(`[ buildStateDetailsArray ] completed for ${stateID}.`);
this.writeLog(`[ buildStateDetailsArray ] Updated LightGroups: ${JSON.stringify(this.LightGroups)}`);
}
} catch (error) {
this.errorHandling(error, "buildStateDetailsArray");
}
}
/**
* Is called from onStateChange
* @param {string} Group Any Group of Lightgroups
* @param {string} prop1 Which State has changed
* @param {any} NewVal New Value of Datapoint
* @param {any} OldVal Old Value of Datapoint
* @param {string} id Object-ID
* @param {boolean} stateNull State is null
*/
async Controller(Group, prop1, NewVal, OldVal, id = "", stateNull = false) {
//Used by all
try {
const LightGroups = this.LightGroups;
let handeled = false;
this.writeLog(
`[ Controller ] Reaching, Group="${Group}" Property="${prop1}" NewVal="${NewVal}", ${
OldVal === undefined ? "" : "OldVal=" + OldVal
}"`,
"info",
);
if (!stateNull) {
if (prop1 !== "power") await helper.SetValueToObject(LightGroups[Group], prop1, NewVal);
}
switch (prop1) {
case "actualLux":
if (!LightGroups[Group].powerCleaningLight) {
//Autofunktionen nur wenn Putzlicht nicht aktiv
await switchingOnOff.AutoOnLux(this, Group);
await switchingOnOff.AutoOffLux(this, Group);
if (this.LightGroups[Group].adaptiveBri)
await lightHandling.SetBrightness(
this,
Group,
await lightHandling.AdaptiveBri(this, Group),
);
await switchingOnOff.AutoOnMotion(this, Group);
}
handeled = true;
break;
case "isMotion":
if (!this.LightGroups[Group].powerCleaningLight) {
if (LightGroups[Group].isMotion && LightGroups[Group].power) {
//AutoOff Timer wird nach jeder Bewegung neugestartet
await switchingOnOff.AutoOffTimed(this, Group);
}
await switchingOnOff.AutoOnMotion(this, Group);
}
handeled = true;
break;
case "rampOn.enabled":
break;
case "rampOn.switchOutletsLast":
break;
case "rampOn.time":
break;
case "rampOff.enabled":
break;
case "rampOff.switchOutletsLast":
break;
case "rampOff.time":
break;
case "autoOffTimed.enabled":
break;
case "autoOffTimed.autoOffTime":
break;
case "autoOffTimed.noAutoOffWhenMotion":
break;
case "autoOffTimed.noAutoOffWhenMotionMode":
break;
case "autoOnMotion.enabled":
break;
case "autoOnMotion.minLux":
break;
case "autoOnMotion.bri":
break;
case "autoOnMotion.color":
break;
case "autoOffLux.enabled":
break;
case "autoOffLux.operator":
break;
case "autoOffLux.minLux":
break;
case "autoOffLux.switchOnlyWhenPresence":
break;
case "autoOffLux.switchOnlyWhenNoPresence":
await switchingOnOff.AutoOffLux(this, Group);
handeled = true;
break;
case "autoOnLux.enabled":
break;
case "autoOnLux.operator":
break;
case "autoOnLux.switchOnlyWhenNoPresence":
break;
case "autoOnLux.switchOnlyWhenPresence":
break;
case "autoOnLux.minLux":
break;
case "autoOnLux.bri":
switchingOnOff.AutoOnLux(this, Group);
handeled = true;
break;
case "autoOnPresenceIncrease.enabled":
break;
case "autoOnPresenceIncrease.bri":
break;
case "autoOnPresenceIncrease.color":
break;
case "autoOnPresenceIncrease.minLux":
await switchingOnOff.AutoOnPresenceIncrease(this);
handeled = true;
break;
case "bri":
await lightHandling.SetBrightness(this, Group, LightGroups[Group].bri);
handeled = true;
break;
case "ct":
await lightHandling.SetCt(this, Group, LightGroups[Group].ct);
await lightHandling.SetWhiteSubstituteColor(this, Group);
handeled = true;
break;
case "color":
// @ts-ignore
if (await helper.CheckHex(NewVal)) {
// @ts-ignore
LightGroups[Group].color = NewVal.toUpperCase();
await lightHandling.SetColor(this, Group, LightGroups[Group].color);
if (LightGroups[Group].color == "#FFFFFF")
await lightHandling.SetWhiteSubstituteColor(this, Group);
await lightHandling.SetColorMode(this, Group);
}
handeled = true;
break;
case "power":
if (NewVal !== OldVal) {
await switchingOnOff.GroupPowerOnOff(this, Group, NewVal); //Alles schalten
if (NewVal) await lightHandling.PowerOnAftercare(this, Group);
if (!NewVal && LightGroups[Group].autoOffTimed.enabled) {
//Wenn ausschalten und autoOffTimed ist aktiv, dieses löschen, da sonst erneute ausschaltung nach Ablauf der Zeit. Ist zusätzlich rampon aktiv, führt dieses zu einem einschalten mit sofort folgenden ausschalten
await timers.clearAutoOffTimeouts(this, Group);
}
if (!NewVal && LightGroups[Group].powerCleaningLight) {
//Wenn via Cleaninglight angeschaltet wurde, jetzt aber normal ausgeschaltet, powerCleaningLight synchen um Blockade der Autofunktionen zu vermeiden
LightGroups[Group].powerCleaningLight = false;
await this.setStateAsync(Group + ".powerCleaningLight", false, true);
}
}
handeled = true;
break;
case "powerCleaningLight":
await switchingOnOff.GroupPowerCleaningLightOnOff(this, Group, NewVal);
handeled = true;
break;
case "adaptiveBri":
await lightHandling.SetBrightness(this, Group, await lightHandling.AdaptiveBri(this, Group));
handeled = true;
break;
case "adaptiveCt":
//await lightHandling.SetCt(this, Group, LightGroups[Group].ct);
//handeled = true;
break;
case "adaptiveCtMode":
break;
case "adaptiveCtTime":
break;
case "dimmUp":
await this.setStateAsync(
Group + "." + "bri",
Math.min(Math.max(LightGroups[Group].bri + LightGroups[Group].dimmAmount, 10), 100),
false,
);
handeled = true;
break;
case "dimmDown":
await this.setStateAsync(
Group + "." + "bri",
Math.min(Math.max(LightGroups[Group].bri - LightGroups[Group].dimmAmount, 2), 100),
false,
);
handeled = true;
break;
case "dimmAmount":
break;
case "blink.blinks":
break;
case "blink.frequency":
break;
case "blink.bri":
break;
case "blink.color":
break;
case "blink.enabled":
if (NewVal && NewVal !== OldVal) {
await helper.SetValueToObject(LightGroups[Group], "blink.infinite", true);
await helper.SetValueToObject(LightGroups[Group], "blink.stop", false);
await switchingOnOff.blink(this, Group);
} else if (!NewVal) {
await helper.SetValueToObject(LightGroups[Group], "blink.stop", true);
}
handeled = true;
break;
case "blink.start":
await helper.SetValueToObject(LightGroups[Group], ["blink.stop", "blink.infinite"], false);
await switchingOnOff.blink(this, Group);
break;
default:
this.writeLog(`[ Controller ] Error, unknown or missing property: "${prop1}"`, "warn");
handeled = true;
}
if (!handeled) {
if (id !== "") {
await this.setStateAsync(id, NewVal, true);
}
}
} catch (error) {
this.errorHandling(error, "Controller");
}
}
/**
* SummarizeSensors
* @param {string} Group
*/
async SummarizeSensors(Group) {
try {
this.writeLog(`[ SummarizeSensors ] Reaching, Group="${Group}"`);
let Motionstate = false;
for (const Sensor of this.LightGroups[Group].sensors) {
if (Sensor.isMotion) {
this.writeLog(
`[ SummarizeSensors ] Group="${Group}" Sensor with target "${Sensor.oid}" has value ${Sensor.isMotion}`,
);
Motionstate = true;
}
}
if (this.LightGroups[Group].isMotion !== Motionstate) {
this.writeLog(
`[ SummarizeSensors ] Summarized IsMotion for Group="${Group}" = ${Motionstate}, go to Controller...`,
);
this.LightGroups[Group].isMotion = Motionstate;