const paramsPageState = require('../../utils/params-page-state') const paramsService = require('../../utils/params-service') const controlService = require('../../utils/control-service') const { getStatusPageState } = require('../../utils/status-page-state') const syncService = require('../../utils/sync-service') const { createPageToast } = require('../../utils/page-toast') const GROUP_LABELS = { dq: 'DQ轴电流环参数', estimator: '估算器参数', oil: '上油参数', preposition: '预定位配置', protection: '保护参数', protectionSwitch: '保护控制', speedLoop: '速度环路', tailwind: '顺逆风配置', vsp: 'VSP曲线' } const COLLAPSIBLE_CARDS = [ 'motor', 'driver', 'estimator', 'dq', 'tailwind', 'preposition', 'speedLoop', 'vsp', 'oil', 'protectionSwitch', 'protection', 'status' ] function createCollapseState() { return COLLAPSIBLE_CARDS.reduce((result, key) => { result[key] = true return result }, {}) } function getGroupLabel(groupKey) { return GROUP_LABELS[groupKey] || '参数' } function getControlViewState(controlState = controlService.getState()) { return { ...controlState, ...getStatusPageState(), canReadStatus: !!controlState.connectedDevice } } function getCollapseState(collapsedCards) { return { ...createCollapseState(), ...(collapsedCards || {}) } } function getPageState( paramsState = syncService.getParamsSnapshot(), controlState = controlService.getState(), collapsedCards ) { return { ...paramsPageState.refreshState(paramsState), ...getControlViewState(controlState), collapsedCards: getCollapseState(collapsedCards) } } Page({ data: getPageState(), onLoad() { this.pageToast = createPageToast(this, this.data) controlService.init() this.unsubscribeSync = syncService.subscribe((syncState) => { if (!syncState.syncVersion || syncState.syncVersion === this.data.syncVersion) return const nextState = getPageState( syncService.getParamsSnapshot(), controlService.getState(), this.data.collapsedCards ) this.setData(nextState) this.pageToast.showFromState(nextState) }) this.unsubscribeControl = controlService.subscribe((controlState) => { const nextState = getControlViewState(controlState) this.setData(nextState) this.pageToast.showFromState(nextState) }) }, onShow() { if (this.pageToast) { this.pageToast.setActive(true) } controlService.syncSharedInputs() const snapshot = syncService.getParamsSnapshot() const nextState = snapshot.syncVersion && snapshot.syncVersion !== this.data.syncVersion ? paramsPageState.refreshState(snapshot) : paramsPageState.refreshState(this.data) const controlViewState = getControlViewState() const pageState = { ...nextState, ...controlViewState, collapsedCards: getCollapseState(this.data.collapsedCards) } this.setData(pageState) this.pageToast.showFromState(pageState) }, onHide() { if (this.pageToast) { this.pageToast.setActive(false) } }, onUnload() { if (this.pageToast) { this.pageToast.destroy() this.pageToast = null } if (this.unsubscribeSync) { this.unsubscribeSync() this.unsubscribeSync = null } if (this.unsubscribeControl) { this.unsubscribeControl() this.unsubscribeControl = null } }, async onGroupRead(event) { if (!this.data.connectedDevice) return const groupKey = event.currentTarget.dataset.group const nextState = await paramsService.readGroup(this.data, groupKey) if (nextState) { this.setData(nextState) if (this.pageToast) this.pageToast.show(`${getGroupLabel(groupKey)}读取完成`) } }, async onGroupWrite(event) { if (!this.data.connectedDevice) return const groupKey = event.currentTarget.dataset.group const written = await paramsService.writeGroup(this.data, groupKey) if (written) { this.setData(paramsPageState.clearGroupDirty(this.data, groupKey)) if (this.pageToast) this.pageToast.show(`${getGroupLabel(groupKey)}写入完成`) } }, onEstimatorUpdate() { this.setData(paramsPageState.refreshState(this.data)) if (this.pageToast) this.pageToast.show('估算器参数更新完成') }, toggleCard(event) { const cardKey = event.currentTarget.dataset.card const collapsedCards = this.data.collapsedCards || {} if (!cardKey) return this.setData({ [`collapsedCards.${cardKey}`]: !collapsedCards[cardKey] }) }, onMotorParameterInput(event) { controlService.updateMotorParameterInput( Number(event.currentTarget.dataset.index), event.detail.value ) }, onMotorParameterBlur(event) { controlService.updateMotorParameterBlur( Number(event.currentTarget.dataset.index), event.detail.value ) }, readMotorParameters() { if (!this.data.connectedDevice) return controlService.readMotorParameters() }, writeMotorParameters() { if (!this.data.connectedDevice) return controlService.writeMotorParameters() }, readDriverParameters() { if (!this.data.connectedDevice) return controlService.readDriverParameters() }, readStatus() { if (!this.data.canReadStatus) return controlService.readStatus() }, onAutoReadStatusTap() { if (!this.data.autoReadStatus && !this.data.canReadStatus) return controlService.setAutoReadStatus(!this.data.autoReadStatus) }, onAutoReadIntervalInput(event) { controlService.setAutoReadInterval(event.detail.value) }, onInputChange(event) { this.setData(paramsPageState.applyParameterInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onAtoBandwidthInput(event) { this.setData(paramsPageState.applyAtoBandwidthInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onDqGainInput(event) { this.setData(paramsPageState.applyDqGainInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onSpeedLoopExtraInput(event) { this.setData(paramsPageState.applySpeedLoopExtraInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onOilParameterInput(event) { this.setData(paramsPageState.applyOilParameterInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onPrepositionParameterInput(event) { this.setData(paramsPageState.applyPrepositionParameterInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) }, onInputBlur(event) { this.setData(paramsPageState.applyInputBlur( this.data, event.currentTarget.dataset.inputGroup, Number(event.currentTarget.dataset.index), event.detail.value )) }, onTailwindSwitchChange(event) { if (!this.data.connectedDevice) return const index = Number(event.currentTarget.dataset.index) const nextState = paramsPageState.applyTailwindSwitchChange( this.data, index, !!event.detail.value ) this.setData(nextState) paramsService.writeSwitchRegister(nextState.tailwindSwitchRegisters[index]).then((written) => { if (written) { this.setData(paramsPageState.clearTailwindSwitchDirty(this.data, index)) if (this.pageToast) this.pageToast.show(`${nextState.tailwindSwitchRegisters[index].name}写入完成`) } }) }, onProtectionSwitchChange(event) { if (!this.data.connectedDevice) return const index = Number(event.currentTarget.dataset.index) const nextState = paramsPageState.applyProtectionSwitchChange( this.data, index, !!event.detail.value ) this.setData(nextState) paramsService.writeSwitchRegister(nextState.protectionSwitchRegisters[index]).then((written) => { if (written) { this.setData(paramsPageState.clearProtectionSwitchDirty(this.data, index)) if (this.pageToast) this.pageToast.show(`${nextState.protectionSwitchRegisters[index].name}写入完成`) } }) }, onProtectionInputChange(event) { this.setData(paramsPageState.applyProtectionInput( this.data, Number(event.currentTarget.dataset.index), event.detail.value )) } })