speedtest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. trait SpeedTestHomepageItem
  3. {
  4. public function getSpeedtestHomepageData()
  5. {
  6. if (!$this->config['homepageSpeedtestEnabled']) {
  7. $this->setAPIResponse('error', 'SpeedTest homepage item is not enabled', 409);
  8. return false;
  9. }
  10. if (!$this->qualifyRequest($this->config['homepageSpeedtestAuth'])) {
  11. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  12. return false;
  13. }
  14. if (empty($this->config['speedtestURL'])) {
  15. $this->setAPIResponse('error', 'SpeedTest URL is not defined', 422);
  16. return false;
  17. }
  18. $api = [];
  19. $url = $this->qualifyURL($this->config['speedtestURL']);
  20. $dataUrl = $url . '/api/speedtest/latest';
  21. try {
  22. $response = Requests::get($dataUrl);
  23. if ($response->success) {
  24. $json = json_decode($response->body, true);
  25. $api['data'] = [
  26. 'current' => $json['data'],
  27. 'average' => $json['average'],
  28. 'max' => $json['max'],
  29. ];
  30. $api['options'] = [
  31. 'title' => $this->config['speedtestHeader'],
  32. 'titleToggle' => $this->config['speedtestHeaderToggle'],
  33. ];
  34. } else {
  35. $this->setAPIResponse('error', 'SpeedTest connection error', 409);
  36. return false;
  37. }
  38. } catch (Requests_Exception $e) {
  39. $this->writeLog('error', 'Speedtest Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  40. $this->setAPIResponse('error', $e->getMessage(), 500);
  41. return false;
  42. };
  43. $api = isset($api) ? $api : false;
  44. $this->setAPIResponse('success', null, 200, $api);
  45. return $api;
  46. }
  47. }