-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path99_myAdvanced_Utils.pm
181 lines (161 loc) · 5.55 KB
/
99_myAdvanced_Utils.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
##############################################
# $Id: myAdvanced_Utils.pm 2021-01-09 Beta-User $
#
package main;
use strict;
use warnings;
sub
myAdvanced_Utils_Initialize
{
my $hash = shift;
}
# Enter you functions below _this_ line.
######## sendemail für den Emailversand verwenden ############
#adopted version from https://wiki.fhem.de/wiki/E-Mail_senden#Raspberry_Pi
sub
mySendEmail {
my ($rcpt, $subject, $text, $attach) = @_;
my $sender = getKeyValue("myEmailAddress"); # use {setKeyValue("myEmailAddress",'[email protected]')} once in commandline to store the parameter
my $konto = getKeyValue("myEmailAccount"); # like before: {setKeyValue("myEmailAccount",'[email protected]')}
my $passwrd = getKeyValue("myEmailPasswrd"); # like before: {setKeyValue("myEmailPasswrd","passwrd")}
my $provider = getKeyValue("myEmailServer"); # like before: {setKeyValue("myEmailServer",'smtp.provider.de:587')}
Log 1, "mySendEmail RCP: $rcpt, Subject: $subject, Text: $text";
Log 1, "mySendEmail Anhang: $attach" if defined $attach;
my $ret ="";
if ($attach) {
$ret .= qx(sendemail -f $sender -t $rcpt -u $subject -m $text -a $attach -s $provider -xu $konto -xp $passwrd -o tls=auto -o message-charset=utf-8);
} else {
$ret .= qx(sendemail -f $sender -t $rcpt -u $subject -m $text -s $provider -xu $konto -xp $passwrd -o tls=auto -o message-charset=utf-8);
}
$ret =~ s,[\r\n]*,,g; # remove CR from return-string
Log3("mySendEmail", 3, "mySendEmail returned: $ret");
return
}
#originally found: https://forum.fhem.de/index.php/topic,85958.msg791048.html#msg791048
sub listInternalTimer {
my $p = shift;
my %cop;
for my $e (@intAtA)
{
my $name = "";
if (ref($e->{ARG}) eq "HASH") {
if (exists($e->{ARG}{NAME}))
{
$name = $e->{ARG}{NAME};
}
elsif (exists($e->{ARG}{arg}))
{
$name = $e->{ARG}{arg};
}
}
elsif (ref($e->{ARG}) eq "REF" && exists(${$e->{ARG}}->{hash}))
{
$name = ${$e->{ARG}}->{hash}{NAME};
}
elsif (ref($e->{ARG}) ne "REF")
{
$name = $e->{ARG};
}
my $time = strftime('%d.%m.%Y %H:%M:%S', localtime($e->{TRIGGERTIME}));
my $function = sprintf("%-25s %-25s", $name, $e->{FN});
my $line = "<td>".$e->{atNr}."</td><td>".$time."</td><td>".$function."</td>";
if ('f' eq $p)
{
$cop{$function." ".$e->{atNr}} = $line;
}
elsif ('t' eq $p)
{
$cop{$time." ".$e->{atNr}} = $line;
}
else
{
$cop{$name." ".$e->{atNr}} = $line;
}
}
my $ret = '<html><table width=50%>';
$ret .= "<td><b>InternalTimer List</b></td>";
$ret .= '</tr></tr>';
$ret .= "<td><b>Number</b></td>";
$ret .= "<td><b>Date/Time</b></td>";
$ret .= "<td><b>Function</b></td>";
$ret .= '</tr>';
for my $k (sort keys %cop) {
$ret .= "$cop{$k}";
$ret .= '</tr>';
}
$ret .= '</table></html>';
return $ret;
}
sub mySendLast20LogEntries {
my $bot = shift;
my $receiver = shift // return;
my $seconds = time();
my $stop_processing = 0;
my @t = localtime($seconds);
my $today_log_filename = ResolveDateWildcards($attr{global}{logfile}, @t);
my @tyesterday = localtime($seconds-DAYSECONDS);
my ($err, @logdata) = FileRead({FileName => $today_log_filename,ForceType => "file"});
my $message = "Error reading logfile: $err";
unless ($err) {
my @datatosend;
if (@logdata > 19) {
@datatosend = @logdata[-20..-1];
} else {
@datatosend = @logdata;
my $yesterday_log_filename = ResolveDateWildcards($attr{global}{logfile}, @tyesterday);
unless ($yesterday_log_filename eq $today_log_filename) {
my ($err2, @logdata2) = FileRead({FileName => $yesterday_log_filename,ForceType => "file"});
my $remainingLines = (@datatosend) - 20;
unshift (@datatosend, @logdata2[${remainingLines}..-1]) unless $err2;
}
}
my $str_today = ResolveDateWildcards("%Y.%m.%d",@t);
my $str_yesterday = ResolveDateWildcards("%Y.%m.%d",@tyesterday);
@datatosend = grep {
$_ =~ s/($str_today|$str_yesterday).//g #get only values from today and yesterday, cut of date and one character
} @datatosend;
for (@datatosend) { s/[;&<>]//g };#remove unwanted characters
$message = join("\n", @datatosend);
}
CommandSet(undef,"$bot _msg \@$receiver ```${message}```");
return;
}
sub identifyMyBestGW {
my $name = shift;
my $maxReadingsAge = shift // AttrVal($name,"maxReadingsAge",600);
my $hash = $defs{$name};
my @rssis = grep { $_ =~ /.*_rssi/ } sort keys %{ $hash->{READINGS} };
my $bestGW = "unknown";
my $bestGWold = ReadingsVal($name,"bestRecentGW","unknownGW");
my $bestRSSI = -1000;
my $currentRSSI = 0;
for (@rssis) {
if (ReadingsAge($name,$_,100) < $maxReadingsAge) {
$currentRSSI = ReadingsVal($name,$_,-1100);
if ($currentRSSI > $bestRSSI) {
$bestRSSI = $currentRSSI ;
$bestGW = $_;
}
}
}
$bestGW =~ s/_.*//g;
return $bestGW ne $bestGWold ? $bestGW : undef;
}
1;
__END__
=pod
=begin html
<a name="myAdvanced_Utils"></a>
<h3>myAdvanced_Utils</h3>
<ul>
<b>listInternalTimer(;$)</b><br>
<ul>
<code>{ listInternalTimer("t") }</code> -> shows InternalTimers sorted by time, earlier will be on top.
<code>{ listInternalTimer("f") }</code> -> sorts by function name.
Without or all other arguments an unsorted list will be returned.
</ul>
<ul>
</ul>
</ul>
=end html
=cut