UPD: Type update event
This commit is contained in:
@@ -121,29 +121,29 @@ app.whenReady().then(() => {
|
||||
ipcMain.on('electron:openExternal', (_event, url) => shell.openExternal(url))
|
||||
ipcMain.on('electron:openDevTools', () => mainWindow.webContents.toggleDevTools())
|
||||
ipcMain.on('electron:reload', () => mainWindow.webContents.reloadIgnoringCache())
|
||||
nanoSerialApi.on('nanoSerialApi:device-attached', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:device-attached', (deviceid, data) => {
|
||||
console.log('Attached event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-attached', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-attached', deviceid, data)
|
||||
})
|
||||
nanoSerialApi.on('nanoSerialApi:device-detached', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:device-detached', (deviceid, data) => {
|
||||
console.log('Detached event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-detached', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-detached', deviceid, data)
|
||||
})
|
||||
nanoSerialApi.on('nanoSerialApi:device-error', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:device-error', (deviceid, data) => {
|
||||
console.log('Error event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-error', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'device-error', deviceid, data)
|
||||
})
|
||||
nanoSerialApi.on('nanoSerialApi:connected', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:connected', (deviceid, data) => {
|
||||
console.log('Connected event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'connected', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'connected', deviceid, data)
|
||||
})
|
||||
nanoSerialApi.on('nanoSerialApi:disconnected', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:disconnected', (deviceid, data) => {
|
||||
console.log('Disconnected event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'disconnected', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'disconnected', deviceid, data)
|
||||
})
|
||||
nanoSerialApi.on('nanoSerialApi:update', (deviceid, ...data) => {
|
||||
nanoSerialApi.on('nanoSerialApi:update', (deviceid, data) => {
|
||||
console.log('Update event', deviceid, data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'update', deviceid, ...data)
|
||||
mainWindow.webContents.send('nanoSerialApi:event', 'update', deviceid, data)
|
||||
})
|
||||
const menu = new Menu()
|
||||
for (const menuItem of Object.values(appMenu)) {
|
||||
|
||||
2
src/preload/index.d.ts
vendored
2
src/preload/index.d.ts
vendored
@@ -2,7 +2,7 @@ export interface INanoSerialApi {
|
||||
listAttachedDevices(): Promise<string[]>
|
||||
connect(deviceid: string): Promise<string>
|
||||
disconnect(deviceid: string): Promise<string>
|
||||
on(callback: (eventid: string, deviceid: string, data: any) => void): void
|
||||
on(callback: (eventid: string, deviceid: string, data: string) => void): void
|
||||
send(deviceid: string, jsonstr: string): Promise<void>
|
||||
save(deviceid: string): Promise<void>
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ contextBridge.exposeInMainWorld('nanoIpc', {
|
||||
return ipcRenderer.invoke('nanoSerialApi:disconnect', deviceid)
|
||||
},
|
||||
on(callback) {
|
||||
ipcRenderer.on('nanoSerialApi:event', (_event, eventid, deviceid, ...data) => {
|
||||
callback(eventid, deviceid, ...data)
|
||||
ipcRenderer.on('nanoSerialApi:event', (_event, eventid, deviceid, data) => {
|
||||
callback(eventid, deviceid, data)
|
||||
})
|
||||
},
|
||||
send(deviceid, obj) {
|
||||
|
||||
@@ -36,6 +36,12 @@ interface Profile {
|
||||
guiEnable: boolean
|
||||
}
|
||||
|
||||
interface UpdateData {
|
||||
a: number | undefined
|
||||
t: number | undefined
|
||||
v: number | undefined
|
||||
}
|
||||
|
||||
const { nanoIpc } = window
|
||||
|
||||
export const useDeviceStore = defineStore('device', {
|
||||
@@ -118,8 +124,14 @@ export const initializeDevices = () => {
|
||||
const deviceStore = useDeviceStore()
|
||||
|
||||
// register event handlers
|
||||
nanoIpc.on((eventid, deviceid, ...data) => {
|
||||
console.log('Received event', eventid, deviceid, data)
|
||||
nanoIpc.on((eventid, deviceid, dataString) => {
|
||||
console.log('Received event', eventid, deviceid, dataString)
|
||||
let update: UpdateData = {} as UpdateData
|
||||
try {
|
||||
update = JSON.parse(dataString) as UpdateData
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
if (eventid === 'device-attached') {
|
||||
deviceStore.attachDevice(deviceid)
|
||||
if (deviceStore.attachedDeviceIds.length === 1) {
|
||||
@@ -129,15 +141,15 @@ export const initializeDevices = () => {
|
||||
if (eventid === 'device-detached') {
|
||||
deviceStore.detachDevice(deviceid)
|
||||
}
|
||||
if (eventid === 'device-connected') {
|
||||
if (eventid === 'connected') {
|
||||
deviceStore.connectDevice(deviceid, false)
|
||||
}
|
||||
if (eventid === 'device-disconnected') {
|
||||
if (eventid === 'disconnected') {
|
||||
deviceStore.disconnectDevice(deviceid, false)
|
||||
}
|
||||
if (eventid === 'update') {
|
||||
if ('a' in data) {
|
||||
deviceStore.setAngle(data.a as number)
|
||||
if (update.a) {
|
||||
deviceStore.setAngle(update.a)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user