-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathSpeedtestTracker.php
More file actions
106 lines (87 loc) · 2.91 KB
/
SpeedtestTracker.php
File metadata and controls
106 lines (87 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace App\SupportedApps\SpeedtestTracker;
use DateTimeZone;
class SpeedtestTracker extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;
//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
public function getRequestAttrs()
{
$api_token = $this->config->apikey;
$attrs = [
"headers" => [
"Accept" => "application/json",
"Authorization" => "Bearer " . $api_token,
],
];
return $attrs;
}
public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}
public function test()
{
$attrs = $this->getRequestAttrs();
$test = parent::appTest($this->url("api/v1/results/latest/"), $attrs);
echo $test->status;
}
public function livestats()
{
$status = "inactive";
$attrs = $this->getRequestAttrs();
$res = parent::execute($this->url("api/v1/results/latest/"), $attrs);
$details = json_decode($res->getBody());
$data = [];
if ($details) {
$data["ping"] = number_format($details->data->ping);
$data["download"] = number_format($details->data->download, 1);
$data["upload"] = number_format($details->data->upload, 1);
foreach ($this->config->availablestats as $stat) {
if (!isset(self::getAvailableStats()[$stat])) {
continue;
}
$newstat = new \stdClass();
$newstat->title = self::getAvailableStats()[$stat];
$newstat->value = self::formatUsingStat(
$stat,
$details->data->{$stat}
);
$data["visiblestats"][] = $newstat;
}
}
return parent::getLiveStats($status, $data);
}
public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}
public static function getAvailableStats()
{
return [
"ping" => "Ping",
"download" => "Down",
"upload" => "Up",
"created_at" => "Time",
];
}
private static function formatUsingStat($stat, $number)
{
if (!isset($number)) {
return "N/A";
}
switch ($stat) {
case "download":
case "upload":
return number_format($number * 8 / 1000000) . "<span>Mbit/s</span>";
case "ping":
return number_format($number) . "<span>ms</span>";
case "created_at":
return (new \DateTime($number))->format("H:i");
default:
return $number;
}
}
}