mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-19 18:56:03 +00:00
70_Pushover: improved error handling
git-svn-id: https://svn.fhem.de/fhem/trunk@13852 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
a8c40cf41e
commit
d8ef10408a
@ -1072,11 +1072,11 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
|
|
||||||
# general values
|
# general values
|
||||||
$values{title} =
|
$values{title} =
|
||||||
$h->{title} ? $h->{title} : AttrVal( $hash->{NAME}, "title", "" );
|
$h->{title} ? $h->{title} : AttrVal( $hash->{NAME}, "title", undef );
|
||||||
$values{device} =
|
$values{device} =
|
||||||
$h->{device} ? $h->{device} : AttrVal( $hash->{NAME}, "device", "" );
|
$h->{device} ? $h->{device} : AttrVal( $hash->{NAME}, "device", undef );
|
||||||
|
|
||||||
# message only
|
# message
|
||||||
if ( $cmd eq "msg" ) {
|
if ( $cmd eq "msg" ) {
|
||||||
if ( defined( $h->{message} ) ) {
|
if ( defined( $h->{message} ) ) {
|
||||||
$values{message} = $h->{message};
|
$values{message} = $h->{message};
|
||||||
@ -1090,24 +1090,63 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
else {
|
else {
|
||||||
$values{message} = join ' ', @$a;
|
$values{message} = join ' ', @$a;
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return "Message cannot be empty"
|
"Usage: $name msg <text> [ option1=<value> option2='<value with space>' ... ]"
|
||||||
unless ( defined( $values{message} ) && $values{message} ne "" );
|
unless ( defined( $values{message} ) && $values{message} ne "" );
|
||||||
}
|
|
||||||
$values{priority} =
|
|
||||||
$h->{priority} ? $h->{priority} : AttrVal( $hash->{NAME}, "priority", 0 );
|
|
||||||
$values{sound} =
|
|
||||||
$h->{sound} ? $h->{sound} : AttrVal( $hash->{NAME}, "sound", "" );
|
|
||||||
$values{timestamp} = $h->{timestamp} ? $h->{timestamp} : undef;
|
|
||||||
$values{retry} = $h->{retry} ? $h->{retry} : "";
|
|
||||||
$values{expire} = $h->{expire} ? $h->{expire} : "";
|
|
||||||
$values{url_title} = $h->{url_title} ? $h->{url_title} : "";
|
|
||||||
$values{action} =
|
|
||||||
$h->{action} ? $h->{action} : ( $h->{url} ? $h->{url} : "" );
|
|
||||||
$values{cancel_id} = $h->{cancel_id}
|
|
||||||
if ( defined( $h->{cancel_id} ) && $values{priority} ge "2" );
|
|
||||||
|
|
||||||
# glances only
|
$values{priority} =
|
||||||
|
$h->{priority}
|
||||||
|
? $h->{priority}
|
||||||
|
: AttrVal( $hash->{NAME}, "priority", undef );
|
||||||
|
return "priority is out of scope"
|
||||||
|
unless ( !$values{priority} || $values{priority} =~ m/^-?\d+$/ );
|
||||||
|
|
||||||
|
return "timestamp is out of scope"
|
||||||
|
unless ( !$values{timestamp} || $values{timestamp} =~ m/\d+$/ );
|
||||||
|
|
||||||
|
$values{retry} = ( $h->{retry} ? $h->{retry} : undef );
|
||||||
|
return "retry is out of scope"
|
||||||
|
unless ( !$values{retry}
|
||||||
|
|| ( $values{retry} =~ m/\d+$/ && $values{retry} >= 30 ) );
|
||||||
|
|
||||||
|
$values{expire} = ( $h->{expire} ? $h->{expire} : undef );
|
||||||
|
return "retry is out of scope"
|
||||||
|
unless ( !$values{expire} || $values{expire} =~ m/\d+$/ );
|
||||||
|
|
||||||
|
return "priority 2 messages require parameters retry and expire"
|
||||||
|
if ( $values{priority}
|
||||||
|
&& $values{priority} == 2
|
||||||
|
&& !defined( $values{retry} )
|
||||||
|
&& !defined( $values{expire} ) );
|
||||||
|
|
||||||
|
$values{action} =
|
||||||
|
$h->{action} ? $h->{action} : ( $h->{url} ? $h->{url} : undef );
|
||||||
|
$values{url_title} = ( $h->{url_title} ? $h->{url_title} : undef );
|
||||||
|
|
||||||
|
return "url_title requires parameter url"
|
||||||
|
if ( defined( $values{url_title} )
|
||||||
|
&& !defined( $values{url} ) );
|
||||||
|
|
||||||
|
return "url requires parameter url_title"
|
||||||
|
if ( defined( $values{url} )
|
||||||
|
&& !defined( $values{url_title} ) );
|
||||||
|
|
||||||
|
return "messages containing a URL require parameter expire"
|
||||||
|
if ( $values{action}
|
||||||
|
&& defined( $values{url_title} )
|
||||||
|
&& !defined( $values{expire} ) );
|
||||||
|
|
||||||
|
$values{sound} =
|
||||||
|
$h->{sound} ? $h->{sound} : AttrVal( $hash->{NAME}, "sound", undef );
|
||||||
|
$values{timestamp} = ( $h->{timestamp} ? $h->{timestamp} : undef );
|
||||||
|
|
||||||
|
$values{cancel_id} = $h->{cancel_id}
|
||||||
|
if ( defined( $h->{cancel_id} )
|
||||||
|
&& $values{priority}
|
||||||
|
&& $values{priority} == 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
# glances
|
||||||
if ( $cmd eq "glance" ) {
|
if ( $cmd eq "glance" ) {
|
||||||
if ( defined( $h->{text} ) ) {
|
if ( defined( $h->{text} ) ) {
|
||||||
$values{text} = $h->{text};
|
$values{text} = $h->{text};
|
||||||
@ -1122,26 +1161,42 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
$values{text} = join ' ', @$a;
|
$values{text} = join ' ', @$a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Text cannot be empty"
|
$values{subtext} = ( defined( $h->{subtext} ) ? $h->{subtext} : undef );
|
||||||
unless ( defined( $values{text} ) && $values{text} ne "" );
|
|
||||||
|
$values{count} = ( defined( $h->{count} ) ? $h->{count} : undef );
|
||||||
|
return "count is out of scope"
|
||||||
|
unless ( !$values{count} || $values{count} =~ m/-?\d+$/ );
|
||||||
|
|
||||||
|
$values{percent} = ( defined( $h->{percent} ) ? $h->{percent} : undef );
|
||||||
|
return "percent is out of scope"
|
||||||
|
unless (
|
||||||
|
!$values{percent}
|
||||||
|
|| ( $values{percent} =~ m/\d+$/
|
||||||
|
&& $values{percent} >= 0
|
||||||
|
&& $values{percent} <= 100 )
|
||||||
|
);
|
||||||
|
|
||||||
|
return
|
||||||
|
"Usage: $name glance [ title='<value>' text='<value>' subtext='<value>' count=<value> percent=<value> ]"
|
||||||
|
unless ( defined( $values{title} )
|
||||||
|
|| ( defined( $values{text} ) && $values{text} ne "" )
|
||||||
|
|| defined( $values{subtext} )
|
||||||
|
|| defined( $values{count} )
|
||||||
|
|| defined( $values{percent} ) );
|
||||||
}
|
}
|
||||||
$values{subtext} =
|
|
||||||
defined( $h->{subtext} ) && $h->{subtext} ne "" ? $h->{subtext} : undef;
|
|
||||||
$values{count} =
|
|
||||||
defined( $h->{count} ) && $h->{count} ne "" ? $h->{count} : undef;
|
|
||||||
$values{percent} =
|
|
||||||
defined( $h->{percent} ) && $h->{percent} ne "" ? $h->{percent} : undef;
|
|
||||||
|
|
||||||
my $callback = (
|
my $callback = (
|
||||||
defined( $attr{$name}{callbackUrl} )
|
defined( $attr{$name}{callbackUrl} )
|
||||||
&& defined( $hash->{fhem}{infix} )
|
&& defined( $hash->{fhem}{infix} )
|
||||||
? $attr{$name}{callbackUrl}
|
? $attr{$name}{callbackUrl}
|
||||||
: ""
|
: undef
|
||||||
);
|
);
|
||||||
|
|
||||||
# check if we got a user or group key as device and use it as
|
# check if we got a user or group key as device and use it as
|
||||||
# user-key instead of hash->USER_KEY
|
# user-key instead of hash->USER_KEY
|
||||||
if ( $values{device} =~ /^(([A-Za-z0-9]{30}):)?([A-Za-z0-9,_-]*)(.*)$/ ) {
|
if ( $values{device}
|
||||||
|
&& $values{device} =~ /^(([A-Za-z0-9]{30}):)?([A-Za-z0-9,_-]*)(.*)$/ )
|
||||||
|
{
|
||||||
$values{USER_KEY} = $2 if ( $2 ne "" );
|
$values{USER_KEY} = $2 if ( $2 ne "" );
|
||||||
$values{device} = $3;
|
$values{device} = $3;
|
||||||
|
|
||||||
@ -1151,34 +1206,9 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if all mandatory arguments are filled:
|
|
||||||
# "message" can not be empty and if "priority" is set to "2" "retry" and
|
|
||||||
# "expire" must also be set.
|
|
||||||
# "url_title" and "action" need to be set together and require "expire"
|
|
||||||
# to be set as well.
|
|
||||||
if (
|
|
||||||
(
|
|
||||||
defined( $values{message} )
|
|
||||||
|| defined( $values{text} )
|
|
||||||
|| defined( $values{subtext} )
|
|
||||||
|| defined( $values{count} )
|
|
||||||
|| defined( $values{percent} )
|
|
||||||
)
|
|
||||||
&& ( ( $values{retry} ne "" && $values{expire} ne "" )
|
|
||||||
|| $values{priority} < 2 )
|
|
||||||
&& (
|
|
||||||
(
|
|
||||||
$values{url_title} ne ""
|
|
||||||
&& $values{action} ne ""
|
|
||||||
&& $values{expire} ne ""
|
|
||||||
)
|
|
||||||
|| ( $values{url_title} eq "" && $values{action} eq "" )
|
|
||||||
)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
my $body;
|
my $body;
|
||||||
$body = "title=" . urlEncode( $values{title} )
|
$body = "title=" . urlEncode( $values{title} )
|
||||||
if ( $values{title} ne "" );
|
if ( defined( $values{title} ) );
|
||||||
|
|
||||||
if ( $values{message}
|
if ( $values{message}
|
||||||
&& $values{message} =~
|
&& $values{message} =~
|
||||||
@ -1243,42 +1273,36 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
$body .= "&subtext=" . $values{subtext};
|
$body .= "&subtext=" . $values{subtext};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defined( $values{count} )
|
if ( defined( $values{count} ) ) {
|
||||||
&& looks_like_number( $values{count} ) )
|
|
||||||
{
|
|
||||||
$body .= "&count=" . $values{count};
|
$body .= "&count=" . $values{count};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defined( $values{percent} )
|
if ( defined( $values{percent} ) ) {
|
||||||
&& looks_like_number( $values{percent} )
|
|
||||||
&& $values{percent} >= 0
|
|
||||||
&& $values{percent} <= 100 )
|
|
||||||
{
|
|
||||||
$body .= "&percent=" . $values{percent};
|
$body .= "&percent=" . $values{percent};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{device} ne "" ) {
|
if ( $values{device} ) {
|
||||||
$body .= "&device=" . $values{device};
|
$body .= "&device=" . $values{device};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{priority} ne "" ) {
|
if ( $values{priority} ) {
|
||||||
$values{priority} = 2 if ( $values{priority} > 2 );
|
$values{priority} = 2 if ( $values{priority} > 2 );
|
||||||
$values{priority} = -2 if ( $values{priority} < -2 );
|
$values{priority} = -2 if ( $values{priority} < -2 );
|
||||||
$body .= "&priority=" . $values{priority};
|
$body .= "&priority=" . $values{priority};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{sound} ne "" ) {
|
if ( $values{sound} ) {
|
||||||
$body .= "&sound=" . $values{sound};
|
$body .= "&sound=" . $values{sound};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{retry} ne "" ) {
|
if ( defined( $values{retry} ) ) {
|
||||||
$body .= "&retry=" . $values{retry};
|
$body .= "&retry=" . $values{retry};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{expire} ne "" ) {
|
if ( defined( $values{expire} ) ) {
|
||||||
$body .= "&expire=" . $values{expire};
|
$body .= "&expire=" . $values{expire};
|
||||||
|
|
||||||
$values{cbNr} = int( time() ) + $values{expire};
|
$values{cbNr} = round( time(), 0 ) + $values{expire};
|
||||||
my $cbReading = "cb_" . $values{cbNr};
|
my $cbReading = "cb_" . $values{cbNr};
|
||||||
until ( ReadingsVal( $name, $cbReading, "" ) eq "" ) {
|
until ( ReadingsVal( $name, $cbReading, "" ) eq "" ) {
|
||||||
$values{cbNr}++;
|
$values{cbNr}++;
|
||||||
@ -1286,33 +1310,33 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{timestamp} ne "" ) {
|
if ( $values{timestamp} ) {
|
||||||
$body .= "×tamp=" . $values{timestamp};
|
$body .= "×tamp=" . $values{timestamp};
|
||||||
}
|
}
|
||||||
elsif ( 1 == AttrVal( $hash->{NAME}, "timestamp", 0 ) ) {
|
elsif ( 1 == AttrVal( $hash->{NAME}, "timestamp", 0 ) ) {
|
||||||
$body .= "×tamp=" . int( time() );
|
$body .= "×tamp=" . int( time() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $callback ne "" && $values{priority} > 1 ) {
|
if ( $callback && $values{priority} && $values{priority} > 1 ) {
|
||||||
Log3 $name, 5,
|
Log3 $name, 5,
|
||||||
"Pushover $name: Adding emergency callback URL $callback";
|
"Pushover $name: Adding emergency callback URL $callback";
|
||||||
$body .= "&callback=" . $callback;
|
$body .= "&callback=" . $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $values{url_title} ne ""
|
if ( $values{url_title}
|
||||||
&& $values{action} ne ""
|
&& $values{action}
|
||||||
&& $values{expire} ne "" )
|
&& defined( $values{expire} ) )
|
||||||
{
|
{
|
||||||
my $url;
|
my $url;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$callback eq ""
|
!$callback
|
||||||
|| ( $values{action} !~ /^http[s]?:\/\/.*$/
|
|| ( $values{action} !~ /^http[s]?:\/\/.*$/
|
||||||
&& $values{action} =~ /^[\w-]+:\/\/.*$/ )
|
&& $values{action} =~ /^[\w-]+:\/\/.*$/ )
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$url = $values{action};
|
$url = $values{action};
|
||||||
$values{expire} = "";
|
$values{expire} = undef;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$url =
|
$url =
|
||||||
@ -1386,14 +1410,7 @@ sub Pushover_SetMessage2 ($$$$) {
|
|||||||
if ( $cmd eq "msg" );
|
if ( $cmd eq "msg" );
|
||||||
return Pushover_SendCommand( $hash, "glances.json", $body, %values )
|
return Pushover_SendCommand( $hash, "glances.json", $body, %values )
|
||||||
if ( $cmd eq "glance" );
|
if ( $cmd eq "glance" );
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
# There was a problem with the arguments, so tell the user the
|
|
||||||
# correct usage of the 'set msg' command
|
|
||||||
return
|
|
||||||
"Syntax: $name msg <text> [ option1=<value> option2='<value with space>' ... ]";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Pushover_CancelMessage ($$$$) {
|
sub Pushover_CancelMessage ($$$$) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user