Skip to content

Commit

Permalink
Merge pull request #401 from krkeegan/fix_issue_397
Browse files Browse the repository at this point in the history
Insteon: Attempt to Reconnect PLM if it Appears to be Down
  • Loading branch information
krkeegan committed Apr 26, 2014
2 parents ec288a7 + 72980f8 commit aa60bbe
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bin/mh
Original file line number Diff line number Diff line change
Expand Up @@ -5702,6 +5702,19 @@ sub serial_port_open {
# print "np=@serial_parms\n";
}

sub serial_port_close {
my ($name) = @_;
my $port = $Serial_Ports{$name}{port};

# Recommended Steps to Close a Serial Port from CPAN
$Serial_Ports{$name}{object}->close() || ::print_log("[Insteon_PLM] Close of serial port failed.");
undef $Serial_Ports{$name}{object};

# Remove all references in Global Vars
delete $Serial_Ports{object_by_port}{$port};
delete $Serial_Ports{$name};
}


sub set_sun_time {
my @parms = (latitude => $config_parms{latitude}, longitude => $config_parms{longitude},
Expand Down
24 changes: 24 additions & 0 deletions lib/Insteon/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ sub send
if $self->setby->debuglevel(1, 'insteon');
$$self{no_hop_increase} = undef;
}

# If No PLM-Receipt has been received for this message
# then attempt to reconnect the PLM
$interface->serial_restart() unless $self->plm_receipt;
}

# need to set timeout as a function of retries; also need to alter hop count
Expand All @@ -262,6 +266,10 @@ sub send
$self->setby->outgoing_hop_count($self->setby->default_hop_count)
if $self->setby->can('outgoing_hop_count');
}

# Clear PLM-Receipt Flag
$self->plm_receipt(0);

$self->send_attempts($self->send_attempts + 1);
$interface->_send_cmd($self, $self->send_timeout);
if ($self->callback)
Expand Down Expand Up @@ -327,6 +335,22 @@ sub to_string
return $self->interface_data;
}

=item C<plm_receipt()>
Used to track whether the PLM has acknowledged receiving this message, either
an ACK or NAK. This is used to determine situations in which the serial
connection to the PLM may have collapsed and may need to be restarted.
=cut

sub plm_receipt
{
my ($self, $receipt) = @_;
$$self{plm_receipt} = $receipt if defined $receipt;
return $$self{plm_receipt};
}


=back
=head2 INI PARAMETERS
Expand Down
46 changes: 46 additions & 0 deletions lib/Insteon_PLM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ sub serial_startup {

}

=item C<serial_restart()>
Attempt to restart/reconnect the serial port connection.
=cut

sub serial_restart {
my ($self) = @_;
my $instance = $$self{port_name};
my $PLM_use_tcp = $::config_parms{$instance . "_use_TCP"};

# TCP Port gets reconnected elsewhere
return if $PLM_use_tcp;

::print_log("[Insteon_PLM] WARN: The PLM did not respond to the last command."
." The port may have closed, attempting to reopen the port.");

#prep vars
my $port = $::config_parms{$instance . "_serial_port"};
my $speed = 19200;

#close the port
::serial_port_close($instance);

#Try and open it again
::serial_port_create($instance, $port, $speed,'none','raw');
}

=item C<new()>
Instantiates a new object.
Expand Down Expand Up @@ -620,6 +648,9 @@ sub _parse_data {

# STEP 4b Is this a PLM Response to a command MH sent?
if ($is_ack) {
#Note Receipt of PLM ACK
$pending_message->plm_receipt(1);

::print_log( "[Insteon_PLM] DEBUG4:\n".
Insteon::MessageDecoder::plm_decode($data))
if $debug_obj->debuglevel(4, 'insteon');
Expand Down Expand Up @@ -676,6 +707,9 @@ sub _parse_data {
$data =~ s/^$ackcmd//;
}
elsif ($is_nack) {
#Note Receipt of PLM NAK
$pending_message->plm_receipt(1);

::print_log( "[Insteon_PLM] DEBUG4:\n".
Insteon::MessageDecoder::plm_decode($data))
if $debug_obj->debuglevel(4, 'insteon');
Expand Down Expand Up @@ -759,6 +793,9 @@ sub _parse_data {
$data =~ s/^$nackcmd//;
}
elsif ($is_badcmd){
#Note Receipt of PLM Bad Cmd
$pending_message->plm_receipt(1);

::print_log( "[Insteon_PLM] DEBUG4:\n".
Insteon::MessageDecoder::plm_decode($data))
if $debug_obj->debuglevel(4, 'insteon');
Expand Down Expand Up @@ -910,6 +947,9 @@ sub _parse_data {
$data = substr($data, 12);
}
elsif ($record_type eq $prefix{all_link_record} and (length($data) >= 20)) {
#Note Receipt of PLM Response
$pending_message->plm_receipt(1);

#ALL-Link Record Response
my $message_data = substr($data,4,16);
&::print_log( "[Insteon_PLM] DEBUG4:\n".Insteon::MessageDecoder::plm_decode($data))
Expand Down Expand Up @@ -962,6 +1002,9 @@ sub _parse_data {
$data = substr($data, 6);
}
elsif ($record_type eq $prefix{plm_info} and (length($data) >= 18)){
#Note Receipt of PLM Response
$pending_message->plm_receipt(1);

::print_log( "[Insteon_PLM] DEBUG4:\n".Insteon::MessageDecoder::plm_decode($data))
if $self->debuglevel(4, 'insteon');

Expand All @@ -972,6 +1015,9 @@ sub _parse_data {
$data = substr($data, 18);
}
elsif ($record_type eq $prefix{plm_get_config} and (length($data) >= 12)){
#Note Receipt of PLM Response
$pending_message->plm_receipt(1);

::print_log( "[Insteon_PLM] DEBUG4:\n".Insteon::MessageDecoder::plm_decode($data))
if $self->debuglevel(4, 'insteon');
my $message_data = substr($data,4,8);
Expand Down

0 comments on commit aa60bbe

Please sign in to comment.