2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from './isDesktop'
|
2024-08-04 00:51:30 -04:00
|
|
|
import { components } from './machine-api'
|
2024-08-16 07:15:42 -04:00
|
|
|
|
|
|
|
export type MachinesListing = {
|
|
|
|
[key: string]: components['schemas']['Machine']
|
|
|
|
}
|
2024-08-04 00:51:30 -04:00
|
|
|
|
|
|
|
export class MachineManager {
|
2024-08-16 07:15:42 -04:00
|
|
|
private _isDesktop: boolean = isDesktop()
|
|
|
|
private _machines: MachinesListing = {}
|
2024-08-04 00:51:30 -04:00
|
|
|
private _machineApiIp: string | null = null
|
|
|
|
private _currentMachine: components['schemas']['Machine'] | null = null
|
|
|
|
|
|
|
|
constructor() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateMachines()
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a background job to update the machines every ten seconds.
|
2024-08-16 07:15:42 -04:00
|
|
|
// If MDNS is already watching, this timeout will wait until it's done to trigger the
|
|
|
|
// finding again.
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout> | undefined = undefined
|
|
|
|
const timeoutLoop = () => {
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
timeoutId = setTimeout(async () => {
|
|
|
|
await this.updateMachineApiIp()
|
|
|
|
await this.updateMachines()
|
|
|
|
timeoutLoop()
|
|
|
|
}, 10000)
|
|
|
|
}
|
|
|
|
timeoutLoop()
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
get machines(): MachinesListing {
|
2024-08-04 00:51:30 -04:00
|
|
|
return this._machines
|
|
|
|
}
|
|
|
|
|
|
|
|
machineCount(): number {
|
|
|
|
return Object.keys(this._machines).length
|
|
|
|
}
|
|
|
|
|
|
|
|
get machineApiIp(): string | null {
|
|
|
|
return this._machineApiIp
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentMachine(): components['schemas']['Machine'] | null {
|
|
|
|
return this._currentMachine
|
|
|
|
}
|
|
|
|
|
|
|
|
set currentMachine(machine: components['schemas']['Machine'] | null) {
|
|
|
|
this._currentMachine = machine
|
|
|
|
}
|
|
|
|
|
|
|
|
private async updateMachines(): Promise<void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
this._machines = await window.electron.listMachines()
|
2024-08-04 00:51:30 -04:00
|
|
|
console.log('Machines:', this._machines)
|
|
|
|
}
|
|
|
|
|
|
|
|
private async updateMachineApiIp(): Promise<void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
if (!this._isDesktop) {
|
2024-08-04 00:51:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
this._machineApiIp = await window.electron.getMachineApiIp()
|
2024-08-04 00:51:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const machineManager = new MachineManager()
|
|
|
|
machineManager.start()
|