From dbac4a8766e3ffb7427ea306ae9f419ff1b0395d Mon Sep 17 00:00:00 2001 From: trashbo4t Date: Sat, 13 Apr 2019 11:39:36 -0400 Subject: [PATCH] first commit --- InternetSpeedTest/api/module.php | 175 +++++++++++++++++++++++++++++++ InternetSpeedTest/js/module.js | 115 ++++++++++++++++++++ InternetSpeedTest/module.html | 107 +++++++++++++++++++ InternetSpeedTest/module.info | 10 ++ 4 files changed, 407 insertions(+) create mode 100644 InternetSpeedTest/api/module.php create mode 100644 InternetSpeedTest/js/module.js create mode 100644 InternetSpeedTest/module.html create mode 100644 InternetSpeedTest/module.info 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... +

+
+ +
+ +
+

Internet Speed Test Result: {{ currentSpeedTest }}

+ +
+ + + + + + + + + + + + + + + + +
Information
Result {{ currentSpeedTestData }}
+
+
+
+
+
+
+
+
+ + + + diff --git a/InternetSpeedTest/module.info b/InternetSpeedTest/module.info new file mode 100644 index 0000000..48d447b --- /dev/null +++ b/InternetSpeedTest/module.info @@ -0,0 +1,10 @@ +{ + "author": "trashbo4t", + "devices": [ + "nano", + "tetra" + ], + "title": "Internet Speed Test", + "description": "Test the download and upload speed of the network shared to your pineapple", + "version": "1.0" +}