Skip to content

Commit f9c4ed9

Browse files
authored
Merge pull request #882 from hplato/razberry
current version
2 parents d65a429 + 931dfba commit f9c4ed9

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

lib/raZberry.pm

+16-31
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ sub process_check {
431431
#check if data comes back unauthenticated
432432
if (($self->{login_success} == 0) and ($self->{login_attempt})) {
433433
if ($main::Time > ($self->{login_attempt} + 30)) { #retry log in every 30 seconds
434-
main::print_log( "[raZberry:" . $self->{host} . "] Attempting to re-authenticate" );
434+
main::print_log( "[raZerry:" . $self->{host} . "] Attempting to re-authenticate" );
435435
$self->login;
436436
}
437437
}
@@ -440,7 +440,7 @@ sub process_check {
440440

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

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

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

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

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

520512
#check for any queued data that needs to be processed $self->{command_timeout}
521513
if ((scalar @{ $self->{cmd_queue} }) and ($self->{cmd_process}->done() )) {
522-
my ($mode, $url, $time, $retry) = @ { ${ $self->{cmd_queue} }[0] };
523-
#print "**** mode=$mode, url=$url\n";
514+
my ($mode, $get_cmd, $time, $retry) = @ { ${ $self->{cmd_queue} }[0] };
515+
#print "**** mode=$mode, get_cmd=$get_cmd\n";
524516
#print "*** time=$time, time_diff=" . ($main::Time - $time) ." timeout=" .$self->{command_timeout} . " retry=$retry\n";
525517
#if there is a retry, then execute at request time + (retry * 5 seconds)
526518
#discard the command if 60 seconds after the request time
527519
#if the item is queued then wait until at least a second after the request time
528520
#discard the item if it's been retried $self->{command_timeout_limit} times
529521
if ($retry > $self->{command_timeout_limit}) {
530-
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: Abandoning command $url due to $retry retry attempts" );
522+
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: Abandoning command $get_cmd due to $retry retry attempts" );
531523
shift @{ $self->{cmd_queue}};
532524
} elsif (($main::Time - $time) > $self->{command_timeout}) {
533-
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: $url request older than " . $self->{command_timeout} . " seconds. Abandoning request" );
525+
main::print_log( "[raZberry:" . $self->{host} . "] ERROR: $get_cmd request older than " . $self->{command_timeout} . " seconds. Abandoning request" );
534526
shift @{ $self->{cmd_queue}};
535527
} elsif (($main::Time > ($time + 1 + ($retry * 5)) and ($self->{cmd_process}->done() ) )) {#the original time isn't a great base for deep queued commands
536-
my $get_cmd = $self->get_cmd_string($url);
537528
if ($retry == 0) {
538529
main::print_log( "[raZberry:" . $self->{host} . "] Command Queue found, processing next item" );
539530
} else {
@@ -772,19 +763,11 @@ sub update_dev {
772763
}
773764

774765
#------------------------------------------------------------------------------------
775-
sub get_cmd_string{
776-
my ( $self, $url ) = @_;
777-
my $cookie = "";
778-
$cookie = $self->{cookie_string} if ( $self->{cookie_string} );
779-
my $get_params = "-ua ";
780-
$get_params .= "-timeout " . $self->{timeout} . " ";
781-
$get_params .= "-cookies " . "'" . $cookie . "' " if ($cookie ne "");
782-
return "get_url $get_params $url";
783-
}
784-
785766
sub _get_JSON_data {
786767
my ( $self, $mode, $cmd ) = @_;
787768

769+
my $cookie = "";
770+
$cookie = $self->{cookie_string} if ( $self->{cookie_string} );
788771
my $host = $self->{host};
789772
my $port = $self->{port};
790773
my $params = "";
@@ -799,8 +782,10 @@ sub _get_JSON_data {
799782
or ( $mode eq "usercode_data" ) );
800783
$method = "ZWaveAPI" if ( $mode eq "controller" );
801784
&main::print_log("[raZberry:" . $self->{host} . "]: contacting http://$host:$port/$method/$rest{$mode}$params") if ( $self->{debug} );
802-
my $url = '"http://' . "$host:$port/$method/$rest{$mode}$params" . '"';
803-
my $get_cmd = $self->get_cmd_string($url);
785+
my $get_params = "-ua ";
786+
$get_params .= "-timeout " . $self->{timeout} . " ";
787+
$get_params .= "-cookies " . "'" . $cookie . "' " if ($cookie ne "");
788+
my $get_cmd = "get_url $get_params " . '"http://' . "$host:$port/$method/$rest{$mode}$params" . '"';
804789

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

0 commit comments

Comments
 (0)