move websocket globally and modify mainn page

This commit is contained in:
Adrian Zuercher
2025-05-06 05:33:54 +02:00
parent 488ce84ae9
commit a059d282ed
9 changed files with 176 additions and 101 deletions

View File

@@ -11,7 +11,7 @@ export default defineConfig((/* ctx */) => {
// app boot file (/src/boot) // app boot file (/src/boot)
// --> boot files are part of "main.js" // --> boot files are part of "main.js"
// https://v2.quasar.dev/quasar-cli-vite/boot-files // https://v2.quasar.dev/quasar-cli-vite/boot-files
boot: [], boot: ['websocket'],
// https://v2.quasar.dev/quasar-cli-vite/quasar-config-file#css // https://v2.quasar.dev/quasar-cli-vite/quasar-config-file#css
css: ['app.scss'], css: ['app.scss'],

21
src/boot/websocket.ts Normal file
View File

@@ -0,0 +1,21 @@
import { boot } from 'quasar/wrappers';
import type { QVueGlobals } from 'quasar';
import { initWebSocket } from 'src/services/websocket';
export default boot(({ app }) => {
const $q = app.config.globalProperties.$q as QVueGlobals;
const ws = initWebSocket('ws://127.0.0.1:8100/ws?id=quasar', $q);
app.config.globalProperties.$socket = ws;
ws.connect();
});
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$socket: {
connect: () => void;
close: () => void;
socket: WebSocket | null;
};
}
}

View File

