From 061c26fb2c06f97f1092d3b3359640b51a2276db Mon Sep 17 00:00:00 2001 From: justme-1968 Date: Mon, 24 Jun 2013 21:00:10 +0000 Subject: [PATCH] new module speedtest to monitor internet connection speed with speedtest-cli git-svn-id: https://svn.fhem.de/fhem/trunk@3324 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/32_speedtest.pm | 244 +++++++++++++++++++++++++++++ fhem/CHANGED | 1 + fhem/MAINTAINER.txt | 1 + fhem/docs/commandref_frame.html | 1 + fhem/docs/commandref_frame_DE.html | 1 + 5 files changed, 248 insertions(+) create mode 100755 fhem/32_speedtest.pm diff --git a/fhem/32_speedtest.pm b/fhem/32_speedtest.pm new file mode 100755 index 000000000..d69b47cea --- /dev/null +++ b/fhem/32_speedtest.pm @@ -0,0 +1,244 @@ + +package main; + +use strict; +use warnings; +use Blocking; + +sub +speedtest_Initialize($) +{ + my ($hash) = @_; + + $hash->{DefFn} = "speedtest_Define"; + $hash->{UndefFn} = "speedtest_Undefine"; + $hash->{SetFn} = "speedtest_Set"; + $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6 ". + "checks-till-disable ". + "disable:0,1 ". + "path ". + $readingFnAttributes; +} + +##################################### + +sub +speedtest_Define($$) +{ + my ($hash, $def) = @_; + + my @a = split("[ \t][ \t]*", $def); + + return "Usage: define speedtest [interval [server]]" if(@a < 2); + + my $name = $a[0]; + + my $interval = 60*60; + $interval = $a[2] if(int(@a)>=3); + $interval = 30*60 if( $interval < 30*60 ); + + my $server = $a[3] if(int(@a)>=4); + + delete( $hash->{SERVER} ); + + $hash->{NAME} = $name; + + $hash->{STATE} = "Initialized"; + $hash->{INTERVAL} = $interval; + + $hash->{SERVER} = $server if( defined( $server ) ); + + RemoveInternalTimer($hash); + InternalTimer(gettimeofday()+$hash->{INTERVAL}, "speedtest_GetUpdate", $hash, 0); + + return undef; +} + +sub +speedtest_Undefine($$) +{ + my ($hash, $arg) = @_; + + RemoveInternalTimer($hash); + + BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID})); + + return undef; +} + +sub +speedtest_Set($$@) +{ + my ($hash, $name, $cmd) = @_; + + if($cmd eq 'statusRequest') { + $hash->{LOCAL} = 1; + speedtest_GetUpdate($hash); + $hash->{LOCAL} = 0; + return undef; + } + + my $list = "statusRequest"; + return "Unknown argument $cmd, choose one of $list"; +} + + + +sub +speedtest_GetUpdate($) +{ + my ($hash) = @_; + my $name = $hash->{NAME}; + + if(!$hash->{LOCAL}) { + RemoveInternalTimer($hash); + InternalTimer(gettimeofday()+$hash->{INTERVAL}, "speedtest_GetUpdate", $hash, 1); + } + + my $server =""; + $server = $hash->{SERVER} if( defined($hash->{SERVER}) ); + + if( !$hash->{LOCAL} ) { + return undef if( AttrVal($name, "disable", 0 ) == 1 ); + + my $checks = AttrVal($name, "checks-till-disable", undef ); + if( defined($checks) ) + { + $checks -= 1; + $attr{$name}{"checks-till-disable"} = max(0,$checks); + + $attr{$name}{"disable"} = 1 if( $checks <= 0 ); + } + } + + $hash->{helper}{RUNNING_PID} = BlockingCall("speedtest_DoSpeedtest", $name."|".$server, "speedtest_SpeedtestDone", 120, "speedtest_SpeedtestAborted", $hash) unless(exists($hash->{helper}{RUNNING_PID})); +} + + +sub +speedtest_DoSpeedtest($) +{ + my ($string) = @_; + my ($name, $server) = split("\\|", $string); + + my $cmd = AttrVal($name, "path", "/usr/local/speedtest-cli" ); + $cmd .= "/speedtest-cli --simple"; + $cmd .= "--server $server" if( $server ); + + Log GetLogLevel($name, 5), "starting speedtest"; + my $speedstr = qx($cmd); + Log GetLogLevel($name, 5), "speedtest done"; + my @speedarr = split(/\n/, $speedstr); + + for( my $i = 0; $i < 3; ++$i ) + { + $speedarr[$i] = $1 if( $speedarr[$i] && $speedarr[$i] =~ m/^\w+: (.*)/i ); + } + + return "$name|$speedarr[0]|$speedarr[1]|$speedarr[2]"; +} +sub +speedtest_SpeedtestDone($) +{ + my ($string) = @_; + + return unless(defined($string)); + + my @a = split("\\|",$string); + my $hash = $defs{$a[0]}; + + delete($hash->{helper}{RUNNING_PID}); + + return if($hash->{helper}{DISABLED}); + + Log GetLogLevel($hash->{NAME}, 5), "speedtest_SpeedtestDone: $string"; + + readingsBeginUpdate($hash); + + readingsBulkUpdate($hash,"ping",$a[1]); + readingsBulkUpdate($hash,"download",$a[2]); + readingsBulkUpdate($hash,"upload",$a[3]); + + readingsEndUpdate($hash,1); +} +sub +speedtest_SpeedtestAborted($) +{ + my ($hash) = @_; + + delete($hash->{helper}{RUNNING_PID}); +} + +1; + +=pod +=begin html + + +

