|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\SupportedApps\PrusaLink; |
| 4 | + |
| 5 | +use Carbon\CarbonInterval; |
| 6 | + |
| 7 | +class PrusaLink extends \App\SupportedApps implements \App\EnhancedApps |
| 8 | +{ |
| 9 | + public $config; |
| 10 | + |
| 11 | + //protected $login_first = true; // Uncomment if api requests need to be authed first |
| 12 | + //protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 17 | + } |
| 18 | + |
| 19 | + public function test() |
| 20 | + { |
| 21 | + $test = parent::appTest($this->url('api/v1/status'), $this->getAttrs()); |
| 22 | + echo $test->status; |
| 23 | + } |
| 24 | + |
| 25 | + public function livestats() |
| 26 | + { |
| 27 | + $status = 'inactive'; |
| 28 | + $res = parent::execute($this->url('api/v1/status'), $this->getAttrs()); |
| 29 | + $details = json_decode($res->getBody()); |
| 30 | + |
| 31 | + // Check if time_remaining exists and convert it |
| 32 | + if (isset($details->job->time_remaining)) { |
| 33 | + $short_time_remaining = $this->secondsToShortTime($details->job->time_remaining); |
| 34 | + } else { |
| 35 | + $short_time_remaining = "N/A"; |
| 36 | + } |
| 37 | + |
| 38 | + if (isset($details->printer->temp_bed)) { |
| 39 | + $temp_bed = $details->printer->temp_bed . " °C"; // Append "°C" |
| 40 | + } else { |
| 41 | + $temp_bed = "N/A"; // Default value |
| 42 | + } |
| 43 | + |
| 44 | + if (isset($details->printer->temp_nozzle)) { |
| 45 | + $temp_nozzle = $details->printer->temp_nozzle . " °C"; // Append "°C" |
| 46 | + } else { |
| 47 | + $temp_nozzle = "N/A"; // Default value |
| 48 | + } |
| 49 | + |
| 50 | + $data = [ |
| 51 | + "state" => $details->printer->state ?? "OFFLINE", // Default state as "OFFLINE" |
| 52 | + "short_time_remaining" => $short_time_remaining, |
| 53 | + "temp_nozzle" => $temp_nozzle, |
| 54 | + "temp_bed" => $temp_bed |
| 55 | + ]; |
| 56 | + |
| 57 | + return parent::getLiveStats($status, $data); |
| 58 | + } |
| 59 | + |
| 60 | + private function secondsToShortTime($seconds) { |
| 61 | + return CarbonInterval::seconds($seconds) |
| 62 | + ->cascade() |
| 63 | + ->forHumans([ |
| 64 | + 'short' => true, |
| 65 | + 'join' => ' ', // Use a space instead of "and" or "," |
| 66 | + 'maximumUnit' => 2 |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + public function url($endpoint) |
| 71 | + { |
| 72 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 73 | + return $api_url; |
| 74 | + } |
| 75 | + |
| 76 | + private function getAttrs() |
| 77 | + { |
| 78 | + return [ |
| 79 | + "headers" => [ |
| 80 | + "X-Api-Key" => $this->config->apikey |
| 81 | + ], |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments