1
0

params.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. const paramsPageState = require('../../utils/params-page-state')
  2. const paramsService = require('../../utils/params-service')
  3. const controlService = require('../../utils/control-service')
  4. const {
  5. getStatusPageState
  6. } = require('../../utils/status-page-state')
  7. const syncService = require('../../utils/sync-service')
  8. const {
  9. createPageToast
  10. } = require('../../utils/page-toast')
  11. const GROUP_LABELS = {
  12. dq: 'DQ轴电流环参数',
  13. estimator: '估算器参数',
  14. oil: '上油参数',
  15. preposition: '预定位配置',
  16. protection: '保护参数',
  17. protectionSwitch: '保护控制',
  18. speedLoop: '速度环路',
  19. tailwind: '顺逆风配置',
  20. vsp: 'VSP曲线'
  21. }
  22. const COLLAPSIBLE_CARDS = [
  23. 'motor',
  24. 'driver',
  25. 'estimator',
  26. 'dq',
  27. 'tailwind',
  28. 'preposition',
  29. 'speedLoop',
  30. 'vsp',
  31. 'oil',
  32. 'protectionSwitch',
  33. 'protection',
  34. 'status'
  35. ]
  36. function createCollapseState() {
  37. return COLLAPSIBLE_CARDS.reduce((result, key) => {
  38. result[key] = true
  39. return result
  40. }, {})
  41. }
  42. function getGroupLabel(groupKey) {
  43. return GROUP_LABELS[groupKey] || '参数'
  44. }
  45. function getControlViewState(controlState = controlService.getState()) {
  46. return {
  47. ...controlState,
  48. ...getStatusPageState(),
  49. canReadStatus: !!controlState.connectedDevice
  50. }
  51. }
  52. function getCollapseState(collapsedCards) {
  53. return {
  54. ...createCollapseState(),
  55. ...(collapsedCards || {})
  56. }
  57. }
  58. function getPageState(
  59. paramsState = syncService.getParamsSnapshot(),
  60. controlState = controlService.getState(),
  61. collapsedCards
  62. ) {
  63. return {
  64. ...paramsPageState.refreshState(paramsState),
  65. ...getControlViewState(controlState),
  66. collapsedCards: getCollapseState(collapsedCards)
  67. }
  68. }
  69. Page({
  70. data: getPageState(),
  71. onLoad() {
  72. this.pageToast = createPageToast(this, this.data)
  73. controlService.init()
  74. this.unsubscribeSync = syncService.subscribe((syncState) => {
  75. if (!syncState.syncVersion || syncState.syncVersion === this.data.syncVersion) return
  76. const nextState = getPageState(
  77. syncService.getParamsSnapshot(),
  78. controlService.getState(),
  79. this.data.collapsedCards
  80. )
  81. this.setData(nextState)
  82. this.pageToast.showFromState(nextState)
  83. })
  84. this.unsubscribeControl = controlService.subscribe((controlState) => {
  85. const nextState = getControlViewState(controlState)
  86. this.setData(nextState)
  87. this.pageToast.showFromState(nextState)
  88. })
  89. },
  90. onShow() {
  91. if (this.pageToast) {
  92. this.pageToast.setActive(true)
  93. }
  94. controlService.syncSharedInputs()
  95. const snapshot = syncService.getParamsSnapshot()
  96. const nextState = snapshot.syncVersion && snapshot.syncVersion !== this.data.syncVersion
  97. ? paramsPageState.refreshState(snapshot)
  98. : paramsPageState.refreshState(this.data)
  99. const controlViewState = getControlViewState()
  100. const pageState = {
  101. ...nextState,
  102. ...controlViewState,
  103. collapsedCards: getCollapseState(this.data.collapsedCards)
  104. }
  105. this.setData(pageState)
  106. this.pageToast.showFromState(pageState)
  107. },
  108. onHide() {
  109. if (this.pageToast) {
  110. this.pageToast.setActive(false)
  111. }
  112. },
  113. onUnload() {
  114. if (this.pageToast) {
  115. this.pageToast.destroy()
  116. this.pageToast = null
  117. }
  118. if (this.unsubscribeSync) {
  119. this.unsubscribeSync()
  120. this.unsubscribeSync = null
  121. }
  122. if (this.unsubscribeControl) {
  123. this.unsubscribeControl()
  124. this.unsubscribeControl = null
  125. }
  126. },
  127. async onGroupRead(event) {
  128. if (!this.data.connectedDevice) return
  129. const groupKey = event.currentTarget.dataset.group
  130. const nextState = await paramsService.readGroup(this.data, groupKey)
  131. if (nextState) {
  132. this.setData(nextState)
  133. if (this.pageToast) this.pageToast.show(`${getGroupLabel(groupKey)}读取完成`)
  134. }
  135. },
  136. async onGroupWrite(event) {
  137. if (!this.data.connectedDevice) return
  138. const groupKey = event.currentTarget.dataset.group
  139. const written = await paramsService.writeGroup(this.data, groupKey)
  140. if (written) {
  141. this.setData(paramsPageState.clearGroupDirty(this.data, groupKey))
  142. if (this.pageToast) this.pageToast.show(`${getGroupLabel(groupKey)}写入完成`)
  143. }
  144. },
  145. onEstimatorUpdate() {
  146. this.setData(paramsPageState.refreshState(this.data))
  147. if (this.pageToast) this.pageToast.show('估算器参数更新完成')
  148. },
  149. toggleCard(event) {
  150. const cardKey = event.currentTarget.dataset.card
  151. const collapsedCards = this.data.collapsedCards || {}
  152. if (!cardKey) return
  153. this.setData({
  154. [`collapsedCards.${cardKey}`]: !collapsedCards[cardKey]
  155. })
  156. },
  157. onMotorParameterInput(event) {
  158. controlService.updateMotorParameterInput(
  159. Number(event.currentTarget.dataset.index),
  160. event.detail.value
  161. )
  162. },
  163. onMotorParameterBlur(event) {
  164. controlService.updateMotorParameterBlur(
  165. Number(event.currentTarget.dataset.index),
  166. event.detail.value
  167. )
  168. },
  169. readMotorParameters() {
  170. if (!this.data.connectedDevice) return
  171. controlService.readMotorParameters()
  172. },
  173. writeMotorParameters() {
  174. if (!this.data.connectedDevice) return
  175. controlService.writeMotorParameters()
  176. },
  177. readDriverParameters() {
  178. if (!this.data.connectedDevice) return
  179. controlService.readDriverParameters()
  180. },
  181. readStatus() {
  182. if (!this.data.canReadStatus) return
  183. controlService.readStatus()
  184. },
  185. onAutoReadStatusTap() {
  186. if (!this.data.autoReadStatus && !this.data.canReadStatus) return
  187. controlService.setAutoReadStatus(!this.data.autoReadStatus)
  188. },
  189. onAutoReadIntervalInput(event) {
  190. controlService.setAutoReadInterval(event.detail.value)
  191. },
  192. onInputChange(event) {
  193. this.setData(paramsPageState.applyParameterInput(
  194. this.data,
  195. Number(event.currentTarget.dataset.index),
  196. event.detail.value
  197. ))
  198. },
  199. onAtoBandwidthInput(event) {
  200. this.setData(paramsPageState.applyAtoBandwidthInput(
  201. this.data,
  202. Number(event.currentTarget.dataset.index),
  203. event.detail.value
  204. ))
  205. },
  206. onDqGainInput(event) {
  207. this.setData(paramsPageState.applyDqGainInput(
  208. this.data,
  209. Number(event.currentTarget.dataset.index),
  210. event.detail.value
  211. ))
  212. },
  213. onSpeedLoopExtraInput(event) {
  214. this.setData(paramsPageState.applySpeedLoopExtraInput(
  215. this.data,
  216. Number(event.currentTarget.dataset.index),
  217. event.detail.value
  218. ))
  219. },
  220. onOilParameterInput(event) {
  221. this.setData(paramsPageState.applyOilParameterInput(
  222. this.data,
  223. Number(event.currentTarget.dataset.index),
  224. event.detail.value
  225. ))
  226. },
  227. onPrepositionParameterInput(event) {
  228. this.setData(paramsPageState.applyPrepositionParameterInput(
  229. this.data,
  230. Number(event.currentTarget.dataset.index),
  231. event.detail.value
  232. ))
  233. },
  234. onInputBlur(event) {
  235. this.setData(paramsPageState.applyInputBlur(
  236. this.data,
  237. event.currentTarget.dataset.inputGroup,
  238. Number(event.currentTarget.dataset.index),
  239. event.detail.value
  240. ))
  241. },
  242. onTailwindSwitchChange(event) {
  243. if (!this.data.connectedDevice) return
  244. const index = Number(event.currentTarget.dataset.index)
  245. const nextState = paramsPageState.applyTailwindSwitchChange(
  246. this.data,
  247. index,
  248. !!event.detail.value
  249. )
  250. this.setData(nextState)
  251. paramsService.writeSwitchRegister(nextState.tailwindSwitchRegisters[index]).then((written) => {
  252. if (written) {
  253. this.setData(paramsPageState.clearTailwindSwitchDirty(this.data, index))
  254. if (this.pageToast) this.pageToast.show(`${nextState.tailwindSwitchRegisters[index].name}写入完成`)
  255. }
  256. })
  257. },
  258. onProtectionSwitchChange(event) {
  259. if (!this.data.connectedDevice) return
  260. const index = Number(event.currentTarget.dataset.index)
  261. const nextState = paramsPageState.applyProtectionSwitchChange(
  262. this.data,
  263. index,
  264. !!event.detail.value
  265. )
  266. this.setData(nextState)
  267. paramsService.writeSwitchRegister(nextState.protectionSwitchRegisters[index]).then((written) => {
  268. if (written) {
  269. this.setData(paramsPageState.clearProtectionSwitchDirty(this.data, index))
  270. if (this.pageToast) this.pageToast.show(`${nextState.protectionSwitchRegisters[index].name}写入完成`)
  271. }
  272. })
  273. },
  274. onProtectionInputChange(event) {
  275. this.setData(paramsPageState.applyProtectionInput(
  276. this.data,
  277. Number(event.currentTarget.dataset.index),
  278. event.detail.value
  279. ))
  280. }
  281. })