refactorings + basis of script editor.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
<script lang="ts" context="module">
|
||||
import { dev } from "$app/environment";
|
||||
import { fetchUIState } from "$lib/client/lucifer";
|
||||
import type { DeviceIconName } from "$lib/components/DeviceIcon.svelte";
|
||||
import type Assignment from "$lib/models/assignment";
|
||||
import type Device from "$lib/models/device";
|
||||
import type { UIStatePatch } from "$lib/models/uistate";
|
||||
import type { UIStatePatch, UIStatePatch2 } from "$lib/models/uistate";
|
||||
import type UIState from "$lib/models/uistate";
|
||||
import { getContext, onMount, setContext } from "svelte";
|
||||
import { derived, writable, type Readable } from "svelte/store";
|
||||
@@ -110,136 +111,61 @@
|
||||
}
|
||||
|
||||
async function connectSocket() {
|
||||
let url = `ws://${window.location.host}/subscribe`;
|
||||
let url = `ws://${window.location.host}/subscribe-simple`;
|
||||
if (import.meta.env.VITE_LUCIFER4_BACKEND_URL != null) {
|
||||
url = import.meta.env.VITE_LUCIFER4_BACKEND_URL.replace("http", "ws") +"/subscribe";
|
||||
url = import.meta.env.VITE_LUCIFER4_BACKEND_URL.replace("http", "ws") +"/subscribe-simple";
|
||||
}
|
||||
|
||||
const currSocket = new WebSocket(url);
|
||||
|
||||
currSocket.onmessage = (msg) => {
|
||||
const patches: UIStatePatch[] = JSON.parse(msg.data);
|
||||
const patch: UIStatePatch2 = JSON.parse(msg.data);
|
||||
|
||||
state.update(s => {
|
||||
for (const patch of patches) {
|
||||
if (patch.device) {
|
||||
if (patch.device.delete) {
|
||||
const devices = {...s.devices};
|
||||
delete devices[patch.device.id];
|
||||
s = {...s, devices};
|
||||
} else {
|
||||
s = {
|
||||
...s,
|
||||
devices: {
|
||||
...s.devices,
|
||||
[patch.device.id]: ((patch) => {
|
||||
if (patch.addAlias) {
|
||||
const aliases = [...(s.devices[patch.id].aliases || [])];
|
||||
const exclPrefix = ["lucifer:icon:", "lucifer:group:", "lucifer:name:"].find(p => patch.addAlias?.startsWith(p));
|
||||
const exclExisting = exclPrefix && aliases.find(a => a.startsWith(exclPrefix));
|
||||
state.update(state => {
|
||||
if (patch.full != null) {
|
||||
state = patch.full;
|
||||
}
|
||||
|
||||
if (patch.addAlias.startsWith("lucifer:name:")) {
|
||||
patch.name = patch.addAlias.slice("lucifer:name:".length)
|
||||
}
|
||||
if (patch.addAlias.startsWith("lucifer:icon:")) {
|
||||
patch.icon = patch.addAlias.slice("lucifer:icon:".length) as DeviceIconName
|
||||
}
|
||||
|
||||
if (exclExisting) {
|
||||
return {
|
||||
...s.devices[patch.id],
|
||||
aliases: [...aliases.filter(a => a != exclExisting), patch.addAlias].sort(),
|
||||
name: patch.name || s.devices[patch.id].name,
|
||||
icon: patch.icon || s.devices[patch.id].icon,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
...s.devices[patch.id],
|
||||
aliases: [...aliases, patch.addAlias].sort(),
|
||||
name: patch.name || s.devices[patch.id].name,
|
||||
icon: patch.icon || s.devices[patch.id].icon,
|
||||
};
|
||||
}
|
||||
} else if (patch.removeAlias != null) {
|
||||
const aliases = [...(s.devices[patch.id].aliases || [])];
|
||||
return {
|
||||
...s.devices[patch.id],
|
||||
aliases: aliases.filter(a => a !== patch.removeAlias),
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
...s.devices[patch.id],
|
||||
...patch,
|
||||
};
|
||||
}
|
||||
})(patch.device)
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const deviceId in patch.devices) {
|
||||
if (!patch.devices.hasOwnProperty(deviceId)) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (patch.assignment) {
|
||||
if (patch.assignment.delete) {
|
||||
const assignments = {...s.assignments};
|
||||
delete assignments[patch.assignment.id];
|
||||
s = {...s, assignments};
|
||||
} else if (patch.assignment.addDeviceId) {
|
||||
s = {
|
||||
...s,
|
||||
assignments: {
|
||||
...s.assignments,
|
||||
[patch.assignment.id]: {
|
||||
...s.assignments[patch.assignment.id],
|
||||
deviceIds: [...s.assignments[patch.assignment.id].deviceIds || [], patch.assignment.addDeviceId],
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (patch.assignment.removeDeviceId) {
|
||||
s = {
|
||||
...s,
|
||||
assignments: {
|
||||
...s.assignments,
|
||||
[patch.assignment.id]: {
|
||||
...s.assignments[patch.assignment.id],
|
||||
deviceIds: s.assignments[patch.assignment.id].deviceIds.map(id => id === patch.assignment.removeDeviceId ? "" : id),
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s = {
|
||||
...s,
|
||||
assignments: {
|
||||
...s.assignments,
|
||||
[patch.assignment.id]: {
|
||||
...s.assignments[patch.assignment.id],
|
||||
...patch.assignment,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (patch.script) {
|
||||
if (patch.script.delete) {
|
||||
const scripts = {...s.scripts};
|
||||
delete scripts[patch.script.id];
|
||||
s = {...s, scripts};
|
||||
} else {
|
||||
s = {
|
||||
...s,
|
||||
scripts: {
|
||||
...s.scripts,
|
||||
[patch.script.id]: {
|
||||
...s.scripts[patch.script.id],
|
||||
...patch.script,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
state.devices = {...state.devices}
|
||||
if (patch.devices[deviceId] != null) {
|
||||
state.devices[deviceId] = patch.devices[deviceId]
|
||||
} else {
|
||||
delete state.devices[deviceId]
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
|
||||
for (const assignmentId in patch.assignments) {
|
||||
if (!patch.assignments.hasOwnProperty(assignmentId)) {
|
||||
continue
|
||||
}
|
||||
|
||||
state.assignments = {...state.assignments}
|
||||
if (patch.assignments[assignmentId] != null) {
|
||||
state.assignments[assignmentId] = patch.assignments[assignmentId]
|
||||
} else {
|
||||
delete state.assignments[assignmentId]
|
||||
}
|
||||
}
|
||||
|
||||
for (const scriptName in patch.scripts) {
|
||||
if (!patch.scripts.hasOwnProperty(scriptName)) {
|
||||
continue
|
||||
}
|
||||
|
||||
state.scripts = {...state.scripts}
|
||||
if (patch.scripts[scriptName] != null) {
|
||||
state.scripts[scriptName] = patch.scripts[scriptName]
|
||||
} else {
|
||||
delete state.scripts[scriptName]
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
})
|
||||
}
|
||||
|
||||
@@ -266,15 +192,11 @@
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
reload();
|
||||
const interval = setInterval(reload, 60000);
|
||||
|
||||
connectSocket();
|
||||
|
||||
window.addEventListener("visibilitychange", () => {
|
||||
if (document.visibilityState == "visible") {
|
||||
console.log("Reconnecting");
|
||||
reload();
|
||||
connectSocket();
|
||||
} else {
|
||||
if (socket != null) {
|
||||
@@ -283,8 +205,6 @@
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return () => clearInterval(interval);
|
||||
});
|
||||
|
||||
setContext<StateContextData>(ctxKey, {
|
||||
|
||||
Reference in New Issue
Block a user