webui/src/routes/Status.svelte

156 lines
4.8 KiB
Svelte
Raw Normal View History

2023-11-17 00:05:35 +00:00
<script lang="ts">
import { PUBLIC_BASE_URL } from '$env/static/public';
import { onMount } from 'svelte';
import { _ } from 'svelte-i18n';
import { writable } from 'svelte/store';
2023-11-17 21:04:28 +00:00
import { Row, Input, Button, ButtonGroup, Card, CardBody, CardHeader, Col, Progress,CardTitle } from 'sveltestrap';
2023-11-17 00:05:35 +00:00
import Rendered from './Rendered.svelte';
2023-11-17 12:12:22 +00:00
export let settings;
2023-11-17 00:05:35 +00:00
const status = writable({
data: ["L", "O", "A", "D", "I", "N", "G"],
espFreeHeap: 0,
espHeapSize: 0,
connectionStatus: {
"price": false,
"blocks": false
}
});
onMount(() => {
fetch(`${PUBLIC_BASE_URL}/api/status`)
.then((res) => res.json())
.then((data) => {
status.set(data);
});
const evtSource = new EventSource(`${PUBLIC_BASE_URL}/events`);
evtSource.addEventListener('status', (e) => {
let dataObj = (JSON.parse(e.data));
status.set(dataObj);
});
});
const toTime = (secs:Number) => {
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
var obj = {
"h": hours,
"m": minutes,
"s": seconds
};
return obj;
}
const toUptimeString = (secs:Number):String => {
let time = toTime(secs);
return `${time.h}h ${time.m}m ${time.s}s`;
}
let memoryFreePercent:number = 50;
2023-11-17 12:12:22 +00:00
let lightMode:boolean = false;
2023-11-17 00:05:35 +00:00
status.subscribe((value) => {
memoryFreePercent = Math.round(value.espFreeHeap / value.espHeapSize * 100);
});
2023-11-17 12:12:22 +00:00
settings.subscribe((value) => {
lightMode = value.bgColor > value.fgColor;
});
2023-11-17 00:05:35 +00:00
const setScreen = (id:number) => () => {
fetch(`${PUBLIC_BASE_URL}/api/show/screen/${id}`).catch(err => { });
}
const toggleTimer = (currentStatus:boolean) => () => {
if (currentStatus) {
fetch(`${PUBLIC_BASE_URL}/api/action/pause`);
} else {
fetch(`${PUBLIC_BASE_URL}/api/action/timer_restart`);
}
}
2023-11-17 12:12:22 +00:00
const isLightMode = () => {
return $settings.bgColor > $settings.fgColor;
}
2023-11-17 00:05:35 +00:00
</script>
<Col>
<Card>
<CardHeader>
2023-11-17 02:15:23 +00:00
<CardTitle>{$_('section.status.title', { default: 'Status' })}</CardTitle>
2023-11-17 00:05:35 +00:00
</CardHeader>
<CardBody>
{#if $settings.screens}
2023-11-17 12:23:26 +00:00
<div class="d-flex justify-content-center">
<ButtonGroup size="sm">
{#each $settings.screens as s}
<Button color="outline-primary" active={$status.currentScreen == s.id} on:click={setScreen(s.id)}>{s.name}</Button>
{/each}
</ButtonGroup>
</div>
2023-11-17 00:05:35 +00:00
<hr>
{#if $status.data}
2023-11-17 12:12:22 +00:00
<section class={lightMode ? 'lightMode': ''}>
<Rendered status="{$status}"></Rendered>
</section>
{ $_('section.status.screenCycle') }: <a style="cursor: pointer" on:click="{toggleTimer($status.timerRunning)}">{#if $status.timerRunning}&#9205; { $_('timer.running') }{:else}&#9208; { $_('timer.stopped') }{/if}</a>
2023-11-17 00:05:35 +00:00
{/if}
{/if}
<hr>
2023-11-17 21:04:28 +00:00
<Row class="justify-content-evenly">
{#if $status.leds}
{#each $status.leds as led}
<Col>
<Input type="color" id="ledColorPicker" bind:value="{led.hex}" class="mx-auto" disabled />
</Col>
{/each}
{/if}
</Row>
<hr>
2023-11-17 00:05:35 +00:00
<Progress striped value={memoryFreePercent}>{ memoryFreePercent }%</Progress>
<div class="d-flex justify-content-between">
2023-11-17 12:12:22 +00:00
<div>{ $_('section.status.memoryFree') } </div>
2023-11-17 00:05:35 +00:00
<div>{ Math.round($status.espFreeHeap / 1024) } / { Math.round($status.espHeapSize / 1024) } KiB</div>
</div>
<hr>
2023-11-17 12:12:22 +00:00
{ $_('section.status.uptime') }: {toUptimeString($status.espUptime)}
2023-11-17 00:05:35 +00:00
<br>
<p>
2023-11-17 12:12:22 +00:00
{ $_('section.status.wsPriceConnection') }:
2023-11-17 00:05:35 +00:00
<span>
{#if $status.connectionStatus && $status.connectionStatus.price}
&#9989;
{:else}
&#10060;
{/if}
</span>
-
2023-11-17 12:12:22 +00:00
{ $_('section.status.wsMempoolConnection') }:
2023-11-17 00:05:35 +00:00
<span>
{#if $status.connectionStatus && $status.connectionStatus.blocks}
&#9989;
{:else}
&#10060;
{/if}
</span><br>
2023-11-17 02:15:23 +00:00
{#if $settings.fetchEurPrice}
2023-11-17 12:12:22 +00:00
<small>{ $_('section.status.fetchEuroNote') }</small>
2023-11-17 02:15:23 +00:00
{/if}
2023-11-17 00:05:35 +00:00
</p>
</CardBody>
</Card>
</Col>