NUKIBridge change getter none blocking with asyncOutput

This commit is contained in:
Marko Oldenburg 2020-01-11 18:01:06 +01:00
parent 9ee8cab8be
commit 01bb010423
2 changed files with 122 additions and 81 deletions

View File

@ -113,7 +113,7 @@ if ($@) {
} }
} }
my $version = '1.8.0'; my $version = '1.9.3';
my $bridgeapi = '1.9'; my $bridgeapi = '1.9';
my %bridgeType = ( my %bridgeType = (
@ -156,8 +156,8 @@ sub NUKIBridge_ResponseProcessing($$$);
sub NUKIBridge_CGI(); sub NUKIBridge_CGI();
sub NUKIBridge_Autocreate($$;$); sub NUKIBridge_Autocreate($$;$);
sub NUKIBridge_InfoProcessing($$); sub NUKIBridge_InfoProcessing($$);
sub NUKIBridge_getLogfile($); sub NUKIBridge_getLogfile($$);
sub NUKIBridge_getCallbackList($); sub NUKIBridge_getCallbackList($$);
sub NUKIBridge_CallBlocking($@); sub NUKIBridge_CallBlocking($@);
sub NUKIBridge_Initialize($) { sub NUKIBridge_Initialize($) {
@ -481,12 +481,12 @@ sub NUKIBridge_Get($@) {
if ( lc($cmd) eq 'logfile' ) { if ( lc($cmd) eq 'logfile' ) {
return 'usage: logFile' if ( @args != 0 ); return 'usage: logFile' if ( @args != 0 );
NUKIBridge_getLogfile($hash) if ( !IsDisabled($name) ); NUKIBridge_Write($hash,'log',undef,undef,undef);
} }
elsif ( lc($cmd) eq 'callbacklist' ) { elsif ( lc($cmd) eq 'callbacklist' ) {
return 'usage: callbackList' if ( @args != 0 ); return 'usage: callbackList' if ( @args != 0 );
NUKIBridge_getCallbackList($hash) if ( !IsDisabled($name) ); NUKIBridge_Write($hash,'callback/list',undef,undef,undef);
} }
else { else {
my $list = ''; my $list = '';
@ -607,8 +607,7 @@ sub NUKIBridge_Call($) {
if ( defined($uri) and $uri ) { if ( defined($uri) and $uri ) {
$hash->{helper}->{iowrite} = 1; $hash->{helper}->{iowrite} = 1;
HttpUtils_NonblockingGet( my $param = {
{
url => $uri, url => $uri,
timeout => 30, timeout => 30,
hash => $hash, hash => $hash,
@ -617,9 +616,14 @@ sub NUKIBridge_Call($) {
header => 'Accept: application/json', header => 'Accept: application/json',
method => 'GET', method => 'GET',
callback => \&NUKIBridge_Distribution, callback => \&NUKIBridge_Distribution,
} };
);
$param->{cl} = $hash->{CL}
if ( ($endpoint eq 'callback/list'
or $endpoint eq 'log')
and ref( $hash->{CL} ) eq 'HASH' );
HttpUtils_NonblockingGet($param);
Log3( $name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri" ); Log3( $name, 4, "NUKIBridge ($name) - Send HTTP POST with URL $uri" );
} }
} }
@ -671,6 +675,10 @@ sub NUKIBridge_Distribution($$$) {
Log3( $name, 4, Log3( $name, 4,
"NUKIBridge ($name) - error while requesting: $err" ); "NUKIBridge ($name) - error while requesting: $err" );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
asyncOutput( $param->{cl}, "Request Error: $err\r\n" )
if ( $param->{cl} && $param->{cl}{canAsyncOutput} );
return $err; return $err;
} }
} }
@ -694,6 +702,9 @@ sub NUKIBridge_Distribution($$$) {
); );
delete $hash->{helper}->{lastDeviceAction}; delete $hash->{helper}->{lastDeviceAction};
} }
asyncOutput( $param->{cl}, "Request Error: $err\r\n" )
if ( $param->{cl} && $param->{cl}{canAsyncOutput} );
return; return;
} }
@ -709,6 +720,9 @@ sub NUKIBridge_Distribution($$$) {
return ('received http code ' return ('received http code '
. $param->{code} . $param->{code}
. ' without any data after requesting' ); . ' without any data after requesting' );
asyncOutput( $param->{cl}, "Request Error: $err\r\n" )
if ( $param->{cl} && $param->{cl}{canAsyncOutput} );
} }
if ( ( $json =~ /Error/i ) and exists( $param->{code} ) ) { if ( ( $json =~ /Error/i ) and exists( $param->{code} ) ) {
@ -726,6 +740,10 @@ sub NUKIBridge_Distribution($$$) {
if ( $param->{code} == 400 and $hash == $dhash ); if ( $param->{code} == 400 and $hash == $dhash );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
asyncOutput( $param->{cl}, "Request Error: $err\r\n" )
if ( $param->{cl} && $param->{cl}{canAsyncOutput} );
return $param->{code}; return $param->{code};
} }
@ -734,6 +752,15 @@ sub NUKIBridge_Distribution($$$) {
and $hash->{helper}->{lastDeviceAction} ); and $hash->{helper}->{lastDeviceAction} );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
if ( $param->{endpoint} eq 'callback/list' ) {
NUKIBridge_getCallbackList($param,$json);
return undef;
}
elsif ( $param->{endpoint} eq 'log' ) {
NUKIBridge_getLogfile($param,$json);
return undef;
}
if ( $hash == $dhash ) { if ( $hash == $dhash ) {
NUKIBridge_ResponseProcessing( $hash, $json, $param->{endpoint} ); NUKIBridge_ResponseProcessing( $hash, $json, $param->{endpoint} );
@ -938,98 +965,112 @@ sub NUKIBridge_InfoProcessing($$) {
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
} }
sub NUKIBridge_getLogfile($) { sub NUKIBridge_getLogfile($$) {
my ($hash) = @_; my ($param,$json) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking( $hash, 'log', undef );
my $decode_json = eval { decode_json($json) };
if ($@) {
Log3( $name, 3, "NUKIBridge ($name) - JSON error while request: $@" );
return;
}
Log3( $name, 4, Log3( $name, 4,
"NUKIBridge ($name) - Log data are collected and processed" ); "NUKIBridge ($name) - Log data are collected and processed" );
if ( $param->{cl} and $param->{cl}->{TYPE} eq 'FHEMWEB' ) {
if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) {
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
if ( ref($decode_json) eq 'ARRAY' and scalar( @{$decode_json} ) > 0 ) { my $ret = '<html><table width=100%><tr><td>';
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" ); $ret .= '<table class="block wide">';
my $ret = '<html><table width=100%><tr><td>'; foreach my $logs ( @{$decode_json} ) {
$ret .= '<table class="block wide">'; $ret .= '<tr class="odd">';
foreach my $logs ( @{$decode_json} ) { if ( $logs->{timestamp} ) {
$ret .= '<tr class="odd">'; $ret .= '<td><b>timestamp:</b> </td>';
$ret .= '<td>' . $logs->{timestamp} . '</td>';
$ret .= '<td> </td>';
}
if ( $logs->{timestamp} ) { if ( $logs->{type} ) {
$ret .= '<td><b>timestamp:</b> </td>'; $ret .= '<td><b>type:</b> </td>';
$ret .= '<td>' . $logs->{timestamp} . '</td>'; $ret .= '<td>' . $logs->{type} . '</td>';
$ret .= '<td> </td>'; $ret .= '<td> </td>';
}
foreach my $d ( reverse sort keys %{$logs} ) {
next if ( $d eq 'type' );
next if ( $d eq 'timestamp' );
$ret .= '<td><b>' . $d . ':</b> </td>';
$ret .= '<td>' . $logs->{$d} . '</td>';
$ret .= '<td> </td>';
}
$ret .= '</tr>';
} }
if ( $logs->{type} ) { $ret .= '</table></td></tr>';
$ret .= '<td><b>type:</b> </td>'; $ret .= '</table></html>';
$ret .= '<td>' . $logs->{type} . '</td>';
$ret .= '<td> </td>';
}
foreach my $d ( reverse sort keys %{$logs} ) { asyncOutput( $param->{cl}, $ret )
next if ( $d eq 'type' ); if ( $param->{cl} and $param->{cl}{canAsyncOutput} );
next if ( $d eq 'timestamp' ); return;
$ret .= '<td><b>' . $d . ':</b> </td>';
$ret .= '<td>' . $logs->{$d} . '</td>';
$ret .= '<td> </td>';
}
$ret .= '</tr>';
} }
$ret .= '</table></td></tr>';
$ret .= '</table></html>';
return $ret;
} }
} }
sub NUKIBridge_getCallbackList($) { sub NUKIBridge_getCallbackList($$) {
my ($hash) = @_; my ($param,$json) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $decode_json = NUKIBridge_CallBlocking( $hash, 'callback/list', undef );
return my $decode_json = eval { decode_json($json) };
unless ( ref($decode_json) eq 'HASH' ); if ($@) {
Log3( $name, 3, "NUKIBridge ($name) - JSON error while request: $@" );
Log3( return;
$name, 4, "NUKIBridge ($name) - Callback data is collected and
processed"
);
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY'
and scalar( @{ $decode_json->{callbacks} } ) > 0 )
{
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">';
$ret .= '<tr class="odd">';
$ret .= '<td><b>Callback-ID</b></td>';
$ret .= '<td> </td>';
$ret .= '<td><b>Callback-URL</b></td>';
$ret .= '</tr>';
foreach my $cb ( @{ $decode_json->{callbacks} } ) {
$ret .= '<td>' . $cb->{id} . '</td>';
$ret .= '<td> </td>';
$ret .= '<td>' . $cb->{url} . '</td>';
$ret .= '</tr>';
}
$ret .= '</table></td></tr>';
$ret .= '</table></html>';
return $ret;
} }
return "No callback data available or error during processing"; Log3( $name, 4,
"NUKIBridge ($name) - Callback data are collected and processed" );
if ( $param->{cl} and $param->{cl}->{TYPE} eq 'FHEMWEB' ) {
if ( ref( $decode_json->{callbacks} ) eq 'ARRAY'
and scalar( @{ $decode_json->{callbacks} } ) > 0 )
{
Log3( $name, 4, "NUKIBridge ($name) - created Table with log file" );
my $ret = '<html><table width=100%><tr><td>';
$ret .= '<table class="block wide">';
$ret .= '<tr class="odd">';
$ret .= '<td><b>Callback-ID</b></td>';
$ret .= '<td> </td>';
$ret .= '<td><b>Callback-URL</b></td>';
$ret .= '</tr>';
foreach my $cb ( @{ $decode_json->{callbacks} } ) {
$ret .= '<td>' . $cb->{id} . '</td>';
$ret .= '<td> </td>';
$ret .= '<td>' . $cb->{url} . '</td>';
$ret .= '</tr>';
}
$ret .= '</table></td></tr>';
$ret .= '</table></html>';
asyncOutput( $param->{cl}, $ret )
if ( $param->{cl} and $param->{cl}{canAsyncOutput} );
return;
}
}
} }
sub NUKIBridge_CallBlocking($@) { sub NUKIBridge_CallBlocking($@) {

View File

@ -101,7 +101,7 @@ if ($@) {
} }
} }
my $version = '1.8.0'; my $version = '1.9.1';
# Declare functions # Declare functions
sub NUKIDevice_Initialize($); sub NUKIDevice_Initialize($);