Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions PrusaLink/PrusaLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\SupportedApps\PrusaLink;

use Carbon\CarbonInterval;

class PrusaLink 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 __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

public function test()
{
$test = parent::appTest($this->url('api/v1/status'), $this->getAttrs());
echo $test->status;
}

public function livestats()
{
$status = 'inactive';
$res = parent::execute($this->url('api/v1/status'), $this->getAttrs());
$details = json_decode($res->getBody());

// Check if time_remaining exists and convert it
if (isset($details->job->time_remaining)) {
$short_time_remaining = $this->secondsToShortTime($details->job->time_remaining);
} else {
$short_time_remaining = "N/A";
}

if (isset($details->printer->temp_bed)) {
$temp_bed = $details->printer->temp_bed . " °C"; // Append "°C"
} else {
$temp_bed = "N/A"; // Default value
}

if (isset($details->printer->temp_nozzle)) {
$temp_nozzle = $details->printer->temp_nozzle . " °C"; // Append "°C"
} else {
$temp_nozzle = "N/A"; // Default value
}

$data = [
"state" => $details->printer->state ?? "OFFLINE", // Default state as "OFFLINE"
"short_time_remaining" => $short_time_remaining,
"temp_nozzle" => $temp_nozzle,
"temp_bed" => $temp_bed
];

return parent::getLiveStats($status, $data);
}

private function secondsToShortTime($seconds) {
return CarbonInterval::seconds($seconds)
->cascade()
->forHumans([
'short' => true,
'join' => ' ', // Use a space instead of "and" or ","
'maximumUnit' => 2
]);
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}

private function getAttrs()
{
return [
"headers" => [
"X-Api-Key" => $this->config->apikey
],
];
}
}
10 changes: 10 additions & 0 deletions PrusaLink/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"appid": "e5f6e96e960a08fa8f1fe702b45690cbc789ebc9",
"name": "PrusaLink",
"website": "https://github.com/prusa3d/Prusa-Link-Web/tree/master",
"license": "CNRI Python Open Source GPL Compatible License Agreement",
"description": "Website for Prusa Printers to Control and get the current Status of the printer\r\nhttps://github.com/prusa3d/Prusa-Link-Web/blob/master/spec/openapi.yaml",
"enhanced": true,
"tile_background": "dark",
"icon": "prusalink.svg"
}
15 changes: 15 additions & 0 deletions PrusaLink/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.apikey') }}</label>
{!! Form::input('password', 'config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
<label style="text-decoration:none;margin:20px;">To obtain the API key: On your printer, navigate to Settings > Network > Prusalink to get the username and password. The API key is the password shown on this screen.</label>
23 changes: 23 additions & 0 deletions PrusaLink/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<style>
strong { text-align: left }
span { text-align: left; margin-left: 0px; }
.livestats-container .livestats strong span { margin-left: 0px;}
</style>
<ul class="livestats">
@if ($state != 'OFFLINE')
<li>
<span class="title">Status/ETA</span>
<strong>{!! $state !!}</strong>
<strong>{!! $short_time_remaining !!}</strong>
</li>
<li>
<span class="title">Temps</span>
<strong><span>N: </span>{!! $temp_nozzle !!}</strong>
<strong><span>B: </span>{!! $temp_bed !!}</strong>
</li>
@else
<li>
<strong><span>STATUS: </span>{!! $state !!}</strong>
</li>
@endif
</ul>
1 change: 1 addition & 0 deletions PrusaLink/prusalink.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.