-
Notifications
You must be signed in to change notification settings - Fork 0
/
70_Growl.pm
349 lines (281 loc) · 9.58 KB
/
70_Growl.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
# $Id$
##############################################################################
#
# 70_Growl.pm
# An FHEM Perl module to send push messages via https://www.Growlapp.com/
#
# Copyright by Oli Merten
# e-mail: oli.merten at gmail.com
#
# This file luckily is not part of fhem.
#
# Fhem is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Fhem is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fhem. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Changelog:
# 09.10.2016: Initial Version
# 11.10.2016: Version 0.1.01: added disabled attribute
##############################################################################
package main;
use strict;
use warnings;
use HttpUtils;
my $missingModul;
eval "use Data::Dumper;1" or $missingModul .= "Data:Dumper ";
eval "use Growl::GNTP;1" or $missingModul .= "Growl::GNTP ";
my $version = "0.1.00";
###################################
sub Growl_Initialize($) {
my ($hash) = @_;
# Module specific attributes
my @Growl_attr =(
"default_prio:-2,-1,0,1,2",
"default_event",
"default_application",
"default_sticky:true,false",
"Growl_passwd",
"disable:0,1"
);
$hash->{GetFn} = "Growl_Get";
$hash->{SetFn} = "Growl_Set";
$hash->{DefFn} = "Growl_Define";
$hash->{UndefFn} = "Growl_Undefine";
$hash->{AttrFn} = "Growl_Attr";
$hash->{AttrList} = join( " ", @Growl_attr ) . " " . $readingFnAttributes;
}
###################################
sub Growl_Define($$) {
my ( $hash, $def ) = @_;
my @a = split( "[ \t][ \t]*", $def );
return "Error: Perl moduls ".$missingModul."are missing on this system" if $missingModul;
my $usage = "syntax: define <name> Growl [<host>]";
if ( int(@a) < 2 or int(@a) > 3) {
return $usage;
}
my ( $name, $type) = @a;
my $host = "localhost";
if ($a[2]) {
$host = $a[2];
};
$hash->{DEF} = $host;
Log3 $name, 3, "Growl defined: $name $type $host";
$hash->{VERSION} = $version;
if (!Growl_addExtension( $name, "Growl_Callback", $name ))
{return "Extension with $name alreay existing?";}
if ($init_done){
#Growl_GetUpdate($hash);
}
return undef;
}
###################################
sub Growl_Undefine($$) {
my ( $hash, $name ) = @_;
RemoveInternalTimer($hash);
Growl_removeExtension( $hash->{fhem}{$name} );
return undef;
}
###################################
sub Growl_Set($@) {
my ( $hash, @a ) = @_;
my $name = $hash->{NAME};
my $state = $hash->{STATE};
return "No Argument given" if ( !defined( $a[1] ) );
if ( AttrVal( $name, "disable", 0 ) == 1 ) {
Log3 $name, 5, "Growl $name: Unable to send message: Device is disabled";
return "Unable to send message: Device is disabled";
}
Log3 $name, 5, "Growl $name: called function Growl_Set() with " . Dumper(@a);
my $usage = "Unknown argument " . $a[1] . ", choose one of msg";
# Send a message
if ( $a[1] eq "msg" ) {
return Growl_SendMessage( $hash, splice( @a, 2 ) );
}
# return usage hint
else {
return $usage;
}
return undef;
}
###################################
sub Growl_Get($@) {
my ( $hash, @a ) = @_;
my $name = $hash->{NAME};
my $state = $hash->{STATE};
return "No Argument given" if ( !defined( $a[1] ) );
# Log3 $name, 5,
# "Growl $name: called function Growl_Get() with " . Dumper(@a);
my $usage =
"Unknown argument " . $a[1] . ", choose one of url";
my $error = undef;
if ( $a[1] eq "url" ) {
my $link = "/$name";
$error = Dumper(%FW_httpheader->{Host});
}
# # get a token
# elsif ( $a[1] eq "token" ) {
# $error = Growl_GetToken($hash);
# }
# return usage hint
else {
return $usage;
}
return $error;
}
###################################
sub Growl_Attr($) {
my ( $cmd, $name, $aName, $aVal ) = @_;
# $cmd can be "del" or "set"
# $name is device name
# aName and aVal are Attribute name and value
if ( $cmd eq "set" ) {
# Priroity has to be between -2 and 2
if ( $aName eq "default_prio" ) {
if ( $aVal > 2 or $aVal < -2 ) {
Log3 $name, 3,
"$name: $aName is a value between -2 and +2: $aVal";
return "Attribute " . $aName
. " has to be a value between -2 and +2";
}
}
elsif ( $aName eq "default_sticky" ) {
if ( $aVal ne "true" and $aVal ne "false" ) {
Log3 $name, 3,
"$name: $aName is either 'true' or 'false': $aVal";
return "Attribute " . $aName
. " has to be either 'true' or 'false'";
}
}
}
return undef;
}
###################################
# Helper Functions #
###################################
sub Growl_SendMessage($@) {
my ( $hash, @a ) = @_;
my $name = $hash->{NAME};
my $msgStr = join( " ", @a );
Log3 $name, 5, "Growl $name received $msgStr";
my ( $desc, $prio, $sticky, $event, $app ) = split /:/, $msgStr;
if ( !$desc ) {
Log3 $name, 1, "Growl $name Message requires a text";
return "set msg: Message requires a text";
}
if ( !$prio ) {
$prio = AttrVal( $name, "default_prio", "1" );
}
if ( !$event ) {
$event = AttrVal( $name, "default_event", "Nachricht" );
}
if ( !$app ) {
$app = AttrVal( $name, "default_application", "FHEM" );
}
if ( !$sticky ) {
$sticky = AttrVal( $name, "default_sticky", "false" );
}
my $pass = AttrVal( $name, "Growl_passwd", "");
my $callback="http://".%FW_httpheader->{Host}."/fhem/$name";
eval {
my $growl = Growl::GNTP->new(
AppName => "FHEM Growl Module",
PeerHost => $hash->{DEF},
Password => $pass,
Debug=>"0"
);
$growl->register([
{
Name => $app,
DisplayName => $app,
#Enabled => 'True',
#Sticky => 'False',
#Priority => 0, # -2 .. 2 low -> severe
#Icon => ''
}
]);
$growl->wait('WAIT_ALL');
$growl->notify(
Event => $app, # name of notification
Title => $event,
Message => $desc,
#Icon => 'http://www.example.com/myface.png',
CallbackTarget => $callback, # Used for causing a HTTP/1.1 GET request exactly as specificed by this URL. Exclusive of CallbackContext
CallbackContextType => time,
CallbackContext => $hash,
#CallbackFunction => \&Growl_Callback,
# should only be used when a callback in use, and CallbackContext in use.
#ID => '', # allows for overriding/updateing an existing notification when in use, and discriminating between alerts of the same Event
#Custom => { CustomHeader => 'value' }, # These will be added as custom headers as X-KEY : value, where 'X-' is prefixed to the key
Priority => $prio,
Sticky => $sticky
);
$growl->wait('WAIT_ALL');
};
if ($@) {
Log3 $name, 1, "Growl $name Error: ".Dumper($@) ;
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, "lastError", $@ );
readingsBulkUpdate( $hash, "state", "error");
readingsEndUpdate( $hash, 1 );
}
else {
Log3 $name, 3, "Growl $name sent message: $desc";
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, "lastMessage", $desc);
readingsBulkUpdate( $hash, "state", "success");
readingsEndUpdate( $hash, 1 );
}
}
###################################
sub Growl_addExtension($$$) {
my ( $name, $func, $link ) = @_;
my $url = "/$link";
return 0
if ( defined( $data{FWEXT}{$url} )
&& $data{FWEXT}{$url}{deviceName} ne $name );
Log3 $name, 2,
"Growl $name: Registering Growl with URL $url ...";
$data{FWEXT}{$url}{deviceName} = $name;
$data{FWEXT}{$url}{FUNC} = $func;
$data{FWEXT}{$url}{LINK} = $link;
return 1;
}
###################################
sub Growl_removeExtension($) {
my ($link) = @_;
my $url = "/$link";
my $name = $data{FWEXT}{$url}{deviceName};
Log3 $name, 2,
"Growl $name: Unregistering Growl for URL $url...";
delete $data{FWEXT}{$url};
}
###################################
sub Growl_Callback(@) {
my ($request) = @_;
Log3 "growl2", 3, "Growl received Callback: ".Dumper($request);
my $hash;
my $name = "";
my $link = "";
my $URI = "";
my $msg = "ok";
# data received
if ( $request =~ m,^(/[^/]+?)(?:\&|\?)(.*)?$, ) {
$link = $1;
$URI = $2;
# get device name
$name = $data{FWEXT}{$link}{deviceName} if ( $data{FWEXT}{$link} );
$hash = $defs{$name};
}
Log3 $name, 3, "Growl $name received Callback: ".Dumper($request);
return ( "text/plain; charset=utf-8", $msg );
}