params-page-state.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. const {
  2. atoBandwidthInputRegisters,
  3. calculatedParameterRegisters,
  4. dqGainInputRegisters,
  5. oilParameterInputRegisters,
  6. parameterInputRegisters,
  7. prepositionParameterInputRegisters,
  8. protectionRegisters,
  9. protectionSwitchRegisters,
  10. speedLoopExtraRegisters,
  11. speedSlopeRegister,
  12. tailwindSwitchRegisters,
  13. getByteRegisterValue
  14. } = require('./registers')
  15. const {
  16. getSharedInputValues,
  17. mergeInputValues,
  18. toFiniteNumber
  19. } = require('./calculation-context')
  20. const {
  21. calculateAtoGainWriteValues,
  22. calculateDqGainWriteValue,
  23. calculateParameterInputWriteValue,
  24. calculateParameterReadValue,
  25. calculateProtectionWriteValue,
  26. calculateSpeedSlope,
  27. formatFixedValue
  28. } = require('./conversions')
  29. const {
  30. toAddressKey,
  31. wordsToFloat
  32. } = require('./register-value-utils')
  33. const {
  34. appendInputUnit
  35. } = require('./input-value-utils')
  36. const VSP_CURVE_ORDER = [
  37. '开机电压',
  38. '关机电压',
  39. '调速最低电压',
  40. '调速最高电压'
  41. ]
  42. const SPEED_LOOP_INPUT_ORDER = [
  43. '速度最小值',
  44. '速度最大值',
  45. 'SOUT_MAX'
  46. ]
  47. const SPEED_LOOP_CALCULATED_NAMES = [
  48. ]
  49. const TAILWIND_CALCULATED_NAMES = [
  50. 'SPEED_KLPF_TAILWIND',
  51. 'OBS_EA_KS_TAILWIND'
  52. ]
  53. const ESTIMATOR_CALCULATED_PREFIXES = [
  54. 'OBS_E',
  55. 'OBS_FBASE',
  56. 'OBS_EA_KS',
  57. 'SPEED_KLPF',
  58. 'FOC_KFG'
  59. ]
  60. function formatInputValue(item, value) {
  61. if (value === '' || value === undefined || value === null) return '--'
  62. const numberValue = toFiniteNumber(value, NaN)
  63. if (!Number.isFinite(numberValue)) return value
  64. if (item.type === 'uint8_t' && (numberValue < 0 || numberValue > 0xFF)) return '--'
  65. if (item.type === 'float') return formatFixedValue(numberValue, 2)
  66. return String(Math.round(numberValue))
  67. }
  68. function getInputValues(registers) {
  69. return mergeInputValues(registers)
  70. }
  71. function getSharedParameterValues(registers, extraRegisters = []) {
  72. return {
  73. ...getSharedInputValues(),
  74. ...getInputValues(registers.concat(extraRegisters))
  75. }
  76. }
  77. function updateAtoBandwidthValues(registers, inputValues) {
  78. return registers.map((item) => ({
  79. ...item,
  80. ...calculateAtoGainWriteValues(item.inputValue, inputValues)
  81. }))
  82. }
  83. function updateInputWriteValues(registers) {
  84. const inputValues = getInputValues(registers)
  85. return registers.map((item) => ({
  86. ...item,
  87. writeValue: calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  88. }))
  89. }
  90. function updateSpeedLoopExtraValues(registers, inputValues) {
  91. return registers.map((item) => {
  92. const writeValue = calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  93. return {
  94. ...item,
  95. actualText: '',
  96. writeValue
  97. }
  98. })
  99. }
  100. function updateOilParameterValues(registers, inputValues) {
  101. return registers.map((item) => ({
  102. ...item,
  103. writeValue: calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  104. }))
  105. }
  106. function updateSpeedSlope(register, inputValues) {
  107. const speedSlope = calculateSpeedSlope(inputValues)
  108. return {
  109. ...register,
  110. writeValue: speedSlope === null ? '--' : formatFixedValue(speedSlope, 2)
  111. }
  112. }
  113. function addSourceIndex(registers) {
  114. return registers.map((item, index) => ({
  115. ...item,
  116. sourceIndex: index
  117. }))
  118. }
  119. function isNameIn(names, item) {
  120. return names.includes(item.name)
  121. }
  122. function isTailwindAtoRegister(item) {
  123. return item.suffix === 'TAILWIND'
  124. }
  125. function isSpeedLoopCalculatedRegister(item) {
  126. return isNameIn(SPEED_LOOP_CALCULATED_NAMES, item)
  127. }
  128. function isTailwindCalculatedRegister(item) {
  129. return isNameIn(TAILWIND_CALCULATED_NAMES, item)
  130. }
  131. function isEstimatorCalculatedRegister(item) {
  132. if (isSpeedLoopCalculatedRegister(item) || isTailwindCalculatedRegister(item)) return false
  133. return ESTIMATOR_CALCULATED_PREFIXES.some((prefix) => item.name.startsWith(prefix))
  134. }
  135. function sortByNameOrder(registers, nameOrder) {
  136. return registers
  137. .filter((item) => nameOrder.includes(item.name))
  138. .slice()
  139. .sort((left, right) => nameOrder.indexOf(left.name) - nameOrder.indexOf(right.name))
  140. }
  141. function buildProtectionGroups(registers) {
  142. return {
  143. protectionDisplayRegisters: addSourceIndex(registers)
  144. }
  145. }
  146. function getWord(readValues, address) {
  147. return readValues.words[toAddressKey(address)]
  148. }
  149. function getRegisterReadValue(item, readValues) {
  150. if (item.area && item.area.key === 'coil') {
  151. const coilValue = readValues.coils[toAddressKey(item.address)]
  152. return coilValue === undefined ? null : coilValue
  153. }
  154. const firstWord = getWord(readValues, item.address)
  155. if (!Number.isInteger(firstWord)) return null
  156. if (item.type === 'uint8_t' && item.bytePosition) {
  157. return getByteRegisterValue(item, firstWord)
  158. }
  159. if (item.type === 'float') {
  160. const nextAddress = (parseInt(item.address, 16) + 1).toString(16).toUpperCase()
  161. return wordsToFloat(firstWord, getWord(readValues, nextAddress))
  162. }
  163. return firstWord
  164. }
  165. function formatReadValue(item, value) {
  166. if (value === null || value === undefined || !Number.isFinite(Number(value))) return null
  167. const numberValue = Number(value)
  168. if (item.type === 'float') return formatFixedValue(numberValue, 2)
  169. return String(Math.round(numberValue))
  170. }
  171. function getReadInputValue(item, readValue, readText, options = {}) {
  172. let inputText = readText
  173. if (options.useCalculatedInputValue) {
  174. const calculatedValue = calculateParameterReadValue(item, readValue, options.inputValues || {})
  175. if (calculatedValue !== null) {
  176. inputText = formatFixedValue(calculatedValue, 2)
  177. }
  178. }
  179. return appendInputUnit(item, inputText)
  180. }
  181. function getDqReadInputValue(item, rawText) {
  182. const rawValue = Number(rawText)
  183. if (!Number.isFinite(rawValue)) return ''
  184. if (item.gainType === 'kp') return formatFixedValue(rawValue / 4095, 2)
  185. if (item.gainType === 'ki') return formatFixedValue(rawValue / 32767, 2)
  186. return rawText
  187. }
  188. function applyReadValuesToRegisters(registers, readValues, options = {}) {
  189. return registers.map((item) => {
  190. const readValue = getRegisterReadValue(item, readValues)
  191. const readText = formatReadValue(item, readValue)
  192. if (readText === null) return item
  193. const nextItem = {
  194. ...item,
  195. isDirty: false,
  196. writeValue: readText
  197. }
  198. if (Object.prototype.hasOwnProperty.call(item, 'value')) {
  199. nextItem.value = Number(readValue) !== 0
  200. }
  201. if (options.updateInputValue) {
  202. nextItem.inputValue = getReadInputValue(item, readValue, readText, options)
  203. }
  204. if (options.updateDqInputValue) {
  205. nextItem.inputValue = getDqReadInputValue(item, readText)
  206. }
  207. return nextItem
  208. })
  209. }
  210. function applyReadValuesToAtoRegisters(registers, readValues) {
  211. return registers.map((item) => {
  212. const kpWord = getWord(readValues, item.kpAddress)
  213. const kiWord = getWord(readValues, item.kiAddress)
  214. if (!Number.isInteger(kpWord) && !Number.isInteger(kiWord)) return item
  215. return {
  216. ...item,
  217. isDirty: false,
  218. kpWriteValue: Number.isInteger(kpWord) ? String(kpWord) : item.kpWriteValue,
  219. kiWriteValue: Number.isInteger(kiWord) ? String(kiWord) : item.kiWriteValue
  220. }
  221. })
  222. }
  223. function clearDirty(registers = [], matcher = () => true) {
  224. return registers.map((item) => (
  225. matcher(item)
  226. ? {
  227. ...item,
  228. isDirty: false
  229. }
  230. : item
  231. ))
  232. }
  233. function clearGroupDirty(data, groupKey) {
  234. const nextState = {
  235. ...data
  236. }
  237. if (groupKey === 'estimator') {
  238. nextState.atoBandwidthInputRegisters = clearDirty(data.atoBandwidthInputRegisters)
  239. nextState.calculatedParameterRegisters = clearDirty(data.calculatedParameterRegisters, isEstimatorCalculatedRegister)
  240. }
  241. if (groupKey === 'dq') nextState.dqGainInputRegisters = clearDirty(data.dqGainInputRegisters)
  242. if (groupKey === 'tailwind') {
  243. nextState.tailwindSwitchRegisters = clearDirty(data.tailwindSwitchRegisters, (item) => item.name !== '预定位启用')
  244. nextState.atoBandwidthInputRegisters = clearDirty(data.atoBandwidthInputRegisters, isTailwindAtoRegister)
  245. nextState.calculatedParameterRegisters = clearDirty(data.calculatedParameterRegisters, isTailwindCalculatedRegister)
  246. }
  247. if (groupKey === 'preposition') {
  248. nextState.tailwindSwitchRegisters = clearDirty(data.tailwindSwitchRegisters, (item) => item.name === '预定位启用')
  249. nextState.prepositionParameterInputRegisters = clearDirty(data.prepositionParameterInputRegisters)
  250. }
  251. if (groupKey === 'speedLoop') {
  252. nextState.parameterInputRegisters = clearDirty(data.parameterInputRegisters, (item) => (
  253. SPEED_LOOP_INPUT_ORDER.includes(item.name)
  254. ))
  255. nextState.calculatedParameterRegisters = clearDirty(data.calculatedParameterRegisters, isSpeedLoopCalculatedRegister)
  256. nextState.speedLoopExtraRegisters = clearDirty(data.speedLoopExtraRegisters)
  257. }
  258. if (groupKey === 'vsp') {
  259. nextState.parameterInputRegisters = clearDirty(data.parameterInputRegisters, (item) => VSP_CURVE_ORDER.includes(item.name))
  260. nextState.speedSlopeRegister = {
  261. ...data.speedSlopeRegister,
  262. isDirty: false
  263. }
  264. }
  265. if (groupKey === 'oil') nextState.oilParameterInputRegisters = clearDirty(data.oilParameterInputRegisters)
  266. if (groupKey === 'protectionSwitch') nextState.protectionSwitchRegisters = clearDirty(data.protectionSwitchRegisters)
  267. if (groupKey === 'protection') nextState.protectionRegisters = clearDirty(data.protectionRegisters)
  268. return {
  269. ...nextState,
  270. ...buildViewState(nextState)
  271. }
  272. }
  273. function clearRegisterDirty(registers = [], index) {
  274. return registers.map((item, currentIndex) => (
  275. currentIndex === index
  276. ? {
  277. ...item,
  278. isDirty: false
  279. }
  280. : item
  281. ))
  282. }
  283. function clearTailwindSwitchDirty(data, index) {
  284. const nextState = {
  285. ...data,
  286. tailwindSwitchRegisters: clearRegisterDirty(data.tailwindSwitchRegisters, index)
  287. }
  288. return {
  289. ...nextState,
  290. ...buildViewState(nextState)
  291. }
  292. }
  293. function clearProtectionSwitchDirty(data, index) {
  294. return {
  295. ...data,
  296. protectionSwitchRegisters: clearRegisterDirty(data.protectionSwitchRegisters, index)
  297. }
  298. }
  299. function buildViewState(state) {
  300. const inputRegisters = addSourceIndex(state.parameterInputRegisters)
  301. const atoRegisters = addSourceIndex(state.atoBandwidthInputRegisters)
  302. const dqRegisters = addSourceIndex(state.dqGainInputRegisters)
  303. const calculatedRegisters = addSourceIndex(state.calculatedParameterRegisters)
  304. const speedLoopExtras = addSourceIndex(state.speedLoopExtraRegisters)
  305. const tailwindSwitches = addSourceIndex(state.tailwindSwitchRegisters)
  306. return {
  307. vspCurveRegisters: sortByNameOrder(inputRegisters, VSP_CURVE_ORDER),
  308. speedLoopInputDisplayRegisters: sortByNameOrder(inputRegisters, SPEED_LOOP_INPUT_ORDER),
  309. speedLoopCalculatedDisplayRegisters: calculatedRegisters.filter(isSpeedLoopCalculatedRegister),
  310. speedLoopExtraDisplayRegisters: speedLoopExtras,
  311. atoBandwidthDisplayRegisters: atoRegisters.filter((item) => !isTailwindAtoRegister(item)),
  312. tailwindAtoBandwidthDisplayRegisters: atoRegisters.filter(isTailwindAtoRegister),
  313. tailwindCalculatedDisplayRegisters: calculatedRegisters.filter(isTailwindCalculatedRegister),
  314. tailwindControlRegisters: tailwindSwitches.filter((item) => item.name !== '预定位启用'),
  315. prepositionSwitchRegisters: tailwindSwitches.filter((item) => item.name === '预定位启用'),
  316. prepositionParameterDisplayRegisters: addSourceIndex(state.prepositionParameterInputRegisters),
  317. dqGainDisplayRegisters: dqRegisters,
  318. estimatorCalculatedDisplayRegisters: calculatedRegisters.filter(isEstimatorCalculatedRegister),
  319. ...buildProtectionGroups(state.protectionRegisters)
  320. }
  321. }
  322. function createInitialState() {
  323. const state = {
  324. atoBandwidthInputRegisters,
  325. atoBandwidthDisplayRegisters: [],
  326. calculatedParameterRegisters,
  327. dqGainInputRegisters,
  328. dqGainDisplayRegisters: [],
  329. estimatorCalculatedDisplayRegisters: [],
  330. oilParameterInputRegisters,
  331. parameterInputRegisters,
  332. prepositionParameterDisplayRegisters: [],
  333. prepositionParameterInputRegisters,
  334. prepositionSwitchRegisters: [],
  335. protectionDisplayRegisters: [],
  336. protectionRegisters,
  337. protectionSwitchRegisters,
  338. speedLoopExtraRegisters,
  339. speedLoopExtraDisplayRegisters: [],
  340. speedLoopCalculatedDisplayRegisters: [],
  341. speedLoopInputDisplayRegisters: [],
  342. speedSlopeRegister,
  343. tailwindAtoBandwidthDisplayRegisters: [],
  344. tailwindCalculatedDisplayRegisters: [],
  345. tailwindControlRegisters: [],
  346. tailwindSwitchRegisters,
  347. vspCurveRegisters: []
  348. }
  349. return {
  350. ...state,
  351. ...buildViewState(state)
  352. }
  353. }
  354. function refreshState(data) {
  355. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  356. const nextSpeedLoopExtraRegisters = updateSpeedLoopExtraValues(data.speedLoopExtraRegisters, inputValues)
  357. const nextState = {
  358. ...data,
  359. atoBandwidthInputRegisters: updateAtoBandwidthValues(data.atoBandwidthInputRegisters, inputValues),
  360. oilParameterInputRegisters: updateOilParameterValues(data.oilParameterInputRegisters, inputValues),
  361. speedLoopExtraRegisters: nextSpeedLoopExtraRegisters,
  362. speedSlopeRegister: updateSpeedSlope(data.speedSlopeRegister, inputValues)
  363. }
  364. return {
  365. ...nextState,
  366. ...buildViewState(nextState)
  367. }
  368. }
  369. function applyReadValues(data, readValues) {
  370. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  371. const nextState = {
  372. ...data,
  373. atoBandwidthInputRegisters: applyReadValuesToAtoRegisters(data.atoBandwidthInputRegisters, readValues),
  374. calculatedParameterRegisters: applyReadValuesToRegisters(data.calculatedParameterRegisters, readValues),
  375. dqGainInputRegisters: applyReadValuesToRegisters(data.dqGainInputRegisters, readValues, {
  376. updateDqInputValue: true
  377. }),
  378. oilParameterInputRegisters: applyReadValuesToRegisters(data.oilParameterInputRegisters, readValues, {
  379. inputValues,
  380. updateInputValue: true,
  381. useCalculatedInputValue: true
  382. }),
  383. parameterInputRegisters: applyReadValuesToRegisters(data.parameterInputRegisters, readValues, {
  384. inputValues,
  385. updateInputValue: true,
  386. useCalculatedInputValue: true
  387. }),
  388. prepositionParameterInputRegisters: applyReadValuesToRegisters(data.prepositionParameterInputRegisters, readValues, {
  389. updateInputValue: true
  390. }),
  391. protectionRegisters: applyReadValuesToRegisters(data.protectionRegisters, readValues, {
  392. inputValues,
  393. updateInputValue: true,
  394. useCalculatedInputValue: true
  395. }),
  396. protectionSwitchRegisters: applyReadValuesToRegisters(data.protectionSwitchRegisters, readValues),
  397. speedLoopExtraRegisters: applyReadValuesToRegisters(data.speedLoopExtraRegisters, readValues, {
  398. updateInputValue: true
  399. }),
  400. speedSlopeRegister: applyReadValuesToRegisters([data.speedSlopeRegister], readValues)[0],
  401. tailwindSwitchRegisters: applyReadValuesToRegisters(data.tailwindSwitchRegisters, readValues)
  402. }
  403. return {
  404. ...nextState,
  405. ...buildViewState(nextState)
  406. }
  407. }
  408. function applyParameterInput(data, index, value) {
  409. const changedRegisters = data.parameterInputRegisters.map((item, currentIndex) => {
  410. if (currentIndex !== index) return item
  411. return {
  412. ...item,
  413. isDirty: true,
  414. inputValue: value,
  415. writeValue: formatInputValue(item, value)
  416. }
  417. })
  418. const nextRegisters = updateInputWriteValues(changedRegisters)
  419. const inputValues = getSharedParameterValues(nextRegisters, data.speedLoopExtraRegisters)
  420. const nextState = {
  421. ...data,
  422. parameterInputRegisters: nextRegisters,
  423. atoBandwidthInputRegisters: updateAtoBandwidthValues(data.atoBandwidthInputRegisters, inputValues),
  424. speedLoopExtraRegisters: updateSpeedLoopExtraValues(data.speedLoopExtraRegisters, inputValues),
  425. speedSlopeRegister: updateSpeedSlope(data.speedSlopeRegister, inputValues)
  426. }
  427. return {
  428. ...nextState,
  429. ...buildViewState(nextState)
  430. }
  431. }
  432. function applyAtoBandwidthInput(data, index, value) {
  433. const inputValues = getSharedParameterValues(data.parameterInputRegisters)
  434. const nextRegisters = data.atoBandwidthInputRegisters.map((item, currentIndex) => {
  435. if (currentIndex !== index) return item
  436. return {
  437. ...item,
  438. isDirty: true,
  439. inputValue: value,
  440. ...calculateAtoGainWriteValues(value, inputValues)
  441. }
  442. })
  443. const nextState = {
  444. ...data,
  445. atoBandwidthInputRegisters: nextRegisters
  446. }
  447. return {
  448. ...nextState,
  449. ...buildViewState(nextState)
  450. }
  451. }
  452. function applyDqGainInput(data, index, value) {
  453. const nextRegisters = data.dqGainInputRegisters.map((item, currentIndex) => {
  454. if (currentIndex !== index) return item
  455. return {
  456. ...item,
  457. isDirty: true,
  458. inputValue: value,
  459. writeValue: calculateDqGainWriteValue(item, value)
  460. }
  461. })
  462. const nextState = {
  463. ...data,
  464. dqGainInputRegisters: nextRegisters
  465. }
  466. return {
  467. ...nextState,
  468. ...buildViewState(nextState)
  469. }
  470. }
  471. function applySpeedLoopExtraInput(data, index, value) {
  472. const changedRegisters = data.speedLoopExtraRegisters.map((item, currentIndex) => {
  473. if (currentIndex !== index) return item
  474. return {
  475. ...item,
  476. isDirty: true,
  477. inputValue: value
  478. }
  479. })
  480. const inputValues = getSharedParameterValues(data.parameterInputRegisters, changedRegisters)
  481. const nextState = {
  482. ...data,
  483. speedLoopExtraRegisters: updateSpeedLoopExtraValues(changedRegisters, inputValues)
  484. }
  485. return {
  486. ...nextState,
  487. ...buildViewState(nextState)
  488. }
  489. }
  490. function applyOilParameterInput(data, index, value) {
  491. const changedRegisters = data.oilParameterInputRegisters.map((item, currentIndex) => {
  492. if (currentIndex !== index) return item
  493. return {
  494. ...item,
  495. isDirty: true,
  496. inputValue: value
  497. }
  498. })
  499. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  500. const nextState = {
  501. ...data,
  502. oilParameterInputRegisters: updateOilParameterValues(changedRegisters, inputValues)
  503. }
  504. return nextState
  505. }
  506. function applyPrepositionParameterInput(data, index, value) {
  507. const nextState = {
  508. ...data,
  509. prepositionParameterInputRegisters: data.prepositionParameterInputRegisters.map((item, currentIndex) => {
  510. if (currentIndex !== index) return item
  511. return {
  512. ...item,
  513. isDirty: true,
  514. inputValue: value,
  515. writeValue: formatInputValue(item, value)
  516. }
  517. })
  518. }
  519. return {
  520. ...nextState,
  521. ...buildViewState(nextState)
  522. }
  523. }
  524. function applyTailwindSwitchChange(data, index, checked) {
  525. const nextState = {
  526. ...data,
  527. tailwindSwitchRegisters: data.tailwindSwitchRegisters.map((item, currentIndex) => {
  528. if (currentIndex !== index) return item
  529. return {
  530. ...item,
  531. isDirty: true,
  532. value: checked,
  533. writeValue: checked ? 1 : 0
  534. }
  535. })
  536. }
  537. return {
  538. ...nextState,
  539. ...buildViewState(nextState)
  540. }
  541. }
  542. function applyProtectionSwitchChange(data, index, checked) {
  543. return {
  544. ...data,
  545. protectionSwitchRegisters: data.protectionSwitchRegisters.map((item, currentIndex) => {
  546. if (currentIndex !== index) return item
  547. return {
  548. ...item,
  549. isDirty: true,
  550. value: checked,
  551. writeValue: checked ? 1 : 0
  552. }
  553. })
  554. }
  555. }
  556. function applyProtectionInput(data, index, value) {
  557. const nextRegisters = data.protectionRegisters.map((item, currentIndex) => {
  558. if (currentIndex !== index) return item
  559. return {
  560. ...item,
  561. isDirty: true,
  562. inputValue: value,
  563. writeValue: value === '' ? '--' : calculateProtectionWriteValue(item, toFiniteNumber(value, NaN))
  564. }
  565. })
  566. return {
  567. ...data,
  568. protectionRegisters: nextRegisters,
  569. ...buildProtectionGroups(nextRegisters)
  570. }
  571. }
  572. function getInputRegister(data, group, index) {
  573. if (group === 'parameter') return data.parameterInputRegisters[index]
  574. if (group === 'ato') return data.atoBandwidthInputRegisters[index]
  575. if (group === 'dq') return data.dqGainInputRegisters[index]
  576. if (group === 'speedLoopExtra') return data.speedLoopExtraRegisters[index]
  577. if (group === 'oil') return data.oilParameterInputRegisters[index]
  578. if (group === 'preposition') return data.prepositionParameterInputRegisters[index]
  579. if (group === 'protection') return data.protectionRegisters[index]
  580. return null
  581. }
  582. function applyInputBlur(data, group, index, value) {
  583. const item = getInputRegister(data, group, index)
  584. if (!item) return data
  585. const inputValue = appendInputUnit(item, value === undefined ? item.inputValue : value)
  586. if (group === 'parameter') return applyParameterInput(data, index, inputValue)
  587. if (group === 'ato') return applyAtoBandwidthInput(data, index, inputValue)
  588. if (group === 'dq') return applyDqGainInput(data, index, inputValue)
  589. if (group === 'speedLoopExtra') return applySpeedLoopExtraInput(data, index, inputValue)
  590. if (group === 'oil') return applyOilParameterInput(data, index, inputValue)
  591. if (group === 'preposition') return applyPrepositionParameterInput(data, index, inputValue)
  592. if (group === 'protection') return applyProtectionInput(data, index, inputValue)
  593. return data
  594. }
  595. module.exports = {
  596. applyAtoBandwidthInput,
  597. clearGroupDirty,
  598. clearProtectionSwitchDirty,
  599. clearTailwindSwitchDirty,
  600. applyDqGainInput,
  601. applyInputBlur,
  602. applyOilParameterInput,
  603. applyParameterInput,
  604. applyPrepositionParameterInput,
  605. applyProtectionInput,
  606. applyProtectionSwitchChange,
  607. applyReadValues,
  608. applySpeedLoopExtraInput,
  609. applyTailwindSwitchChange,
  610. createInitialState,
  611. refreshState
  612. }