Skip to content

Commit e67fb3e

Browse files
authored
Add PrusaLink as enhanced app (#821)
* Add PrusaLink as enhanced app * increase logo size * fix lint
1 parent 9f15392 commit e67fb3e

5 files changed

Lines changed: 133 additions & 0 deletions

File tree

PrusaLink/PrusaLink.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

PrusaLink/app.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"appid": "e5f6e96e960a08fa8f1fe702b45690cbc789ebc9",
3+
"name": "PrusaLink",
4+
"website": "https://github.com/prusa3d/Prusa-Link-Web/tree/master",
5+
"license": "CNRI Python Open Source GPL Compatible License Agreement",
6+
"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",
7+
"enhanced": true,
8+
"tile_background": "dark",
9+
"icon": "prusalink.svg"
10+
}

PrusaLink/config.blade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items">
3+
<div class="input">
4+
<label>{{ strtoupper(__('app.url')) }}</label>
5+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
6+
</div>
7+
<div class="input">
8+
<label>{{ __('app.apps.apikey') }}</label>
9+
{!! Form::input('password', 'config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
10+
</div>
11+
<div class="input">
12+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
13+
</div>
14+
</div>
15+
<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>

PrusaLink/livestats.blade.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<style>
2+
strong { text-align: left }
3+
span { text-align: left; margin-left: 0px; }
4+
.livestats-container .livestats strong span { margin-left: 0px;}
5+
</style>
6+
<ul class="livestats">
7+
@if ($state != 'OFFLINE')
8+
<li>
9+
<span class="title">Status/ETA</span>
10+
<strong>{!! $state !!}</strong>
11+
<strong>{!! $short_time_remaining !!}</strong>
12+
</li>
13+
<li>
14+
<span class="title">Temps</span>
15+
<strong><span>N: </span>{!! $temp_nozzle !!}</strong>
16+
<strong><span>B: </span>{!! $temp_bed !!}</strong>
17+
</li>
18+
@else
19+
<li>
20+
<strong><span>STATUS: </span>{!! $state !!}</strong>
21+
</li>
22+
@endif
23+
</ul>

PrusaLink/prusalink.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)