mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-03-13 05:06:35 +00:00
34_ESPEasy.pm: add use of encode_utf8 for json_decode
git-svn-id: https://svn.fhem.de/fhem/trunk@14184 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
e37bc7a603
commit
94b5e3de39
@ -1,5 +1,6 @@
|
|||||||
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
|
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
|
||||||
# Do not insert empty lines here, update check depends on it.
|
# Do not insert empty lines here, update check depends on it.
|
||||||
|
- feature: 34_ESPEasy: add use of encode_utf8 for json_decode
|
||||||
- new: 36_Vallox: support Vallox DigitSE ventilation system
|
- new: 36_Vallox: support Vallox DigitSE ventilation system
|
||||||
- feature: 71_YAMAHA_AVR: new set command & readings for displayBrightness,
|
- feature: 71_YAMAHA_AVR: new set command & readings for displayBrightness,
|
||||||
extraBass, surroundDecoder and ypaoVolume
|
extraBass, surroundDecoder and ypaoVolume
|
||||||
|
@ -29,7 +29,6 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
use Data::Dumper;
|
use Data::Dumper;
|
||||||
use MIME::Base64;
|
use MIME::Base64;
|
||||||
#use Encode qw(encode_utf8);
|
|
||||||
use TcpServerUtils;
|
use TcpServerUtils;
|
||||||
use HttpUtils;
|
use HttpUtils;
|
||||||
use Color;
|
use Color;
|
||||||
@ -37,7 +36,7 @@ use Color;
|
|||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# global/default values
|
# global/default values
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
my $module_version = 1.06; # Version of this module
|
my $module_version = 1.07; # Version of this module
|
||||||
my $minEEBuild = 128; # informational
|
my $minEEBuild = 128; # informational
|
||||||
my $minJsonVersion = 1.02; # checked in received data
|
my $minJsonVersion = 1.02; # checked in received data
|
||||||
|
|
||||||
@ -293,6 +292,9 @@ sub ESPEasy_Define($$) # only called when defined, not on reload.
|
|||||||
$hash->{VERSION} = $module_version;
|
$hash->{VERSION} = $module_version;
|
||||||
$hash->{NOTIFYDEV} = "global";
|
$hash->{NOTIFYDEV} = "global";
|
||||||
|
|
||||||
|
eval "use Encode qw(encode_utf8);";
|
||||||
|
$hash->{helper}{pmEncode} = $@ ? 0 : 1;
|
||||||
|
|
||||||
#--- BRIDGE -------------------------------------------------
|
#--- BRIDGE -------------------------------------------------
|
||||||
if ($hash->{HOST} eq "bridge") {
|
if ($hash->{HOST} eq "bridge") {
|
||||||
$hash->{SUBTYPE} = "bridge";
|
$hash->{SUBTYPE} = "bridge";
|
||||||
@ -316,6 +318,9 @@ sub ESPEasy_Define($$) # only called when defined, not on reload.
|
|||||||
$hash->{MAX_HTTP_SESSIONS} = $d_maxHttpSessions;
|
$hash->{MAX_HTTP_SESSIONS} = $d_maxHttpSessions;
|
||||||
$hash->{MAX_QUEUE_SIZE} = $d_maxQueueSize;
|
$hash->{MAX_QUEUE_SIZE} = $d_maxQueueSize;
|
||||||
|
|
||||||
|
Log3 $name, 2, "$type $name: WARNING: Perl module Encode not installed. No utf-8 encoding available."
|
||||||
|
if !$hash->{helper}{pmEncode};
|
||||||
|
|
||||||
ESPEasy_removeGit($hash);
|
ESPEasy_removeGit($hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,14 +637,16 @@ sub ESPEasy_Read($) {
|
|||||||
my $json;
|
my $json;
|
||||||
if (defined $data[1] && $data[1] =~ m/"module":"ESPEasy"/) {
|
if (defined $data[1] && $data[1] =~ m/"module":"ESPEasy"/) {
|
||||||
|
|
||||||
# remove illegal chars but keep JSON relevant chars.
|
# use encode_utf8 if available else replace any disturbing chars
|
||||||
$data[1] =~ s/[^A-Za-z\d_\.\-\/\{}:,"]/_/g;
|
if ( $bhash->{helper}{pmEncode} ) {
|
||||||
|
eval { $json = decode_json( encode_utf8($data[1]) ); 1; }
|
||||||
eval {$json = decode_json($data[1]);1;};
|
}
|
||||||
|
else {
|
||||||
|
eval { $json = decode_json( $data[1] =~ s/[^\x20-\x7E]/_/gr ); 1; }
|
||||||
|
};
|
||||||
if ($@) {
|
if ($@) {
|
||||||
Log3 $bname, 2, "$btype $name: WARNING: invalid JSON data or utf-8 "
|
Log3 $bname, 2, "$btype $name: WARNING: Invalid JSON received. "
|
||||||
. "encoding failed ($peer).";
|
. "Check your ESP configuration ($peer).\n$@";
|
||||||
Log3 $bname, 2, "$btype $name: WARNING: $@";
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1181,6 +1188,9 @@ sub ESPEasy_dispatchParse($$$) # called by logical device (defined by
|
|||||||
: ": unknown node type id"
|
: ": unknown node type id"
|
||||||
if $reading eq "node_type_id";
|
if $reading eq "node_type_id";
|
||||||
|
|
||||||
|
# no value given
|
||||||
|
$value = "not defined" if !defined $value || $value eq "";
|
||||||
|
|
||||||
# set internal
|
# set internal
|
||||||
$hash->{"ESP_".uc($reading)} = $value;
|
$hash->{"ESP_".uc($reading)} = $value;
|
||||||
|
|
||||||
@ -1366,7 +1376,13 @@ sub ESPEasy_httpReqParse($$$)
|
|||||||
."cmd:'$param->{cmd},$param->{plist}' => '$logData'";
|
."cmd:'$param->{cmd},$param->{plist}' => '$logData'";
|
||||||
if ($data =~ m/^{/) { #it could be json...
|
if ($data =~ m/^{/) { #it could be json...
|
||||||
my $res;
|
my $res;
|
||||||
eval {$res = decode_json($data);1;};
|
|
||||||
|
if ( $hash->{helper}{pmEncode} ) {
|
||||||
|
eval { $res = decode_json( encode_utf8($data) ); 1; };
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
eval { $res = decode_json( $data =~ s/[^\x20-\x7E]/_/gr ); 1; };
|
||||||
|
}
|
||||||
if ($@) {
|
if ($@) {
|
||||||
Log3 $name, 2, "$type $name: WARNING: deformed JSON data received "
|
Log3 $name, 2, "$type $name: WARNING: deformed JSON data received "
|
||||||
."from $param->{host} requested by $param->{ident}.";
|
."from $param->{host} requested by $param->{ident}.";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user