Hallo DS_starter, und alle andere,
Ich versuche eine default X_DbLog_splitFn($$) zu machen. Was geht falsch mit das Folgende?
sub X_DbLog_splitFn($$)
{
use feature "switch";
my ($event, $device) = @_ ;
my ($reading, $value, $unit) ;
# An array of known units, preferably linked to SI:
my @units = ( '°C', 'Pa,' 's', 'V ', 'W' )
# An array of terms, that say more about the reading.
my @mods = ( 'measured')
my @words = split ' ', $event ;
my $l = @words - 1;
$reading = @words[0] ;
$value = '' ;
given ($wordz) {
when ($wordz == 0) {
$value = $reading ;
$reading = 'state' ;
}
when ($wordz == 1) { # '$reading $value', like most events
$value = @words[1] ;
$unit = '' ;
}
default {
$unit = @words[$wordz] ;
if (grep ($unit, @mods)) { # if the last word is a mod:
$reading = join (' ',$reading , $unit ) ; # append it to the reading,
$wordz = $wordz - 1 ; # shorten the event string
$unit = @words[$wordz] ; # and set the last word as the unit
} ;
for (my $i = 1; $i < $wordz; $i++) { # reading value_of_some_length unit
$i > 1 ? $value = join (' ', $value . @words[$i]) : $value = @words[$i] ; # append the word to the value, with a space if it a next one
}
unless (grep ($unit, @units)) { # if it's an unknown unit:
$value = join (' ', $value , $unit ) ; # add it to the value
$unit = '' ; # and discard the unit
}
}
}
return ($reading, $value, $unit) ;
}
Und wiso wird $device die Subroutine zugeschickt?
Freundliche Grüße,
Paul
Hallo Paul,
zunächst einmal musst du das "X" in der Vorlage mit dem Namen deines Moduls in welches du die Funktion implementieren willst ersetzen.
Als weiteren Schritt machst du diese Funktion im Initialize deines Moduls bekannt, z.B.:
sub SMAPortal_Initialize($) {
my ($hash) = @_;
......
$hash->{DbLog_splitFn} = "SMAPortal_DbLog_split";
.....
return;
}
Dann kannst du die Funktion implementieren, z.B. :
sub DbLog_split($$) {
my ($event, $device) = @_;
my $devhash = $defs{$device};
my ($reading, $value, $unit);
if($event =~ m/[_\-fd]Consumption|Quote/) {
$event =~ /^L(.*):\s(.*)\s(.*)/;
if($1) {
$reading = "L".$1;
$value = $2;
$unit = $3;
}
}
return ($reading, $value, $unit);
}
ZitatUnd wiso wird $device die Subroutine zugeschickt?
Der Subroutine wird $device übergeben damit man zum Beispiel auf den Hash des Devices zugreifen kann um bestimmte Informationen abzufragen oder einfach nur mit Log3 eine Logausgabe zu erstellen.
Grüße,
Heiko