diff --git a/InternetSpeedTest/api/module.php b/InternetSpeedTest/api/module.php new file mode 100644 index 0000000..8623f8c --- /dev/null +++ b/InternetSpeedTest/api/module.php @@ -0,0 +1,175 @@ +request->action) { + case 'clearLogFile': + $this->clearLogFile(); + break; + case 'startSpeedTest': + $this->startSpeedTest(); + break; + case 'getSpeedTestFromFile': + $this->getSpeedTestFromFile(); + break; + case 'getPreviousTests': + $this->getPreviousTests(); + break; + } + } + + // + // log + // this function will write to the log file inside the IST directory + // + private function log($msg) + { + exec("echo {$msg} >> {$this->LOG_FILE}"); + } + + // + // clearLogFile + // this function will wipe the log file inside the IST directory + // + private function clearLogFile() + { + exec("echo '' > {$this->LOG_FILE}"); + } + + // makeSpeedTestDir + // this function will create the directory for speed test outputs + // IFF the directory does not exist + public function makeSpeedTestsDir() + { + exec("mkdir {$this->SPEED_TEST_DIR}"); + } + + // + // touchSpeedTestFile + // this function will create an empty speedtest file + // + public function touchSpeedTestFile($file) + { + if ($file == null) + { + exec("touch {$this->ALL_TESTS_FILE}"); + return "{$this->ALL_TESTS_FILE}"; + } + else + { + exec("touch {$file}"); + return $file; + } + } + + // + // runSpeedTest + // this function will execute the wget command download a 500mb file from softlayer.com + // as and return the output of @getSpeedTestFile + // + public function runSpeedTest($file) + { + $firstcmd = "wget -a ".$file." --output-document=/dev/null https://www.wifipineapple.com/downloads/tetra/2.5.2"; + $secondcmd = "echo Downloading 50 MB of data finished on `tail -n 2 ".$file." | cut -d '-' -f 1,2,3` > ".$file; + $this->log("running new test"); + $this->log("file: ".$file); + $this->log("first command: ".$firstcmd); + $this->log("second command: ".$secondcmd); + + $tsStart = time(); + + // run the 25MB download twice + exec($firstcmd); + exec($firstcmd); + exec($secondcmd); + + $tsTook = time() - $tsStart; + + exec("echo took ".$tsTook." seconds >> ".$file); + + return $this->getSpeedTestFile($file); + } + + // + // getSpeedTestFile + // this function will return the contents of a speed tests file + // + public function getSpeedTestFile($file) + { + $this->log("getSpeedTestFile"); + $this->log($file); + + return file_get_contents($file); + } + + // + // getSpeedTestFromFile + // return the wget output associated with a speed test via file contents + // + public function getSpeedTestFromFile() + { + $this->log("requesting file"); + $this->log($this->request->file); + + $this->makeSpeedTestsDir(); + $file = $this->touchSpeedTestFile($this->request->file); + $output = $this->getSpeedTestFile($file); + $this->response = $output; + + $this->log($output); + } + + // + // addToSpeedTestFile + // this function will add the results of a speedtest to a file + // + public function addToSpeedTestFile($file) + { + exec("echo {$file} >> {$this->ALL_TESTS_FILE}"); + } + + // + // startSpeedTest + // return the output of wget speed test + // + public function startSpeedTest() + { + $this->makeSpeedTestsDir(); + $file = "{$this->SPEED_TEST_DIR}/".date("d-m-Y-h-i-s")."-speedtest"; + $output = $this->runSpeedTest($file); + + $this->addToSpeedTestFile($file); + + $this->response = $output; + } + + // + // getPreviousTests + // return the tests file as an array + // + public function getPreviousTests() + { + + $this->makeSpeedTestsDir(); + $this->touchSpeedTestFile(); + + $this->response = array(); + + $lines = file($this->ALL_TESTS_FILE); + foreach ($lines as $line) + { + array_push($this->response, $line); + } + } +} diff --git a/InternetSpeedTest/js/module.js b/InternetSpeedTest/js/module.js new file mode 100644 index 0000000..c4b9088 --- /dev/null +++ b/InternetSpeedTest/js/module.js @@ -0,0 +1,115 @@ +registerController("InternetSpeedTestController", ['$api', '$scope','$window','$route', '$http', function ($api, $scope, $window, $route, $http) { + + /* + * Author: trashbo4t (github.com/trashbo4t) + */ + getPreviousTests(); + + $scope.previous = []; + $scope.previousDisplay = []; + $scope.throbber = true; + $scope.loading = "Running"; + $scope.working = "Running speed test..."; + $scope.library = true; + $scope.currentSpeedTest = false; + $scope.currentSpeedTestData = {}; + $scope.fileToLookup = ""; + + function getPreviousTests() { + $api.request({ + module: "InternetSpeedTest", + action: "getPreviousTests" + }, function (response) { + console.log("getPreviousTests", response); + + for (var i = 0; i < response.length; i++) + { + var ok = $scope.previous.includes(response[i]) + if (!ok) + { + var res = response[i].split("/"); + $scope.previous.push(response[i]); + var res2 = res[5].split("-speedtest"); + $scope.previousDisplay.push(res2[0]); + } + } + + $scope.previousDisplay.reverse(); + }); + }; + + $scope.reloadPage = function () { + $api.request({ + module: "InternetSpeedTest", + action: "clearLogFile", + }, function (response) { + }); + + $scope.currentSpeedTest = false; + $window.location.reload() + }; + + $scope.getSpeedTestFromFile = function (file) { + file = file.replace(/(\r\n\t|\n|\r\t)/gm,""); + $scope.fileToLookup = "/pineapple/modules/InternetSpeedTest/tests/"+file+"-speedtest"; + + console.log("getSpeedTestFromFile", "looking up speed test file " + $scope.fileToLookup); + + $api.request({ + module: "InternetSpeedTest", + action: "getSpeedTestFromFile", + file: $scope.fileToLookup + }, function (response) { + $scope.currentSpeedTest = $scope.speedTestToLookup; + + if (response == false) + { + $scope.currentSpeedTest = "Failed"; + $scope.currentSpeedTestData = "Invalid filename.."; + } + else + { + $scope.currentSpeedTest = "Success"; + $scope.currentSpeedTestData = response; + } + + console.log("getSpeedTestFromFile response:", $scope.currentSpeedTestData); + $scope.library = false; + }); + }; + + $scope.startSpeedTest = function () { + $scope.loading = "Loading"; + $scope.working = "Your Internet Speed Test is running. Please be patient, this may take a minute to finish depending on your internet speed."; + $scope.throbber = true; + + console.log("startSpeedTest", "starting test..."); + + $api.request({ + module: "InternetSpeedTest", + action: "startSpeedTest", + }, function (response) { + console.log("startSpeedTest", response); + + $scope.currentSpeedTest = ""; + if (response == false) + { + $scope.currentSpeedTest = "Failed"; + $scope.currentSpeedTestData = "Test failed. Verify you are connected to the internet."; + } + else + { + $scope.currentSpeedTest = "Success"; + $scope.currentSpeedTestData = response; + $scope.fileToLookup = "Running speed test"; + } + + // fire up the throbber + $scope.working = "click anywhere to continue"; + $scope.loading = "Done"; + $scope.library = false; + $scope.throbber = false; + }); + + }; +}]); diff --git a/InternetSpeedTest/module.html b/InternetSpeedTest/module.html new file mode 100644 index 0000000..5d0a578 --- /dev/null +++ b/InternetSpeedTest/module.html @@ -0,0 +1,107 @@ + + + + +
| History | + + +
|---|
| + + Speed Test: {{ file }} (Click to view) + + | + +
+ Looks like there are no tests to display... +
+| Information | + + +|
|---|---|
| Result | +{{ currentSpeedTestData }} | +
| + | +