UPD: Show actual device profiles 🤩
This commit is contained in:
@@ -107,7 +107,9 @@ app.whenReady().then(() => {
|
|||||||
ipcMain.handle('nanoSerialApi:list_devices', () => nanoSerialApi.list_devices())
|
ipcMain.handle('nanoSerialApi:list_devices', () => nanoSerialApi.list_devices())
|
||||||
ipcMain.handle('nanoSerialApi:connect', (event, deviceid) => nanoSerialApi.connect(deviceid))
|
ipcMain.handle('nanoSerialApi:connect', (event, deviceid) => nanoSerialApi.connect(deviceid))
|
||||||
ipcMain.handle('nanoSerialApi:disconnect', () => nanoSerialApi.disconnect)
|
ipcMain.handle('nanoSerialApi:disconnect', () => nanoSerialApi.disconnect)
|
||||||
ipcMain.handle('nanoSerialApi:send', (event, ...data) => nanoSerialApi.send(data[0], data[1]))
|
ipcMain.handle('nanoSerialApi:send', (event, ...data) =>
|
||||||
|
nanoSerialApi.send(data[0], JSON.parse(data[1]))
|
||||||
|
)
|
||||||
const mainWindow = createMainWindow()
|
const mainWindow = createMainWindow()
|
||||||
ipcMain.on('electron:minimizeWindow', () => mainWindow.minimize())
|
ipcMain.on('electron:minimizeWindow', () => mainWindow.minimize())
|
||||||
ipcMain.on('electron:toggleMaximizeWindow', () => {
|
ipcMain.on('electron:toggleMaximizeWindow', () => {
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ class NanoSerialApi extends EventEmitter {
|
|||||||
if (connected_port === undefined) {
|
if (connected_port === undefined) {
|
||||||
return Promise.reject('Device not connected')
|
return Promise.reject('Device not connected')
|
||||||
} else {
|
} else {
|
||||||
|
console.log('Sending:', jsonstr)
|
||||||
connected_port.port.write(jsonstr + '\n')
|
connected_port.port.write(jsonstr + '\n')
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ interface UpdateData {
|
|||||||
a: number | undefined
|
a: number | undefined
|
||||||
t: number | undefined
|
t: number | undefined
|
||||||
v: number | undefined
|
v: number | undefined
|
||||||
|
profiles: string[] | undefined
|
||||||
|
current: string | undefined
|
||||||
|
profile: Profile | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const { nanoIpc } = window
|
const { nanoIpc } = window
|
||||||
@@ -67,7 +70,10 @@ export const useDeviceStore = defineStore('device', {
|
|||||||
profileTags: (state) => state.profiles.map((profile) => profile.profileTag),
|
profileTags: (state) => state.profiles.map((profile) => profile.profileTag),
|
||||||
profilesByTag: (state) =>
|
profilesByTag: (state) =>
|
||||||
state.profiles.reduce((acc, profile) => {
|
state.profiles.reduce((acc, profile) => {
|
||||||
acc[profile.profileTag] = profile
|
if (!acc[profile.profileTag]) {
|
||||||
|
acc[profile.profileTag] = []
|
||||||
|
}
|
||||||
|
acc[profile.profileTag].push(profile)
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {})
|
||||||
},
|
},
|
||||||
@@ -80,16 +86,37 @@ export const useDeviceStore = defineStore('device', {
|
|||||||
this.attachedDeviceIds.push(deviceId)
|
this.attachedDeviceIds.push(deviceId)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
addProfile(profile: Profile, updateDevice: boolean = true) {
|
||||||
|
if (!this.profileNames.includes(profile.name)) {
|
||||||
|
this.profileNames.push(profile.name)
|
||||||
|
}
|
||||||
|
const existingProfile = this.profiles.find((p) => p.name === profile.name)
|
||||||
|
if (existingProfile) {
|
||||||
|
Object.assign(existingProfile, profile)
|
||||||
|
} else {
|
||||||
|
this.profiles.push(profile)
|
||||||
|
}
|
||||||
|
if (updateDevice) {
|
||||||
|
nanoIpc.send(
|
||||||
|
this.currentDeviceId!,
|
||||||
|
JSON.stringify({ profile: profile.name, updates: profile })
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
detachDevice(deviceId: string) {
|
detachDevice(deviceId: string) {
|
||||||
const index = this.attachedDeviceIds.indexOf(deviceId)
|
const index = this.attachedDeviceIds.indexOf(deviceId)
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.attachedDeviceIds.splice(index, 1)
|
this.attachedDeviceIds.splice(index, 1)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
connectDevice(deviceId: string, updateDevice: boolean = true) {
|
connectDevice(deviceId: string | undefined = undefined, updateDevice: boolean = true) {
|
||||||
this.currentDeviceId = deviceId
|
if (deviceId) {
|
||||||
if (updateDevice) {
|
this.currentDeviceId = deviceId
|
||||||
nanoIpc.connect(deviceId)
|
if (updateDevice) {
|
||||||
|
nanoIpc.connect(deviceId)
|
||||||
|
}
|
||||||
|
} else if (this.attachedDeviceIds.length > 0) {
|
||||||
|
this.connectDevice(this.attachedDeviceIds[0])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
disconnectDevice(deviceId: string, updateDevice: boolean = true) {
|
disconnectDevice(deviceId: string, updateDevice: boolean = true) {
|
||||||
@@ -98,6 +125,12 @@ export const useDeviceStore = defineStore('device', {
|
|||||||
nanoIpc.disconnect(deviceId)
|
nanoIpc.disconnect(deviceId)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
setProfileNames(profileNames: string[], updateDevice: boolean = true) {
|
||||||
|
this.profileNames = profileNames
|
||||||
|
if (updateDevice) {
|
||||||
|
nanoIpc.send(this.currentDeviceId!, JSON.stringify({ profiles: profileNames }))
|
||||||
|
}
|
||||||
|
},
|
||||||
setCurrentProfile(profileName: string, updateDevice: boolean = true) {
|
setCurrentProfile(profileName: string, updateDevice: boolean = true) {
|
||||||
this.currentProfileName = profileName
|
this.currentProfileName = profileName
|
||||||
if (updateDevice) {
|
if (updateDevice) {
|
||||||
@@ -126,12 +159,6 @@ export const initializeDevices = () => {
|
|||||||
// register event handlers
|
// register event handlers
|
||||||
nanoIpc.on((eventid, deviceid, dataString) => {
|
nanoIpc.on((eventid, deviceid, dataString) => {
|
||||||
console.log('Received event', 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') {
|
if (eventid === 'device-attached') {
|
||||||
deviceStore.attachDevice(deviceid)
|
deviceStore.attachDevice(deviceid)
|
||||||
if (deviceStore.attachedDeviceIds.length === 1) {
|
if (deviceStore.attachedDeviceIds.length === 1) {
|
||||||
@@ -143,21 +170,43 @@ export const initializeDevices = () => {
|
|||||||
}
|
}
|
||||||
if (eventid === 'connected') {
|
if (eventid === 'connected') {
|
||||||
deviceStore.connectDevice(deviceid, false)
|
deviceStore.connectDevice(deviceid, false)
|
||||||
|
nanoIpc.send(deviceid, JSON.stringify({ profiles: '#all' }))
|
||||||
}
|
}
|
||||||
if (eventid === 'disconnected') {
|
if (eventid === 'disconnected') {
|
||||||
deviceStore.disconnectDevice(deviceid, false)
|
deviceStore.disconnectDevice(deviceid, false)
|
||||||
}
|
}
|
||||||
if (eventid === 'update') {
|
if (eventid === 'update') {
|
||||||
|
let update: UpdateData = {} as UpdateData
|
||||||
|
if (dataString) {
|
||||||
|
try {
|
||||||
|
update = JSON.parse(dataString) as UpdateData
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
if (update.a) {
|
if (update.a) {
|
||||||
deviceStore.setAngle(update.a)
|
deviceStore.setAngle(update.a)
|
||||||
}
|
}
|
||||||
|
if (update.profiles) {
|
||||||
|
deviceStore.setProfileNames(update.profiles, false)
|
||||||
|
for (const profileName of update.profiles) {
|
||||||
|
console.log('Requesting profile', profileName)
|
||||||
|
nanoIpc.send(deviceid, JSON.stringify({ profile: profileName }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (update.current) {
|
||||||
|
deviceStore.setCurrentProfile(update.current, false)
|
||||||
|
}
|
||||||
|
if (update.profile) {
|
||||||
|
deviceStore.addProfile(update.profile, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// get initial device list
|
// get initial device list
|
||||||
nanoIpc.listAttachedDevices().then((deviceIds) => {
|
nanoIpc.listAttachedDevices().then((deviceIds) => {
|
||||||
deviceStore.setAttachedDeviceIds(deviceIds)
|
deviceStore.setAttachedDeviceIds(deviceIds)
|
||||||
if (deviceIds.length > 0) {
|
if (!deviceStore.connected && deviceIds.length > 0) {
|
||||||
nanoIpc.connect(deviceIds[0])
|
nanoIpc.connect(deviceIds[0])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user