MYSENSORS: fix perl-warning on unparsable data, add attribute 'last-sensorid'

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@6803 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ntruchsess 2014-10-24 06:12:17 +00:00
parent 4e4a690554
commit 82c0aceb2b
2 changed files with 44 additions and 38 deletions

View File

@ -59,6 +59,7 @@ sub MYSENSORS_Initialize($) {
"autocreate:1 ". "autocreate:1 ".
"requestAck:1 ". "requestAck:1 ".
"first-sensorid ". "first-sensorid ".
"last-sensorid ".
"stateFormat"; "stateFormat";
} }
@ -235,36 +236,39 @@ sub Read {
my $txt; my $txt;
($txt,$data) = split("\n", $data, 2); ($txt,$data) = split("\n", $data, 2);
$txt =~ s/\r//; $txt =~ s/\r//;
my $msg = parseMsg($txt); if (my $msg = parseMsg($txt)) {
Log3 ($name,5,"MYSENSORS Read: ".dumpMsg($msg)); Log3 ($name,5,"MYSENSORS Read: ".dumpMsg($msg));
if ($msg->{ack}) { if ($msg->{ack}) {
delete $hash->{messages}->{$txt}; delete $hash->{messages}->{$txt};
$hash->{outstandingAck} = keys %{$hash->{messages}}; $hash->{outstandingAck} = keys %{$hash->{messages}};
} }
my $type = $msg->{cmd}; my $type = $msg->{cmd};
MESSAGE_TYPE: { MESSAGE_TYPE: {
$type == C_PRESENTATION and do { $type == C_PRESENTATION and do {
onPresentationMsg($hash,$msg); onPresentationMsg($hash,$msg);
last; last;
}; };
$type == C_SET and do { $type == C_SET and do {
onSetMsg($hash,$msg); onSetMsg($hash,$msg);
last; last;
}; };
$type == C_REQ and do { $type == C_REQ and do {
onRequestMsg($hash,$msg); onRequestMsg($hash,$msg);
last; last;
}; };
$type == C_INTERNAL and do { $type == C_INTERNAL and do {
onInternalMsg($hash,$msg); onInternalMsg($hash,$msg);
last; last;
}; };
$type == C_STREAM and do { $type == C_STREAM and do {
onStreamMsg($hash,$msg); onStreamMsg($hash,$msg);
last; last;
}; };
}
} else {
Log3 ($name,5,"MYSENSORS Read: ".$txt."is no parsable mysensors message");
} }
} }
$hash->{PARTIAL} = $data; $hash->{PARTIAL} = $data;
@ -338,13 +342,13 @@ sub onInternalMsg($$) {
}; };
$type == I_ID_REQUEST and do { $type == I_ID_REQUEST and do {
if ($hash->{'inclusion-mode'}) { if ($hash->{'inclusion-mode'}) {
my %nodes = map {$_ => 1} (AttrVal($hash->{NAME},"first-sensorid",20) ... 254); my %nodes = map {$_ => 1} (AttrVal($hash->{NAME},"first-sensorid",20) ... AttrVal($hash->{NAME},"last-sensorid",254));
GP_ForallClients($hash,sub { GP_ForallClients($hash,sub {
my $client = shift; my $client = shift;
delete $nodes{$client->{radioId}}; delete $nodes{$client->{radioId}};
}); });
if (keys %nodes) { if (keys %nodes) {
my $newid = (keys %nodes)[0]; my $newid = (sort keys %nodes)[0];
sendMessage($hash,radioId => 255, childId => 255, cmd => C_INTERNAL, ack => 0, subType => I_ID_RESPONSE, payload => $newid); sendMessage($hash,radioId => 255, childId => 255, cmd => C_INTERNAL, ack => 0, subType => I_ID_RESPONSE, payload => $newid);
Log3($hash->{NAME},4,"MYSENSORS $hash->{NAME} assigned new nodeid $newid"); Log3($hash->{NAME},4,"MYSENSORS $hash->{NAME} assigned new nodeid $newid");
} else { } else {

View File

@ -12,14 +12,16 @@ use warnings;
sub parseMsg($) { sub parseMsg($) {
my $txt = shift; my $txt = shift;
my @fields = split(/;/,$txt); if ($txt =~ /^(\d+);(\d+);(\d+);(\d+);(\d+);(.*)$/) {
my $msgRef = { radioId => $fields[0], return { radioId => $1,
childId => $fields[1], childId => $2,
cmd => $fields[2], cmd => $3,
ack => $fields[3], ack => $4,
subType => $fields[4], subType => $5,
payload => $fields[5] }; payload => $6 };
return $msgRef; } else {
return undef;
};
} }
sub createMsg(%) { sub createMsg(%) {