Files
dmxconnect/frontend/src/runtime-events.js

201 lines
6.5 KiB
JavaScript
Raw Normal View History

import { EventsOn, EventsOff } from "../wailsjs/runtime/runtime.js"
import { peripherals, generateToast, needProjectSave, showInformation } from './stores'
import { get } from "svelte/store"
import { _ } from 'svelte-i18n'
// New peripheral has been added to the system
function peripheralArrival (peripheralInfo){
// If not exists, add it to the map
// isDetected key to true
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[peripheralInfo.SerialNumber]: {
...storedPeripherals[peripheralInfo.SerialNumber],
Name: peripheralInfo.Name,
ProtocolName: peripheralInfo.ProtocolName,
SerialNumber: peripheralInfo.SerialNumber,
Settings: peripheralInfo.Settings,
isDetected: true,
},
}})
console.log("Hardware has been added to the system");
generateToast('info', 'bxs-hdd', get(_)("peripheralArrivalToast") + ' <b>' + peripheralInfo.Name + '</b>')
}
// Peripheral is removed from the system
function peripheralRemoval (peripheralInfo){
// If not exists, add it to the map
// isDetected key to false
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[peripheralInfo.SerialNumber]: {
...storedPeripherals[peripheralInfo.SerialNumber],
Name: peripheralInfo.Name,
ProtocolName: peripheralInfo.ProtocolName,
SerialNumber: peripheralInfo.SerialNumber,
Settings: peripheralInfo.Settings,
isDetected: false,
status: "PERIPHERAL_DISCONNECTED",
},
}})
console.log("Hardware has been removed from the system");
generateToast('warning', 'bxs-hdd', get(_)("peripheralRemovalToast") + ' <b>' + peripheralInfo.Name + '</b>')
}
// Update peripheral status
function peripheralUpdateStatus(peripheralInfo, status){
// If not exists, add it to the map
// change status key
peripherals.update((storedPeripherals) => {
console.log(status)
return {
...storedPeripherals,
[peripheralInfo.SerialNumber]: {
...storedPeripherals[peripheralInfo.SerialNumber],
Name: peripheralInfo.Name,
ProtocolName: peripheralInfo.ProtocolName,
SerialNumber: peripheralInfo.SerialNumber,
Settings: peripheralInfo.Settings,
status: status,
},
}})
console.log("Hardware status has been updated to " + status);
}
// Load the peripheral in the project
function loadPeripheral (peripheralInfo) {
// If not exists, add it to the map
// isSaved key to true
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[peripheralInfo.SerialNumber]: {
...storedPeripherals[peripheralInfo.SerialNumber],
Name: peripheralInfo.Name,
ProtocolName: peripheralInfo.ProtocolName,
SerialNumber: peripheralInfo.SerialNumber,
Settings: peripheralInfo.Settings,
isSaved: true,
},
}})
console.log("Hardware has been added to the project");
//TODO: Lors d'un chargement/déchargement natif au démarrage, il ne doit pas y avoir de nécessité de sauvegarder
needProjectSave.set(true)
}
function loadProject (showInfo){
// Store project information
showInformation.set(showInfo)
console.log("Project has been opened");
generateToast('info', 'bx-folder-open', get(_)("projectOpenedToast") + ' <b>' + showInfo.Name + '</b>')
}
// Unload the hardware from the project
function unloadPeripheral (peripheralInfo) {
// If not exists, add it to the map
// isSaved key to false
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[peripheralInfo.SerialNumber]: {
...storedPeripherals[peripheralInfo.SerialNumber],
Name: peripheralInfo.Name,
ProtocolName: peripheralInfo.ProtocolName,
SerialNumber: peripheralInfo.SerialNumber,
Settings: peripheralInfo.Settings,
isSaved: false,
},
}})
console.log("Hardware has been removed from the project");
//TODO: Lors d'un chargement/déchargement natif au démarrage, il ne doit pas y avoir de nécessité de sauvegarder
needProjectSave.set(true)
}
// A peripheral event has been emitted
function onPeripheralEvent(sn, event) {
// If not exists, add it to the map
// eventEmitted key to true for 0.2 sec
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[sn]: {
...storedPeripherals[sn],
eventEmitted: true
},
}})
setTimeout(() => {
peripherals.update((storedPeripherals) => {
return {
...storedPeripherals,
[sn]: {
...storedPeripherals[sn],
eventEmitted: false
},
}})
}, 200);
}
let initialized = false
export function initRuntimeEvents(){
if (initialized) return
initialized = true
// Handle the event when a new peripheral is detected
EventsOn('PERIPHERAL_ARRIVAL', peripheralArrival)
// Handle the event when a peripheral is removed from the system
EventsOn('PERIPHERAL_REMOVAL', peripheralRemoval)
// Handle the event when a peripheral status is updated
EventsOn('PERIPHERAL_STATUS', peripheralUpdateStatus)
// Handle the event when a new project need to be loaded
EventsOn('LOAD_PROJECT', loadProject)
// Handle a peripheral loaded in the project
EventsOn('LOAD_PERIPHERAL', loadPeripheral)
// Handle a peripheral unloaded from the project
EventsOn('UNLOAD_PERIPHERAL', unloadPeripheral)
// Handle a peripheral event
EventsOn('PERIPHERAL_EVENT_EMITTED', onPeripheralEvent)
}
export function destroyRuntimeEvents(){
if (!initialized) return
initialized = false
// Handle the event when a new peripheral is detected
EventsOff('PERIPHERAL_ARRIVAL')
// Handle the event when a peripheral is removed from the system
EventsOff('PERIPHERAL_REMOVAL')
// Handle the event when a peripheral status is updated
EventsOff('PERIPHERAL_STATUS')
// Handle the event when a new project need to be loaded
EventsOff('LOAD_PROJECT')
// Handle a peripheral loaded in the project
EventsOff('LOAD_PERIPHERAL')
// Handle a peripheral unloaded from the project
EventsOff('UNLOAD_PERIPHERAL')
// Handle a peripheral event
EventsOff('PERIPHERAL_EVENT_EMITTED')
}