32_speedtest: added ookla attribute

git-svn-id: https://svn.fhem.de/fhem/trunk@22919 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
justme-1968 2020-10-05 17:54:25 +00:00
parent d9540f601a
commit 086c89a2ff
2 changed files with 68 additions and 8 deletions

View File

@ -1,5 +1,7 @@
# 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: 32_speedtest: added ookla attribute
- feature: 31_HUEDevice: added lastseen attribute
- change: 57_SSCal: integrate SMUtils lib, minor fixes - change: 57_SSCal: integrate SMUtils lib, minor fixes
- bugfix: 02_FTUISRV: fix empty loop includes results - bugfix: 02_FTUISRV: fix empty loop includes results
- feature: 31_HUEDevice: added gesture reading for Aqara Cube - feature: 31_HUEDevice: added gesture reading for Aqara Cube

View File

@ -7,6 +7,9 @@ use strict;
use warnings; use warnings;
use Blocking; use Blocking;
my $speedtest_hasJSON = 1;
sub sub
speedtest_Initialize($) speedtest_Initialize($)
{ {
@ -17,8 +20,12 @@ speedtest_Initialize($)
$hash->{SetFn} = "speedtest_Set"; $hash->{SetFn} = "speedtest_Set";
$hash->{AttrList} = "checks-till-disable ". $hash->{AttrList} = "checks-till-disable ".
"disable:0,1 ". "disable:0,1 ".
"ookla:1,0 ".
"path ". "path ".
$readingFnAttributes; $readingFnAttributes;
eval "use JSON";
$speedtest_hasJSON = 0 if($@);
} }
##################################### #####################################
@ -123,19 +130,35 @@ speedtest_DoSpeedtest($)
my ($string) = @_; my ($string) = @_;
my ($name, $server) = split("\\|", $string); my ($name, $server) = split("\\|", $string);
my $cmd = AttrVal($name, "path", "/usr/local/speedtest-cli" ); my $ookla = AttrVal($name, "ookla", undef);
$cmd .= "/speedtest-cli --simple";
$cmd .= " --server $server" if( $server ); my $cmd;
if( $ookla ) {
$cmd = AttrVal($name, "path", "/usr/local/bin" );
$cmd .= "/speedtest --accept-license --accept-gdpr -f json";
$cmd .= " -s $server" if( $server );
} else {
$cmd = AttrVal($name, "path", "/usr/local/speedtest-cli" );
$cmd .= "/speedtest-cli --simple";
$cmd .= " --server $server" if( $server );
}
Log3 $name, 5, "starting speedtest"; Log3 $name, 5, "starting speedtest";
my $speedstr = qx($cmd); my $speedstr = qx($cmd);
Log3 $name, 5, "speedtest done"; Log3 $name, 5, "speedtest done";
if( $ookla ) {
$speedstr =~ s/\n//g;
return "$name|$speedstr";
}
my @speedarr = split(/\n/, $speedstr); my @speedarr = split(/\n/, $speedstr);
for( my $i = 0; $i < 3; ++$i ) for( my $i = 0; $i < 3; ++$i ) {
{
$speedarr[$i] = $1 if( $speedarr[$i] && $speedarr[$i] =~ m/^\w+: (.*)/i ); $speedarr[$i] = $1 if( $speedarr[$i] && $speedarr[$i] =~ m/^\w+: (.*)/i );
} }
return "$name|$speedarr[0]|$speedarr[1]|$speedarr[2]"; return "$name|$speedarr[0]|$speedarr[1]|$speedarr[2]";
} }
@ -143,11 +166,11 @@ sub
speedtest_SpeedtestDone($) speedtest_SpeedtestDone($)
{ {
my ($string) = @_; my ($string) = @_;
return unless(defined($string)); return unless(defined($string));
my @a = split("\\|",$string); my @a = split("\\|",$string);
my $hash = $defs{$a[0]}; my $name = $a[0];
my $hash = $defs{$name};
delete($hash->{helper}{RUNNING_PID}); delete($hash->{helper}{RUNNING_PID});
@ -155,6 +178,38 @@ speedtest_SpeedtestDone($)
Log3 $hash, 5, "speedtest_SpeedtestDone: $string"; Log3 $hash, 5, "speedtest_SpeedtestDone: $string";
if( $a[1] =~ m/^\{.*\}$/ ) {
if( !$speedtest_hasJSON ) {
Log3 $name, 1, "json needed for ookla speedtest";
return;
}
my $decoded = eval { decode_json($a[1]) };
readingsBeginUpdate($hash);
if( $decoded->{message} ) {
readingsBulkUpdate($hash,"state", "failed" );
readingsBulkUpdate($hash,"lastError", $decoded->{message} );
} else {
readingsBulkUpdate($hash,"ping",$decoded->{ping}{latency}) if( defined( $decoded->{ping}) );
readingsBulkUpdate($hash,"upload",round($decoded->{upload}{bandwidth}*8/1024/1024,1)) if( defined( $decoded->{upload}) );
readingsBulkUpdate($hash,"download",round($decoded->{download}{bandwidth}*8/1024/1024,1)) if( defined( $decoded->{download}) );
readingsBulkUpdate($hash,"packetLoss",$decoded->{packetLoss}) if( defined( $decoded->{packetLoss}) );
readingsBulkUpdate($hash,"id",$decoded->{server}{id}) if( defined( $decoded->{server}) );
readingsBulkUpdate($hash,"name",$decoded->{server}{name}) if( defined( $decoded->{server}) );
readingsBulkUpdate($hash,"location",$decoded->{server}{location}) if( defined( $decoded->{server}) );
readingsBulkUpdate($hash,"state","ok");
}
readingsEndUpdate($hash,1);
return;
}
if( $a[1] eq "Invalid server ID" ) { if( $a[1] eq "Invalid server ID" ) {
readingsSingleUpdate($hash,"state", "failed", 1); readingsSingleUpdate($hash,"state", "failed", 1);
return; return;
@ -242,6 +297,9 @@ speedtest_SpeedtestAborted($)
how often the speedtest should be run before it is automaticaly set to disabled. the value will be decreased by 1 for every run.</li> how often the speedtest should be run before it is automaticaly set to disabled. the value will be decreased by 1 for every run.</li>
<li>disable<br> <li>disable<br>
set to 1 to disable the test.</li> set to 1 to disable the test.</li>
<li>ookla<br>
set to 1 to use the official ookla speedtest cli binary. will be searched in /usr/local/bin if path is not set.</li>
</ul> </ul>
</ul> </ul>