change Code PBP conform

This commit is contained in:
Marko Oldenburg 2020-04-28 11:16:22 +02:00
parent ca21aabac8
commit c27b8ff1e7
2 changed files with 115 additions and 121 deletions

View File

@ -62,6 +62,7 @@ use strict;
use warnings;
use POSIX;
use FHEM::Meta;
use Data::Dumper;
use HttpUtils;
@ -69,7 +70,7 @@ my $missingModul = '';
eval "use Encode qw(encode encode_utf8 decode_utf8);1"
or $missingModul .= "Encode ";
# eval "use JSON;1" or $missingModul .= 'JSON ';
# eval "use JSON;1" || $missingModul .= 'JSON ';
eval "use IO::Socket::SSL;1" or $missingModul .= 'IO::Socket::SSL ';
# try to use JSON::MaybeXS wrapper
@ -275,7 +276,7 @@ sub Attr {
my $hash = $defs{$name};
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal eq '1' ) {
if ( $cmd eq 'set' && $attrVal eq '1' ) {
RemoveInternalTimer($hash);
readingsSingleUpdate( $hash, 'state', 'inactive', 1 );
Log3 $name, 3, "GardenaSmartBridge ($name) - disabled";
@ -289,7 +290,7 @@ sub Attr {
if ( $cmd eq 'set' ) {
return
"check disabledForIntervals Syntax HH:MM-HH:MM or 'HH:MM-HH:MM HH:MM-HH:MM ...'"
unless ( $attrVal =~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/ );
if ( $attrVal !~ /^((\d{2}:\d{2})-(\d{2}:\d{2})\s?)+$/ );
Log3 $name, 3, "GardenaSmartBridge ($name) - disabledForIntervals";
}
elsif ( $cmd eq 'del' ) {
@ -301,7 +302,7 @@ sub Attr {
if ( $cmd eq 'set' ) {
RemoveInternalTimer($hash);
return 'Interval must be greater than 0'
unless ( $attrVal > 0 );
if ( $attrVal == 0 );
$hash->{INTERVAL} = $attrVal;
Log3 $name, 3,
"GardenaSmartBridge ($name) - set interval: $attrVal";
@ -343,7 +344,7 @@ sub Notify {
if (
(
$devtype eq 'Global'
and (
&& (
grep /^INITIALIZED$/,
@{$events} or grep /^REREADCFG$/,
@{$events} or grep /^DEFINED.$name$/,
@ -353,11 +354,11 @@ sub Notify {
)
)
or (
|| (
$devtype eq 'GardenaSmartBridge'
and (
&& (
grep /^gardenaAccountPassword.+/,
@{$events} or ReadingsVal( '$devname', 'token', '' ) eq 'none'
@{$events} || ReadingsVal( '$devname', 'token', '' ) eq 'none'
)
)
);
@ -365,19 +366,19 @@ sub Notify {
getDevices($hash)
if (
$devtype eq 'Global'
and (
&& (
grep /^DELETEATTR.$name.disable$/,
@{$events} or grep /^ATTR.$name.disable.0$/,
@{$events} or grep /^DELETEATTR.$name.interval$/,
@{$events} or grep /^ATTR.$name.interval.[0-9]+/,
@{$events}
)
and $init_done
&& $init_done
);
if (
$devtype eq 'GardenaSmartBridge'
and (
&& (
grep /^state:.Connected$/,
@{$events} or grep /^lastRequestState:.request_error$/,
@{$events}
@ -484,7 +485,7 @@ sub ErrorHandling {
my $dhash = $hash;
$dhash = $modules{GardenaSmartDevice}{defptr}{ $param->{'device_id'} }
unless ( not defined( $param->{'device_id'} ) );
if ( defined( $param->{'device_id'} ) );
my $dname = $dhash->{NAME};
@ -510,7 +511,7 @@ sub ErrorHandling {
}
elsif ($err =~ /Keine Route zum Zielrechner/
or $err =~ /no route to target/ )
|| $err =~ /no route to target/ )
{
Log3 $dname, 5,
@ -535,7 +536,7 @@ sub ErrorHandling {
}
}
if ( $data eq "" and exists( $param->{code} ) and $param->{code} != 200 ) {
if ( $data eq "" && exists( $param->{code} ) && $param->{code} != 200 ) {
readingsBeginUpdate($dhash);
readingsBulkUpdate( $dhash, "state", $param->{code}, 1 )
@ -544,7 +545,7 @@ sub ErrorHandling {
readingsBulkUpdateIfChanged( $dhash, "lastRequestState",
"request_error", 1 );
if ( $param->{code} == 401 and $hash eq $dhash ) {
if ( $param->{code} == 401 && $hash eq $dhash ) {
if ( ReadingsVal( $dname, 'token', 'none' ) eq 'none' ) {
readingsBulkUpdate( $dhash, "state", "no token available", 1 );
@ -556,9 +557,9 @@ sub ErrorHandling {
"GardenaSmartBridge ($dname) - RequestERROR: " . $param->{code};
}
elsif ( $param->{code} == 204
and $dhash ne $hash
and defined( $dhash->{helper}{deviceAction} ) )
elsif ($param->{code} == 204
&& $dhash ne $hash
&& defined( $dhash->{helper}{deviceAction} ) )
{
readingsBulkUpdate( $dhash, "state", "the command is processed",
@ -591,9 +592,9 @@ sub ErrorHandling {
if (
$data =~ /Error/
or ( defined($decode_json)
and ref($decode_json) eq 'HASH'
and defined( $decode_json->{errors} ) )
|| ( defined($decode_json)
&& ref($decode_json) eq 'HASH'
&& defined( $decode_json->{errors} ) )
)
{
readingsBeginUpdate($dhash);
@ -605,7 +606,7 @@ sub ErrorHandling {
if ( $param->{code} == 400 ) {
if ($decode_json) {
if ( ref( $decode_json->{errors} ) eq "ARRAY"
and defined( $decode_json->{errors} ) )
&& defined( $decode_json->{errors} ) )
{
readingsBulkUpdate(
$dhash,
@ -644,7 +645,7 @@ sub ErrorHandling {
}
elsif ( $param->{code} == 404 ) {
if ( defined( $dhash->{helper}{deviceAction} ) and $dhash ne $hash )
if ( defined( $dhash->{helper}{deviceAction} ) && $dhash ne $hash )
{
readingsBulkUpdate( $dhash, "state", "device Id not found", 1 );
readingsBulkUpdate( $dhash, "lastRequestState",
@ -708,7 +709,9 @@ sub ResponseProcessing {
}
}
if ( defined( $decode_json->{sessions} ) and $decode_json->{sessions} ) {
# print Dumper $decode_json;
if ( defined( $decode_json->{sessions} ) && $decode_json->{sessions} ) {
$hash->{helper}{session_id} = $decode_json->{sessions}{token};
$hash->{helper}{user_id} = $decode_json->{sessions}{user_id};
@ -720,13 +723,12 @@ sub ResponseProcessing {
return;
}
elsif ( not defined( $hash->{helper}{locations_id} )
and defined( $decode_json->{locations} )
and ref( $decode_json->{locations} ) eq "ARRAY"
and scalar( @{ $decode_json->{locations} } ) > 0 )
elsif ( !defined( $hash->{helper}{locations_id} )
&& defined( $decode_json->{locations} )
&& ref( $decode_json->{locations} ) eq 'ARRAY'
&& scalar( @{ $decode_json->{locations} } ) > 0 )
{
foreach my $location ( @{ $decode_json->{locations} } ) {
for my $location ( @{ $decode_json->{locations} } ) {
$hash->{helper}{locations_id} = $location->{id};
@ -739,11 +741,10 @@ sub ResponseProcessing {
Write( $hash, undef, undef, undef );
return;
}
elsif ( defined( $decode_json->{devices} )
and ref( $decode_json->{devices} ) eq "ARRAY"
and scalar( @{ $decode_json->{devices} } ) > 0 )
elsif (defined( $decode_json->{devices} )
&& ref( $decode_json->{devices} ) eq 'ARRAY'
&& scalar( @{ $decode_json->{devices} } ) > 0 )
{
my @buffer = split( '"devices":\[', $json );
@ -765,19 +766,18 @@ sub ResponseProcessing {
. " Tail: "
. $tail;
unless ( not defined($tail) and not($tail) ) {
if ( defined($tail) and $tail ) {
$decode_json = eval { decode_json($json) };
if ($@) {
Log3 $name, 3,
Log3 $name, 5,
"GardenaSmartBridge ($name) - JSON error while request: $@";
}
Dispatch( $hash, $json, undef )
unless ( $decode_json->{category} eq 'gateway' );
if ( $decode_json->{category} ne 'gateway' );
WriteReadings( $hash, $decode_json )
if ( defined( $decode_json->{category} )
and $decode_json->{category} eq 'gateway' );
&& $decode_json->{category} eq 'gateway' );
}
( $json, $tail ) = ParseJSON( $hash, $tail );
@ -808,9 +808,9 @@ sub WriteReadings {
my $name = $hash->{NAME};
if ( defined( $decode_json->{id} )
and $decode_json->{id}
and defined( $decode_json->{name} )
and $decode_json->{name} )
&& $decode_json->{id}
&& defined( $decode_json->{name} )
&& $decode_json->{name} )
{
readingsBeginUpdate($hash);
if ( $decode_json->{id} eq $hash->{helper}{locations_id} ) {
@ -830,9 +830,9 @@ sub WriteReadings {
readingsBulkUpdateIfChanged( $hash, 'zones',
scalar( @{ $decode_json->{zones} } ) );
}
elsif ( $decode_json->{id} ne $hash->{helper}{locations_id}
and ref( $decode_json->{abilities} ) eq 'ARRAY'
and ref( $decode_json->{abilities}[0]{properties} ) eq 'ARRAY' )
elsif ($decode_json->{id} ne $hash->{helper}{locations_id}
&& ref( $decode_json->{abilities} ) eq 'ARRAY'
&& ref( $decode_json->{abilities}[0]{properties} ) eq 'ARRAY' )
{
my $properties =
scalar( @{ $decode_json->{abilities}[0]{properties} } );
@ -852,20 +852,20 @@ sub WriteReadings {
{name} . '-' . $t,
$v
)
unless (
if (
$decode_json->{abilities}[0]{properties}[$properties]
{name} eq 'ethernet_status'
or $decode_json->{abilities}[0]{properties}[$properties]
{name} eq 'wifi_status' );
{name} ne 'ethernet_status'
|| $decode_json->{abilities}[0]{properties}
[$properties]{name} ne 'wifi_status' );
if (
(
$decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'ethernet_status'
or $decode_json->{abilities}[0]{properties}
|| $decode_json->{abilities}[0]{properties}
[$properties]{name} eq 'wifi_status'
)
and ref($v) eq 'HASH'
&& ref($v) eq 'HASH'
)
{
if ( $decode_json->{abilities}[0]{properties}
@ -945,17 +945,17 @@ sub getToken {
if ( AttrVal( $name, 'gardenaAccountEmail', 'none' ) eq 'none' );
return readingsSingleUpdate( $hash, 'state',
'please set gardena account password first', 1 )
if ( not defined( ReadPassword( $hash, $name ) ) );
if ( !defined( ReadPassword( $hash, $name ) ) );
readingsSingleUpdate( $hash, 'state', 'get token', 1 );
delete $hash->{helper}{session_id}
if ( defined( $hash->{helper}{session_id} )
and $hash->{helper}{session_id} );
&& $hash->{helper}{session_id} );
delete $hash->{helper}{user_id}
if ( defined( $hash->{helper}{user_id} ) and $hash->{helper}{user_id} );
if ( defined( $hash->{helper}{user_id} ) && $hash->{helper}{user_id} );
delete $hash->{helper}{locations_id}
if ( defined( $hash->{helper}{locations_id} )
and $hash->{helper}{locations_id} );
&& $hash->{helper}{locations_id} );
Write(
$hash,
@ -1072,14 +1072,14 @@ sub ParseJSON {
my $tail = '';
if ($buffer) {
foreach my $c ( split //, $buffer ) {
if ( $open == $close and $open > 0 ) {
for my $c ( split //, $buffer ) {
if ( $open == $close && $open > 0 ) {
$tail .= $c;
Log3 $name, 5,
"GardenaSmartBridge ($name) - $open == $close and $open > 0";
}
elsif ( ( $open == $close ) and ( $c ne '{' ) ) {
elsif ( ( $open == $close ) && ( $c ne '{' ) ) {
Log3 $name, 5,
"GardenaSmartBridge ($name) - Garbage character before message: "
@ -1124,25 +1124,25 @@ sub createHttpValueStrings {
$header .= "\r\nX-Session: $session_id"
if ( defined( $hash->{helper}{session_id} ) );
$payload = '{' . $payload . '}' if ( defined($payload) );
$payload = '{}' if ( not defined($payload) );
$payload = '{}' if ( !defined($payload) );
if ( $payload eq '{}' ) {
$method = 'GET';
$uri .= '/locations/?user_id=' . $hash->{helper}{user_id}
if ( exists( $hash->{helper}{user_id} )
and not defined( $hash->{helper}{locations_id} ) );
&& !defined( $hash->{helper}{locations_id} ) );
readingsSingleUpdate( $hash, 'state', 'fetch locationId', 1 )
if ( not defined( $hash->{helper}{locations_id} ) );
$uri .= '/sessions' if ( not defined( $hash->{helper}{session_id} ) );
if ( !defined( $hash->{helper}{locations_id} ) );
$uri .= '/sessions' if ( !defined( $hash->{helper}{session_id} ) );
$uri .= '/devices'
if ( not defined($abilities)
and defined( $hash->{helper}{locations_id} ) );
if (!defined($abilities)
&& defined( $hash->{helper}{locations_id} ) );
}
$uri .= '/sessions' if ( not defined( $hash->{helper}{session_id} ) );
$uri .= '/sessions' if ( !defined( $hash->{helper}{session_id} ) );
if ( defined( $hash->{helper}{locations_id} ) ) {
if ( defined($abilities) and $abilities eq 'mower_settings' ) {
if ( defined($abilities) && $abilities eq 'mower_settings' ) {
$method = 'PUT';
my $dhash = $modules{GardenaSmartDevice}{defptr}{$deviceId};
@ -1152,13 +1152,13 @@ sub createHttpValueStrings {
. '/settings/'
. $dhash->{helper}{STARTINGPOINTID}
if ( defined($abilities)
and defined($payload)
and $abilities eq 'mower_settings' );
&& defined($payload)
&& $abilities eq 'mower_settings' );
}
elsif ( defined($abilities)
and defined($payload)
and $abilities eq 'watering' )
elsif (defined($abilities)
&& defined($payload)
&& $abilities eq 'watering' )
{
my $valve_id;
$method = 'PUT';
@ -1175,9 +1175,9 @@ sub createHttpValueStrings {
. $valve_id;
}
elsif ( defined($abilities)
and defined($payload)
and $abilities eq 'manual_watering' )
elsif (defined($abilities)
&& defined($payload)
&& $abilities eq 'manual_watering' )
{
my $valve_id;
$method = 'PUT';
@ -1190,9 +1190,9 @@ sub createHttpValueStrings {
. '/properties/manual_watering_timer';
}
elsif ( defined($abilities)
and defined($payload)
and $abilities eq 'power' )
elsif (defined($abilities)
&& defined($payload)
&& $abilities eq 'power' )
{
my $valve_id;
$method = 'PUT';
@ -1208,7 +1208,7 @@ sub createHttpValueStrings {
else {
$uri .=
'/devices/' . $deviceId . '/abilities/' . $abilities . '/command'
if ( defined($abilities) and defined($payload) );
if ( defined($abilities) && defined($payload) );
}
$uri .= '?locationId=' . $hash->{helper}{locations_id};

View File

@ -57,8 +57,6 @@
package FHEM::GardenaSmartDevice;
use GPUtils qw(GP_Import GP_Export);
my $missingModul = "";
use strict;
use warnings;
use POSIX;
@ -198,9 +196,6 @@ sub Define {
return
"too few parameters: define <NAME> GardenaSmartDevice <device_Id> <model>"
if ( scalar( @{$a} ) < 3 );
return
"Cannot define Gardena Bridge device. Perl modul $missingModul is missing."
if ($missingModul);
my $name = $a->[0];
my $deviceId = $a->[2];
@ -233,8 +228,8 @@ sub Define {
return
"GardenaSmartDevice device $name on GardenaSmartBridge $iodev already defined."
if ( defined($d)
and $d->{IODev} == $hash->{IODev}
and $d->{NAME} ne $name );
&& $d->{IODev} == $hash->{IODev}
&& $d->{NAME} ne $name );
CommandAttr( undef, $name . ' room GardenaSmart' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
@ -335,9 +330,9 @@ sub Set {
. ',"valve_id":'
. $valve_id . '}}';
}
elsif ( lc $cmd eq 'on' or lc $cmd eq 'off' or lc $cmd eq 'on-for-timer' ) {
elsif ( lc $cmd eq 'on' || lc $cmd eq 'off' || lc $cmd eq 'on-for-timer' ) {
my $val = (
defined($a) and ref($a) eq 'ARRAY'
defined($a) && ref($a) eq 'ARRAY'
? $a->[0] * 60
: lc $cmd
);
@ -402,7 +397,7 @@ sub Set {
$abilities = 'mower'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'mower' )
and $abilities ne 'mower_settings';
&& $abilities ne 'mower_settings';
$abilities = 'watering'
if ( AttrVal( $name, 'model', 'unknown' ) eq 'ic24'
|| AttrVal( $name, 'model', 'unknown' ) eq 'watering_computer' );
@ -480,11 +475,10 @@ sub WriteReadings {
if (
ref( $decode_json->{abilities}[$abilities]{properties} ) eq "ARRAY"
and
scalar( @{ $decode_json->{abilities}[$abilities]{properties} } ) >
0 )
&& scalar( @{ $decode_json->{abilities}[$abilities]{properties} } )
> 0 )
{
foreach my $propertie (
for my $propertie (
@{ $decode_json->{abilities}[$abilities]{properties} } )
{
readingsBulkUpdateIfChanged(
@ -494,21 +488,21 @@ sub WriteReadings {
RigRadingsValue( $hash, $propertie->{value} )
)
if ( defined( $propertie->{value} )
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'radio-quality'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'battery-level'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'internal_temperature-temperature'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'ambient_temperature-temperature'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'soil_temperature-temperature'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'humidity-humidity'
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} ne 'light-light'
and ref( $propertie->{value} ) ne "HASH" );
&& ref( $propertie->{value} ) ne "HASH" );
readingsBulkUpdate(
$hash,
@ -518,21 +512,21 @@ sub WriteReadings {
)
if (
defined( $propertie->{value} )
and ( $decode_json->{abilities}[$abilities]{name} . '-'
&& ( $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'radio-quality'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'battery-level'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq
'internal_temperature-temperature'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq
'ambient_temperature-temperature'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'soil_temperature-temperature'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'humidity-humidity'
or $decode_json->{abilities}[$abilities]{name} . '-'
|| $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'light-light' )
);
@ -543,7 +537,7 @@ sub WriteReadings {
join( ',', @{ $propertie->{value} } )
)
if ( defined( $propertie->{value} )
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'ic24-valves_connected' );
readingsBulkUpdateIfChanged(
@ -553,7 +547,7 @@ sub WriteReadings {
join( ',', @{ $propertie->{value} } )
)
if ( defined( $propertie->{value} )
and $decode_json->{abilities}[$abilities]{name} . '-'
&& $decode_json->{abilities}[$abilities]{name} . '-'
. $propertie->{name} eq 'ic24-valves_master_config' );
if ( ref( $propertie->{value} ) eq "HASH" ) {
@ -576,7 +570,7 @@ sub WriteReadings {
do {
if ( ref( $decode_json->{settings}[$settings]{value} ) eq "ARRAY"
and $decode_json->{settings}[$settings]{name} eq 'starting_points' )
&& $decode_json->{settings}[$settings]{name} eq 'starting_points' )
{
#save the startingpointid needed to update the startingpoints
if ( $hash->{helper}{STARTINGPOINTID} ne
@ -591,7 +585,7 @@ sub WriteReadings {
. encode_json( $decode_json->{settings}[$settings]{value} ) . '}';
my $startpoint_cnt = 0;
foreach my $startingpoint (
for my $startingpoint (
@{ $decode_json->{settings}[$settings]{value} } )
{
$startpoint_cnt++;
@ -762,9 +756,9 @@ sub ReadingLangGerman {
if (
defined( $langGermanMapp{$readingValue} )
and ( AttrVal( 'global', 'language', 'none' ) eq 'DE'
or AttrVal( $name, 'readingValueLanguage', 'none' ) eq 'de' )
and AttrVal( $name, 'readingValueLanguage', 'none' ) ne 'en'
&& ( AttrVal( 'global', 'language', 'none' ) eq 'DE'
|| AttrVal( $name, 'readingValueLanguage', 'none' ) eq 'de' )
&& AttrVal( $name, 'readingValueLanguage', 'none' ) ne 'en'
)
{
return $langGermanMapp{$readingValue};
@ -841,9 +835,9 @@ sub SetPredefinedStartPoints {
my $payload;
my $abilities;
if ( defined($startpoint_state) and defined($startpoint_num) ) {
if ( defined($startpoint_state) && defined($startpoint_num) ) {
if ( defined( $hash->{helper}{STARTINGPOINTS} )
and $hash->{helper}{STARTINGPOINTS} ne '' )
&& $hash->{helper}{STARTINGPOINTS} ne '' )
{
# add needed parameters to saved settings config and change the value in request
my $decode_json_settings =
@ -861,8 +855,8 @@ sub SetPredefinedStartPoints {
#set more startpoints
if (
defined scalar(@morestartpoints)
and ( scalar(@morestartpoints) == 2
or scalar(@morestartpoints) == 4 )
&& ( scalar(@morestartpoints) == 2
|| scalar(@morestartpoints) == 4 )
)
{
if ( scalar(@morestartpoints) == 2 ) {