started refactoring to make all calls async
This commit is contained in:
		| @@ -9,7 +9,11 @@ use HttpUtils; | |||||||
| use JSON; | use JSON; | ||||||
|  |  | ||||||
| my %EaseeWallbox_gets = ( | my %EaseeWallbox_gets = ( | ||||||
|     update   => "noArg", |     update   => { | ||||||
|  |                   args => "noArg", | ||||||
|  |                   urlTemplate => "", | ||||||
|  |                   callback => \&EaseeWallbox_httpSimpleOperationCallback | ||||||
|  |               }, | ||||||
|     health   => "noArg", |     health   => "noArg", | ||||||
|     baseData => "noArg", |     baseData => "noArg", | ||||||
|     chargers => "noArg", |     chargers => "noArg", | ||||||
| @@ -148,7 +152,7 @@ sub EaseeWallbox_getCmdList ($$$) { | |||||||
|             if ( defined($myOpt) and ( length($myOpt) > 0 ) ); |             if ( defined($myOpt) and ( length($myOpt) > 0 ) ); | ||||||
|         $myOpt = "" if ( !defined($myOpt) ); |         $myOpt = "" if ( !defined($myOpt) ); | ||||||
|  |  | ||||||
|         Log3 ($name, 5, "parse cmd-table - Set:$mySet, Option:$myOpt, RetVal:$retVal"); |         #Log3 ($name, 5, "parse cmd-table - Set:$mySet, Option:$myOpt, RetVal:$retVal"); | ||||||
|     } |     } | ||||||
|     if ( !defined($retVal) ) { |     if ( !defined($retVal) ) { | ||||||
|         $retVal = "error while parsing set-table"; |         $retVal = "error while parsing set-table"; | ||||||
| @@ -533,8 +537,58 @@ sub EaseeWallbox_TokenRefresh { | |||||||
|     return; |     return; | ||||||
| } | } | ||||||
|  |  | ||||||
| sub EaseeWallbox_httpSimpleOperationOAuth($$$;$) { | sub EaseeWallbox_httpSimpleOperationCallback { | ||||||
|     my ( $hash, $url, $operation, $message ) = @_; |     my ( $param, $err, $data ) = @_; | ||||||
|  |     my $hash = $param->{hash}; | ||||||
|  |     my $name = $hash->{NAME}; | ||||||
|  |  | ||||||
|  |     Log3 $name, 4, "Callback received." . $param->{url}; | ||||||
|  |  | ||||||
|  |     if ( $err ne "" )   # wenn ein Fehler bei der HTTP Abfrage aufgetreten ist | ||||||
|  |     { | ||||||
|  |         Log3 $name, 3,"error while requesting ". $param->{url}. " - $err";    # Eintrag fürs Log | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", "ERROR $err", 1 ); | ||||||
|  |         return undef; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     my $code = $param->{code}; | ||||||
|  |     if ($code >= 400){ | ||||||
|  |         Log3 $name, 3,"HTTPS error while requesting ". $param->{url}. " - $code";    # Eintrag fürs Log | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", "ERROR: HTTP Code $code", 1 ); | ||||||
|  |         return undef; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     Log3 $name, 3, | ||||||
|  |         "Received non-blocking data from EaseeWallbox regarding current session "; | ||||||
|  |  | ||||||
|  |     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $param->{url}; | ||||||
|  |     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $param->{message} | ||||||
|  |         if ( defined $param->{message} ); | ||||||
|  |     Log3 $name, 4, "EaseeWallbox -> FHEM: " . $data; | ||||||
|  |     Log3 $name, 5, '$err: ' . $err; | ||||||
|  |     Log3 $name, 5, "method: " . $param->{method}; | ||||||
|  |     Log3 $name, 2, "Something gone wrong" | ||||||
|  |         if ( $data =~ "/EaseeWallboxMode/" ); | ||||||
|  |     eval { | ||||||
|  |         my $d = decode_json($data) if ( !$err ); | ||||||
|  |         Log3 $name, 5, 'Decoded: ' . Dumper($d); | ||||||
|  |         if ( defined $d and  not $d eq '') { | ||||||
|  |             readingsSingleUpdate( $hash, "lastResponse", 'OK - Action '. $d->{commandId}, 1 )       if defined $d->{commandId}; | ||||||
|  |             readingsSingleUpdate( $hash, "lastResponse", 'ERROR: '. $d->{title}.' ('.$d->{status}.')', 1 )     if defined $d->{status} and defined $d->{title}; | ||||||
|  |             return undef; | ||||||
|  |         }  else { | ||||||
|  |             readingsSingleUpdate( $hash, "lastResponse", 'OK', 1); | ||||||
|  |             return undef; | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  |     if ($@) { | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", 'ERROR while deconding response: '. $@, 1 ); | ||||||
|  |         Log3 $name, 5, 'Failure decoding: ' . $@; | ||||||
|  |     }  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | sub EaseeWallbox_httpSimpleOperationOAuth { | ||||||
|  |     my ( $hash, $url, $operation, $message, $callback ) = @_; | ||||||
|     my ( $json, $err, $data, $decoded ); |     my ( $json, $err, $data, $decoded ); | ||||||
|     my $name             = $hash->{NAME}; |     my $name             = $hash->{NAME}; | ||||||
|     my $CurrentTokenData = EaseeWallbox_LoadToken($hash); |     my $CurrentTokenData = EaseeWallbox_LoadToken($hash); | ||||||
| @@ -549,42 +603,21 @@ sub EaseeWallbox_httpSimpleOperationOAuth($$$;$) { | |||||||
|             "Authorization" => |             "Authorization" => | ||||||
|                 "$CurrentTokenData->{'tokenType'} $CurrentTokenData->{'accessToken'}" |                 "$CurrentTokenData->{'tokenType'} $CurrentTokenData->{'accessToken'}" | ||||||
|         }, |         }, | ||||||
|  |         callback => \&EaseeWallbox_httpSimpleOperationCallback, | ||||||
|         method  => $operation, |         method  => $operation, | ||||||
|         timeout => 6, |         timeout => 6, | ||||||
|         hideurl => 1 |         hideurl => 1, | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|  |     $request->{callback} = $callback    if defined $callback; | ||||||
|     $request->{data} = $message if ( defined $message ); |     $request->{data} = $message if ( defined $message ); | ||||||
|  |  | ||||||
|     Log3 $name, 5, 'Request: ' . Dumper($request); |     Log3 $name, 5, 'Request: ' . Dumper($request); | ||||||
|  |     $request->{hash} = $hash; | ||||||
|  |  | ||||||
|     ( $err, $data ) = HttpUtils_BlockingGet($request); |     HttpUtils_NonblockingGet($request); | ||||||
|  |      Log3 $name, 3, | ||||||
|     $json = "" if ( !$json ); |         "Async call executed. Waiting for callback..."; | ||||||
|     $data = "" if ( !$data ); |  | ||||||
|     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $url; |  | ||||||
|     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $message |  | ||||||
|         if ( defined $message ); |  | ||||||
|     Log3 $name, 4, "EaseeWallbox -> FHEM: " . $data if ( defined $data ); |  | ||||||
|     Log3 $name, 4, "EaseeWallbox -> FHEM: Got empty response." |  | ||||||
|         if ( not defined $data ); |  | ||||||
|     Log3 $name, 5, '$err: ' . $err; |  | ||||||
|     Log3 $name, 5, "method: " . $operation; |  | ||||||
|     Log3 $name, 2, "Something gone wrong" |  | ||||||
|         if ( $data =~ "/EaseeWallboxMode/" ); |  | ||||||
|  |  | ||||||
|     $err = 1 if ( $data =~ "/EaseeWallboxMode/" ); |  | ||||||
|     if ( defined $data and ( not $data eq '' ) and $operation ne 'DELETE' ) { |  | ||||||
|         eval { |  | ||||||
|             $decoded = decode_json($data) if ( !$err ); |  | ||||||
|             Log3 $name, 5, 'Decoded: ' . Dumper($decoded); |  | ||||||
|             return $decoded; |  | ||||||
|         } or do { |  | ||||||
|             Log3 $name, 5, 'Failure decoding: ' . $@; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     else { |  | ||||||
|         return undef; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| sub EaseeWallbox_ExecuteParameterlessCommand($$) { | sub EaseeWallbox_ExecuteParameterlessCommand($$) { | ||||||
| @@ -649,21 +682,49 @@ sub EaseeWallbox_Attr(@) { | |||||||
|     return undef; |     return undef; | ||||||
| } | } | ||||||
|  |  | ||||||
| sub EaseeWallbox_GetChargers($) { | sub EaseeWallbox_GetChargersCallback(){ | ||||||
|  |  | ||||||
|     my ($hash) = @_; |     my ( $param, $err, $data ) = @_; | ||||||
|  |     my $hash = $param->{hash}; | ||||||
|     my $name = $hash->{NAME}; |     my $name = $hash->{NAME}; | ||||||
|  |     my $d; | ||||||
|  |  | ||||||
|     if ( not defined $hash ) { |     Log3 $name, 4, "Callback received." . $param->{url}; | ||||||
|         my $msg = "Error on EaseeWallbox_GetChargers. Missing hash variable"; |  | ||||||
|         Log3 'EaseeWallbox', 1, $msg; |     if ( $err ne "" )   # wenn ein Fehler bei der HTTP Abfrage aufgetreten ist | ||||||
|         return $msg; |     { | ||||||
|  |         Log3 $name, 3,"error while requesting ". $param->{url}. " - $err";    # Eintrag fürs Log | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", "ERROR $err", 1 ); | ||||||
|  |         return undef; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     my $readTemplate = $EaseeWallbox_urls{"getChargers"}; |     my $code = $param->{code}; | ||||||
|  |     if ($code >= 400){ | ||||||
|  |         Log3 $name, 3,"HTTPS error while requesting ". $param->{url}. " - $code";    # Eintrag fürs Log | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", "ERROR: HTTP Code $code", 1 ); | ||||||
|  |         return undef; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     my $d = EaseeWallbox_httpSimpleOperationOAuth( $hash, $readTemplate, |     Log3 $name, 3, | ||||||
|         'GET' ); |         "Received non-blocking data from EaseeWallbox regarding current session "; | ||||||
|  |  | ||||||
|  |     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $param->{url}; | ||||||
|  |     Log3 $name, 4, "FHEM -> EaseeWallbox: " . $param->{message} | ||||||
|  |         if ( defined $param->{message} ); | ||||||
|  |     Log3 $name, 4, "EaseeWallbox -> FHEM: " . $data; | ||||||
|  |     Log3 $name, 5, '$err: ' . $err; | ||||||
|  |     Log3 $name, 5, "method: " . $param->{method}; | ||||||
|  |     Log3 $name, 2, "Something gone wrong" | ||||||
|  |         if ( $data =~ "/EaseeWallboxMode/" ); | ||||||
|  |     eval { | ||||||
|  |         $d = decode_json($data) if ( !$err ); | ||||||
|  |         Log3 $name, 5, 'Decoded: ' . Dumper($d); | ||||||
|  |     }; | ||||||
|  |     if ($@) { | ||||||
|  |         readingsSingleUpdate( $hash, "lastResponse", 'ERROR while deconding response: '. $@, 1 ); | ||||||
|  |         Log3 $name, 5, 'Failure decoding: ' . $@; | ||||||
|  |         return undef; | ||||||
|  |     }  | ||||||
|  |  | ||||||
|      if ( defined $d && ref($d) eq "HASH" && defined $d->{errors} ) { |      if ( defined $d && ref($d) eq "HASH" && defined $d->{errors} ) { | ||||||
|             log 1, Dumper $d; |             log 1, Dumper $d; | ||||||
| @@ -689,28 +750,47 @@ sub EaseeWallbox_GetChargers($) { | |||||||
|             #readingsBulkUpdate( $hash, "charger_createdOn", $charger->{createdOn} ); |             #readingsBulkUpdate( $hash, "charger_createdOn", $charger->{createdOn} ); | ||||||
|             readingsEndUpdate( $hash, 1 ); |             readingsEndUpdate( $hash, 1 ); | ||||||
|  |  | ||||||
|         $readTemplate = $EaseeWallbox_urls{"getChargerDetails"}; |  #           $readTemplate = $EaseeWallbox_urls{"getChargerDetails"}; | ||||||
|         $readTemplate =~ s/#ChargerID#/$chargerId/g; |  #           $readTemplate =~ s/#ChargerID#/$chargerId/g; | ||||||
|         $d = EaseeWallbox_httpSimpleOperationOAuth( $hash, $readTemplate, |  #           $d = EaseeWallbox_httpSimpleOperationOAuth( $hash, $readTemplate, | ||||||
|             'GET' ); |  #               'GET' ); | ||||||
|  |  | ||||||
|         if ( defined $d && ref($d) eq "HASH" && defined $d->{errors} ) { |  #           if ( defined $d && ref($d) eq "HASH" && defined $d->{errors} ) { | ||||||
|             log 1, Dumper $d; |  #               log 1, Dumper $d; | ||||||
|             readingsSingleUpdate( $hash, |  #               readingsSingleUpdate( $hash, | ||||||
|                 "Error: $d->{errors}[0]->{code} / $d->{errors}[0]->{title}", |  #                   "Error: $d->{errors}[0]->{code} / $d->{errors}[0]->{title}", | ||||||
|                 'Undefined', 1 ); |  #                   'Undefined', 1 ); | ||||||
|  |  #               return undef; | ||||||
|  |  #           } | ||||||
|  |  #           else { | ||||||
|  |  #               readingsBeginUpdate($hash); | ||||||
|  |  #               readingsBulkUpdate( $hash, "product",  $d->{product} ); | ||||||
|  |  #               readingsBulkUpdate( $hash, "pincode",  $d->{pinCode} ); | ||||||
|  |  #               readingsBulkUpdate( $hash, "unitType", $d->{unitType} ); | ||||||
|  |  #               readingsEndUpdate( $hash, 1 ); | ||||||
|  |  #           } | ||||||
|  |             readingsSingleUpdate( $hash, "lastResponse", 'OK', 1); | ||||||
|             return undef; |             return undef; | ||||||
|         } |         } | ||||||
|         else { | } | ||||||
|             readingsBeginUpdate($hash); |  | ||||||
|             readingsBulkUpdate( $hash, "product",  $d->{product} ); | sub EaseeWallbox_GetChargers($) { | ||||||
|             readingsBulkUpdate( $hash, "pincode",  $d->{pinCode} ); |  | ||||||
|             readingsBulkUpdate( $hash, "unitType", $d->{unitType} ); |     my ($hash) = @_; | ||||||
|             readingsEndUpdate( $hash, 1 ); |     my $name = $hash->{NAME}; | ||||||
|  |  | ||||||
|  |     if ( not defined $hash ) { | ||||||
|  |         my $msg = "Error on EaseeWallbox_GetChargers. Missing hash variable"; | ||||||
|  |         Log3 'EaseeWallbox', 1, $msg; | ||||||
|  |         return $msg; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     my $readTemplate = $EaseeWallbox_urls{"getChargers"}; | ||||||
|  |  | ||||||
|  |     EaseeWallbox_httpSimpleOperationOAuth( $hash, $readTemplate, 'GET', '', \&EaseeWallbox_GetChargersCallback ); | ||||||
|     return undef; |     return undef; | ||||||
|     } |  | ||||||
|  |     | ||||||
| } | } | ||||||
|  |  | ||||||
| sub EaseeWallbox_GetChargerConfig($) { | sub EaseeWallbox_GetChargerConfig($) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user