make better code

This commit is contained in:
Marko Oldenburg 2021-03-13 18:30:40 +01:00
parent 8a2ce7eca4
commit bb9f2a89c7
2 changed files with 56 additions and 23 deletions

View File

@ -257,7 +257,7 @@ sub Attr {
my $hash = $defs{$name}; my $hash = $defs{$name};
if ( $attrName eq 'disable' ) { if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' and $attrVal eq '1' ) { if ( $cmd eq 'set' && $attrVal eq '1' ) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
readingsSingleUpdate( $hash, 'state', 'disabled', 1 ); readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
Log3($name, 3, qq(TeslaPowerwall2AC \(${name}\) - disabled)); Log3($name, 3, qq(TeslaPowerwall2AC \(${name}\) - disabled));
@ -393,7 +393,7 @@ sub Get {
return 'There are still path commands in the action queue' return 'There are still path commands in the action queue'
if ( defined( $hash->{actionQueue} ) if ( defined( $hash->{actionQueue} )
and scalar( @{ $hash->{actionQueue} } ) > 0 ); && scalar( @{ $hash->{actionQueue} } ) > 0 );
unshift( @{ $hash->{actionQueue} }, $arg ); unshift( @{ $hash->{actionQueue} }, $arg );
Write($hash); Write($hash);
@ -448,7 +448,7 @@ sub Timer_GetData {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
if ( defined( $hash->{actionQueue} ) if ( defined( $hash->{actionQueue} )
and scalar( @{ $hash->{actionQueue} } ) == 0 ) && scalar( @{ $hash->{actionQueue} } ) == 0 )
{ {
if ( !IsDisabled($name) ) { if ( !IsDisabled($name) ) {
return readingsSingleUpdate( $hash, 'state', return readingsSingleUpdate( $hash, 'state',
@ -538,7 +538,10 @@ sub ErrorHandling {
} }
} }
if ( $data eq '' and exists( $param->{code} ) && $param->{code} != 200 ) { if ( $data eq ''
&& exists($param->{code})
&& $param->{code} != 200 )
{
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, 'state', $param->{code}, 1 ); readingsBulkUpdate( $hash, 'state', $param->{code}, 1 );
@ -583,7 +586,7 @@ sub ErrorHandling {
InternalTimer( gettimeofday() + 3, 'TeslaPowerwall2AC_Write', $hash ) InternalTimer( gettimeofday() + 3, 'TeslaPowerwall2AC_Write', $hash )
if ( defined( $hash->{actionQueue} ) if ( defined( $hash->{actionQueue} )
and scalar( @{ $hash->{actionQueue} } ) > 0 ); && scalar( @{ $hash->{actionQueue} } ) > 0 );
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Recieve JSON data: $data"; Log3 $name, 4, "TeslaPowerwall2AC ($name) - Recieve JSON data: $data";
@ -611,10 +614,10 @@ sub ResponseProcessing {
return return
if ( ref($decode_json) eq 'HASH' if ( ref($decode_json) eq 'HASH'
and defined( $decode_json->{error} ) && defined($decode_json->{error})
and $decode_json->{error} && $decode_json->{error}
and defined( $decode_json->{code} ) && defined($decode_json->{code})
and $decode_json->{code} ); && $decode_json->{code} );
#### Verarbeitung der Readings zum passenden Path #### Verarbeitung der Readings zum passenden Path
@ -676,7 +679,7 @@ sub WriteReadings {
$hash, 'state', $hash, 'state',
( (
defined( $hash->{actionQueue} ) defined( $hash->{actionQueue} )
and scalar( @{ $hash->{actionQueue} } ) == 0 && scalar( @{ $hash->{actionQueue} } ) == 0
? 'ready' ? 'ready'
: 'fetch data - ' : 'fetch data - '
. scalar( @{ $hash->{actionQueue} } ) . scalar( @{ $hash->{actionQueue} } )
@ -719,7 +722,7 @@ sub ReadingsProcessing_Powerwalls {
for (keys %{$decode_json}) { for (keys %{$decode_json}) {
$readings{$_} = $decode_json->{$_} $readings{$_} = $decode_json->{$_}
if ( ref($decode_json->{$_}) ne 'ARRAY' if ( ref($decode_json->{$_}) ne 'ARRAY'
and defined($decode_json->{$_}) ); && defined($decode_json->{$_}) );
if ( ref($decode_json->{$_}) eq 'ARRAY' ) { if ( ref($decode_json->{$_}) eq 'ARRAY' ) {
my $i = 0; my $i = 0;
@ -729,8 +732,8 @@ sub ReadingsProcessing_Powerwalls {
$r1 =~ s/s$//g; $r1 =~ s/s$//g;
$readings{qq(${r1}_${i}_${_})} = $hRef->{$_} $readings{qq(${r1}_${i}_${_})} = $hRef->{$_}
if ( ref($hRef->{$_}) ne 'ARRAY' if ( ref($hRef->{$_}) ne 'ARRAY'
and ref($hRef->{$_}) ne 'HASH' && ref($hRef->{$_}) ne 'HASH'
and defined($hRef->{$_}) ); && defined($hRef->{$_}) );
if ( ref($hRef->{$_}) eq 'HASH' ) { if ( ref($hRef->{$_}) eq 'HASH' ) {
my $r2 = $_; my $r2 = $_;
@ -738,7 +741,7 @@ sub ReadingsProcessing_Powerwalls {
for (keys %{$r3}) { for (keys %{$r3}) {
$readings{qq(${r1}_${i}_${r2}_${_})} = $r3->{$_} $readings{qq(${r1}_${i}_${r2}_${_})} = $r3->{$_}
if ( ref($r3->{$_}) ne 'ARRAY' if ( ref($r3->{$_}) ne 'ARRAY'
and defined($r3->{$_}) ); && defined($r3->{$_}) );
if ( ref($r3->{$_}) eq 'ARRAY' ) { if ( ref($r3->{$_}) eq 'ARRAY' ) {
my $ii = 0; my $ii = 0;
@ -748,7 +751,7 @@ sub ReadingsProcessing_Powerwalls {
$r4 =~ s/s$//g; $r4 =~ s/s$//g;
$readings{qq(${r1}_${i}_${r2}_${r4}_${ii}_${_})} = $hRef->{$_} $readings{qq(${r1}_${i}_${r2}_${r4}_${ii}_${_})} = $hRef->{$_}
if ( ref($hRef->{$_}) ne 'HASH' if ( ref($hRef->{$_}) ne 'HASH'
and defined($hRef->{$_}) ); && defined($hRef->{$_}) );
} }
$ii++ $ii++
@ -801,7 +804,7 @@ sub ReadingsProcessing_Meters_Site {
my %readings; my %readings;
if ( ref($decode_json) eq 'ARRAY' if ( ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0 ) && scalar( @{$decode_json} ) > 0 )
{ {
if ( ref( $decode_json->[0] ) eq 'HASH' ) { if ( ref( $decode_json->[0] ) eq 'HASH' ) {
while ( my $obj = each %{ $decode_json->[0] } ) { while ( my $obj = each %{ $decode_json->[0] } ) {
@ -850,7 +853,7 @@ sub ReadingsProcessing_Meters_Solar {
my %readings; my %readings;
if ( ref($decode_json) eq 'ARRAY' if ( ref($decode_json) eq 'ARRAY'
and scalar( @{$decode_json} ) > 0 ) && scalar( @{$decode_json} ) > 0 )
{ {
if ( ref( $decode_json->[0] ) eq 'HASH' ) { if ( ref( $decode_json->[0] ) eq 'HASH' ) {
while ( my $obj = each %{ $decode_json->[0] } ) { while ( my $obj = each %{ $decode_json->[0] } ) {
@ -1020,6 +1023,36 @@ sub Rename {
return; return;
} }
# My little Helpers
sub PathTimestamp {
my $hash = shift;
my $path = shift // return;
$hash->{helper}->{pathTimestamp}->{$path}->{TIME} =
gettimeofday();
}
sub PathTimeAge {
my $hash = shift;
my $path = shift;
$hash->{helper}{updateTimeCallBattery} = 0
if ( not defined( $hash->{helper}{updateTimeCallBattery} ) );
my $UpdateTimeAge = gettimeofday() - $hash->{helper}{updateTimeCallBattery};
return $UpdateTimeAge;
}
sub IsPathTimeAgeToOld {
my $hash = shift;
my $maxAge = shift;
return ( CallBattery_UpdateTimeAge($hash) > $maxAge ? 1 : 0 );
}
1; 1;
=pod =pod

View File

@ -1 +1 @@
UPD 2021-03-09_08:35:01 32310 FHEM/46_TeslaPowerwall2AC.pm UPD 2021-03-10_18:27:18 32895 FHEM/46_TeslaPowerwall2AC.pm