-
Notifications
You must be signed in to change notification settings - Fork 2
/
98_expandJSON.pm
executable file
·367 lines (302 loc) · 10.6 KB
/
98_expandJSON.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# $Id: 98_expandJSON.pm 12345 2018-02-08 08:20:45Z dev0 $
################################################################################
#
# Copyright (c) 2017 dev0
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# This copyright notice MUST APPEAR in all copies of the script!
#
################################################################################
my $module_version = "1.12_github_w_array";
package main;
use strict;
use warnings;
use POSIX;
use Encode;
sub expandJSON_expand($$$$;$$); # Forum #66761
sub expandJSON_Initialize($$) {
my ($hash) = @_;
$hash->{DefFn} = "expandJSON_Define";
$hash->{NotifyFn} = "expandJSON_Notify";
$hash->{AttrFn} = "expandJSON_Attr";
$hash->{AttrList} = "addStateEvent:1,0 "
. "addReadingsPrefix:1,0 "
. "disable:1,0 "
. "disabledForIntervals "
. "do_not_notify:1,0";
}
sub expandJSON_Define(@) {
my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def);
my $usg = "\nUse 'define <name> expandJSON <event regexp>"
. " [<target reading regexp>]";
return "Wrong syntax: $usg" if(int(@a) < 3);
return "ERROR: Perl module JSON is not installed"
if (expandJSON_isPmInstalled($hash,"JSON"));
my $name = $a[0];
my $type = $a[1];
# source regexp
my $re = $a[2];
return "Bad regexp: starting with *" if($re =~ m/^\*/);
eval { "test" =~ m/^$re$/ };
return "Bad regexp $re: $@" if($@);
$hash->{s_regexp} = $re;
InternalTimer(gettimeofday(), sub(){notifyRegexpChanged($hash, $re);}, $hash);
# dest regexp
if (defined $a[3]) {
$re = $a[3];
return "Bad regexp: starting with *" if($re =~ m/^\*/);
eval { "test" =~ m/^$re$/ };
return "Bad regexp $re: $@" if($@);
$hash->{t_regexp} = $re;
}
else {
$hash->{t_regexp} = ".*";
}
my $doTrigger = ($name !~ m/^$re$/); # Forum #34516
readingsSingleUpdate($hash, "state", "active", $doTrigger);
$hash->{version} = $module_version;
return undef;
}
sub expandJSON_Attr($$) {
my ($cmd,$name,$aName,$aVal) = @_;
my $hash = $defs{$name};
my $type = $hash->{TYPE};
my $ret;
if ($cmd eq "set" && !defined $aVal) {
$ret = "not empty"
}
elsif ($aName eq "addReadingsPrefix") {
$cmd eq "set"
? $aVal =~ m/^[01]$/ ? ($hash->{helper}{$aName} = $aVal) : ($ret = "0|1")
: delete $hash->{helper}{$aName}
}
elsif ($aName eq "do_not_notify") {
$ret = "0|1" if $cmd eq "set" && $aVal !~ m/^[01]$/;
}
elsif ($aName eq "disable") {
if ($cmd eq "set") {
if ($aVal !~ m/^[01]$/) { $ret = "0|1" }
else { readingsSingleUpdate($hash, "state", $aVal?"disabled":"active", 1) }
}
elsif ($cmd eq "del") { readingsSingleUpdate($hash, "state", "active", 1) }
}
if ($ret) {
my $v = defined $aVal ? $aVal : "";
my $msg = "$type: attr $name $aName $v: value must be: ";
Log3 $name, 2, $msg.$ret;
return $msg.$ret;
}
return undef;
}
sub expandJSON_Notify($$) {
my ($hash, $dhash) = @_;
my $name = $hash->{NAME};
return "" if(IsDisabled($name));
my $events = deviceEvents($dhash, AttrVal($name, "addStateEvent", 0));
return if( !grep { m/{.*}\s*$/s } @{ $events } ); #there is no JSON content
for (my $i = 0; $i < int(@{$events}); $i++) {
my $event = $events->[$i];
$event = "" if(!defined($event));
my $re = $hash->{s_regexp};
my $devName = $dhash->{NAME};
my $found = ($devName =~ m/^$re$/ || "$devName:$event" =~ m/^$re$/s);
if ($found) {
my $type = $hash->{TYPE};
Log3 $name, 5, "$type $name: Found $devName:$event";
my ($reading,$value) = $event =~ m/^\s*{.*}\s*$/s
? ("state", $event)
: split(": ", $event, 2);
$hash->{STATE} = $dhash->{NTFY_TRIGGERTIME};
if ($value !~ m/^\s*{.*}\s*$/s) { # eg. state with an invalid json
Log3 $name, 5, "$type $name: Invalid JSON: $value";
return;
}
Log3 $name, 5, "$type $name: Yield decode: $hash | $devName | $reading "
. "| $value";
InternalTimer(
gettimeofday(),
sub(){ expandJSON_decode($hash,$devName,$reading,$value) },
$hash);
}
}
return undef;
}
sub expandJSON_decode($$$$) {
my ($p) = @_;
my ($hash,$dname,$dreading,$dvalue) = @_;
my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
my $dhash = $defs{$dname};
my $h;
eval { $h = decode_json(encode_utf8($dvalue)); 1; };
if ( $@ ) {
Log3 $name, 2, "$type $name: Bad JSON: $dname $dreading: $dvalue";
Log3 $name, 2, "$type $name: $@";
return undef;
}
my $sPrefix = $hash->{helper}{addReadingsPrefix} ? $dreading."_" : "";
readingsBeginUpdate($dhash);
expandJSON_expand($hash,$dhash,$sPrefix,$h);
readingsEndUpdate($dhash, AttrVal($name,"do_not_notify",0) ? 0 : 1);
return undef;
}
sub expandJSON_expand($$$$;$$) {
# thanx to bgewehr for the root position of this recursive snippet
# https://github.com/bgewehr/fhem
my ($hash,$dhash,$sPrefix,$ref,$prefix,$suffix) = @_;
$prefix = "" if( !$prefix );
$suffix = "" if( !$suffix );
$suffix = "_$suffix" if( $suffix );
if( ref( $ref ) eq "ARRAY" ) {
while( my ($key,$value) = each @{ $ref } ) {
if( ref( $value ) ) {
expandJSON_expand($hash,$dhash,$sPrefix,$value,$prefix.sprintf("%02i",$key+1)."_");
}
else {
(my $reading = $sPrefix.$prefix.sprintf("%02i",$key+1)) =~ s/[^A-Za-z\d_\.\-\/]/_/g;
readingsBulkUpdate($dhash, $reading, $value)
if $reading =~ m/^$hash->{t_regexp}$/;
}
}
}
elsif( ref( $ref ) eq "HASH" ) {
while( my ($key,$value) = each %{ $ref } ) {
if( ref( $value ) ) {
expandJSON_expand($hash,$dhash,$sPrefix,$value,$prefix.$key.$suffix."_");
}
else {
# replace illegal characters in reading names
(my $reading = $sPrefix.$prefix.$key.$suffix) =~ s/[^A-Za-z\d_\.\-\/]/_/g;
readingsBulkUpdate($dhash, $reading, $value)
if $reading =~ m/^$hash->{t_regexp}$/;
}
}
}
elsif( ref( $ref ) eq "JSON::PP::Boolean" ) { # Forum #82734
(my $reading = $sPrefix.$prefix) =~ s/[^A-Za-z\d_\.\-\/]/_/g;
$reading = substr($reading, 0, -1); #remove tailing _
readingsBulkUpdate($dhash, $reading, $ref ? 1 : 0)
if $reading =~ m/^$hash->{t_regexp}$/;
}
}
sub expandJSON_isPmInstalled($$)
{
my ($hash,$pm) = @_;
my ($name,$type) = ($hash->{NAME},$hash->{TYPE});
if (not eval "use $pm;1")
{
Log3 $name, 1, "$type $name: perl modul missing: $pm. Install it, please.";
$hash->{MISSING_MODULES} .= "$pm ";
return "failed: $pm";
}
return undef;
}
1;
=pod
=item helper
=item summary Expand a JSON string from a reading into individual readings
=item summary_DE Expandiert eine JSON Zeichenkette in individuelle Readings
=begin html
<a name="expandJSON"></a>
<h3>expandJSON</h3>
<ul>
<p>Expand a JSON string from a reading into individual readings</p>
<ul>
<li>Requirement: perl module JSON<br>
Use "cpan install JSON" or operating system's package manager to install
Perl JSON Modul. Depending on your os the required package is named:
libjson-perl or perl-JSON.
</li>
</ul><br>
<a name="expandJSONdefine"></a>
<b>Define</b><br><br>
<ul>
<code>define <name> expandJSON <source_regex>
[<target_regex>]</code><br><br>
<li>
<a name=""><name></a><br>
A name of your choice.</li><br>
<li>
<a name=""><source_regex></a><br>
Regexp that must match your devices, readings and values that contain
the JSON strings. Regexp syntax is the same as used by notify and must not
contain spaces.<br>
</li><br>
<li>
<a name=""><target_regex></a><br>
Optional: This regexp is used to determine whether the target reading is
converted or not at all. If not set then all readings will be used. If set
then only matching readings will be used. Regexp syntax is the same as
used by notify and must not contain spaces.<br>
</li><br>
<li>
Examples:<br>
<br>
<u>Source reading:</u><br>
<code>
device:{.*} #state without attribute addStateEvent<br>
device:state:.{.*} #state with attribute addStateEvent<br>
device:reading:.{.*}<br>
Sonoff.*:ENERGY.*:.{.*}<br>
.*wifiIOT.*:.*sensor.*:.{.*}<br>
(?i)dev.*:(sensor1|sensor2|teleme.*):.{.*}<br>
(devX:{.*}|devY.*:jsonY:.{.*Wifi.*{.*SSID.*}.*})
</code><br>
<br>
<u>Target reading:</u><br>
<code>.*power.*</code><br>
<code>(Power|Current|Voltage|.*day)</code><br><br>
<u>Complete definitions:</u><br>
<code>define ej1 expandJSON device:sourceReading:.{.*} targetReading
</code><br>
<code>define ej2 expandJSON Sonoff.*:ENERGY.*:.{.*} (Power|.*day)
</code><br>
<code>define ej3 expandJSON (?i).*_sensordev_.*:.*:.{.*}
</code><br><br>
</li><br>
</ul>
<a name="expandJSONset"></a>
<b>Set</b><br><br>
<ul>
N/A<br><br>
</ul>
<a name="expandJSONget"></a>
<b>Get</b><br><br>
<ul>
N/A<br><br>
</ul>
<a name="expandJSON_attr"></a>
<b>Attributes</b><br><br>
<ul>
<li><a name="expandJSON_attr_addReadingsPrefix">addReadingsPrefix</a><br>
Add source reading as prefix to new generated readings. Useful if you have
more than one reading with a JSON string that should be converted.
</li><br>
<li><a name="expandJSON_attr_disable">disable</a><br>
Used to disable this device.
</li><br>
<li><a name="expandJSON_attr_do_not_notify">do_not_notify</a><br>
Do not generate events for converted readings at all. Think twice before
using this attribute. In most cases, it is more appropriate to use
event-on-change-reading in target devices.
</li><br>
<li><a href="#disabledForIntervals">disabledForIntervals</a></li>
<li><a href="#addStateEvent">addStateEvent</a></li>
</ul>
</ul>
=end html
=cut