insert more error handling routines

This commit is contained in:
Marko Oldenburg 2019-09-27 14:00:38 +02:00
parent 57a51e8984
commit 581e49aba2

View File

@ -456,22 +456,22 @@ sub Write($) {
} }
); );
# #### temporär # #### temporär
# ErrorHandling( # ErrorHandling(
# { # {
# url => 'http://' . $uri, # url => 'http://' . $uri,
# timeout => 5, # timeout => 5,
# method => $method, # method => $method,
# data => $data, # data => $data,
# header => $header, # header => $header,
# hash => $hash, # hash => $hash,
# setCmd => $path, # setCmd => $path,
# doTrigger => 1, # doTrigger => 1,
# callback => \&ErrorHandling, # callback => \&ErrorHandling,
# }, # },
# undef, # undef,
# '{"none": "none"}' # '{"none": "none"}'
# ); # );
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: http://$uri"; Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: http://$uri";
} }
@ -480,16 +480,9 @@ sub ErrorHandling($$$) {
my ( $param, $err, $data ) = @_; my ( $param, $err, $data ) = @_;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
# my $path = $param->{setCmd}; # temporär
# my $path = $param->{setCmd}; # temporär
print('TESLA DEBUG - ResponseString: ' . Dumper $data);
print('TESLA DEBUG - Error: ' . $err . "\n")
unless ($err);
# #### temporär # #### temporär
# if ( $path eq 'statussoe' ) { # if ( $path eq 'statussoe' ) {
# $data = '{"percentage":69.1675560298826}'; # $data = '{"percentage":69.1675560298826}';
@ -711,16 +704,13 @@ sub ErrorHandling($$$) {
# } # }
# ]'; # ]';
# } # }
# #
# elsif ( $path eq 'gridstatus' ) { # elsif ( $path eq 'gridstatus' ) {
# $data = '{"grid_status":"SystemGridConnected"}'; # $data = '{"grid_status":"SystemGridConnected"}';
# } # }
# elsif ( $path eq 'sitename' ) { # elsif ( $path eq 'sitename' ) {
# $data = '{"site_name":"Home Energy Gateway","timezone":"America/Los_Angeles"}'; # $data = '{"site_name":"Home Energy Gateway","timezone":"America/Los_Angeles"}';
# } # }
### Begin Error Handling ### Begin Error Handling
@ -735,7 +725,7 @@ sub ErrorHandling($$$) {
Log3 $name, 3, "TeslaPowerwall2AC ($name) - RequestERROR: $err"; Log3 $name, 3, "TeslaPowerwall2AC ($name) - RequestERROR: $err";
$hash->{actionQueue} = []; $hash->{actionQueue} = [];
# return; return;
} }
} }
@ -757,7 +747,7 @@ sub ErrorHandling($$$) {
. " without any data after requesting"; . " without any data after requesting";
$hash->{actionQueue} = []; $hash->{actionQueue} = [];
# return; return;
} }
if ( ( $data =~ /Error/i ) and exists( $param->{code} ) ) { if ( ( $data =~ /Error/i ) and exists( $param->{code} ) ) {
@ -774,7 +764,35 @@ sub ErrorHandling($$$) {
. $param->{code}; . $param->{code};
$hash->{actionQueue} = []; $hash->{actionQueue} = [];
# return; return;
}
if ( $data =~ m#{"code":(\d+),"error":"(.+)","message":"(.+)"}$# ) {
readingsBeginUpdate($hash);
readingsBulkUpdate( $hash, 'state', $1, 1 );
readingsBulkUpdate(
$hash,
'lastRequestError',
'Path: '
. $param->{setCmd} . ' '
. $1
. ' - Error: '
. $2
. ' Messages: '
. $3,
1
);
readingsEndUpdate( $hash, 1 );
Log3 $name, 3,
"TeslaPowerwall2AC ($name) - statusRequestERROR: http error "
. $param->{code};
$hash->{actionQueue} = [];
return;
### End Error Handling ### End Error Handling
} }
@ -784,7 +802,11 @@ sub ErrorHandling($$$) {
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Recieve JSON data: $data"; Log3 $name, 4, "TeslaPowerwall2AC ($name) - Recieve JSON data: $data";
# ResponseProcessing( $hash, $param->{setCmd}, $data ); print( 'TESLA DEBUG - ResponseString: ' . Dumper $data);
print( 'TESLA DEBUG - Error: ' . $err . "\n" )
unless ($err);
# ResponseProcessing( $hash, $param->{setCmd}, $data );
} }
sub ResponseProcessing($$$) { sub ResponseProcessing($$$) {
@ -943,46 +965,54 @@ sub ReadingsProcessing_Meters_Site($$) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my %readings; my %readings;
# print('Ausgabe1: ' . Dumper $decode_json . "\n"); # print('Ausgabe1: ' . Dumper $decode_json . "\n");
if ( ref( $decode_json ) eq 'ARRAY' if ( ref($decode_json) eq 'ARRAY'
and scalar( @{ $decode_json } ) > 0 ) and 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] } ) {
# print('Ausgabe2: ' . Dumper $obj . "\n");
if ( ref($decode_json->[0]->{$obj}) eq 'ARRAY' # print('Ausgabe2: ' . Dumper $obj . "\n");
or ref($decode_json->[0]->{$obj}) eq 'HASH' ) if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
or ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
{ {
if ( ref($decode_json->[0]->{$obj}) eq 'HASH' ) { if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
# print('Ausgabe3: ' . Dumper $obj . "\n");
while ( my ( $r, $v ) = each %{ $decode_json->[0]->{$obj} } ) { # print('Ausgabe3: ' . Dumper $obj . "\n");
while ( my ( $r, $v ) =
each %{ $decode_json->[0]->{$obj} } )
{
if ( ref($v) ne 'HASH' ) { if ( ref($v) ne 'HASH' ) {
# print('Ausgabe4: ' . $obj . '-' . $r . ' = ' . $v . "\n"); # print('Ausgabe4: ' . $obj . '-' . $r . ' = ' . $v . "\n");
$readings{ $obj . '-' . $r } = $v; $readings{ $obj . '-' . $r } = $v;
} }
else { else {
# print('Ausgabe5: ' . Dumper $decode_json->[0]->{$obj}->{$r} . "\n"); # print('Ausgabe5: ' . Dumper $decode_json->[0]->{$obj}->{$r} . "\n");
while ( my ( $r2, $v2 ) = each %{ $decode_json->[0]->{$obj}->{$r} } ) { while ( my ( $r2, $v2 ) =
each %{ $decode_json->[0]->{$obj}->{$r} } )
{
# print('Ausgabe6: ' . $obj . '-' . $r2 . ' = ' . $v2 . "\n"); # print('Ausgabe6: ' . $obj . '-' . $r2 . ' = ' . $v2 . "\n");
$readings{ $obj . '-' . $r . '-' . $r2 } = $v2; $readings{ $obj . '-' . $r . '-' . $r2 } =
$v2;
} }
} }
} }
} }
elsif ( ref($decode_json->[0]->{$obj}) eq 'ARRAY' ) { elsif ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' ) {
} }
} }
else { else {
# print('Ausgabe7: ' . Dumper $decode_json->[0]->{$obj} . "\n"); # print('Ausgabe7: ' . Dumper $decode_json->[0]->{$obj} . "\n");
$readings{ $obj } = $decode_json->[0]->{$obj}; $readings{$obj} = $decode_json->[0]->{$obj};
} }
} }
} }
} }
else { else {
# print('Ausgabe8: ' . "\n"); # print('Ausgabe8: ' . "\n");
$readings{'error'} = 'metes site response is not a Array'; $readings{'error'} = 'metes site response is not a Array';
} }
@ -994,44 +1024,52 @@ sub ReadingsProcessing_Meters_Solar($$) {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my %readings; my %readings;
if ( ref( $decode_json ) eq 'ARRAY' if ( ref($decode_json) eq 'ARRAY'
and scalar( @{ $decode_json } ) > 0 ) and 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] } ) {
# print('Ausgabe2: ' . Dumper $obj . "\n");
if ( ref($decode_json->[0]->{$obj}) eq 'ARRAY' # print('Ausgabe2: ' . Dumper $obj . "\n");
or ref($decode_json->[0]->{$obj}) eq 'HASH' ) if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
or ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
{ {
if ( ref($decode_json->[0]->{$obj}) eq 'HASH' ) { if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
# print('Ausgabe3: ' . Dumper $obj . "\n");
while ( my ( $r, $v ) = each %{ $decode_json->[0]->{$obj} } ) { # print('Ausgabe3: ' . Dumper $obj . "\n");
while ( my ( $r, $v ) =
each %{ $decode_json->[0]->{$obj} } )
{
if ( ref($v) ne 'HASH' ) { if ( ref($v) ne 'HASH' ) {
# print('Ausgabe4: ' . $obj . '-' . $r . ' = ' . $v . "\n"); # print('Ausgabe4: ' . $obj . '-' . $r . ' = ' . $v . "\n");
$readings{ $obj . '-' . $r } = $v; $readings{ $obj . '-' . $r } = $v;
} }
else { else {
# print('Ausgabe5: ' . Dumper $decode_json->[0]->{$obj}->{$r} . "\n"); # print('Ausgabe5: ' . Dumper $decode_json->[0]->{$obj}->{$r} . "\n");
while ( my ( $r2, $v2 ) = each %{ $decode_json->[0]->{$obj}->{$r} } ) { while ( my ( $r2, $v2 ) =
each %{ $decode_json->[0]->{$obj}->{$r} } )
{
# print('Ausgabe6: ' . $obj . '-' . $r2 . ' = ' . $v2 . "\n"); # print('Ausgabe6: ' . $obj . '-' . $r2 . ' = ' . $v2 . "\n");
$readings{ $obj . '-' . $r . '-' . $r2 } = $v2; $readings{ $obj . '-' . $r . '-' . $r2 } =
$v2;
} }
} }
} }
} }
elsif ( ref($decode_json->[0]->{$obj}) eq 'ARRAY' ) { elsif ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY' ) {
} }
} }
else { else {
# print('Ausgabe7: ' . Dumper $decode_json->[0]->{$obj} . "\n"); # print('Ausgabe7: ' . Dumper $decode_json->[0]->{$obj} . "\n");
$readings{ $obj } = $decode_json->[0]->{$obj}; $readings{$obj} = $decode_json->[0]->{$obj};
} }
} }
} }
} }
else { else {
# print('Ausgabe8: ' . "\n"); # print('Ausgabe8: ' . "\n");
$readings{'error'} = 'metes solar response is not a Array'; $readings{'error'} = 'metes solar response is not a Array';
} }
@ -1155,7 +1193,7 @@ sub CreateUri($$) {
], ],
"release_status": "under develop", "release_status": "under develop",
"license": "GPL_2", "license": "GPL_2",
"version": "v0.6.102", "version": "v0.6.103",
"author": [ "author": [
"Marko Oldenburg <leongaultier@gmail.com>" "Marko Oldenburg <leongaultier@gmail.com>"
], ],