-
Notifications
You must be signed in to change notification settings - Fork 0
/
75_msgConfig.pm
executable file
·956 lines (811 loc) · 34.4 KB
/
75_msgConfig.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
###############################################################################
# $Id$
package main;
use strict;
use warnings;
use Data::Dumper;
use FHEM::Meta;
# initialize ##################################################################
sub msgConfig_Initialize($) {
my ($hash) = @_;
my $TYPE = "msgConfig";
require "$attr{global}{modpath}/FHEM/msgSchema.pm";
$hash->{DefFn} = $TYPE . "_Define";
$hash->{SetFn} = $TYPE . "_Set";
$hash->{GetFn} = $TYPE . "_Get";
$hash->{UndefFn} = $TYPE . "_Undefine";
$hash->{NotifyFn} = $TYPE . "_Notify";
# add attributes for configuration
no warnings 'qw';
my @attrList = qw(
msgCmdAudio
msgCmdAudioShort
msgCmdAudioShortPrio
msgCmdLight
msgCmdLightHigh
msgCmdLightLow
msgCmdMail
msgCmdMailHigh
msgCmdMailLow
msgCmdPush
msgCmdPushHigh
msgCmdPushLow
msgCmdScreen
msgCmdScreenHigh
msgCmdScreenLow
msgFwPrioAbsentAudio:-2,-1,0,1,2
msgFwPrioAbsentLight:-2,-1,0,1,2
msgFwPrioAbsentScreen:-2,-1,0,1,2
msgFwPrioEmergencyAudio:-2,-1,0,1,2
msgFwPrioEmergencyLight:-2,-1,0,1,2
msgFwPrioEmergencyPush:-2,-1,0,1,2
msgFwPrioEmergencyScreen:-2,-1,0,1,2
msgFwPrioGoneAudio:-2,-1,0,1,2
msgFwPrioGoneLight:-2,-1,0,1,2
msgFwPrioGoneScreen:-2,-1,0,1,2
msgLocationDevs
msgParamsAudio
msgParamsAudioShort
msgParamsAudioShortPrio
msgParamsLight
msgParamsLightHigh
msgParamsLightLow
msgParamsMail
msgParamsMailHigh
msgParamsMailLow
msgParamsPush
msgParamsPushHigh
msgParamsPushLow
msgParamsScreen
msgParamsScreenHigh
msgParamsScreenLow
msgParamsText
msgParamsTextHigh
msgParamsTextLow
msgPriorityAudio:-2,-1,0,1,2
msgPriorityLight:-2,-1,0,1,2
msgPriorityMail:-2,-1,0,1,2
msgPriorityPush:-2,-1,0,1,2
msgPriorityScreen:-2,-1,0,1,2
msgPriorityText:-2,-1,0,1,2
msgRcv:1,0
msgResidentsDev
msgSwitcherDev
msgThPrioHigh:-2,-1,0,1,2
msgThPrioNormal:-2,-1,0,1,2
msgThPrioAudioEmergency:-2,-1,0,1,2
msgThPrioAudioHigh:-2,-1,0,1,2
msgThPrioTextEmergency:-2,-1,0,1,2
msgThPrioTextNormal:-2,-1,0,1,2
msgThPrioGwEmergency:-2,-1,0,1,2
msgTitleAudio
msgTitleAudioShort
msgTitleAudioShortPrio
msgTitleLight
msgTitleLightHigh
msgTitleLightLow
msgTitleMail
msgTitleMailHigh
msgTitleMailLow
msgTitlePush
msgTitlePushHigh
msgTitlePushLow
msgTitleScreen
msgTitleScreenHigh
msgTitleScreenLow
msgTitleText
msgTitleTextHigh
msgTitleTextLow
msgTitleShrtAudio
msgTitleShrtAudioShort
msgTitleShrtAudioShortPrio
msgTitleShrtLight
msgTitleShrtLightHigh
msgTitleShrtLightLow
msgTitleShrtMail
msgTitleShrtMailHigh
msgTitleShrtMailLow
msgTitleShrtPush
msgTitleShrtPushHigh
msgTitleShrtPushLow
msgTitleShrtScreen
msgTitleShrtScreenHigh
msgTitleShrtScreenLow
msgTitleShrtText
msgTitleShrtTextHigh
msgTitleShrtTextLow
);
use warnings 'qw';
$hash->{AttrList} = join( " ", @attrList ) . " " . $readingFnAttributes;
# add global attributes
foreach (
"msgContactAudio",
"msgContactMail",
"msgContactPush",
"msgContactScreen",
"msgContactLight",
"msgParams",
"msgPriority",
"msgRecipient",
"msgRecipientAudio",
"msgRecipientMail",
"msgRecipientPush",
"msgRecipientScreen",
"msgRecipientText",
"msgRecipientLight",
"msgTitle",
"msgTitleShrt",
"msgType:text,push,mail,screen,light,audio,queue",
)
{
addToAttrList( $_, 'msgConfig' );
}
return FHEM::Meta::InitMod( __FILE__, $hash );
}
# regular Fn ##################################################################
sub msgConfig_Define($$) {
my ( $hash, $def ) = @_;
my $TYPE = $hash->{TYPE};
my @a = split( "[ \t]+", $def, 5 );
return "Usage: define <name> $TYPE"
if ( int(@a) < 2 );
my $name = $a[0];
return "Global configuration device already defined: "
. $modules{$TYPE}{defptr}{NAME}
if ( defined( $modules{$TYPE}{defptr} )
&& $init_done
&& !defined( $hash->{OLDDEF} ) );
# Initialize the device
return $@ unless ( FHEM::Meta::SetInternals($hash) );
# create global unique device definition
$modules{$TYPE}{defptr} = $hash;
# set default settings on first define
if ( $init_done && !defined( $hash->{OLDDEF} ) ) {
my $group = AttrVal( "global", "group", "Global" );
my $room = AttrVal( "global", "room", "" );
my $verbose = AttrVal( "global", "verbose", 3 );
$attr{$name}{group} = $group;
$attr{$name}{verbose} = $verbose;
$attr{$name}{room} = $room if ( $room ne "" );
$attr{$name}{comment} = "FHEM Global Configuration for command 'msg'";
$attr{$name}{stateFormat} = "fhemMsgState";
$attr{$name}{msgType} = "text";
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, "fhemMsgState", "initialized" );
readingsEndUpdate( $hash, 1 );
}
setNotifyDev( $hash, 'TYPE=(Jabber|TelegramBot|yowsup|Signalbot)' );
return;
}
sub msgConfig_Undefine($$) {
my ( $hash, $name ) = @_;
my $TYPE = $hash->{TYPE};
# release global unique device definition
delete $modules{$TYPE}{defptr};
return undef;
}
sub msgConfig_Set($@) {
my ( $hash, @a ) = @_;
my $name = $hash->{NAME};
my $TYPE = $hash->{TYPE};
shift @a;
my $what = shift @a;
Log3 $name, 5, "$TYPE $name: called function $TYPE" . "_Set()";
my @msgTypes = ( "audio", "light", "mail", "push", "screen", "queue" );
# cleanReadings
if ( lc($what) eq "cleanreadings" ) {
my $device = defined( $a[0] ) ? $a[0] : ".*";
return fhem( "deletereading $device fhemMsg.*", 1 );
}
# addLocation
elsif ( lc($what) eq "addlocation" ) {
my $location = join( " ", @a );
my $group = AttrVal( $name, "group", $TYPE );
my $room = AttrVal( $name, "room", "" );
my $return = "";
return "Missing argument 'location'"
if ( $location eq "" );
my $device = "msgRoom_" . $location;
$device =~ s/[\s\t-]+/_/g;
return "Device $device is already existing but not a dummy device"
if ( IsDevice($device)
&& GetType($device) ne "dummy" );
if ( !IsDevice($device) ) {
$return = fhem( "define $device dummy", 1 );
$return .= "Device $device was created"
if ( $return eq "" );
}
else {
$return = "Existing dummy device $device was updated";
}
$attr{$device}{group} = $group if ( !defined( $attr{$device}{group} ) );
$attr{$device}{room} = $room
if ( !defined( $attr{$device}{room} ) && $room ne "" );
$attr{$device}{comment} = "Auto-created by $name"
if ( !defined( $attr{$device}{comment} ) );
$attr{$device}{userattr} .= " msgLocationName"
if ( defined( $attr{$device}{userattr} )
&& $attr{$device}{userattr} !~
m/^msgLocationName$|^msgLocationName\s|\smsgLocationName\s|\smsgLocationName$/
);
$attr{$device}{userattr} = "msgLocationName"
if ( !defined( $attr{$device}{userattr} ) );
$attr{$device}{msgLocationName} = $location;
fhem("set $device $location");
$attr{$name}{msgLocationDevs} .= "," . $device
if ( defined( $attr{$name}{msgLocationDevs} )
&& $attr{$name}{msgLocationDevs} !~
/^$device\$|^$device,|,$device,|,$device$/ );
$attr{$name}{msgLocationDevs} = $device
if ( !defined( $attr{$name}{msgLocationDevs} ) );
return $return;
}
# createSwitcherDev
elsif ( lc($what) eq "createswitcherdev" ) {
my $device = AttrVal( $name, "msgSwitcherDev", "HouseAnn" );
my $state = AttrVal( $device, "state", "???" );
my $return = "";
my $lang = "en";
$lang = $a[0]
if ( defined( $a[0] ) && $a[0] eq "de" );
return "Device $device is already existing but not a dummy device"
if ( IsDevice($device)
&& GetType($device) ne "dummy" );
if ( !IsDevice($device) ) {
$return = fhem( "define $device dummy", 1 );
$return .= "Device $device was created"
if ( $return eq "" );
}
else {
$return = "Existing dummy device $device was updated";
}
if ( $lang eq "de" ) {
$attr{$device}{alias} = "Durchsagen";
$attr{$device}{eventMap} =
"active:aktiv long:lang short:kurz visual:visuell off:aus";
$attr{$device}{room} = "Haus"
if ( !defined( $attr{$device}{room} ) );
$attr{$device}{setList} = "state:lang,kurz,visuell,aus";
}
else {
$attr{$device}{alias} = "Announcements";
$attr{$device}{room} = "House"
if ( !defined( $attr{$device}{room} ) );
$attr{$device}{setList} = "state:long,short,visual,off";
delete $attr{$device}{eventMap}
if ( defined( $attr{$device}{eventMap} ) );
}
$attr{$device}{comment} = "Auto-created by $name"
if ( !defined( $attr{$device}{comment} )
|| $attr{$device}{comment} ne "Auto-created by $name" );
$attr{$device}{devStateIcon} =
'aktiv:general_an@90EE90 active:general_an@90EE90 lang:general_an@green:off long:general_an@green:off aus:general_aus@red:long off:general_aus@red:long kurz:general_an@orange:long short:general_an@orange:long visuell:general_an@orange:long visual:general_an@orange:long';
$attr{$device}{"event-on-change-reading"} = "state"
if ( !defined( $attr{$device}{"event-on-change-reading"} ) );
$attr{$device}{group} = "Automation"
if ( !defined( $attr{$device}{group} ) );
$attr{$device}{icon} = "audio_volume_mid";
$attr{$device}{webCmd} = "state";
fhem("set $device long") if ( $state eq "???" );
$return .=
"\nAttribute msgSwitcherDev at device $name was changed to $device"
if ( defined( $attr{$name}{msgSwitcherDev} ) );
$return .= "\nAdded attribute msgSwitcherDev to device $name"
if ( !defined( $attr{$name}{msgSwitcherDev} ) );
$attr{$name}{msgSwitcherDev} = $device;
return $return;
}
# createResidentsDev
elsif ( lc($what) eq "createresidentsdev" ) {
my $device = AttrVal( $name, "msgResidentsDev", "rgr_Residents" );
my $return = "";
my $lang = defined( $a[0] ) ? uc( $a[0] ) : "EN";
return
"Device $device is already existing but not a RESIDENTS or ROOMMATE device"
if ( IsDevice($device)
&& !IsDevice( $device, "RESIDENTS|ROOMMATE" ) );
if ( !IsDevice($device) ) {
$return = fhem( "define $device RESIDENTS", 1 );
$return .= "RESIDENTS device $device was created."
if ( $return eq "" );
}
else {
$return =
"Existing " . GetType($device) . " device $device was updated.";
}
my $txt = fhem("attr $device rgr_lang $lang")
unless ( $lang eq "EN" );
$return .= $txt if ($txt);
$attr{$device}{comment} = "Auto-created by $name"
if ( !defined( $attr{$device}{comment} )
|| $attr{$device}{comment} ne "Auto-created by $name" );
$return .=
"\nIf you would like this device to act as an overall presence device for ALL msg commands, please adjust attribute msgResidentsDev at device $name to $device."
if ( defined( $attr{$name}{msgResidentsDev} )
&& $attr{$name}{msgResidentsDev} ne $device );
$return .=
"\nNext, set a device's msgResidentsDev attribute to '$device' (think of using 'userattr' to add 'msgResidentsDev' to the list of available attributes). \nIf you would like '$device' to act as an overall presence device for ALL msg commands, sett attribute msgResidentsDev at device $name to $device."
if ( !defined( $attr{$name}{msgResidentsDev} ) );
return $return;
}
else {
return
"Unknown argument $what, choose one of cleanReadings addLocation createSwitcherDev:de,en createResidentsDev:de,en";
}
return undef;
}
sub msgConfig_Get($@) {
my ( $hash, @a ) = @_;
my $name = $hash->{NAME};
my $TYPE = $hash->{TYPE};
shift @a;
my $what = shift @a;
Log3 $name, 5, "$TYPE $name: called function $TYPE" . "_Get()";
my @msgTypes = ( "audio", "light", "mail", "push", "screen" );
# routeCmd
if ( lc($what) eq "routecmd" ) {
my $return = "";
my $msgTypesReq =
defined( $a[0] ) ? lc( $a[0] ) : join( ',', @msgTypes );
my $devicesReq = defined( $a[1] ) ? $a[1] : $name;
my $cmdSchema = msgSchema::get();
my $UserDeviceTypes = "";
foreach my $msgType ( split( /,/, $msgTypesReq ) ) {
# Check device
if ( $devicesReq ne "" ) {
foreach my $device ( split( /,/, $devicesReq ) ) {
if ( IsDevice($device) ) {
$UserDeviceTypes .= "," . GetType($device)
if ( $UserDeviceTypes ne ""
&& $msgType ne "mail"
&& $device ne $name );
$UserDeviceTypes = GetType($device)
if ( $UserDeviceTypes eq ""
&& $msgType ne "mail"
&& $device ne $name );
$UserDeviceTypes .= ",fhemMsgMail"
if ( $UserDeviceTypes ne ""
&& $msgType eq "mail"
&& $device ne $name );
$UserDeviceTypes = "fhemMsgMail"
if ( $UserDeviceTypes eq ""
&& $msgType eq "mail"
&& $device ne $name );
my $typeUc = ucfirst($msgType);
my @priorities;
@priorities = ( "Normal", "ShortPrio", "Short" )
if ( $msgType eq "audio" );
@priorities = ( "Normal", "High", "Low" )
if ( $msgType ne "audio" );
my $output = 0;
foreach my $prio (@priorities) {
my $priorityCat = "";
$priorityCat = $prio if ( $prio ne "Normal" );
my $cmd = MSG_FindAttrVal( $device,
"msgCmd$typeUc$priorityCat", $typeUc, "" );
next
if ( $cmd eq ""
&& $device eq $name
&& $output == 0 );
$return .=
uc($msgType)
. ": USER DEFINED COMMANDS WITH PRECEDENCE\n-------------------------------------------------------------------------------\n\n"
if ( $output == 0 );
$return .=
" $device (DEVICE TYPE: "
. GetType($device) . ")\n"
if ( $output == 0 );
$output = 1 if ( $output == 0 );
$return .= " Priority $prio:\n $cmd\n"
if ( $cmd ne "" );
$return .=
" Priority $prio:\n [DEFAULT COMMAND]\n"
if ( $cmd eq "" );
}
$return .= "\n" if ( $return ne "" );
}
}
$return .= "\n" if ( $return ne "" );
}
# Default commands
if ( defined( $cmdSchema->{$msgType} ) ) {
my $deviceTypes = $devicesReq;
$deviceTypes = join( ',', keys %{ $cmdSchema->{$msgType} } )
if ( $deviceTypes eq "" || $devicesReq eq $name );
$deviceTypes = $UserDeviceTypes
if ( $UserDeviceTypes ne "" );
my $outout = 0;
foreach my $deviceType ( split( /,/, $deviceTypes ) ) {
if ( defined( $cmdSchema->{$msgType}{$deviceType} ) ) {
$return .=
uc($msgType)
. ": DEFAULT COMMANDS\n-------------------------------------------------------------------------------\n\n"
if ( $outout == 0 );
$outout = 1;
$return .= " $deviceType\n";
my @priorities;
@priorities = ( "Normal", "ShortPrio", "Short" )
if ( $msgType eq "audio" );
@priorities = ( "Normal", "High", "Low" )
if ( $msgType ne "audio" );
foreach my $prio (@priorities) {
$return .=
" Priority $prio:\n "
. $cmdSchema->{$msgType}{$deviceType}{$prio}
. "\n";
if (
defined(
$cmdSchema->{$msgType}{$deviceType}
{defaultValues}{$prio}
)
)
{
$return .= " Default Values:\n";
foreach my $key (
keys %{
$cmdSchema->{$msgType}{$deviceType}
{defaultValues}{$prio}
}
)
{
if ( $cmdSchema->{$msgType}{$deviceType}
{defaultValues}{$prio}{$key} ne "" )
{
$return .=
" $key = "
. $cmdSchema->{$msgType}{$deviceType}
{defaultValues}{$prio}{$key} . "\n";
}
else {
$return .= " $key = [EMPTY]\n";
}
}
}
}
$return .= "\n" if ( $return ne "" );
}
}
}
else {
$return .= "Unknown messaging type '$msgType'\n"
if ( $msgType ne "text" );
$return .=
"Messaging type 'text' does not have dedicated routing commands. This is a wrapper type to dynamically distinguish between push and mail.\n"
if ( $msgType eq "text" );
}
$return .= "\n" if ( $return ne "" );
}
$return =
"Non-existing device or unknown module messaging schema definition: $devicesReq"
if ( $return eq "" );
return $return;
}
else {
return
"Unknown argument $what, choose one of routeCmd:,audio,light,mail,push,screen,queue";
}
return undef;
}
sub msgConfig_Notify($$) {
my ( $hash, $dev ) = @_;
my $name = $hash->{NAME};
my $TYPE = GetType($name);
my $devName = $dev->{NAME};
my $devType = GetType($devName);
return ""
if ( IsDisabled($name)
or IsDisabled($devName)
or !AttrVal( $devName, "msgRcv", AttrVal( $name, "msgRcv", "1" ) ) );
Log3 $name, 5, "$TYPE $name: called function $TYPE" . "_Notify()";
my @events = @{ deviceEvents( $dev, 1 ) };
return "" unless (@events);
foreach my $event (@events) {
next
unless (
$event =~ m/^(msgText|queryData|((?:OTR)?Last)Message|message)/ );
my ( $sender, $msg );
# TelegramBot
if ( $devType eq "TelegramBot" ) {
if ( $1 eq "msgText" ) {
$sender = ReadingsVal( $devName, "msgPeerId", undef );
$msg = ReadingsVal( $devName, "msgText", undef );
}
elsif ( $1 eq "queryData" ) {
$sender = ReadingsVal( $devName, "queryPeerId", undef );
$msg = ReadingsVal( $devName, "queryData", undef );
}
}
# Jabber
elsif ( $devType eq "Jabber" ) {
($sender) =
( ReadingsVal( $devName, $2 . "SenderJID", undef ) =~ m/[^\/]/g );
$msg = ReadingsVal( $devName, $2 . "Message", undef );
}
# yowsup
elsif ( $devType eq "yowsup" ) {
$sender = $devName;
$devName = $modules{yowsup}{defptr}{yowsup}->{NAME};
$msg = ReadingsVal( $devName, "message", undef );
}
#Signalbot
elsif ( $devType eq 'Signalbot' ) {
$sender = ReadingsVal( $devName, 'msgSender', undef );
$msg = ReadingsVal( $devName, 'msgText', undef );
}
next unless ( $msg && $msg ne "" );
unless ( $sender && $sender ne "" ) {
Log3 $name, 5,
"msg: ERROR RCV $devName: "
. "Unable to retrieve sender reference";
next;
}
my $delivered = 0;
foreach my $t ( "push", "screen" ) {
my @contacts = devspec2array(
"msgContact" . ucfirst($t) . "=.*$devName:[@#]*$sender.*" );
if (@contacts) {
foreach (@contacts) {
next unless ( IsDevice($_) );
Log3 $_, 4,
"msg $_: " . "Received $t message from $devName: $msg";
$delivered = 1;
my $recipient = $defs{$_};
readingsBeginUpdate($recipient);
readingsBulkUpdate( $recipient,
"fhemMsgRcv" . ucfirst($t), $msg );
readingsBulkUpdate( $recipient,
"fhemMsgRcv" . ucfirst($t) . "Gw", $devName );
readingsEndUpdate( $recipient, 1 );
}
}
last if ($delivered);
}
unless ($delivered) {
Log3 $name, 4,
"msg: ERROR RCV $devName $sender: "
. "Missing reference in msgContact attribute of any device";
DoTrigger( $name,
"ERROR RCV $devName $sender: "
. "Missing reference in msgContact attribute of any device" );
}
}
return "";
}
# module Fn ####################################################################
sub MSG_FindAttrVal($$$$) {
my ( $d, $n, $msgType, $default ) = @_;
$msgType = "" unless ($msgType);
$msgType = ucfirst($msgType);
$n .= $msgType if ( $n =~ /^msg(Contact)$/ );
my $g = (
(
defined( $modules{msgConfig}{defptr} )
&& $n !~ /^(verbose|msgContact.*)$/
)
? $modules{msgConfig}{defptr}{NAME}
: ""
);
return
# look for direct
AttrVal(
$d, $n,
# look for indirect
AttrVal(
AttrVal( $d, "msgRecipient$msgType", "" ),
$n,
# look for indirect, type-independent
AttrVal(
AttrVal( $d, "msgRecipient", "" ),
$n,
# look for global direct
AttrVal(
$g, $n,
# look for global indirect
AttrVal(
AttrVal( $g, "msgRecipient$msgType", "" ),
$n,
# look for global indirect, type-independent
AttrVal(
AttrVal( $g, "msgRecipient", "" ),
$n,
# default
$default
)
)
)
)
)
);
}
sub msgConfig_FindReadingsVal($$$$) {
my ( $d, $n, $msgType, $default ) = @_;
$msgType = ucfirst($msgType) if ($msgType);
return
# look for direct
ReadingsVal(
$d, $n,
# look for indirect
ReadingsVal(
AttrVal( $d, "msgRecipient$msgType", "" ),
$n,
# look for indirect, type-independent
ReadingsVal(
AttrVal( $d, "msgRecipient", "" ),
$n,
# default
$default
)
)
);
}
sub msgConfig_QueueAdd(@) {
my (
$msgA, $params, $datetime, $msgID,
$minorID, $type, $recipient, $subRecipient,
$termRecipient, $priority, $title, $msg
) = @_;
my $name = $modules{msgConfig}{defptr}{NAME};
return 0 if ( $defs{$name}{queue}{$recipient}{"$msgID.$minorID"} );
$defs{$name}{queue}{$recipient}{"$msgID.$minorID"} = {
msgOrig => $msgA,
msgOrigParams => $params,
datetime => $datetime,
majorId => $msgID,
minorId => $minorID,
type => $type,
recipient => $recipient,
subRecipient => $subRecipient,
terminalRecipient => $termRecipient,
priority => $priority,
title => $title,
message => $msg,
};
delete $defs{$name}{queue}{$recipient}{"$msgID.$minorID"}{msgOrigParams}
{msgQID};
readingsSingleUpdate(
$defs{$name},
"Q_" . $recipient,
scalar keys %{ $defs{$name}{queue}{$recipient} }, 1
);
return 1;
}
sub msgConfig_QueueReleaseMsgId($$) {
my ( $recipient, $msgID ) = @_;
my $name = $modules{msgConfig}{defptr}{NAME};
return 0
unless ( $defs{$name}{queue}
&& $defs{$name}{queue}{$recipient}
&& $defs{$name}{queue}{$recipient}{$msgID} );
delete $defs{$name}{queue}{$recipient}{$msgID};
my $c = scalar keys %{ $defs{$name}{queue}{$recipient} };
delete $defs{$name}{queue}{$recipient} unless ($c);
readingsSingleUpdate( $defs{$name}, "Q_" . $recipient, $c, 1 );
return 1;
}
1;
__END__
=pod
=encoding utf8
=item helper
=item summary global settings and tools for FHEM command <a href="#MSG">msg</a>
=item summary_DE globale Einstellungen und Tools für das FHEM Kommando <a href="#MSG">msg</a>
=begin html
<p>
<a id="msgConfig"></a>
</p>
<h3>
msgConfig
</h3>
<ul>
Provides global settings and tools for FHEM command <a href="#MSG">msg</a>.<br>
A device named globalMsg will be created automatically when using msg-command for the first time and no msgConfig device could be found.<br>
The device name can be renamed and reconfigured afterwards if desired.<br>
<br>
<a id="msgConfig-define"></a> <h4>Define</h4>
<ul>
<code>define <name> msgConfig</code><br>
</ul><br>
<br>
<a id="msgConfig-set"></a> <h4>Set</h4>
<ul>
<ul>
<li>
<a id="msgConfig-set-addLocation"></a><b>addLocation</b> <Location Name> <br>
Conveniently creates a Dummy device based on the given location name. It will be pre-configured to be used together with location-based routing when using the msg-command. The dummy device will be added to attribute msgLocationDevs automatically. Afterwards additional configuration is required by adding msgContact* or msgRecipient* attributes for gateway devices placed at this specific location.
</li>
<li>
<a id="msgConfig-set-cleanReadings"></a><b>cleanReadings</b> [<device and/or regex>] <br>
Easy way to cleanup all fhemMsg readings. A parameter is optional and can be a concrete device name or mixed together with regex. This command is an alias for "deletereading <device and/or regex> fhemMsg.*".
</li>
<li>
<a id="msgConfig-set-createResidentsDev"></a><b>createResidentsDev</b> <de|en> <br>
Creates a new device named rgr_Residents of type <a href="#RESIDENTS">RESIDENTS</a>. It will be pre-configured based on the given language. In case rgr_Residents exists it will be updated based on the given language (basically only a language change). Afterwards next configuration steps will be displayed to use RESIDENTS together with presence-based routing of the msg-command.<br>
This next step is basically to set attribute msgResidentsDevice to refer to this RESIDENTS device either globally or for any other specific FHEM device (most likely you do NOT want to have this attribute set globally as otherwise this will affect ALL devices and therefore ALL msg-commands in your automations).<br>
Note that use of RESIDENTS only makes sense together with ROOMMATE and or GUEST devices which still need to be created manually. See <a href="#RESIDENTSset">RESIDENTS Set commands</a> addRoommate and addGuest respectively.
</li>
<li>
<a id="msgConfig-set-createSwitcherDev"></a><b>createSwitcherDev</b> <de|en> <br>
Creates a pre-configured Dummy device named HouseAnn and updates globalMsg attribute msgSwitcherDev to refer to it.
</li>
</ul>
</ul>
</ul>
=end html
=begin html_DE
<p>
<a id="msgConfig"></a>
</p>
<h3>
msgConfig
</h3>
<ul>
Stellt globale Einstellungen und Tools für das FHEM Kommando <a href="#MSG">msg</a> bereit.<br>
Ein Device mit dem Namen globalMsg wird automatisch bei der ersten Benutzung des msg Kommandos angelegt, sofern kein msgConfig Device gefunden wurde.<br>
Der Device Name kann anschließend beliebig umbenannt und umkonfiguriert werden.<br>
<br>
<a id="msgConfig-define"></a> <h4>Define</h4>
<ul>
<code>define <name> msgConfig</code><br>
</ul><br>
<br>
<a id="msgConfig-set"></a> <h4>Set</h4>
<ul>
<ul>
<li>
<a id="msgConfig-set-addLocation"></a><b>addLocation</b> <Name der Lokation> <br>
Erstellt auf einfache Weise ein Dummy Device basierend auf dem übergebenen Lokationsnamen. Es wird for die lokations-basierte Verwendung mit dem msg-Kommando vorkonfiguriert. Das Dummy Device wird automatisch zum Attribut msgLocationDevs hinzugefügt. Anschließend ist eine weitere Konfiguration über die Attribute msgContact* oder msgRecipient* notwendig, die auf entsprechende Gateway Devices verweisen, die an dieser Lokation stehen.
</li>
<li>
<a id="msgConfig-set-cleanReadings"></a><b>cleanReadings</b> [<device and/or regex>] <br>
Einfache Methode, um alle fhemMsg-Readings zu säubern. Optional kann ein Parameter angegeben werden, um ein bestimmtes Device zu säubern, als Device Name kann auch regex angegeben werden. Dieses Kommando ist ein Alias for "deletereading <device and/or regex> fhemMsg.*".
</li>
<li>
<a id="msgConfig-set-createResidentsDev"></a><b>createResidentsDev</b> <de|en> <br>
Creates a new device named rgr_Residents of type <a href="#RESIDENTS">RESIDENTS</a>. It will be pre-configured based on the given language. In case rgr_Residents exists it will be updated based on the given language (basically only a language change). Afterwards next configuration steps will be displayed to use RESIDENTS together with presence-based routing of the msg-command.<br>
This next step is basically to set attribute msgResidentsDevice to refer to this RESIDENTS device either globally or for any other specific FHEM device (most likely you do NOT want to have this attribute set globally as otherwise this will affect ALL devices and therefore ALL msg-commands in your automations).<br>
Note that use of RESIDENTS only makes sense together with ROOMMATE and or GUEST devices which still need to be created manually. See <a href="#RESIDENTSset">RESIDENTS Set commands</a> addRoommate and addGuest respectively.
</li>
<li>
<a id="msgConfig-set-createSwitcherDev"></a><b>createSwitcherDev</b> <de|en> <br>
Creates a pre-configured Dummy device named HouseAnn and updates globalMsg attribute msgSwitcherDev to refer to it.
</li>
</ul>
</ul>
<a id="msgConfig-attr"></a> <h4>Attribute</h4>
<ul>
<ul>
<li>
<a id="msgConfig-attr-msgContact" data-pattern="msgContact.*"></a><b>msgContact<TYPE></b> <br>
FHEM Gerätename, welcher zur Übermittlung von Nachrichten des jeweiligen Typs angesprochen werden soll.
<ul>
<li>Muss bei Audio Nachrichten ohne eigene Definition von msgCmdAudio* ein Gerät vom Typ SONOSPLAYER sein.</li>
<li>Muss bei Screen Nachrichten ohne eigene Definition von msgCmdScreen* ein Gerät vom Typ ENIGMA2 sein.</li>
<li>Muss bei Light Nachrichten ohne eigene Definition von msgCmdLight* ein Gerät vom Typ HUEDevice sein.</li>
<li>Muss bei Push Nachrichten ohne eigene Definition von msgCmdPush* ein Gerät vom Typ Pushover sein.</li>
<li>Muss bei Mail Nachrichten eine oder mehrere gültige E-Mail Adressen enthalten.</li>
</ul>
Bei FHEM Gerätenamen, über die mehrere Empfänger adressiert werden können, kann der Empfänger mittels Doppelpunkt getrennt vom FHEM Gerätenamen angegeben werden. Je nach Modul ist das optional oder verbindlich.
</li>
</ul>
</ul>
</ul>
=end html_DE
=for :application/json;q=META.json 75_msgConfig.pm
{
"author": [
"Julian Pawlowski <julian.pawlowski@gmail.com>"
],
"x_fhem_maintainer": [
"loredo"
],
"x_fhem_maintainer_github": [
"jpawlowski"
],
"keywords": [
"configure",
"messaging",
"messenger"
]
}
=end :application/json;q=META.json
=cut