2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-23 20:52:13 +00:00

98_JsonMod.pm: UTF8 decode with pp parser, Umlaute (Forum: 113798.75)

git-svn-id: https://svn.fhem.de/fhem/trunk@24420 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
herrmannj 2021-05-11 21:25:20 +00:00
parent 1738476b30
commit 775db7275f

View File

@ -261,7 +261,11 @@ sub JsonMod_DoReadings {
my ($r, $v) = @_; my ($r, $v) = @_;
# convert into valid reading # convert into valid reading
#printf "0 %s %s %s %s\n", $r, length($r), $v, length($v); # special treatment of "umlaute"
my %umlaute = ("ä" => "ae", "Ä" => "Ae", "ü" => "ue", "Ü" => "Ue", "ö" => "oe", "Ö" => "Oe", "ß" => "ss" );
my $umlautkeys = join ("|", keys(%umlaute));
$r =~ s/($umlautkeys)/$umlaute{$1}/g;
# normalize remaining special chars
$r = Unicode::Normalize::NFD($r); $r = Unicode::Normalize::NFD($r);
utf8::encode($r) if utf8::is_utf8($r); utf8::encode($r) if utf8::is_utf8($r);
$r =~ s/\s/_/g; # whitespace $r =~ s/\s/_/g; # whitespace
@ -287,6 +291,7 @@ sub JsonMod_DoReadings {
# processing attr readingList # processing attr readingList
my $readingList = AttrVal($name, 'readingList', ''); my $readingList = AttrVal($name, 'readingList', '');
$readingList =~ s/\s+$//s; # rtrim
utf8::decode($readingList); # data from "ouside" utf8::decode($readingList); # data from "ouside"
while ($readingList) { while ($readingList) {
@ -561,7 +566,7 @@ sub JsonMod_ApiRequest {
my $data; my $data;
open(my $fh, '<', $filename) or do { open(my $fh, '<', $filename) or do {
$hash->{'SOURCE'} = sprintf('%s (%s)', $filename, (stat $filename)[7]); $hash->{'SOURCE'} = sprintf('%s (%s)', $filename, (stat $filename)[7]);
$hash->{'API__LAST_MSG'} = $!; $hash->{'API_LAST_MSG'} = $!;
return; return;
}; };
{ {
@ -572,11 +577,11 @@ sub JsonMod_ApiRequest {
my $json = JsonMod::JSON::StreamReader->new()->parse($data); my $json = JsonMod::JSON::StreamReader->new()->parse($data);
JsonMod_DoReadings($hash, $json); JsonMod_DoReadings($hash, $json);
$hash->{'SOURCE'} = sprintf('%s (%s)', $filename, (stat $filename)[7]); $hash->{'SOURCE'} = sprintf('%s (%s)', $filename, (stat $filename)[7]);
$hash->{'API__LAST_MSG'} = 200; $hash->{'API_LAST_MSG'} = 200;
return; return;
} else { } else {
$hash->{'SOURCE'} = sprintf('%s', $filename); $hash->{'SOURCE'} = sprintf('%s', $filename);
$hash->{'API__LAST_MSG'} = 404; $hash->{'API_LAST_MSG'} = 404;
}; };
}; };
@ -625,13 +630,13 @@ sub JsonMod_ApiResponse {
}; };
$hash->{'SOURCE'} = sprintf('%s (%s)', $url, $param->{'code'} //= ''); $hash->{'SOURCE'} = sprintf('%s (%s)', $url, $param->{'code'} //= '');
$hash->{'API__LAST_MSG'} = $param->{'code'} //= 'failed'; $hash->{'API_LAST_MSG'} = $param->{'code'} //= 'failed';
my sub doError { my sub doError {
my ($msg) = @_; my ($msg) = @_;
$hash->{'API__LAST_MSG'} = $msg; $hash->{'API_LAST_MSG'} = $msg;
my $next = Time::HiRes::time() + 600; my $next = Time::HiRes::time() + 600;
#$hash->{'API__NEXT_REQ'} = $next; #$hash->{'API_NEXT_REQ'} = $next;
return InternalTimer($next, \&JsonMod_ApiRequest, $hash); return InternalTimer($next, \&JsonMod_ApiRequest, $hash);
}; };
@ -979,7 +984,7 @@ sub parse {
my $xs = eval 'Cpanel::JSON::XS::decode_json($in)'; my $xs = eval 'Cpanel::JSON::XS::decode_json($in)';
return $xs if ($xs); return $xs if ($xs);
my $err = _decode(\my $value, $in, 1); my $err = _decode(\my $value, $in, 0);
return defined $err ? $err : $value; return defined $err ? $err : $value;
}; };
@ -1197,8 +1202,10 @@ sub get {
foreach my $child (@childList) { foreach my $child (@childList) {
$self->getSingle($child, $property, $deep, $path, $normalized, $query); $self->getSingle($child, $property, $deep, $path, $normalized, $query);
}; };
} elsif ($property =~ /^\d+$/) { } elsif ($property =~ m/^\d+$/) {
$self->getSingle($property, $property, $deep, $path, $normalized, $query); $self->getSingle($property, $property, $deep, $path, $normalized, $query);
} elsif ($property =~ m/^\s*\d+\s*,\s*\d+\s*(?:,\s*\d+\s*)*$/) { # [n,n(,n)]
} else { } else {
die ("JsonPath filter property $property failure"); die ("JsonPath filter property $property failure");
}; };