2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-07 16:59:18 +00:00

70_PushNotifier.pm: regex error handling

git-svn-id: https://svn.fhem.de/fhem/trunk@9220 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
xusader 2015-09-10 05:21:41 +00:00
parent da03d244d5
commit bdf87ffc18
2 changed files with 53 additions and 23 deletions

View File

@ -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

View File

@ -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 <deviceID>";
}
$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;
}