69_SoftliqCloud.pm: Fix garbage JSON

git-svn-id: https://svn.fhem.de/fhem/trunk@24401 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
KernSani 2021-05-08 19:58:14 +00:00
parent 766d947942
commit bced2c7104

View File

@ -21,6 +21,7 @@
# #
############################################################################## ##############################################################################
# Changelog: # Changelog:
# 0.1.07: - 2021-05-08 - Optimize garbage JSON processing
# 0.1.06: - 2021-04-26 - Split JSON strings to avoid processing multiple root nodes # 0.1.06: - 2021-04-26 - Split JSON strings to avoid processing multiple root nodes
# 0.1.05: Fixed setting numeric parameters # 0.1.05: Fixed setting numeric parameters
# 0.1.04: ANother fix to avoid "garbage" in JSON # 0.1.04: ANother fix to avoid "garbage" in JSON
@ -55,7 +56,7 @@ use Digest::MD5 qw(md5);
use FHEM::Core::Authentication::Passwords qw(:ALL); use FHEM::Core::Authentication::Passwords qw(:ALL);
my $version = "0.1.06"; my $version = "0.1.07";
my $missingModul = ''; my $missingModul = '';
eval 'use MIME::Base64::URLSafe;1' or $missingModul .= 'MIME::Base64::URLSafe '; eval 'use MIME::Base64::URLSafe;1' or $missingModul .= 'MIME::Base64::URLSafe ';
@ -296,7 +297,7 @@ sub Define {
$hash->{helper}->{passObj} = FHEM::Core::Authentication::Passwords->new( $hash->{TYPE} ); $hash->{helper}->{passObj} = FHEM::Core::Authentication::Passwords->new( $hash->{TYPE} );
# get password form old storage and save to new format # get password form old storage and save to new format
if ( !defined(ReadPassword($hash)) ) { if ( !defined( ReadPassword($hash) ) ) {
if ( defined( ReadPasswordOld($hash) ) ) { if ( defined( ReadPasswordOld($hash) ) ) {
my ( $passResp, $passErr ) = $hash->{helper}->{passObj}->setStorePassword( $name, ReadPasswordOld($hash) ); my ( $passResp, $passErr ) = $hash->{helper}->{passObj}->setStorePassword( $name, ReadPasswordOld($hash) );
if ( defined($passErr) ) { if ( defined($passErr) ) {
@ -1965,6 +1966,23 @@ sub Ready {
return; return;
} }
sub splitTest {
my $buf = shift;
my $name = "sd18";
my @bufs;
$buf =~ s///xsm;
my $index = index( $buf, '}{' );
if ( $index > 0 ) {
Log3( $name, LOG_RECEIVE, "[$name] - Splitting double-JSON buffer" );
push( @bufs, decode_json(substr( $buf, 0, $index + 1 ) ));
push( @bufs, decode_json(substr( $buf, $index + 1 )));
}
else {
push( @bufs, $buf );
}
return Dumper(@bufs);
}
sub wsReadDevIo { sub wsReadDevIo {
my $hash = shift; my $hash = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -1976,6 +1994,7 @@ sub wsReadDevIo {
} }
$buf =~ s///xsm; $buf =~ s///xsm;
$buf =~ s/\\x\{1e\}//xsm; $buf =~ s/\\x\{1e\}//xsm;
#if ( !( $buf =~ /}$/xsm ) ) { #if ( !( $buf =~ /}$/xsm ) ) {
# $buf = substr( $buf, 0, rindex( $buf, "}" ) ); # $buf = substr( $buf, 0, rindex( $buf, "}" ) );
#} #}
@ -1985,17 +2004,18 @@ sub wsReadDevIo {
Log3( $name, LOG_DEBUG, qq([$name] Received from DevIo: $buf) ); Log3( $name, LOG_DEBUG, qq([$name] Received from DevIo: $buf) );
my @bufs; my @bufs;
my $index = index($buf, '}{'); my $index = index( $buf, '}{' );
if ($index > 0) { if ( $index > 0 ) {
Log3 ($name, LOG_RECEIVE, "[$name] - Splitting double-JSON buffer"); Log3( $name, LOG_RECEIVE, "[$name] - Splitting double-JSON buffer" );
@bufs = split(/,/xsm,join($COMMA,substr($buf,0,$index),substr($buf,$index+1))); push( @bufs, substr( $buf, 0, $index + 1 ) );
push( @bufs, substr( $buf, $index + 1 ) );
} }
else { else {
push(@bufs,$buf); push( @bufs, $buf );
} }
foreach my $bufi (@bufs) { foreach my $bufi (@bufs) {
Log3 ($name, LOG_RECEIVE, "[$name] - Extracted".$bufi ); Log3( $name, LOG_RECEIVE, "[$name] - Extracted" . $bufi );
parseWebsocketRead( $hash, $bufi ); parseWebsocketRead( $hash, $bufi );
} }