98_JsonMod.pm: wip (Forum #134554)

git-svn-id: https://svn.fhem.de/fhem/trunk@27932 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
herrmannj
2023-09-04 11:53:31 +00:00
parent 8045b773f8
commit a74f78fa56

View File

@@ -466,11 +466,11 @@ sub JsonMod_DoReadings {
# my sub m1 { # my sub m1 {
local *m1 = sub { local *m1 = sub {
my ($jsonPathExpression, $readingName, $readingValue) = @_; my ($jsonPathExpression, $readingName, $readingValue, $defaultReading, $defaultValue) = @_;
$warnings = 1; $warnings = 1;
if (ref($resultSet) eq 'ARRAY') { if (ref($resultSet) eq 'ARRAY' and scalar @$resultSet) {
foreach my $res (@{$resultSet}) { foreach my $res (@{$resultSet}) {
$resultNode = $res->[0]; $resultNode = $res->[0];
$resultObject = $res->[1]; # value $resultObject = $res->[1]; # value
@@ -480,6 +480,11 @@ sub JsonMod_DoReadings {
eval 'm2'.$args; warn $@ if $@; eval 'm2'.$args; warn $@ if $@;
$index++; $index++;
}; };
} elsif($defaultReading) {
JsonMod_Logger($hash, 4, '%s is empty or missing: use defaults (%s:%s)', $jsonPathExpression, $defaultReading, $defaultValue);
sanitizedSetReading($defaultReading, $defaultValue);
} else {
JsonMod_Logger($hash, 4, '%s is empty or missing: discarded', $jsonPathExpression);
}; };
}; };
@@ -551,24 +556,20 @@ sub JsonMod_DoReadings {
}; };
# reading is not used anymore # reading is not used anymore
foreach my $k (keys %{$oldReadings}) { foreach my $k (keys %{$oldReadings}) {
readingsDelete($hash, $k) if ($oldReadings->{$k} == 0 and any { $_ eq $k} @oldReadings); if ($oldReadings->{$k} == 0 and any { $_ eq $k} @oldReadings) {
readingsDelete($hash, $k);
DoTrigger($name, ".readingsDelete $k");
};
}; };
readingsBulkUpdate($hash, '.computedReadings', join ',', @newReadings); readingsBulkUpdate($hash, '.computedReadings', join ',', @newReadings);
readingsEndUpdate($hash, 1); readingsEndUpdate($hash, 1);
} else { } else {
# an empty answer received: # in case all readings removed
if (AttrVal($name, 'onEmptyResponse', undef) and AttrVal($name, 'onEmptyResponse', '') =~ m/^keep$/i) { for (split ',', ReadingsVal($name, '.computedReadings', '')) {
DoTrigger($name, '.computedReadingsKeep'); readingsDelete($hash, $_);
} elsif (AttrVal($name, 'onEmptyResponse', undef) and AttrVal($name, 'onEmptyResponse', '') =~ m/^set\s.*/) { DoTrigger($name, ".readingsDelete $_");
my ($r, $v) = (AttrVal($name, 'onEmptyResponse', '') =~ m/^set\s([A-Za-z0-9\/_\.-]+)(?:\s(.*))*/); };
readingsSingleUpdate($hash, $r, $v // '', 1) if ($r); readingsSingleUpdate($hash, '.computedReadings', '', 1);
# } else {
} elsif(0) {
# delete entire readings
readingsDelete($hash, $_) for (split ',', ReadingsVal($name, '.computedReadings', ''));
readingsDelete($hash,'.computedReadings');
DoTrigger($name, '.computedReadingsNone');
}
}; };
}; };