| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- 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
- ))
- }
- })
|