JSONMETER: better debugging

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@6853 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
tpoitzsch 2014-11-01 17:28:08 +00:00
parent beeb8ad513
commit a50ec41d16

View File

@ -62,7 +62,7 @@ sub JSONMETER_doStatisticDelta ($$$$$);
sub JSONMETER_doStatisticDeltaSingle ($$$$$$); sub JSONMETER_doStatisticDeltaSingle ($$$$$$);
# Modul Version for remote debugging # Modul Version for remote debugging
my $modulVersion = "2014-04-29"; my $modulVersion = "2014-04-29";
my $MODUL = "JSONMETER";
############################################################## ##############################################################
# Syntax: meterType => port URL-Path # Syntax: meterType => port URL-Path
############################################################## ##############################################################
@ -114,6 +114,19 @@ sub JSONMETER_doStatisticDeltaSingle ($$$$$$);
); );
############################################################## ##############################################################
sub ##########################################
JSONMETER_Log($$$)
{
my ( $hash, $loglevel, $text ) = @_;
my $xline = ( caller(0) )[2];
my $xsubroutine = ( caller(1) )[3];
my $sub = ( split( ':', $xsubroutine ) )[2];
$sub =~ s/JSONMETER_//;
my $instName = ( ref($hash) eq "HASH" ) ? $hash->{NAME} : $hash;
Log3 $hash, $loglevel, "$MODUL $instName: $sub.$xline " . $text;
}
sub ########################################## sub ##########################################
JSONMETER_Initialize($) JSONMETER_Initialize($)
@ -188,7 +201,7 @@ JSONMETER_Define($$)
$hash->{fhem}{jsonInterpreter} = ""; $hash->{fhem}{jsonInterpreter} = "";
$hash->{fhem}{modulVersion} = $modulVersion; $hash->{fhem}{modulVersion} = $modulVersion;
Log3 $hash,5,"$name: JSONMETER.pm version is $modulVersion."; JSONMETER_Log $hash, 5, "JSONMETER.pm version is $modulVersion.";
return undef; return undef;
} #end JSONMETER_Define } #end JSONMETER_Define
@ -218,7 +231,7 @@ JSONMETER_Attr($@)
if ($aName eq "1allowSetParameter") { if ($aName eq "1allowSetParameter") {
eval { qr/$aVal/ }; eval { qr/$aVal/ };
if ($@) { if ($@) {
Log3 $name, 3, "JSONMETER: Invalid allowSetParameter in attr $name $aName $aVal: $@"; JSONMETER_Log $name, 3, "Invalid allowSetParameter in attr $name $aName $aVal: $@";
return "Invalid allowSetParameter $aVal"; return "Invalid allowSetParameter $aVal";
} }
} }
@ -348,7 +361,7 @@ JSONMETER_GetUpdate($)
if ( ( $type eq "url" || $type eq "file" ) && ! defined($attr{$name}{"pathString"}) ) if ( ( $type eq "url" || $type eq "file" ) && ! defined($attr{$name}{"pathString"}) )
{ {
Log3 $name,2,"$name - Error reading device: Please define the attribute 'pathString'"; JSONMETER_Log $name,2,"Error reading device: Please define the attribute 'pathString'";
$hash->{STATE} = "pathString missing"; $hash->{STATE} = "pathString missing";
return "$name|0|Error reading device: Please define the attribute 'pathString'."; return "$name|0|Error reading device: Please define the attribute 'pathString'.";
} }
@ -402,15 +415,15 @@ JSONMETER_ReadFromFile($)
my ($string) = @_; my ($string) = @_;
my ($name, $pathString) = split /\|/, $string; my ($name, $pathString) = split /\|/, $string;
Log3 $name, 4, "$name: Open file '$pathString'"; JSONMETER_Log $name, 4, "Open file '$pathString'";
if (open(IN, "<" . $pathString)) { if (open(IN, "<" . $pathString)) {
my $message = join " ", <IN>; my $message = join " ", <IN>;
close(IN); close(IN);
Log3 $name, 4, "$name: Close file"; JSONMETER_Log $name, 4, "Close file";
$message = encode_base64($message,""); $message = encode_base64($message,"");
return "$name|1|$message" ; return "$name|1|$message" ;
} else { } else {
Log3 $name, 2, "$name Error: Cannot open file $pathString: $!"; JSONMETER_Log $name, 2, "Cannot open file $pathString: $!";
return "$name|0|Error: Cannot open file $pathString: $!";; return "$name|0|Error: Cannot open file $pathString: $!";;
} }
} # end JSONMETER_ReadFromFile } # end JSONMETER_ReadFromFile
@ -426,7 +439,7 @@ JSONMETER_ReadFromUrl($)
my $buf ; my $buf ;
my $message ; my $message ;
Log3 $name, 4, "$name: opening socket to host $ip port $port" ; JSONMETER_Log $name, 4, "opening socket to host $ip port $port" ;
my $socket = new IO::Socket::INET ( my $socket = new IO::Socket::INET (
PeerAddr => $ip, PeerAddr => $ip,
@ -436,22 +449,22 @@ JSONMETER_ReadFromUrl($)
Timeout => 9 Timeout => 9
); );
if (!$socket) { if (!$socket) {
Log3 $name, 1, "$name Error: Could not open connection to ip $ip port $port"; JSONMETER_Log $name, 1, "Could not open connection to ip $ip port $port";
return "$name|0|Can't connect to ip $ip port $port"; return "$name|0|Can't connect to ip $ip port $port";
} }
if (defined ($socket) and $socket and $socket->connected()) if (defined ($socket) and $socket and $socket->connected())
{ {
print $socket "GET /$pathString HTTP/1.0\r\n\r\n"; print $socket "GET /$pathString HTTP/1.0\r\n\r\n";
Log3 $name, 4, "$name: Get json file from http://$ip:$port/$pathString"; JSONMETER_Log $name, 4, "Get json file from http://$ip:$port/$pathString";
$socket->autoflush(1); $socket->autoflush(1);
while ((read $socket, $buf, 1024) > 0) while ((read $socket, $buf, 1024) > 0)
{ {
$message .= $buf; $message .= $buf;
} }
Log3 $name, 5, "$name: received:\n $message"; JSONMETER_Log $name, 5, "received:\n $message";
$socket->close(); $socket->close();
Log3 $name, 4, "$name: Socket closed"; JSONMETER_Log $name, 4, "Socket closed";
if ($message =~ /^HTTP\/1.\d 404 Not Found/) { if ($message =~ /^HTTP\/1.\d 404 Not Found/) {
return "$name|0|Error: URL 'http://$ip:$port/$pathString' returned 'Error 404: Page Not Found'"; return "$name|0|Error: URL 'http://$ip:$port/$pathString' returned 'Error 404: Page Not Found'";
} }
@ -495,8 +508,8 @@ if ( $a[1] == 1 ){
# value type and reading name in the jsonInterpreter # value type and reading name in the jsonInterpreter
#################################### ####################################
if ( $jsonInterpreter eq "" || $alwaysAnalyse == 1 ) { if ( $jsonInterpreter eq "" || $alwaysAnalyse == 1 ) {
Log3 $name, 3, "$name: Analyse JSON pathString for known readings" if $alwaysAnalyse != 1; JSONMETER_Log $name, 3, "Analyse JSON pathString for known readings" if $alwaysAnalyse != 1;
Log3 $name, 4, "$name: Analyse JSON pathString for known readings" if $alwaysAnalyse == 1; JSONMETER_Log $name, 4, "Analyse JSON pathString for known readings" if $alwaysAnalyse == 1;
foreach my $f (@jsonFields) foreach my $f (@jsonFields)
{ {
for(my $i=0; $i<=$#fields; $i++) for(my $i=0; $i<=$#fields; $i++)
@ -505,42 +518,36 @@ if ( $a[1] == 1 ){
if ($$f[0] == 1) { if ($$f[0] == 1) {
if ($fields[$i] =~ /"obis"\s*:\s*"($$f[1])"\s*[,}]/ && $fields[$i] =~ /"value"/) { if ($fields[$i] =~ /"obis"\s*:\s*"($$f[1])"\s*[,}]/ && $fields[$i] =~ /"value"/) {
$jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4]"; $jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4]";
Log3 $name,4,"$name: OBIS code \"$$f[1]\" will be stored in $$f[2]"; JSONMETER_Log $name,4,"OBIS code \"$$f[1]\" will be stored in $$f[2]";
$returnStr .= "OBIS code \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n"; $returnStr .= "OBIS code \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n";
} }
} elsif ($$f[0] == 2) { } elsif ($$f[0] == 2) {
if ($fields[$i] =~ /"obis"\s*:\s*"($$f[1])"\s*[,}]/ && $fields[$i] =~ /"valueString"/) { if ($fields[$i] =~ /"obis"\s*:\s*"($$f[1])"\s*[,}]/ && $fields[$i] =~ /"valueString"/) {
$jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4]"; $jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4]";
Log3 $name,4,"$name: OBIS code \"$$f[1]\" will be stored in $$f[2]"; JSONMETER_Log $name,4,"OBIS code \"$$f[1]\" will be stored in $$f[2]";
$returnStr .= "OBIS code \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n"; $returnStr .= "OBIS code \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n";
} }
} elsif ($$f[0] == 3) { } elsif ($$f[0] == 3) {
if ($fields[$i] =~ /"($$f[1])"\s*:/) { if ($fields[$i] =~ /"($$f[1])"\s*:/) {
$jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4] $$f[1]"; $jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4] $$f[1]";
Log3 $name,4,"$name: Property \"$$f[1]\" will be stored in $$f[2]"; JSONMETER_Log $name,4,"Property \"$$f[1]\" will be stored in $$f[2]";
$returnStr .= "Property \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n"; $returnStr .= "Property \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n";
} }
} elsif ($$f[0] == 4) { } elsif ($$f[0] == 4) {
if ($fields[$i] =~ /"($$f[1])"\s*:/) { if ($fields[$i] =~ /"($$f[1])"\s*:/) {
$jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4] $$f[1]"; $jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4] $$f[1]";
Log3 $name,4,"$name: Property \"$$f[1]\" will be stored in $$f[2]"; JSONMETER_Log $name,4,"Property \"$$f[1]\" will be stored in $$f[2]";
$returnStr .= "Property \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n"; $returnStr .= "Property \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n";
} }
# } elsif ($$f[0] == 6) {
# if ($fields[$i] =~ /"($$f[1])"\s*:/) {
# $jsonInterpreter .= "|$i $$f[0] $$f[2] $$f[3] $$f[4] $$f[1]";
# Log3 $name,4,"$name: Property \"$$f[1]\" will be stored in $$f[2]";
# $returnStr .= "Property \"$$f[1]\" will be extracted as reading '$$f[2]' (statistic type: $$f[3]) from part $i:\n$fields[$i]\n\n";
# }
} }
} }
} }
if ($jsonInterpreter ne "") { if ($jsonInterpreter ne "") {
Log3 $name, 3, "$name: Store results of JSON analysis for next device readings" if $alwaysAnalyse != 1; JSONMETER_Log $name, 3, "Store results of JSON analysis for next device readings" if $alwaysAnalyse != 1;
$jsonInterpreter = substr $jsonInterpreter, 1; $jsonInterpreter = substr $jsonInterpreter, 1;
$hash->{fhem}{jsonInterpreter} = $jsonInterpreter; $hash->{fhem}{jsonInterpreter} = $jsonInterpreter;
} else { } else {
Log3 $name, 2, "$name: Could not interpret the JSON file => please contact FHEM community" if $jsonInterpreter eq ""; JSONMETER_Log $name, 2, "Could not interpret the JSON file => please contact FHEM community" if $jsonInterpreter eq "";
} }
} else { } else {
$jsonInterpreter = $hash->{fhem}{jsonInterpreter} if exists $hash->{fhem}{jsonInterpreter}; $jsonInterpreter = $hash->{fhem}{jsonInterpreter} if exists $hash->{fhem}{jsonInterpreter};
@ -554,34 +561,34 @@ if ( $a[1] == 1 ){
$returnStr .= "\n================= Extract JSON values ==================\n\n"; $returnStr .= "\n================= Extract JSON values ==================\n\n";
my @a = split /\|/, $jsonInterpreter; my @a = split /\|/, $jsonInterpreter;
Log3 $name, 4, "$name: Extract ".($#a+1)." readings from ".($#fields+1)." json parts"; JSONMETER_Log $name, 4, "Extract ".($#a+1)." readings from ".($#fields+1)." json parts";
foreach (@a) { foreach (@a) {
$statisticType = 0; $statisticType = 0;
Log3 $name, 5, "$name: Handle $_"; JSONMETER_Log $name, 5, "Handle $_";
my @b = split / /, $_ ; my @b = split / /, $_ ;
#obis value #obis value
if ($b[1] == 1) { if ($b[1] == 1) {
if ($fields[$b[0]] =~ /"value"\s*:\s*"(.*?)"\s*[,\}]/g || $fields[$b[0]] =~ /"value"\s*:\s*(.*?)\s*[,\}]/g) { if ($fields[$b[0]] =~ /"value"\s*:\s*"(.*?)"\s*[,\}]/g || $fields[$b[0]] =~ /"value"\s*:\s*(.*?)\s*[,\}]/g) {
$value = $1; $value = $1;
# $value =~ s/^\s+|\s+$//g; # $value =~ s/^\s+|\s+$//g;
Log3 $name, 4, "$name: Value $value for reading $b[2] extracted from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Value $value for reading $b[2] extracted from '$fields[$b[0]]'";
$returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n";
readingsBulkUpdate($hash,$b[2],$value); readingsBulkUpdate($hash,$b[2],$value);
$statisticType = $b[3]; $statisticType = $b[3];
} else { } else {
Log3 $name, 4, "$name: Could not extract value for reading $b[2] from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Could not extract value for reading $b[2] from '$fields[$b[0]]'";
$returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n";
} }
#obis valueString #obis valueString
} elsif ($b[1] == 2) { } elsif ($b[1] == 2) {
if ($fields[$b[0]] =~ /"valueString"\s*:\s*"(.*?)"\s*[,}]/g ) { if ($fields[$b[0]] =~ /"valueString"\s*:\s*"(.*?)"\s*[,}]/g ) {
$value = $1; $value = $1;
Log3 $name, 4, "$name: Value $value for reading $b[2] extracted from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Value $value for reading $b[2] extracted from '$fields[$b[0]]'";
$returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n";
readingsBulkUpdate($hash,$b[2],$value); readingsBulkUpdate($hash,$b[2],$value);
$statisticType = $b[3]; $statisticType = $b[3];
} else { } else {
Log3 $name, 4, "$name: Could not extract value for reading $b[2] from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Could not extract value for reading $b[2] from '$fields[$b[0]]'";
$returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n";
} }
# JSON-Property # JSON-Property
@ -589,25 +596,25 @@ if ( $a[1] == 1 ){
if ($fields[$b[0]] =~ /"$b[5]"\s*:\s*"(.*?)"\s*[,}]/g || $fields[$b[0]] =~ /"$b[5]"\s*:\s*(.*?)\s*[,}]/g ) { if ($fields[$b[0]] =~ /"$b[5]"\s*:\s*"(.*?)"\s*[,}]/g || $fields[$b[0]] =~ /"$b[5]"\s*:\s*(.*?)\s*[,}]/g ) {
$value = $1; $value = $1;
$value =~ /^ *\d+(,\d\d\d)+/ && $value =~ s/,| //g; $value =~ /^ *\d+(,\d\d\d)+/ && $value =~ s/,| //g;
Log3 $name, 4, "$name: Value $value for reading $b[2] extracted from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Value $value for reading $b[2] extracted from '$fields[$b[0]]'";
$returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n";
readingsBulkUpdate($hash, $b[2], $value); readingsBulkUpdate($hash, $b[2], $value);
$statisticType = $b[3]; $statisticType = $b[3];
} else { } else {
Log3 $name, 4, "$name: Could not extract value for reading $b[2] from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Could not extract value for reading $b[2] from '$fields[$b[0]]'";
$returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n";
} }
# JSON-Property Time # JSON-Property Time
} elsif ($b[1] == 4) { } elsif ($b[1] == 4) {
if ($fields[$b[0]] =~ /"$b[5]"\s*:\s"?(\d*)"?\s*[,}]/g ) { if ($fields[$b[0]] =~ /"$b[5]"\s*:\s"?(\d*)"?\s*[,}]/g ) {
$value = $1; $value = $1;
Log3 $name, 4, "$name: Value $value for reading $b[2] extracted from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Value $value for reading $b[2] extracted from '$fields[$b[0]]'";
$returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Value \"$value\" for reading '$b[2]' extracted from part $b[0]:\n$fields[$b[0]]\n\n";
$value = strftime "%Y-%m-%d %H:%M:%S", localtime($value); $value = strftime "%Y-%m-%d %H:%M:%S", localtime($value);
readingsBulkUpdate($hash, $b[2], $value); readingsBulkUpdate($hash, $b[2], $value);
$statisticType = $b[3]; $statisticType = $b[3];
} else { } else {
Log3 $name, 4, "$name: Could not extract value for reading $b[2] from '$fields[$b[0]]'"; JSONMETER_Log $name, 4, "Could not extract value for reading $b[2] from '$fields[$b[0]]'";
$returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n"; $returnStr .= "Could not extract value for reading '$b[2]' from part $b[0]:\n$fields[$b[0]]\n\n";
} }
} }
@ -642,7 +649,7 @@ JSONMETER_UpdateAborted($)
delete($hash->{helper}{RUNNING_PID}); delete($hash->{helper}{RUNNING_PID});
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $host = $hash->{HOST}; my $host = $hash->{HOST};
Log3 $hash, 1, "$name Error: Timeout when connecting to host $host"; JSONMETER_Log $hash, 1, "Timeout when connecting to host $host";
} # end JSONMETER_UpdateAborted } # end JSONMETER_UpdateAborted