fix for invalid json

git-svn-id: https://svn.fhem.de/fhem/trunk@4908 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
justme1968
2014-02-13 14:35:50 +00:00
parent 4e091a6351
commit 66e009efde

View File

@@ -6,7 +6,7 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Encode qw(encode_utf8); use Encode qw(encode);
use JSON; use JSON;
use LWP::Simple; use LWP::Simple;
use HTTP::Request; use HTTP::Request;
@@ -170,7 +170,9 @@ withings_getToken($)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
my $once = $json->{body}{once}; my $once = $json->{body}{once};
$hash->{Token} = $once; $hash->{Token} = $once;
@@ -209,7 +211,9 @@ withings_getSessionKey($)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
foreach my $account (@{$json->{body}{account}}) { foreach my $account (@{$json->{body}{account}}) {
next if( !defined($account->{id}) ); next if( !defined($account->{id}) );
$hash->{AccountID} = $account->{id} if($account->{email} eq $hash->{Login}); $hash->{AccountID} = $account->{id} if($account->{email} eq $hash->{Login});
@@ -356,7 +360,8 @@ withings_getUsers($)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
my @users = (); my @users = ();
foreach my $user (@{$json->{body}{users}}) { foreach my $user (@{$json->{body}{users}}) {
@@ -380,7 +385,8 @@ withings_getDevices($)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );;
my @devices = (); my @devices = ();
foreach my $association (@{$json->{body}{associations}}) { foreach my $association (@{$json->{body}{associations}}) {
@@ -406,7 +412,8 @@ withings_getDeviceDetail($$)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
return $json->{body}; return $json->{body};
} }
@@ -431,19 +438,8 @@ withings_getDeviceReadings($$)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
if(open(FH, "</tmp/getmeashf.txt")) {
my $content;
while (my $line = <FH>) {
chomp $line;
next if($line =~ m/^#.*$/);
$content .= $line;
}
close(FH);
$json = JSON->new->utf8(0)->decode($content);
}
return $json; return $json;
} }
@@ -460,7 +456,8 @@ withings_getUserDetail($$)
my $request = HTTP::Request->new('GET', $URL, $header); my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request); my $response = $agent->request($request);
my $json = JSON->new->utf8(0)->decode($response->content); my $json = ();
$json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );
return $json->{body}{users}[0]; return $json->{body}{users}[0];
} }
@@ -556,7 +553,9 @@ withings_pollUser($)
$url .= "&userid=$hash->{User}&publickey=$hash->{Key}"; $url .= "&userid=$hash->{User}&publickey=$hash->{Key}";
$url .= "&lastupdate=$lastupdate" if( $lastupdate ); $url .= "&lastupdate=$lastupdate" if( $lastupdate );
my $ret = get($url); my $ret = get($url);
my $json = JSON->new->utf8(0)->decode($ret); #my $json = JSON->new->utf8(0)->decode($ret);
my $json = ();
$json = JSON->new->utf8->decode(encode('UTF-8', $ret)) if( $ret =~ m/^{.*}$/ );
$hash->{status} = $json->{status}; $hash->{status} = $json->{status};
if( $hash->{status} == 0 ) { if( $hash->{status} == 0 ) {
@@ -646,7 +645,6 @@ withings_Get($$@)
} }
} }
Log 3, "$name: >$hash->{SUBTYPE}< >>$cmd<<";
return "Unknown argument $cmd, choose one of $list"; return "Unknown argument $cmd, choose one of $list";
} }