@@ -57,17 +57,14 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted } from 'vue'; import { onMounted } from 'vue';
import { useWebSocket } from 'src/composables/useWebSocket';
import type { TreeNode } from 'src/composables/dbmTree'; import type { TreeNode } from 'src/composables/dbmTree';
import { subs, dbmData, buildTree } from 'src/composables/dbmTree'; import { subs, dbmData, buildTree } from 'src/composables/dbmTree';
import { openContextMenu } from 'src/composables/useContextMenu'; import { openContextMenu } from 'src/composables/useContextMenu';
import SubMenu from 'src/components/SubMenu.vue'; import SubMenu from 'src/components/SubMenu.vue';
import { QCard } from 'quasar'; import { QCard } from 'quasar';
import { send } from 'src/services/websocket';
const { connect, send } = useWebSocket('ws://127.0.0.1:8100/ws?id=quasar');
onMounted(() => { onMounted(() => {
connect();
send({ send({
subscribe: [ subscribe: [
{ {

View File

@@ -101,11 +101,9 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { watch, onMounted, reactive } from 'vue'; import { watch, reactive } from 'vue';
import { useWebSocket } from 'src/composables/useWebSocket';
import type { Light } from 'src/models/Light'; import type { Light } from 'src/models/Light';
import { send } from 'src/services/websocket';
const { connect, send } = useWebSocket('ws://127.0.0.1:8100/ws?id=quasar');
const light = reactive<Light>({ const light = reactive<Light>({
State: false, State: false,
@@ -118,10 +116,6 @@ const light = reactive<Light>({
Purple: 0, Purple: 0,
}); });
onMounted(() => {
connect();
});
watch(light, (newVal: Light) => { watch(light, (newVal: Light) => {
send({ send({
set: [ set: [

View File

@@ -10,7 +10,7 @@ select
<div class="q-pa-md"> <div class="q-pa-md">
<q-card> <q-card>
<q-card-section class="q-mt-md q-mr-sm row items-start"> <q-card-section class="q-mt-md q-mr-sm row items-start">
<div class="column justify-center q-mr-lg" style="height: 200px"> <div class="column justify-center q-ma-lg" style="height: 200px">
<q-btn <q-btn
@click="light.State = !light.State" @click="light.State = !light.State"
round round
@@ -22,7 +22,7 @@ select
<q-item-label class="text-bold">Dimmer</q-item-label> <q-item-label class="text-bold">Dimmer</q-item-label>
<q-slider <q-slider
label label
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.Brightness" v-model="light.Brightness"
@@ -34,7 +34,7 @@ select
/> />
<q-item-label class="text-bold">Red</q-item-label> <q-item-label class="text-bold">Red</q-item-label>
<q-slider <q-slider
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.Red" v-model="light.Red"
@@ -47,7 +47,7 @@ select
/> />
<q-item-label class="text-bold">Green</q-item-label> <q-item-label class="text-bold">Green</q-item-label>
<q-slider <q-slider
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.Green" v-model="light.Green"
@@ -60,7 +60,7 @@ select
/> />
<q-item-label class="text-bold">Blue</q-item-label> <q-item-label class="text-bold">Blue</q-item-label>
<q-slider <q-slider
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.Blue" v-model="light.Blue"
@@ -71,9 +71,9 @@ select
color="blue" color="blue"
style="opacity: 0.8" style="opacity: 0.8"
/> />
<q-item-label class="text-bold">White</q-item-label> <q-item-label class="text-bold items-center">White</q-item-label>
<q-slider <q-slider
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.White" v-model="light.White"
@@ -86,7 +86,7 @@ select
/> />
<q-item-label class="text-bold">Zoom</q-item-label> <q-item-label class="text-bold">Zoom</q-item-label>
<q-slider <q-slider
class="q-mr-lg" class="q-ma-lg"
vertical vertical
reverse reverse
v-model="light.Zoom" v-model="light.Zoom"
@@ -97,6 +97,7 @@ select
color="black" color="black"
style="opacity: 1" style="opacity: 1"
/> />
<div class="row q-ma-sm">
<q-item-label class="text-bold">Tilt</q-item-label> <q-item-label class="text-bold">Tilt</q-item-label>
<q-slider <q-slider
class="q-mr-sm" class="q-mr-sm"
@@ -143,17 +144,18 @@ select
style="opacity: 1" style="opacity: 1"
/> />
</div> </div>
</div>
</q-card-section> </q-card-section>
</q-card> </q-card>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch, onMounted, reactive, computed } from 'vue'; import { ref, watch, reactive, computed, onMounted } from 'vue';
import { useWebSocket } from 'src/composables/useWebSocket';
import type { MovingHead } from 'src/models/MovingHead'; import type { MovingHead } from 'src/models/MovingHead';
import { send } from 'src/services/websocket';
import { subs, dbmData, buildTree } from 'src/composables/dbmTree';
const { connect, send } = useWebSocket('ws://127.0.0.1:8100/ws?id=quasar');
const pad = ref<HTMLElement | null>(null); const pad = ref<HTMLElement | null>(null);
const dragging = ref(false); const dragging = ref(false);
@@ -169,10 +171,6 @@ const light = reactive<MovingHead>({
Tilt: 50, Tilt: 50,
}); });
onMounted(() => {
connect();
});
const markerStyle = computed(() => ({ const markerStyle = computed(() => ({
position: 'absolute' as const, position: 'absolute' as const,
top: `${2 * (100 - light.Tilt)}px`, top: `${2 * (100 - light.Tilt)}px`,
@@ -186,6 +184,71 @@ const markerStyle = computed(() => ({
transform: 'translate(-50%, -50%)', transform: 'translate(-50%, -50%)',
})); }));
onMounted(() => {
LoadData();
});
function LoadData() {
send({
subscribe: [
{
path: 'MovingHead',
depth: 2,
},
],
})
.then((response) => {
if (response?.subscribe) {
subs.value = response.subscribe;
dbmData.value = buildTree(subs.value);
} else {
console.log('Response from server:', response);
}
})
.catch((err) => {
console.error('Error fetching data:', err);
});
send({
get: [
{
path: 'MovingHead',
query: { depth: 2 },
},
],
})
.then((response) => {
if (response?.get) {
const pathToLightKey: Record<string, keyof typeof light> = {
'MovingHead:Dimmer': 'Brightness',
'MovingHead:State': 'State',
'MovingHead:Red': 'Red',
'MovingHead:Green': 'Green',
'MovingHead:Blue': 'Blue',
'MovingHead:White': 'White',
'MovingHead:Zoom': 'Zoom',
'MovingHead:Pan': 'Pan',
'MovingHead:Tilt': 'Tilt',
};
Object.entries(pathToLightKey).forEach(([path, key]) => {
const sub = subs.value.find((s) => s.path === path);
if (sub) {
if (key === 'State') {
light[key] = Boolean(sub.value);
} else {
light[key] = Math.round((100 / 255) * Number(sub.value));
}
}
});
} else {
console.log('Response from server:', response);
}
})
.catch((err) => {
console.error('Error fetching data:', err);
});
}
function startDrag(e: MouseEvent) { function startDrag(e: MouseEvent) {
dragging.value = true; dragging.value = true;
updatePosition(e); updatePosition(e);
@@ -222,85 +285,89 @@ function updatePosition(e: MouseEvent | Touch) {
light.Pan = Math.round((newX / rect.width) * 100); light.Pan = Math.round((newX / rect.width) * 100);
light.Tilt = Math.round(100 - (newY / rect.height) * 100); light.Tilt = Math.round(100 - (newY / rect.height) * 100);
console.log(light.Pan, light.Tilt);
} }
watch(light, (newVal: MovingHead) => { watch(light, (newVal: MovingHead) => {
send({ send({
set: [ set: [
{
// State
path: 'MovingHead:State',
value: Number(newVal.State),
},
{ {
// Red // Red
path: 'MovingHead:001:Red', path: 'MovingHead:Red',
value: Math.round((255 / 100) * newVal.Red * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Red * Number(newVal.State)),
}, },
{ {
//Red fine //Red fine
path: 'MovingHead:001:RedFine', path: 'MovingHead:RedFine',
value: Math.round((255 / 100) * newVal.Red * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Red * Number(newVal.State)),
}, },
{ {
// Green // Green
path: 'MovingHead:001:Green', path: 'MovingHead:Green',
value: Math.round((255 / 100) * newVal.Green * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Green * Number(newVal.State)),
}, },
{ {
// Green fine // Green fine
path: 'MovingHead:001:GreenFine', path: 'MovingHead:GreenFine',
value: Math.round((255 / 100) * newVal.Green * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Green * Number(newVal.State)),
}, },
{ {
// Blue // Blue
path: 'MovingHead:001:Blue', path: 'MovingHead:Blue',
value: Math.round((255 / 100) * newVal.Blue * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Blue * Number(newVal.State)),
}, },
{ {
// Blue fine // Blue fine
path: 'MovingHead:001:BlueFine', path: 'MovingHead:BlueFine',
value: Math.round((255 / 100) * newVal.Blue * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Blue * Number(newVal.State)),
}, },
{ {
// White // White
path: 'MovingHead:001:White', path: 'MovingHead:White',
value: Math.round((255 / 100) * newVal.White * Number(newVal.State)), value: Math.round((255 / 100) * newVal.White * Number(newVal.State)),
}, },
{ {
// White fine // White fine
path: 'MovingHead:001:WhiteFine', path: 'MovingHead:WhiteFine',
value: Math.round((255 / 100) * newVal.White * newVal.Brightness * Number(newVal.State)), value: Math.round((255 / 100) * newVal.White * newVal.Brightness * Number(newVal.State)),
}, },
{ {
// Dimmer // Dimmer
path: 'MovingHead:001:Dimmer', path: 'MovingHead:Brightness',
value: Math.round((255 / 100) * newVal.Brightness * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Brightness * Number(newVal.State)),
}, },
{ {
// Dimmer fine // Dimmer fine
path: 'MovingHead:001:DimmerFine', path: 'MovingHead:BrightnessFine',
value: Math.round((255 / 100) * newVal.Brightness * Number(newVal.State)), value: Math.round((255 / 100) * newVal.Brightness * Number(newVal.State)),
}, },
{ {
// Zoom // Zoom
path: 'MovingHead:001:Zoom', path: 'MovingHead:Zoom',
value: Math.round((255 / 100) * newVal.Zoom), value: Math.round((255 / 100) * newVal.Zoom),
}, },
{ {
// Pan // Pan
path: 'MovingHead:001:Pan', path: 'MovingHead:Pan',
value: Math.round((255 / 100) * newVal.Pan), value: Math.round((255 / 100) * newVal.Pan),
}, },
{ {
// Pan fine // Pan fine
path: 'MovingHead:001:PanFine', path: 'MovingHead:PanFine',
value: Math.round((255 / 100) * newVal.Pan), value: Math.round((255 / 100) * newVal.Pan),
}, },
{ {
// Tilt // Tilt
path: 'MovingHead:001:Tilt', path: 'MovingHead:Tilt',
value: Math.round((255 / 100) * newVal.Tilt), value: Math.round((255 / 100) * newVal.Tilt),
}, },
{ {
// Tilt fine // Tilt fine
path: 'MovingHead:001:TiltFine', path: 'MovingHead:TiltFine',
value: Math.round((255 / 100) * newVal.Tilt), value: Math.round((255 / 100) * newVal.Tilt),
}, },
], ],

View File

@@ -13,8 +13,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { contextMenu } from 'src/composables/useContextMenu'; import { contextMenu } from 'src/composables/useContextMenu';
import { useWebSocket } from 'src/composables/useWebSocket'; import { send } from 'src/services/websocket';
const { send } = useWebSocket('ws://127.0.0.1:8100/ws?id=quasar');
function handleAction(action: string) { function handleAction(action: string) {
console.log(`Action '${action}' on node:`, contextMenu.value.node); console.log(`Action '${action}' on node:`, contextMenu.value.node);

View File

@@ -1,4 +1,4 @@
type Set = { export type Set = {
path: string; path: string;
value: number | undefined; value: number | undefined;
}; };

View File

@@ -1,11 +1,9 @@
<template> <template>
<q-page> <q-page>
<light-component />
<moving-head></moving-head> <moving-head></moving-head>
</q-page> </q-page>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import LightComponent from 'src/components/LightComponent.vue';
import MovingHead from 'src/components/MovingHead.vue'; import MovingHead from 'src/components/MovingHead.vue';
</script> </script>

View File

@@ -1,18 +1,16 @@
import type { Response } from 'src/models/Response'; import type { Response } from 'src/models/Response';
import type { Publish } from 'src/models/Publish'; import type { Publish } from 'src/models/Publish';
import type { Request } from 'src/models/Request'; import type { Request } from 'src/models/Request';
import type { QVueGlobals } from 'quasar';
import { subs, buildTree, dbmData } from 'src/composables/dbmTree'; import { subs, buildTree, dbmData } from 'src/composables/dbmTree';
import { onBeforeUnmount, ref } from 'vue'; import { ref } from 'vue';
import { useQuasar } from 'quasar';
const pendingResponses = new Map<string, (data: Response | undefined) => void>(); const pendingResponses = new Map<string, (data: Response | undefined) => void>();
let socket: WebSocket | null = null; export let socket: WebSocket | null = null;
const isConnected = ref(false); const isConnected = ref(false);
export function useWebSocket(url: string) { export function initWebSocket(url: string, $q?: QVueGlobals) {
const $q = useQuasar();
const connect = () => { const connect = () => {
socket = new WebSocket(url); socket = new WebSocket(url);
@@ -22,22 +20,24 @@ export function useWebSocket(url: string) {
}; };
socket.onclose = () => { socket.onclose = () => {
isConnected.value = false; isConnected.value = false;
$q.notify({ console.log('WebSocket disconnected');
$q?.notify({
message: 'WebSocket disconnected', message: 'WebSocket disconnected',
color: 'orange', color: 'orange',
position: 'bottom-right', position: 'bottom-right',
icon: 'warning', icon: 'warning',
timeout: 5000, timeout: 10000,
}); });
}; };
socket.onerror = (err) => { socket.onerror = (err) => {
console.log(`WebSocket error: ${err.type}`);
isConnected.value = false; isConnected.value = false;
$q.notify({ $q?.notify({
message: `WebSocket error: ${err.type}`, message: `WebSocket error: ${err.type}`,
color: 'red', color: 'red',
position: 'bottom-right', position: 'bottom-right',
icon: 'error', icon: 'error',
timeout: 5000, timeout: 10000,
}); });
}; };
socket.onmessage = (event) => { socket.onmessage = (event) => {
@@ -67,13 +67,12 @@ export function useWebSocket(url: string) {
} }
}; };
onBeforeUnmount(() => { // onBeforeUnmount(() => {
close(); // close();
}); // });
return { return {
connect, connect,
send,
close, close,
socket, socket,
}; };
@@ -102,7 +101,7 @@ function waitForSocketConnection(): Promise<void> {
}); });
} }
function send(data: Request): Promise<Response | undefined> { export function send(data: Request): Promise<Response | undefined> {
const id = generateId(); const id = generateId();
const payload = { ...data, id }; const payload = { ...data, id };