-
Notifications
You must be signed in to change notification settings - Fork 1
/
98_weekprofile.pm
2258 lines (1956 loc) · 74.5 KB
/
98_weekprofile.pm
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
##############################################
# $Id: 98_weekprofile.pm 25318 2022-10-10 Beta-User - follow reference $
#
# Usage
#
# define <name> weekprofile [device]
##############################################
package main;
use strict;
use warnings;
use JSON; #libjson-perl
use Time::HiRes qw(gettimeofday);
use Storable qw(dclone);
use vars qw(%defs);
use vars qw($FW_ME);
use vars qw($FW_wname);
use vars qw($FW_subdir);
use vars qw($init_done);
use vars qw($readingFnAttributes);
use vars qw(%attr);
use vars qw(%FW_hiddenroom);
my @shortDays = ("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
my %LAST_SEND;
my @DEVLIST_SEND = ("MAX","CUL_HM","HMCCUDEV","HMCCUCHN","weekprofile","dummyWT","WeekdayTimer","MQTT2_DEVICE");
my $CONFIG_VERSION = "1.1";
my %DEV_READINGS;
# MAX
$DEV_READINGS{"Mon"}{"MAX"} = "weekprofile-2-Mon";
$DEV_READINGS{"Tue"}{"MAX"} = "weekprofile-3-Tue";
$DEV_READINGS{"Wed"}{"MAX"} = "weekprofile-4-Wed";
$DEV_READINGS{"Thu"}{"MAX"} = "weekprofile-5-Thu";
$DEV_READINGS{"Fri"}{"MAX"} = "weekprofile-6-Fri";
$DEV_READINGS{"Sat"}{"MAX"} = "weekprofile-0-Sat";
$DEV_READINGS{"Sun"}{"MAX"} = "weekprofile-1-Sun";
# CUL_HM
$DEV_READINGS{"Mon"}{"CUL_HM"} = "2_tempListMon";
$DEV_READINGS{"Tue"}{"CUL_HM"} = "3_tempListTue";
$DEV_READINGS{"Wed"}{"CUL_HM"} = "4_tempListWed";
$DEV_READINGS{"Thu"}{"CUL_HM"} = "5_tempListThu";
$DEV_READINGS{"Fri"}{"CUL_HM"} = "6_tempListFri";
$DEV_READINGS{"Sat"}{"CUL_HM"} = "0_tempListSat";
$DEV_READINGS{"Sun"}{"CUL_HM"} = "1_tempListSun";
# TYPE=HMCCUDEV or HMCCUCHN
# ccutype=HmIP.*
$DEV_READINGS{"Mon"}{"HMCCU_IP"} = "MONDAY";
$DEV_READINGS{"Tue"}{"HMCCU_IP"} = "TUESDAY";
$DEV_READINGS{"Wed"}{"HMCCU_IP"} = "WEDNESDAY";
$DEV_READINGS{"Thu"}{"HMCCU_IP"} = "THURSDAY";
$DEV_READINGS{"Fri"}{"HMCCU_IP"} = "FRIDAY";
$DEV_READINGS{"Sat"}{"HMCCU_IP"} = "SATURDAY";
$DEV_READINGS{"Sun"}{"HMCCU_IP"} = "SUNDAY";
# TYPE=HMCCUDEV or HMCCUCHN
# ccutype = HM-.*
$DEV_READINGS{"Mon"}{"HMCCU_HM"} = "MONDAY";
$DEV_READINGS{"Tue"}{"HMCCU_HM"} = "TUESDAY";
$DEV_READINGS{"Wed"}{"HMCCU_HM"} = "WEDNESDAY";
$DEV_READINGS{"Thu"}{"HMCCU_HM"} = "THURSDAY";
$DEV_READINGS{"Fri"}{"HMCCU_HM"} = "FRIDAY";
$DEV_READINGS{"Sat"}{"HMCCU_HM"} = "SATURDAY";
$DEV_READINGS{"Sun"}{"HMCCU_HM"} = "SUNDAY";
sub weekprofile_findPRF($$$$);
##############################################
sub weekprofile_minutesToTime($)
{
my ($minutes) = @_;
my $hours =($minutes - $minutes % 60)/60;
$minutes = $minutes - $hours * 60;
if (length($hours) eq 1){
$hours = "0$hours";
}
if (length($minutes) eq 1){
$minutes = "0$minutes";
}
return "$hours:$minutes";
}
##############################################
sub weekprofile_timeToMinutes($)
{
my ($time) = @_;
my ($hours, $minutes) = split(':',$time, 2);
return $hours * 60 + $minutes;
}
##############################################
sub tempValue($) {
my ($in) = @_;
my $val = sprintf("%.1f", $in);
return $val;
}
##############################################
sub weekprofile_getDeviceType($$;$)
{
my ($me,$device,$sndrcv) = @_;
if (IsDummy($device)){
Log3($me, 4, "$me(getDeviceType): $device is dummy - ignored");
return undef;
}
if (IsIgnored($device)){
Log3($me, 4, "$me(getDeviceType): $device is ignored");
return undef;
}
$sndrcv = "RCV" if (!defined($sndrcv));
# determine device type
my $devHash = $main::defs{$device};
if (!defined($devHash)){
return undef;
}
my $type = undef;
my $devType = $devHash->{TYPE};
$devType = $devHash->{wt_type} if ($devType =~ /dummyWT/); #special dummy fake WT for testing
Log3($me, 5, "$me(getDeviceType): type: $devType");
if ($devType =~ /CUL_HM/){
my $model = AttrVal($device,"model","");
my $readonly = AttrVal($device,"readOnly",0);
if ($readonly) {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME} is readonly - ignored");
return undef;
}
#models: HM-TC-IT-WM-W-EU, HM-CC-RT-DN, HM-CC-TC
unless ($model =~ m/.*HM-[C|T]C-.*/) {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME}, model $model is not supported");
return undef;
}
if (!defined($devHash->{chanNo})) { #no channel device
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME}, model $model has no chanNo");
return undef;
}
my $channel = $devHash->{chanNo};
unless ($channel =~ /^\d+?$/) {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME}, model $model chanNo $channel is no number");
return undef;
}
$channel += 0;
Log3($me, 5, "$me(getDeviceType): $devHash->{NAME}, $model, $channel");
$type = "CUL_HM" if ( ($model =~ m/.*HM-CC-RT.*/) && ($channel == 4) );
$type = "CUL_HM" if ( ($model =~ m/.*HM-TC.*/) && ($channel == 2) );
$type = "CUL_HM" if ( ($model =~ m/.*HM-CC-TC.*/) && ($channel == 2) );
}
#avoid max shutter contact
elsif ( ($devType =~ /MAX/) && ($devHash->{type} =~ /.*Thermostat.*/) ){
$type = "MAX";
}
elsif ( $devType =~ /HMCCUDEV/ or $devType =~/HMCCUCHN/){
my $readonly = AttrVal($device,"readOnly",0);
if ($readonly) {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME} is readonly - ignored");
return undef;
}
my $model = $devHash->{ccutype};
if (!defined($model)) {
Log3($me, 4, "$me(getDeviceType): ccutype not defined for device $device - take NAME");
$model = $devHash->{NAME};
return undef if (!defined($model));
}
Log3($me, 5, "$me(getDeviceType): $devHash->{NAME}, $model");
$type = "HMCCU_IP" if ( $model =~ m/HmIP.*/ );
$type = "HMCCU_HM" if ( $model =~ m/HM-.*/ );
}
return $type if ($sndrcv eq "RCV");
if ($devType =~ /weekprofile/){
$type = "WEEKPROFILE";
}
elsif ($devType =~ /WeekdayTimer/){
my $def = $defs{$device}{DEF};
Log3($me, 5, "$me(getDeviceType): def WDT $def");
#if (index($def, $me) != -1) {
if ($def =~ m/.*weekprofile.*/) {
$type = "WDT";
}
else {
Log3($me, 4, "$me(getDeviceType): found WDT but not configured for weekprofile");
}
}
elsif ($devType eq 'MQTT2_DEVICE'){
my $attr = AttrVal($device,'weekprofile','');
Log3($me, 5, "$me(getDeviceType): attr MQTT2_DEVICE $attr");
if ($attr ne "") {
$type = "MQTT2_DEVICE";
}
else {
Log3($me, 4, "$me(getDeviceType): found MQTT2_DEVICE but not configured for weekprofile");
}
}
if (defined($type)) {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME} is type $type");
} else {
Log3($me, 4, "$me(getDeviceType): $devHash->{NAME} is not supported");
}
return $type;
}
##############################################
sub weekprofile_get_prefix_HM($@)
{
my ($device,$base_name,$me) = @_;
my @prefix_lst = ("","R-1.P1_","R-","R-P1_","P1_");
my $prefix = undef;
foreach (@prefix_lst) {
my $reading = "$_"."$base_name";
my $time = ReadingsVal($device, $reading, "");
Log3($me, 5, "$me(weekprofile_get_prefix_HM): check: $reading $time");
if ($time ne "") {
$prefix = $_;
last;
}
}
return $prefix;
}
##############################################
sub weekprofile_readDayProfile($@)
{
my ($device,$day,$type,$me) = @_;
my @times;
my @temps;
$type = weekprofile_getDeviceType($me,$device) if (!defined($type));
return if (!defined($type));
Log3($me, 5, "$me(readDayProfile): read from type $type");
my $reading = $DEV_READINGS{$day}{$type};
#Log3($me, 5, "$me(ReadDayProfile): $reading");
if($type eq "MAX") {
@temps = split('/',ReadingsVal($device,"$reading-temp",""));
@times = split('/',ReadingsVal($device,"$reading-time",""));
# only use to from interval 'from-to'
for(my $i = 0; $i < scalar(@times); $i+=1){
my $interval = $times[$i];
my @parts = split('-',$interval);
$times[$i] = ($parts[1] ne "00:00") ? $parts[1] : "24:00";
}
} elsif ($type eq "CUL_HM") {
# get temp list for the day
my $prf = ReadingsVal($device,"R_$reading","");
$prf = ReadingsVal($device,"R_P1_$reading","") if (!$prf); #HM-TC-IT-WM-W-EU
# split into time temp time temp etc.
# 06:00 17.0 22:00 21.0 24:00 17.0
my @timeTemp = split(' ', $prf);
for(my $i = 0; $i < scalar(@timeTemp); $i += 2) {
push(@times, $timeTemp[$i]);
push(@temps, $timeTemp[$i+1]);
}
}
elsif ($type =~ /HMCCU.*/){
my $lastTime = "";
my $prefix = weekprofile_get_prefix_HM($device,"ENDTIME_$reading"."_1",$me);
if (!defined($prefix)) {
Log3($me, 2, "$me(readDayProfile): no readings for $reading found");
return (\@times, \@temps);
}
Log3($me, 5, "$me(readDayProfile): HM-Prefix is $prefix");
for (my $i = 1; $i < 14; $i+=1){
my $prfTime = ReadingsVal($device, "$prefix"."ENDTIME_$reading"."_$i", "");
my $prfTemp = ReadingsVal($device, "$prefix"."TEMPERATURE_$reading"."_$i", "");
if ($prfTime eq "" or $prfTemp eq ""){
Log3($me, 2, "$me(readDayProfile): no readings for $reading found ($i)");
return (\@times, \@temps);
}
else{
Log3($me, 5, "$me(readDayProfile): $reading"."_$i $prfTime $prfTemp");
}
$prfTime = weekprofile_minutesToTime($prfTime);
if ($lastTime ne "24:00"){
$lastTime = $prfTime;
push(@temps, $prfTemp);
push(@times, $prfTime);
}
}
}
else {
Log3($me, 3, "$me(readDayProfile): unsupported device type $type");
}
my $hash = $defs{$me};
for(my $i = 0; $i < scalar(@temps); $i+=1) {
Log3($me, 5, "$me(ReadDayProfile): temp $i $temps[$i]");
my $tv = "\"".$temps[$i]."\"";
$tv = weekprofile_replaceKeywords($hash, $tv, 'toValue');
$tv =~s/\"//g;
$tv =~s/[^\d.]//g; #only numbers
$temps[$i] = tempValue($tv);
}
for(my $i = 0; $i < scalar(@times); $i+=1){
$times[$i] =~ s/^\s+|\s+$//g; #trim whitespace both ends
}
return (\@times, \@temps);
}
##############################################
sub weekprofile_readDevProfile(@)
{
my ($device,$type,$me) = @_;
$type = weekprofile_getDeviceType($me, $device) if (!defined($type));
return "" if (!defined ($type));
my $prf = {};
my $logDaysWarning="";
my $logDaysCnt=0;
foreach my $day (@shortDays){
my ($dayTimes, $dayTemps) = weekprofile_readDayProfile($device,$day,$type,$me);
if (scalar(@{$dayTemps})==0) {
push(@{$dayTimes}, "24:00");
push(@{$dayTemps}, "18.0");
$logDaysWarning .= "\n" if ($logDaysCnt>0);
$logDaysWarning .= "WARNING master device $device has no day profile for $day - create default";
$logDaysCnt++;
}
$prf->{$day}->{"temp"} = $dayTemps;
$prf->{$day}->{"time"} = $dayTimes;
}
if ( ($logDaysCnt>0) && ($logDaysCnt<(@shortDays)) ) {
Log3($me, 3, $logDaysWarning);
} else {
if ($logDaysCnt == (@shortDays)) {
Log3($me, 3, "WARNING master device $device has no week profile - create default");
}
}
return $prf;
}
##############################################
sub weekprofile_createDefaultProfile(@)
{
my ($hash) = @_;
my $prf = {};
foreach my $day (@shortDays){
my @times; push(@times, "24:00");
my @temps; push(@temps, "18.0");
$prf->{$day}->{"temp"} = \@temps;
$prf->{$day}->{"time"} = \@times;
}
return $prf;
}
##############################################
sub weekprofile_sendDevProfile(@)
{
my ($device,$prf,$me) = @_;
my $type = weekprofile_getDeviceType($me, $device,"SND");
return "Error device type not supported" if (!defined ($type));
return "profile has no data" if (!defined($prf->{DATA}));
if ($type eq "WEEKPROFILE") {
my $json = JSON->new->allow_nonref;
my $json_text = undef;
eval ( $json_text = $json->encode($prf->{DATA}) );
return "Error in profile data" if (!defined($json_text));
return fhem("set $device profile_data $prf->{TOPIC}:$prf->{NAME} $json_text",1);
}
elsif ($type eq "WDT") {
my $cmd = "set $device weekprofile $me:$prf->{TOPIC}:$prf->{NAME}";
Log3($me, 4, "$me(sendDevProfile): send to WDT $cmd");
return fhem("$cmd",1);
}
elsif ($type eq "MQTT2_DEVICE") {
my $cmd = "set $device weekprofile $me $prf->{TOPIC}:$prf->{NAME}";
Log3($me, 4, "$me(sendDevProfile): send to MQTT2_DEVICE $cmd");
return fhem("$cmd",1);
}
my $devPrf = weekprofile_readDevProfile($device,$type,$me);
my $force = AttrVal($me,"forceCompleteProfile",0);
# only send changed days
my @dayToTransfer = ();
foreach my $day (@shortDays){
my $tmpCnt = scalar(@{$prf->{DATA}->{$day}->{"temp"}});
next if ($tmpCnt <= 0);
if ($tmpCnt != scalar(@{$devPrf->{$day}->{"temp"}})) {
push @dayToTransfer , $day;
next;
}
my $equal = 1;
for (my $i = 0; $i < $tmpCnt; $i++) {
if ( ($prf->{DATA}->{$day}->{"temp"}[$i] ne $devPrf->{$day}->{"temp"}[$i] ) ||
$prf->{DATA}->{$day}->{"time"}[$i] ne $devPrf->{$day}->{"time"}[$i] ) {
$equal = 0;
last;
}
}
if ($equal == 0 || $force > 0) {
push @dayToTransfer , $day;
next;
}
}
if (scalar(@dayToTransfer) <=0) {
Log3($me, 4, "$me(sendDevProfile): nothing to do");
return undef;
}
# make a copy to manipulate temps to keywords
my $prfData = dclone($prf->{DATA});
# send keywords to device if attr 'sendKeywordsToDevices' is set
my $useKeywords = AttrVal($me, "sendKeywordsToDevices", 0);
if ($useKeywords) {
my $hash = $defs{$me};
my $json = JSON->new->allow_nonref;
my $json_text = undef;
eval { $json_text = $json->encode($prfData) };
$json_text = weekprofile_replaceKeywords($hash, $json_text, 'toKey');
eval { $prfData = $json->decode($json_text) };
}
my $cmd="";
if($type eq "MAX") {
$cmd = "set $device weekProfile ";
foreach my $day (@dayToTransfer){
my $tmpCnt = scalar(@{$prfData->{$day}->{"temp"}});
$cmd.=$day.' ';
for (my $i = 0; $i < $tmpCnt; $i++) {
my $endTime = $prfData->{$day}->{"time"}[$i];
$endTime = ($endTime eq "24:00") ? ' ' : ','.$endTime.',';
$cmd.=$prfData->{$day}->{"temp"}[$i].$endTime;
}
}
} elsif ($type eq "CUL_HM") {
my $k=0;
my $dayCnt = scalar(@dayToTransfer);
foreach my $day (@dayToTransfer){
$cmd .= "set $device tempList";
$cmd .= $day;
$cmd .= ($k < $dayCnt-1) ? " prep": " exec";
my $tmpCnt = scalar(@{$prfData->{$day}->{"temp"}});
for (my $i = 0; $i < $tmpCnt; $i++) {
$cmd .= " ".$prfData->{$day}->{"time"}[$i]." ".$prfData->{$day}->{"temp"}[$i];
}
$cmd .= ($k < $dayCnt-1) ? "; ": "";
$k++;
}
} elsif ($type =~ /HMCCU.*/){
$cmd .= "set $device config device" if ($type eq "HMCCU_HM");
$cmd .= "set $device config 1" if ($type eq "HMCCU_IP");
my $k=0;
my $dayCnt = scalar(@dayToTransfer);
my $prefix = weekprofile_get_prefix_HM($device,"ENDTIME_SUNDAY_1",$me);
$prefix = ""; # always no prefix by set #msg1113658
if (!defined($prefix)) {
Log3($me, 3, "$me(sendDevProfile): no prefix found");
$prefix = "";
}
foreach my $day (@dayToTransfer){
my $reading = $DEV_READINGS{$day}{$type};
my $dpTime = "$prefix"."ENDTIME_$reading";
my $dpTemp = "$prefix"."TEMPERATURE_$reading";
my $tmpCnt = scalar(@{$prfData->{$day}->{"temp"}});
for (my $i = 0; $i < $tmpCnt; $i++) {
$cmd .= " " . $dpTemp . "_" . ($i + 1) . "=" . $prfData->{$day}->{"temp"}[$i];
$cmd .= " " . $dpTime . "_" . ($i + 1) . "=" . weekprofile_timeToMinutes($prfData->{$day}->{"time"}[$i]);
$cmd .= ":" if ($type eq "HMCCU_HM"); # ':' after time see #msg1191311
}
$k++;
}
}
my $ret = undef;
if ($cmd) {
$cmd =~ s/^\s+|\s+$//g;
#transfer profil data delayed e.q. to avoid messages like "queue is full, dropping packet" by HM devices
my $snd_delay = AttrVal($me,"sendDelay",0);
if ($snd_delay>0) {
my $datetimenow = gettimeofday();
my $last_profile_send = $LAST_SEND{$type};
if (!($last_profile_send)) {
$last_profile_send = $datetimenow - $snd_delay;
} else {
my $last_profile_send_fmt = FmtDateTime($last_profile_send);
Log3($me, 4, "$me(sendDevProfile): last profile to device type $type wars or will be at ($last_profile_send_fmt)");
}
if ($last_profile_send <= $datetimenow - $snd_delay) {
$last_profile_send = $datetimenow - $snd_delay;
}
$last_profile_send = $last_profile_send + $snd_delay;
my $last_profile_send_fmt = FmtDateTime($last_profile_send);
my $sleepTime = $last_profile_send - $datetimenow;
Log3($me, 4, "$me(sendDevProfile): profile data to $device ($type) will be sent $sleepTime seconds delayed at ($last_profile_send_fmt)");
$LAST_SEND{$type} = $last_profile_send;
$cmd=$cmd.";trigger $me PROFILE_TRANSFERED $device";
Log3($me, 4, "$me(sendDevProfile): sleep $sleepTime; $cmd");
$ret = fhem("sleep $sleepTime; $cmd",1);
}
else {
Log3($me, 4, "$me(sendDevProfile): $cmd");
$ret = fhem($cmd,1);
DoTrigger($me,"PROFILE_TRANSFERED $device",1);
}
}
return $ret;
}
##############################################
sub weekprofile_refreshSendDevList($)
{
my ($hash) = @_;
my $me = $hash->{NAME};
splice(@{$hash->{SNDDEVLIST}});
my $useTopics = AttrVal($me,"useTopics",0);
Log3($me, 5, "$me(weekprofile_refreshSendDevList): start");
foreach my $d (keys %defs)
{
next if ($defs{$d}{NAME} eq $me);
my $module = $defs{$d}{TYPE};
my %sndHash;
@sndHash{@DEVLIST_SEND}=();
next if (!exists $sndHash{$module});
my $type = weekprofile_getDeviceType($me, $defs{$d}{NAME},"SND");
next if (!defined($type));
my $dev = {};
$dev->{NAME} = $defs{$d}{NAME};
$dev->{ALIAS} = AttrVal($dev->{NAME},"alias",$dev->{NAME});
# add userattr weekprofile to device
# help of attr weekprofile will come from module weekprofile
addToDevAttrList($dev->{NAME},"weekprofile","weekprofile") if ($useTopics);
push @{$hash->{SNDDEVLIST}} , $dev;
Log3($me, 5, "$me(weekprofile_refreshSendDevList): add device $dev->{NAME}");
}
my $cnt = scalar(@{$hash->{SNDDEVLIST}});
Log3($me, 5, "$me(weekprofile_refreshSendDevList): $cnt devices in list");
return undef;
}
##############################################
sub weekprofile_receiveList($)
{
my ($hash) = @_;
my $me = $hash->{NAME};
my @rcvList = ();
foreach my $d (keys %defs)
{
next if ($defs{$d}{NAME} eq $me);
my $module = $defs{$d}{TYPE};
my %sndHash;
@sndHash{@DEVLIST_SEND}=();
next if (!exists $sndHash{$module});
my $type = weekprofile_getDeviceType($me, $defs{$d}{NAME});
next if (!defined($type));
push @rcvList, $defs{$d}{NAME};
}
return @rcvList;
}
##############################################
sub weekprofile_assignDev($)
{
my ($hash) = @_;
my $me = $hash->{NAME};
my $prf = undef;
if (defined($hash->{MASTERDEV})) {
Log3($me, 5, "$me(assignDev): assign to device $hash->{MASTERDEV}->{NAME}");
my $type = weekprofile_getDeviceType($me, $hash->{MASTERDEV}->{NAME});
if (!defined($type)) {
Log3($me, 2, "$me(assignDev): device $hash->{MASTERDEV}->{NAME} not supported or defined");
} else {
$hash->{MASTERDEV}->{TYPE} = $type;
my $prfDev = weekprofile_readDevProfile($hash->{MASTERDEV}->{NAME},$type, $me);
$prf = {};
$prf->{NAME} = 'master';
$prf->{TOPIC} = 'default';
if(defined($prfDev)) {
$prf->{DATA} = $prfDev;
} else {
Log3($me, 3, "WARNING master device $hash->{MASTERDEV}->{NAME} has no week profile - create default profile");
$prf->{DATA} = weekprofile_createDefaultProfile($hash);
}
$hash->{STATE} = "assigned";
}
}
if (!defined($prf)) {
Log3($me, 5, "create default profile");
my $prfDev = weekprofile_createDefaultProfile($hash);
if(defined($prfDev)) {
$prf = {};
$prf->{DATA} = $prfDev;
$prf->{NAME} = 'default';
$prf->{TOPIC} = 'default';
$hash->{STATE} = "created";
}
}
if(defined($prf)) {
push @{$hash->{PROFILES}} , $prf;
}
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,"state",$hash->{STATE});
readingsEndUpdate($hash, 1);
}
##############################################
sub weekprofile_updateReadings($)
{
my ($hash) = @_;
my $prfCnt = scalar(@{$hash->{PROFILES}});
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,"profile_count",$prfCnt);
#readings with profile names???
#my $idx = 1;
#foreach my $prf (@{$hash->{PROFILES}}){
#my $str = sprintf("profile_name_%02d",$idx);
#readingsBulkUpdate($hash,$str,$prf->{NAME});
#$idx++;
#}
my $topic_list="";
splice(@{$hash->{TOPICS}});
foreach my $prf (@{$hash->{PROFILES}}) {
if ( !grep( /^$prf->{TOPIC}$/, @{$hash->{TOPICS}}) ) {
push @{$hash->{TOPICS}}, $prf->{TOPIC};
if (length($topic_list) > 0){
$topic_list = $topic_list . ':' . $prf->{TOPIC};
} else {
$topic_list = $prf->{TOPIC};
}
}
}
my $useTopics = AttrVal($hash->{NAME},"useTopics",0);
if ($useTopics) {
readingsBulkUpdate($hash,"topics",$topic_list);
}
readingsEndUpdate($hash, 1);
}
##############################################
sub weekprofile_Initialize($)
{
my ($hash) = @_;
$hash->{DefFn} = "weekprofile_Define";
$hash->{SetFn} = "weekprofile_Set";
$hash->{GetFn} = "weekprofile_Get";
$hash->{SetFn} = "weekprofile_Set";
$hash->{StateFn} = "weekprofile_State";
$hash->{NotifyFn} = "weekprofile_Notify";
$hash->{AttrFn} = "weekprofile_Attr";
$hash->{AttrList} = "useTopics:0,1 widgetTranslations widgetWeekdays widgetTempRange widgetEditOnNewPage:0,1 widgetEditDaysInRow:1,2,3,4,5,6,7 \
sendDelay tempON tempOFF configFile forceCompleteProfile:0,1 tempMap sendKeywordsToDevices:0,1 ".$readingFnAttributes;
$hash->{FW_summaryFn} = "weekprofile_SummaryFn";
$hash->{FW_atPageEnd} = 1;
}
##############################################
sub weekprofile_Define($$)
{
my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def);
if(@a < 1) {
my $msg = "wrong syntax: define <name> weekprofile [device]";
Log3(undef, 2, $msg);
return $msg;
}
my $me = $a[0];
my $devName = undef;
if (@a > 1) {
$devName = $a[2];
$devName =~ s/(^\s+|\s+$)//g if ($devName);
}
$hash->{MASTERDEV}->{NAME} = $devName if (defined($devName));
$hash->{STATE} = "defined";
my @profiles = ();
my @sendDevList = ();
my @topics = ();
$hash->{PROFILES} = \@profiles;
$hash->{SNDDEVLIST} = \@sendDevList;
$hash->{TOPICS} = \@topics;
$hash->{TEMPMAP} = {};
#$attr{$me}{verbose} = 5;
if ($init_done) {
weekprofile_refreshSendDevList($hash);
weekprofile_assignDev($hash);
weekprofile_updateReadings($hash);
}
return undef;
}
##############################################
sub sort_by_name
{
return lc("$a->{TOPIC}:$a->{NAME}") cmp lc("$b->{TOPIC}:$b->{NAME}");
}
##############################################
sub dumpData($$$)
{
require Data::Dumper;
my ($hash,$prefix,$data) = @_;
my $me = $hash->{NAME};
my $dmp = Dumper($data);
$dmp =~ s/^\s+|\s+$//g; #trim whitespace both ends
if (AttrVal($me,"verbose",3) < 4) {
Log3($me, 1, "$me$prefix - set verbose to 4 to see the data");
} else {
Log3($me, 4, "$me$prefix $dmp");
}
}
##############################################
sub weekprofile_Get($$@)
{
my ($hash, $name, $cmd, @params) = @_;
my $me = $hash->{NAME};
my $list = '';
my $prfCnt = scalar(@{$hash->{PROFILES}});
my $useTopics = AttrVal($name,"useTopics",0);
$list.= 'profile_data:' if ($prfCnt > 0);
foreach my $prf (sort sort_by_name @{$hash->{PROFILES}}){
$list.= $prf->{TOPIC}.":" if ($useTopics);
$list.= $prf->{NAME} if ($useTopics || (!$useTopics && ($prf->{TOPIC} eq 'default')));
$list.= ',';
}
$list = substr($list, 0, -1) if ($prfCnt > 0);
#-----------------------------------------------------------------------------
if($cmd eq "profile_data") {
return 'usage: profile_data <name> [usekeywords]' if(@params < 1);
return "no profile" if ($prfCnt <= 0);
my ($topic, $name) = weekprofile_splitName($me, $params[0]);
my ($prf,$idx) = weekprofile_findPRF($hash,$name,$topic,1);
return "profile $params[0] not found" if (!defined($prf));
return "profile $params[0] has no data" if (!defined($prf->{DATA}));
my $json = JSON->new->allow_nonref;
my $json_text = undef;
eval { $json_text = $json->encode($prf->{DATA}) };
dumpData($hash,"(Get): invalid profile data",$prf->{DATA}) if (!defined($json_text));
my $useKeywords = 1;
# 1 - return data with keywords
# 0 - depending on attr
# -1 - return data with values instead of keywords
Log3($me, 5, "$me(weekprofile_Get): params: @params");
if(@params >= 2) {
$useKeywords = AttrVal($me, "sendKeywordsToDevices", 1) if ($params[1] == 0);
$useKeywords = 0 if ($params[1] < 0);
}
my $direction = -1;
$direction = 1 if ($useKeywords <=0);
$json_text = weekprofile_replaceKeywords($hash, $json_text, $direction);
return $json_text;
}
#-----------------------------------------------------------------------------
$list.= ' profile_names';
if($cmd eq "profile_names") {
my $names = '';
my $topic = 'default';
$topic = $params[0] if(@params == 1);
foreach my $prf (sort sort_by_name @{$hash->{PROFILES}}){
$names .=$prf->{NAME}."," if ($topic eq $prf->{TOPIC});
$names .="$prf->{TOPIC}:$prf->{NAME}," if ($topic eq '*');
}
if ($names) {
$names = substr($names, 0, -1);
$names =~ s/ $//;
}
return $names;
}
#-----------------------------------------------------------------------------
$list.= ' profile_references' if ($useTopics);
if($cmd eq "profile_references") {
return 'usage: profile_references <name>' if(@params < 1);
my $refs = '';
my $topic = 'default';
if ($params[0] eq '*') {
foreach my $prf (sort sort_by_name @{$hash->{PROFILES}}){
next if (!defined($prf->{REF}));
$refs .= "$prf->{TOPIC}:$prf->{NAME}>$prf->{REF},";
}
$refs = substr($refs, 0, -1);
} else {
my ($topic, $name) = weekprofile_splitName($me, $params[0]);
my ($prf,$idx) = weekprofile_findPRF($hash,$name,$topic,0);
return "profile $params[0] not found" unless ($prf);
$refs = '0';
$refs = "$prf->{REF}" if ($prf->{REF});
}
return $refs;
}
#-----------------------------------------------------------------------------
$list.= ' topic_names:noArg' if ($useTopics);
if($cmd eq "topic_names") {
my $names = '';
foreach my $topic (sort {lc($a) cmp lc($b)} @{$hash->{TOPICS}}) {
$names .= "$topic,";
}
if ($names) {
$names = substr($names, 0, -1);
$names =~ s/ $//;
}
return $names;
}
#-----------------------------------------------------------------------------
$list .= ' associations:0,1' if ($useTopics);
if($cmd eq "associations") {
my $retType = 1;
$retType = $params[0] if(@params >= 1);
# html only if FHEMWEB and canAsyncOutput
if (defined($hash->{CL})) {
$retType = ($hash->{CL}{TYPE} eq "FHEMWEB" && $hash->{CL}{canAsyncOutput}) ? $retType : 1;
}
else {
$retType = 1;
}
# dumpData($hash,"(Get): asso",$hash->{CL}) if defined($hash->{CL});
my @not_asso = ();
my @json_arr = ();
my $retHTML = "<html><table><thead><tr>";
$retHTML .= "<th width='150'><b>Device</b></th><th width='150'><b>Profile</b></th></tr>";
$retHTML .= "<th> </th><th></th></tr>";
$retHTML .= "</thead><tbody>";
foreach my $dev (@{$hash->{SNDDEVLIST}}) {
my $entry = {};
$entry->{DEVICE}->{NAME} = $dev->{NAME};
$entry->{PROFILE}->{NAME} = "";
my $prfName = AttrVal($dev->{NAME},"weekprofile",undef);
if (!defined($prfName)) {
push @not_asso, $dev->{NAME};
push @json_arr , $entry;
next;
}
my ($prf,$idx) = weekprofile_findPRF($hash, $prfName, undef, 0);
my $color = defined($prf) ? "" : "color:red" ;
$entry->{PROFILE}->{NAME} = $prfName;
$entry->{PROFILE}->{EXISTS} = defined($prf) + 0;
push @json_arr , $entry;
$retHTML .= "<tr><td style='text-align:left'>$dev->{NAME}</td><td style='text-align:center;$color'>$prfName</td></tr>";
}
$retHTML .= "<tr><td colspan='2'><i>Not associated devices</i></td></tr>" if (scalar(@not_asso));
foreach my $devname (@not_asso) {
$retHTML .= "<tr><td style='text-align:left'>$devname</td><td style='text-align:center'></td></tr>";
}
$retHTML.= "</tbody></table></html>";
my $ret = $retHTML;
if ($retType == 1) {
my $json_text = undef;
my $json = JSON->new->allow_nonref;
eval { $json_text = $json->encode(\@json_arr) };
$ret = $json_text;
}
return $ret;
}
# hidden functions
#-----------------------------------------------------------------------------
if($cmd eq "sndDevList") {
my $json = JSON->new->allow_nonref;
my @sortDevList = sort {lc($a->{ALIAS}) cmp lc($b->{ALIAS})} @{$hash->{SNDDEVLIST}};
my $json_text = undef;
eval { $json_text = $json->encode(\@sortDevList) };
dumpData($hash,"(Get): invalid device list",\@sortDevList) if (!defined($json_text));
return $json_text;
}
#-----------------------------------------------------------------------------
if($cmd eq "tempList") {
my $tempList = weekprofile_createTempList($hash);
return $tempList;
}
$list =~ s/ $//;
return "Unknown argument $cmd choose one of $list";
}
##############################################
sub weekprofile_findPRF($$$$)
{
my ($hash, $name, $topic, $followRef) = @_;
my $me = $hash->{NAME};
if (!$topic) {
$topic = ReadingsVal($me, "active_topic", "default");
Log3($me, 3, "$me(weekprofile_findPRF): use topic $topic");
}
$followRef = '0' if (!$followRef);
my $found = undef;
my $idx = 0;
my $topicOk = 0;
foreach my $prf (@{$hash->{PROFILES}}){
$topicOk = defined($topic) ? ($prf->{TOPIC} eq $topic) : 1;
if ( ($prf->{NAME} eq $name) && $topicOk ){
$found = $prf;