Skip to content

Commit

Permalink
Added core functions to store and read node lists and find a free id.
Browse files Browse the repository at this point in the history
  • Loading branch information
kolaf committed Aug 27, 2014
1 parent 7b31222 commit b964d13
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions mqttGateway2.pl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ sub variableTypeToIdx
my $cv;
my $serialPort = "/dev/ttyUSB2";
my $serialDevice;
my $nodeFile = "nodes.txt";
my %knownNodes;

my @subscriptions;

Expand All @@ -117,6 +119,50 @@ sub debug
print "##" . join(" ", @_)."\n";
}

sub saveNode
{
my $message = shift;
my $address = $message->{'radioId'};
my $name = $message->{'payload'};
if (exists($knownNodes{$address}) && $knownNodes{$address} ne $name) {
print "Possible duplicate\n";
}
$knownNodes{$address} = $name;
writeNodeFile();
}

sub writeNodeFile
{
open OUTPUT, ">", $nodeFile or die $!;
foreach my $key (sort keys %knownNodes) {
print OUTPUT $key."=".$knownNodes{$key}."\n";
}
close OUTPUT;
}

sub readNodeFile
{
open INPUT, "<", $nodeFile or return;
while (<INPUT>) {
my ($address, $name) = split /=/;
$knownNodes {$address} = $name;
}
close INPUT;
}

sub findFreeID
{
my @keys= keys %knownNodes;
if ($keys >254) return 255;
for my $index (1..254) {
if (!exists($knownNodes{$index}) {
return $index;
}
}
return 255;
}


sub initialiseSerialPort
{
print "Initialising serial port $serialPort\n";
Expand Down Expand Up @@ -409,6 +455,7 @@ sub handleMessage
when ([C_PRESENTATION, C_SET, C_INTERNAL])
{
onNodePresenation($msgRef) if (($msgRef->{'childId'} == 255) && ($msgRef->{'cmd'} == C_PRESENTATION));
saveNode($msgRef) if($msgRef->{'subType'} == I_SKETCH_NAME);
publish( createTopic($msgRef), $msgRef->{'payload'} );
}
when (C_REQ)
Expand All @@ -425,6 +472,7 @@ sub handleMessage
sub doShutdown
{
store(\@subscriptions, $subscriptionStorageFile) ;

}

sub onCtrlC
Expand Down

0 comments on commit b964d13

Please sign in to comment.