diff --git a/CHANGED b/CHANGED index 8d77c494c..e77d96155 100644 --- a/CHANGED +++ b/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - bugfix: 70_PushNotifier.pm: regex error handling - bugfix: 31_MilightDevice.pm: fixed bug that lead to crash on lost bridge connection, added module name prefix to round function - bugfix: 38_CO20.pm: removed unneccesary error messages due to USB timeout - feature: 70_PushNotifier: use regexe for deviceID diff --git a/FHEM/70_PushNotifier.pm b/FHEM/70_PushNotifier.pm index eb09c54fc..76cf61d04 100644 --- a/FHEM/70_PushNotifier.pm +++ b/FHEM/70_PushNotifier.pm @@ -1,19 +1,33 @@ ############################################### -#$Id: 70_PushNotifier.pm 2015-09-07 12:24:00 xusader +#$Id: 70_PushNotifier.pm 2015-09-10 06:30:00 xusader +# +# regex part by pirmanji # # download client-app http://pushnotifier.de/apps/ # create account http://pushnotifier.de/login/ -# get apiToken from http://gidix.de/setings/api/ and add a new app +# +# register your app: +# http://pushnotifier.de/settings/api # -# Define example: -# define yourname PushNotifier apiToken appname user password deviceID +# Define example for all devices: +# define yourname PushNotifier apiToken appname user password .* +# +# Define example for device group: +# define yourname PushNotifier apiToken appname user password iPhone.* +# +# Define example for specific device: +# define yourname PushNotifier apiToken appname user password iPhone5 # # notify example: # define LampON notify Lamp:on set yourDefineName message Your message! # +# notify with two lines: +# define LampON notify Lamp:on set yourDefineName message Your message!_Second Line message +# package main; use LWP::UserAgent; +use Try::Tiny; sub PushNotifier_Initialize($) @@ -34,6 +48,10 @@ PushNotifier_Define($$) my ($name, $type, $apiToken, $app, $user, $passwd, $deviceID) = @args; + if (! eval { qr/$deviceID/ }) { + return "$deviceID is not a valid regex for "; + } + $hash->{STATE} = 'Initialized'; if(defined($apiToken) && defined($app)&& defined($user)&& defined($passwd)&& defined($deviceID)) { @@ -87,32 +105,43 @@ PushNotifier_Send_Message $msg =~ s/\_/\n/g; my $result=""; + my $mc=0; - while ($hash->{devices} =~ /title:([^,.]+),id:(\d+),model:([^,^\].]+)/g) { - my ($nd_title, $nd_id, $nd_model) = ("$1", "$2", "$3"); + try { + while ($hash->{devices} =~ /title:(.*?),id:(\d+),model:(.*?)(?=,title:|\])/g) { + my ($nd_title, $nd_id, $nd_model) = ("$1", "$2", "$3"); - # Log3 (undef, 3, "PushNotifier: Send Message $msg to device title: $nd_title, id: $nd_id, model: $nd_model"); + # Log3 (undef, 3, "PushNotifier: Send Message $msg to device title: $nd_title, id: $nd_id, model: $nd_model"); - if ( $nd_id =~ m/$hash->{deviceID}/ || $nd_title =~ m/$hash->{deviceID}/ || $nd_model =~ m/$hash->{deviceID}/ ) { - my $response = LWP::UserAgent->new()->post('http://a.pushnotifier.de/1/sendToDevice', - ['apiToken' => $hash->{apiToken}, - 'appToken' => $hash->{appToken}, - 'app' => $hash->{app}, - 'deviceID' => $nd_id, - 'type' => 'MESSAGE', - 'content' => "$msg"]); + if ( $nd_id =~ m/$hash->{deviceID}/ || $nd_title =~ m/$hash->{deviceID}/ || $nd_model =~ m/$hash->{deviceID}/ ) { + my $response = LWP::UserAgent->new()->post('http://a.pushnotifier.de/1/sendToDevice', + ['apiToken' => $hash->{apiToken}, + 'appToken' => $hash->{appToken}, + 'app' => $hash->{app}, + 'deviceID' => $nd_id, + 'type' => 'MESSAGE', + 'content' => "$msg"]); - my $error_chk = $response->as_string; + my $error_chk = $response->as_string; - if($error_chk =~ m/"status":"ok"/) { - $result.="OK! Message sent to $nd_title (id: $nd_id)\n\n$msg\n\n"; - } - else - { - $result.="ERROR sending message to $nd_title (id: $nd_id)\n\nResponse:\n$error_chk\n\n"; + $mc++; + + if($error_chk =~ m/"status":"ok"/) { + $result.="OK! Message sent to $nd_title (id: $nd_id)\n\n$msg\n\n"; + } + else + { + $result.="ERROR sending message to $nd_title (id: $nd_id)\n\nResponse:\n$error_chk\n\n"; + } } } - } + + }; + + if ( !$mc ) { + $result.="Regex ".$hash->{deviceID}." seems not to fit on any of your devices."; + } + return $result; }