webui/src/routes/Control.svelte

167 lines
4.1 KiB
Svelte
Raw Normal View History

2023-11-17 00:05:35 +00:00
<script lang="ts">
import { PUBLIC_BASE_URL } from '$lib/config';
2023-11-18 12:55:55 +00:00
import { onDestroy } from 'svelte';
2023-11-19 19:27:22 +00:00
import { _ } from 'svelte-i18n';
2023-11-18 12:55:55 +00:00
import type { Writable } from 'svelte/store';
2023-11-19 19:27:22 +00:00
import {
Button,
Card,
CardBody,
CardHeader,
CardTitle,
Col,
Form,
Input,
Label,
Row
} from 'sveltestrap';
2023-11-17 00:05:35 +00:00
2023-11-19 19:27:22 +00:00
export let settings = {};
export let customText: string;
export let ledColor: string = '#FFCC00';
export let status: Writable<{ leds: [] }>;
let ledStatus = [];
let keepLedsSameColor = false;
2023-11-17 00:05:35 +00:00
2023-11-19 19:27:22 +00:00
const setCustomText = () => {
fetch(`${PUBLIC_BASE_URL}/api/show/text/${customText}`).catch(() => {});
};
2023-11-17 00:05:35 +00:00
2023-11-19 19:27:22 +00:00
const checkSyncLeds = (e: Event) => {
console.log('checksyncleds', keepLedsSameColor);
if (keepLedsSameColor) {
console.log(e.target.value);
2023-11-18 12:55:55 +00:00
2023-11-19 19:27:22 +00:00
ledStatus.forEach((element, i) => {
if (ledStatus[i].hex != e.target_value) {
ledStatus[i].hex = e.target.value;
}
});
}
};
2023-11-18 12:55:55 +00:00
2023-11-19 19:27:22 +00:00
const setLEDcolor = () => {
console.log(`${PUBLIC_BASE_URL}/api/lights/${ledColor}`);
fetch(`${PUBLIC_BASE_URL}/api/lights`, {
headers: {
'Content-Type': 'application/json'
},
method: 'PATCH',
body: JSON.stringify(ledStatus)
}).catch(() => {});
};
2023-11-17 00:05:35 +00:00
2023-11-19 19:27:22 +00:00
const turnOffLeds = () => {
fetch(`${PUBLIC_BASE_URL}/api/lights/off`).catch(() => {});
};
2023-11-17 02:15:23 +00:00
2023-11-19 19:27:22 +00:00
const restartClock = () => {
fetch(`${PUBLIC_BASE_URL}/api/restart`).catch(() => {});
};
2023-11-17 02:15:23 +00:00
2023-11-19 19:27:22 +00:00
const forceFullRefresh = () => {
fetch(`${PUBLIC_BASE_URL}/api/full_refresh`).catch(() => {});
};
2023-11-17 02:15:23 +00:00
2023-11-19 19:27:22 +00:00
let firstLedDataSubscription = () => {};
2023-11-18 12:55:55 +00:00
2023-11-19 19:27:22 +00:00
firstLedDataSubscription = status.subscribe(async (val) => {
if (val && val.leds) {
ledStatus = val.leds.map((obj) => ({ ['hex']: obj['hex'] }));
for (let led of ledStatus) {
if (led['hex'] == '#000000') {
led['hex'] = `#${Math.floor(Math.random() * 16777215)
.toString(16)
.padStart(6, '0')}`;
}
}
2023-11-19 19:27:22 +00:00
firstLedDataSubscription();
}
});
2023-11-18 12:55:55 +00:00
2023-11-19 19:27:22 +00:00
onDestroy(firstLedDataSubscription);
2023-11-17 00:05:35 +00:00
</script>
<Col>
<Card>
<CardHeader>
2023-11-17 02:15:23 +00:00
<CardTitle>{$_('section.control.title', { default: 'Control' })}</CardTitle>
2023-11-17 00:05:35 +00:00
</CardHeader>
<CardBody>
<Form>
<Row>
2023-11-21 15:05:00 +00:00
<Label md={4} for="customText">{$_('section.control.text')}</Label>
<Col md="8">
2023-11-19 19:27:22 +00:00
<Input
type="text"
id="customText"
bind:value={customText}
bsSize="sm"
maxLength={$settings.numScreens}
/>
2023-11-17 00:05:35 +00:00
</Col>
</Row>
2023-11-19 19:27:22 +00:00
<Button color="primary" on:click={setCustomText}>{$_('section.control.showText')}</Button>
2023-11-17 00:05:35 +00:00
</Form>
<hr />
2024-03-11 20:09:15 +00:00
{#if !$settings.disableLeds}
<h3>LEDs</h3>
<Form>
<Row>
<Label md={4} for="ledColorPicker" size="sm">{$_('section.control.ledColor')}</Label>
<Col md="8">
<Row class="justify-content-between">
{#if ledStatus}
{#each ledStatus as led, i}
<Col>
<Input
type="color"
id="ledColorPicker[{i}]"
bind:value={led.hex}
class="mx-auto"
on:change={checkSyncLeds}
/>
</Col>
{/each}
{/if}
</Row>
<Row class="justify-content-between">
<Col>
<Input
bind:checked={keepLedsSameColor}
type="switch"
class="mx-auto"
label={$_('sections.control.keepSameColor')}
/>
</Col>
</Row>
</Col>
</Row>
<Button color="secondary" id="turnOffLedsBtn" on:click={turnOffLeds}
>{$_('section.control.turnOff')}</Button
>
<Button color="primary" on:click={setLEDcolor}>{$_('section.control.setColor')}</Button>
</Form>
<hr />
{/if}
2023-11-19 19:27:22 +00:00
<h3>{$_('section.control.systemInfo')}</h3>
2023-11-17 00:05:35 +00:00
<ul class="small system_info">
2023-11-19 19:27:22 +00:00
<li>{$_('section.control.version')}: {$settings.gitRev}</li>
<li>
{$_('section.control.buildTime')}: {new Date(
$settings.lastBuildTime * 1000
).toLocaleString()}
</li>
2023-11-17 00:05:35 +00:00
<li>IP: {$settings.ip}</li>
2023-11-19 19:27:22 +00:00
<li>{$_('section.control.hostname')}: {$settings.hostname}</li>
2023-11-17 00:05:35 +00:00
</ul>
2023-11-19 19:27:22 +00:00
<Button color="danger" id="restartBtn" on:click={restartClock}>{$_('button.restart')}</Button>
<Button color="warning" id="forceFullRefresh" on:click={forceFullRefresh}
>{$_('button.forceFullRefresh')}</Button
>
2023-11-17 00:05:35 +00:00
</CardBody>
</Card>
</Col>