speedtest

+ + +=end html +=cut diff --git a/fhem/CHANGED b/fhem/CHANGED index 224b2d775..6ef642984 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII - SVN + - feature: new module speedtest to monitor internet connection speed with speedtest-cli - feature: new module "remotecontrol" to display a graphical remotecontrol for any device - feature: HUEDevice: new attribute color-icons to colored svg icons diff --git a/fhem/MAINTAINER.txt b/fhem/MAINTAINER.txt index 44f057421..adea7dad1 100644 --- a/fhem/MAINTAINER.txt +++ b/fhem/MAINTAINER.txt @@ -83,6 +83,7 @@ FHEM/30_HUEBridge.pm justme1968 http://forum.fhem.de Sonstige FHEM/31_HUEDevice.pm justme1968 http://forum.fhem.de Sonstige Systeme FHEM/31_LightScene.pm justme1968 http://forum.fhem.de Automatisierung FHEM/32_SYSSTAT.pm justme1968 http://forum.fhem.de Unterstützende Dienste +FHEM/32_speedtest.pm justme1968 http://forum.fhem.de Sonstiges FHEM/40_RFXCOM.pm wherzig http://forum.fhem.de RFXTRX FHEM/41_OREGON.pm wherzig http://forum.fhem.de Sonstiges FHEM/42_RFXMETER.pm wherzig http://forum.fhem.de RFXTRX diff --git a/fhem/docs/commandref_frame.html b/fhem/docs/commandref_frame.html index 398d73e5b..8450993db 100644 --- a/fhem/docs/commandref_frame.html +++ b/fhem/docs/commandref_frame.html @@ -99,6 +99,7 @@ remotecontrol   SUNRISE_EL   sequence   + speedtest   structure   telnet   Twilight   diff --git a/fhem/docs/commandref_frame_DE.html b/fhem/docs/commandref_frame_DE.html index a532c273d..d60287d4b 100644 --- a/fhem/docs/commandref_frame_DE.html +++ b/fhem/docs/commandref_frame_DE.html @@ -99,6 +99,7 @@ remotecontrol   SUNRISE_EL   sequence   + speedtest   structure   telnet   Twilight