diagnostics: add tptest_stop functionality
[juci.git] / plugins / juci-diagnostics / src / widgets / diagnostics-widget-speedtest.js
index adc1051..36b326c 100644 (file)
@@ -12,23 +12,28 @@ JUCI.app
 .controller("diagnosticsWidget90Speedtest", function($scope, $rpc, $events, $uci, utilsAddTestserverPicker, $tr, gettext){
        $scope.data = {
                test_type: "up_down",
-               result: "",
+               result: [],
                state: "",
-               manually: false
-       }; 
-       
-       function getDefaultPacketSize(){
-               $rpc.$call("juci.speedtest", "get_packet_size").done(function(data){
-                       $scope.data.downsize = parseInt(data.packetsize_down);
-                       $scope.data.upsize = parseInt(data.packetsize_up);
-                       $scope.$apply();
-               }).fail(function(e){ console.log(e); });
+               auto: true
+       };
+
+       function refresh(){
+               var def = $.Deferred();
+               $rpc.$call("juci.diagnostics", "tptest_running").done(function(res){
+                       if(!res){
+                               def.reject();
+                               return;
+                       }
+                       if(res.running){
+                               $scope.data.state = "running";
+                       }
+                       else if($scope.data.state !== "error"){
+                               $scope.data.state = "";
+                       }
+                       def.resolve();
+               }).fail(function(e){def.reject(e);});
+               return def.promise();
        }
-       getDefaultPacketSize();
-       
-       $scope.$watch('data.manually', function(new_value){
-               if(new_value === true){ getDefaultPacketSize(); }
-       }, false);
 
        function getServers(){
                $scope.allTestServers = $scope.testServers.map(function(x){
@@ -39,12 +44,35 @@ JUCI.app
                });
                if($scope.allTestServers.length)
                        $scope.data.server = $scope.allTestServers[0].value;
+               else
+                       $scope.data.server = null;
+       }
+
+       JUCI.interval.repeat("diagnostics-tptest-running", 10000, function(next){
+               refresh().done(function(){
+                       $scope.$apply();
+               }).always(function(){
+                       next();
+               });
+       });
+
+       $scope.removeResult = function(res){
+               if(!res) return;
+               $scope.data.result = $scope.data.result.filter(function(r){
+                       return r !== res;
+               });
        }
 
+       $rpc.$call("juci.diagnostics", "tptest_packet_size").done(function(data){
+               $scope.data.downsize = parseInt(data.packetsize_down);
+               $scope.data.upsize = parseInt(data.packetsize_up);
+               $scope.$apply();
+       }).fail(function(e){ console.log(e); });
+
        $scope.testType = [
-               {value:"down", label: $tr(gettext("Down")) },
-               {value:"up", label: $tr(gettext("Up")) }, 
-               {value:"up_down", label: $tr(gettext("Down and Up")) }
+               {value:"down", label: $tr(gettext("Download")) },
+               {value:"up", label: $tr(gettext("Upload")) },
+               {value:"up_down", label: $tr(gettext("Download/Upload")) }
        ];
 
        $uci.$sync("speedtest").done(function(){
@@ -54,50 +82,60 @@ JUCI.app
        });
 
        $scope.runTest = function(){
-               if($scope.data.test_type.indexOf("up") !== -1 && !$scope.data.upsize){ alert("Upstream packet size missing"); return; }
-               if($scope.data.test_type.indexOf("down") !==-1 && !$scope.data.downsize){ alert("Downstream packet size missing"); return; }
+               if($scope.data.state === "running") return;
+               if($scope.data.test_type.match(/up/) && !$scope.data.upsize){ alert("Upstream packet size missing"); return; }
+               if($scope.data.test_type.match(/down/) && !$scope.data.downsize){ alert("Downstream packet size missing"); return; }
                if(!$scope.testServers.length){
                        window.alert($tr(gettext("Server and port is mandatory")));
                        return;
                }
-               if($scope.data.state == "running"){
-                       window.alert($tr(gettext("Only one test can be run at a time")));
-                       return;
-               }
                var server = $scope.testServers.find(function(x){ return $scope.data.server == x.server.value;});
                var port = server.port.value;
                var address = server.server.value;
                $scope.data.state="running";
                var speedtestArgs = {
-                       "auto": !$scope.data.manually,
+                       "auto": $scope.data.auto,
                        "testmode": $scope.data.test_type,
                        "port": port,
-                       "address": address
+                       "host": address,
                };
 
-               if($scope.data.test_type.indexOf("up") !== -1){
+               if($scope.data.test_type.match(/up/) && !$scope.data.auto){
                        speedtestArgs.packetsize_up = $scope.data.upsize * 1000000;
                }
-               if($scope.data.test_type.indexOf("down") !== -1){
+               if($scope.data.test_type.match(/down/) && !$scope.data.auto){
                        speedtestArgs.packetsize_down = $scope.data.downsize * 1000000;
                }
 
-               $rpc.$call("juci.speedtest", "run", speedtestArgs).done(function(response){
+               $rpc.$call("juci.diagnostics", "tptest_start", speedtestArgs).done(function(response){
                        if(response && response.message=="success"){
                                $scope.data.state="running";
                        }else{
                                $scope.data.state="";
                        }
                        $scope.$apply();
+               }).fail(function(e){
+                       console.log(e);
+                       $scope.data.state = "";
+                       $scope.$apply();
                });
-               setTimeout(function(){
-                       if($scope.data.state === "running"){
-                               $scope.data.state = "error";
-                               $scope.data.result = $tr(gettext("Unknown error"));
+       };
+
+       $scope.abortTest = function(){
+               $rpc.$call("juci.diagnostics", "tptest_stop").done(function(response){
+                       if(response && response.message=="success"){
+                               $scope.data.state="aborted";
+                       }else{
+                               $scope.data.state="";
                        }
-               }, 15000);
+                       $scope.$apply();
+               }).fail(function(e){
+                       console.log(e);
+                       $scope.data.state = "";
+                       $scope.$apply();
+               });
        };
-       
+
        $scope.onRemoveAddress = function(){
                var server = $scope.testServers.find(function(x){
                        return $scope.data.server == x.server.value
@@ -125,7 +163,7 @@ JUCI.app
                        });
                });
        }
-       $events.subscribe("juci.utils.speedtest", function(res){
+       $events.subscribe("diagnostics.speedtest", function(res){
                if(res.data && res.data.status != undefined){
                        switch(res.data.status) {
                        case 0:
@@ -142,26 +180,24 @@ JUCI.app
                                        downstream = downstream / 1000 / 1000;
                                }
                                if(res.data.upstream != "none" && res.data.downstream != "none"){
-                                       $scope.data.result=$tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " " + $tr(gettext("Mbit/s\nUpstream:"))+" " + upstream.toFixed(2) +" "+$tr(gettext("Mbit/s"));
+                                       $scope.data.result.push($tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " " + $tr(gettext("Mbit/s\nUpstream:"))+" " + upstream.toFixed(2) +" "+$tr(gettext("Mbit/s")));
                                }else if(res.data.upstream != "none"){
-                                       $scope.data.result=$tr(gettext("Upstream:"))+" " + upstream.toFixed(2) + " "+$tr(gettext("Mbit/s"));
+                                       $scope.data.result.push($tr(gettext("Upstream:"))+" " + upstream.toFixed(2) + " "+$tr(gettext("Mbit/s")));
                                }else if(res.data.downstream != "none"){
-                                       $scope.data.result=$tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " "+$tr(gettext("Mbit/s"));
+                                       $scope.data.result.push($tr(gettext("Downstream:"))+" " + downstream.toFixed(2) + " "+$tr(gettext("Mbit/s")));
                                }else {
-                                       $scope.data.result="No speeds found";
+                                       $scope.data.result.push($tr(gettext("No speeds found")));
                                }
                                $scope.data.state="result";
                                break;
                        case -1:
-                               $scope.data.result=$tr(gettext("Wrong TP-test address and/or port"));
-                               $scope.data.state="error";
-                               break;
-                       case -2:
-                               $scope.data.result=$tr(gettext("Wrong TP-test port but correct address"));
+                               $scope.data.result.push($tr(gettext("Wrong TP-test address and/or port")));
                                $scope.data.state="error";
                                break;
                        }
-                       $scope.$apply();
+                       refresh().always(function(){
+                               $scope.$apply();
+                       });
                }
        });
-}); 
+});