Skip to content

Commit

Permalink
Merge pull request #266 from krkeegan/enhance_hopcount
Browse files Browse the repository at this point in the history
Insteon: Change Hopcount Calculation to a Moving Average
  • Loading branch information
krkeegan committed Oct 20, 2013
2 parents a095870 + 6e950fa commit ebc77b8
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/Insteon/BaseInsteon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,31 @@ Returns the highest hop count of the past 20 hop counts
sub default_hop_count
{
my ($self, $hop_count) = @_;
unshift(@{$$self{hop_array}}, $$self{default_hop_count}) if (!defined(@{$$self{hop_array}}));
if (defined($hop_count)){
::print_log("[Insteon::BaseObject] DEBUG3: Adding hop count of " . $hop_count . " to hop_array of "
. $self->get_object_name) if $main::Debug{insteon} >= 3;
unshift(@{$$self{hop_array}}, $hop_count)
if (!defined(@{$$self{hop_array}})) {
unshift(@{$$self{hop_array}}, $$self{default_hop_count});
$$self{hop_sum} = $$self{default_hop_count};
}
#Calculate a simple moving average
unshift(@{$$self{hop_array}}, $hop_count);
$$self{hop_sum} += ${$$self{hop_array}}[0];
$$self{hop_sum} -= pop(@{$$self{hop_array}}) if (scalar(@{$$self{hop_array}}) >10);
$$self{default_hop_count} = int(($$self{hop_sum} / scalar(@{$$self{hop_array}})) + 0.5);

::print_log("[Insteon::BaseObject] DEBUG4: ".$self->get_object_name
."->default_hop_count()=".$$self{default_hop_count}
." :: hop_array[]=". join("",@{$$self{hop_array}}))
if $main::Debug{insteon} >= 4;
}
pop(@{$$self{hop_array}}) if (scalar(@{$$self{hop_array}}) >20);
my $high = 0;
foreach (@{$$self{hop_array}}){
$high = $_ if ($high < $_);;
}
$$self{default_hop_count} = $high;

#Allow for per-device settings
$$self{default_hop_count} = $$self{max_hops} if ($$self{max_hops} &&
$$self{default_hop_count} > $$self{max_hops});
$$self{default_hop_count} = $$self{min_hops} if ($$self{min_hops} &&
$$self{default_hop_count} < $$self{min_hops});
$$self{default_hop_count} < $$self{min_hops});

return $$self{default_hop_count};
}

Expand Down Expand Up @@ -1865,7 +1874,8 @@ Hop Count, Engine Version, ALDB Type, ALDB Health, and Last ALDB Scan Time
sub log_aldb_status
{
my ($self) = @_;
main::print_log( " Hop Count: ".$self->default_hop_count());
main::print_log( " Device ID: ".$self->device_id());
main::print_log( " Hop Count: ".$self->default_hop_count()." :: [". join("",@{$$self{hop_array}})."]");
main::print_log( "Engine Version: ".$self->engine_version());
my $aldb = $self->get_root()->_aldb;
if ($aldb)
Expand Down

0 comments on commit ebc77b8

Please sign in to comment.