Files
modeling-app/src/lib/machineManager.ts

75 lines
1.6 KiB
TypeScript
Raw Normal View History

Add print button (#3133) * add print button Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * generate more types Signed-off-by: Jess Frazelle <github@jessfraz.com> * add a github action to generate machine api-types Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * actually print on the real machine Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * add more Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * get the current machine Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * know when error Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * add fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * empty * empty * update machine api Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * empty * New machine-api types * emptuy * no circular deps Signed-off-by: Jess Frazelle <github@jessfraz.com> * New machine-api types * remove recursive dep Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Jess Frazelle <jessfraz@users.noreply.github.com>
2024-08-04 00:51:30 -04:00
import { isTauri } from './isTauri'
import { components } from './machine-api'
import { getMachineApiIp, listMachines } from './tauri'
export class MachineManager {
private _isTauri: boolean = isTauri()
private _machines: {
[key: string]: components['schemas']['Machine']
} = {}
private _machineApiIp: string | null = null
private _currentMachine: components['schemas']['Machine'] | null = null
constructor() {
if (!this._isTauri) {
return
}
this.updateMachines()
}
start() {
if (!this._isTauri) {
return
}
// Start a background job to update the machines every ten seconds.
setInterval(() => {
this.updateMachineApiIp()
this.updateMachines()
}, 10000)
}
get machines(): {
[key: string]: components['schemas']['Machine']
} {
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> {
if (!this._isTauri) {
return
}
this._machines = await listMachines()
console.log('Machines:', this._machines)
}
private async updateMachineApiIp(): Promise<void> {
if (!this._isTauri) {
return
}
this._machineApiIp = await getMachineApiIp()
}
}
export const machineManager = new MachineManager()
machineManager.start()