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") + ' ' + peripheralInfo.Name + '') } // 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") + ' ' + peripheralInfo.Name + '') } // 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") + ' ' + showInfo.Name + '') } // 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) } 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) } 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') }