const themeService = require('../../store/theme-store.js') const { controlService, getControlPageState } = require('../../features/motor-control/index.js') const { createPageToast } = require('../../utils/page-toast.js') Page({ data: getControlPageState(), onLoad() { this.pageToast = createPageToast(this, this.data) controlService.init() themeService.init() this.unsubscribeControl = controlService.subscribe((controlState) => { const nextState = getControlPageState(controlState) this.setData(nextState) this.pageToast.showFromState(nextState) }) this.unsubscribeTheme = themeService.subscribe((themeState) => { this.setData(getControlPageState(controlService.getState(), themeState)) }) }, onShow() { if (this.pageToast) { this.pageToast.setActive(true) } controlService.syncSharedInputs() this.setData(getControlPageState()) }, onHide() { if (this.pageToast) { this.pageToast.setActive(false) } }, onUnload() { if (this.pageToast) { this.pageToast.destroy() this.pageToast = null } if (this.unsubscribeControl) { this.unsubscribeControl() this.unsubscribeControl = null } if (this.unsubscribeTheme) { this.unsubscribeTheme() this.unsubscribeTheme = null } }, readStatus() { if (!this.data.canReadStatus) return controlService.readStatus() }, onAutoReadStatusTap() { if (!this.data.autoReadStatus && !this.data.canReadStatus) return controlService.setAutoReadStatus(!this.data.autoReadStatus) }, onSpeedCommandInput(event) { controlService.updateSpeedCommandInput(event.detail.value) }, onSpeedCommandBlur(event) { controlService.updateSpeedCommandBlur(event.detail.value) }, readControlStatus() { if (!this.data.connectedDevice || this.data.isBootloaderBusy) return controlService.readControlStatus() }, onControlButtonTap(event) { if (!this.data.connectedDevice || this.data.isBootloaderBusy) return controlService.sendControlCommand(event.currentTarget.dataset.key) }, chooseFirmwareFile() { if (this.data.isBootloaderBusy) return controlService.chooseFirmwareFile('message') }, startFirmwareUpgrade() { if (!this.data.connectedDevice || !this.data.isFirmwareReady || this.data.isBootloaderBusy) return controlService.startFirmwareUpgrade() }, readProgramChecksum() { if (!this.data.connectedDevice || this.data.isBootloaderBusy) return controlService.readProgramChecksum() }, handshakeBootloader() { if (!this.data.connectedDevice || this.data.isBootloaderBusy) return controlService.handshakeBootloader() }, exitBootloader() { if (!this.data.connectedDevice || this.data.isBootloaderBusy) return controlService.exitBootloader() } })