Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

current version #882

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions lib/raZberry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ sub process_check {
#check if data comes back unauthenticated
if (($self->{login_success} == 0) and ($self->{login_attempt})) {
if ($main::Time > ($self->{login_attempt} + 30)) { #retry log in every 30 seconds
main::print_log( "[raZberry:" . $self->{host} . "] Attempting to re-authenticate" );
main::print_log( "[raZerry:" . $self->{host} . "] Attempting to re-authenticate" );
$self->login;
}
}
Expand All @@ -440,7 +440,7 @@ sub process_check {

$com_status = "online";
$processed_data = 1;
main::print_log( "[raZberry:" . $self->{host} . "] Background poll " . $self->{poll_process_mode} . " process completed" ) if ( $self->{debug} );
main::print_log( "[raZerry:" . $self->{host} . "] Background poll " . $self->{poll_process_mode} . " process completed" ) if ( $self->{debug} );

my $file_data = &main::file_read( $self->{poll_data_file} );
exit unless ($file_data); #if there is no data, then don't process
Expand All @@ -460,10 +460,6 @@ sub process_check {
main::print_log( "[raZberry:" . $self->{host} . "] ERROR! bad data returned by poll" );
main::print_log( "[raZberry:" . $self->{host} . "] ERROR! file data is [$file_data]. json data is [$json_data]" );
$com_status = "offline";
if ($file_data =~ /.*Not logged in 401.*/i){
$self->{cookie_jar}->clear();
$self->login;
}
} else {
push @process_data, $json_data;
}
Expand All @@ -472,7 +468,7 @@ sub process_check {
$com_status = "online";
$processed_data = 2;

main::print_log( "[raZberry:" . $self->{host} . "] Command " . $self->{cmd_process_mode} . " process completed" ) if ( $self->{debug} );
main::print_log( "[raZerry:" . $self->{host} . "] Command " . $self->{cmd_process_mode} . " process completed" ) if ( $self->{debug} );

my $file_data = &main::file_read( $self->{cmd_data_file} );
exit unless ($file_data); #if there is no data, then don't process
Expand Down Expand Up @@ -500,15 +496,11 @@ sub process_check {

# print "debug: json_data=$json_data\n" if ( $self->{debug} > 2);
unless ( ($file_data) and ($json_data) ) {
main::print_log( "[raZberry:" . $self->{host} . "] ERROR! bad data returned by command" );
main::print_log( "[raZberry:" . $self->{host} . "] ERROR! bad data returned by poll" );
main::print_log( "[raZberry:" . $self->{host} . "] ERROR! file data is [$file_data]. json data is [$json_data]" );
$com_status = "offline";
#update the retry on the failed item.
$ {$self->{cmd_queue}}[0][3]++;
if ($file_data =~ /.*Not logged in 401.*/i){
$self->{cookie_jar}->clear();
$self->login;
}
} else {
push @process_data, $json_data;
shift @{ $self->{cmd_queue} }; #successfully processed to remove item from the queue
Expand All @@ -519,21 +511,20 @@ sub process_check {

#check for any queued data that needs to be processed $self->{command_timeout}
if ((scalar @{ $self->{cmd_queue} }) and ($self->{cmd_process}->done() )) {
my ($mode, $url, $time, $retry) = @ { ${ $self->{cmd_queue} }[0] };
#print "**** mode=$mode, url=$url\n";
my ($mode, $get_cmd, $time, $retry) = @ { ${ $self->{cmd_queue} }[0] };
#print "**** mode=$mode, get_cmd=$get_cmd\n";
#print "*** time=$time, time_diff=" . ($main::Time - $time) ." timeout=" .$self->{command_timeout} . " retry=$retry\n";
#if there is a retry, then execute at request time + (retry * 5 seconds)
#discard the command if 60 seconds after the request time
#if the item is queued then wait until at least a second after the request time
#discard the item if it's been retried $self->{command_timeout_limit} times
if ($retry > $self->{command_timeout_limit}) {
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: Abandoning command $url due to $retry retry attempts" );
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: Abandoning command $get_cmd due to $retry retry attempts" );
shift @{ $self->{cmd_queue}};
} elsif (($main::Time - $time) > $self->{command_timeout}) {
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: $url request older than " . $self->{command_timeout} . " seconds. Abandoning request" );
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: $get_cmd request older than " . $self->{command_timeout} . " seconds. Abandoning request" );
shift @{ $self->{cmd_queue}};
} elsif (($main::Time > ($time + 1 + ($retry * 5)) and ($self->{cmd_process}->done() ) )) {#the original time isn't a great base for deep queued commands
my $get_cmd = $self->get_cmd_string($url);
if ($retry == 0) {
main::print_log( "[raZberry:" . $self->{host} . "] Command Queue found, processing next item" );
} else {
Expand Down Expand Up @@ -772,19 +763,11 @@ sub update_dev {
}

#------------------------------------------------------------------------------------
sub get_cmd_string{
my ( $self, $url ) = @_;
my $cookie = "";
$cookie = $self->{cookie_string} if ( $self->{cookie_string} );
my $get_params = "-ua ";
$get_params .= "-timeout " . $self->{timeout} . " ";
$get_params .= "-cookies " . "'" . $cookie . "' " if ($cookie ne "");
return "get_url $get_params $url";
}

sub _get_JSON_data {
my ( $self, $mode, $cmd ) = @_;

my $cookie = "";
$cookie = $self->{cookie_string} if ( $self->{cookie_string} );
my $host = $self->{host};
my $port = $self->{port};
my $params = "";
Expand All @@ -799,8 +782,10 @@ sub _get_JSON_data {
or ( $mode eq "usercode_data" ) );
$method = "ZWaveAPI" if ( $mode eq "controller" );
&main::print_log("[raZberry:" . $self->{host} . "]: contacting http://$host:$port/$method/$rest{$mode}$params") if ( $self->{debug} );
my $url = '"http://' . "$host:$port/$method/$rest{$mode}$params" . '"';
my $get_cmd = $self->get_cmd_string($url);
my $get_params = "-ua ";
$get_params .= "-timeout " . $self->{timeout} . " ";
$get_params .= "-cookies " . "'" . $cookie . "' " if ($cookie ne "");
my $get_cmd = "get_url $get_params " . '"http://' . "$host:$port/$method/$rest{$mode}$params" . '"';

if (( $cmd eq "") or ($cmd =~ m/^\?since=/)) {
$self->{poll_process}->stop() unless ($self->{poll_process}->done() );
Expand All @@ -815,12 +800,12 @@ sub _get_JSON_data {
$self->{cmd_process}->start();
$self->{cmd_process_pid}->{ $self->{cmd_process}->pid() } = $mode; #capture the type of information requested in order to parse;
$self->{cmd_process_mode} = $mode;
push @{ $self->{cmd_queue} }, [$mode,$url,$main::Time,0];
push @{ $self->{cmd_queue} }, [$mode,$get_cmd,$main::Time,0];
main::print_log( "[raZberry:" . $self->{host} . "] Backgrounding Command (" . $self->{cmd_process}->pid() . ") command $mode, $get_cmd" ) if ( $self->{debug} );
} else {
main::print_log( "[raZberry:" . $self->{host} . "] Queing Command command $mode, $get_cmd, time " . $main::Time ) if ( $self->{debug} );
if (scalar @{ $self->{cmd_queue} } <= $self->{max_cmd_queue} ) {
push @{ $self->{cmd_queue} }, [$mode,$url,$main::Time,0];
push @{ $self->{cmd_queue} }, [$mode,$get_cmd,$main::Time,0];
} else {
main::print_log( "[raZberry:" . $self->{host} . "] Max Queue Length ($self->{max_cmd_queue}) reached! Discarding queued command" );
#@{ $self->{cmd_queue} } = ();
Expand Down
Loading