1
0

params-state.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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.js')
  15. const {
  16. parseHexInteger
  17. } = require('../../utils/base-utils.js')
  18. const {
  19. getSharedInputValues,
  20. mergeInputValues,
  21. toFiniteNumber
  22. } = require('./calculation-context.js')
  23. const {
  24. calculateAtoGainWriteValues,
  25. calculateDqGainWriteValue,
  26. calculateParameterInputWriteValue,
  27. calculateParameterReadValue,
  28. calculateProtectionWriteValue,
  29. calculateSpeedSlope,
  30. formatFixedValue
  31. } = require('./conversions.js')
  32. const {
  33. toAddressKey,
  34. wordsToFloat
  35. } = require('../../utils/register-value-utils.js')
  36. const {
  37. appendInputUnit
  38. } = require('./input-value-utils.js')
  39. const VSP_CURVE_ORDER = [
  40. '开机电压',
  41. '关机电压',
  42. '调速最低电压',
  43. '调速最高电压'
  44. ]
  45. const SPEED_LOOP_INPUT_ORDER = [
  46. '速度最小值',
  47. '速度最大值',
  48. 'SOUT_MAX'
  49. ]
  50. const TAILWIND_CALCULATED_NAMES = [
  51. 'SPEED_KLPF_TAILWIND',
  52. 'OBS_EA_KS_TAILWIND'
  53. ]
  54. const ESTIMATOR_CALCULATED_PREFIXES = [
  55. 'OBS_E',
  56. 'OBS_FBASE',
  57. 'OBS_EA_KS',
  58. 'SPEED_KLPF',
  59. 'FOC_KFG'
  60. ]
  61. const PROTECTION_SECTION_DEFINITIONS = [
  62. {
  63. key: 'base',
  64. title: '基础',
  65. rows: [
  66. [
  67. { kind: 'switch', name: '保护使能' },
  68. { kind: 'switch', name: '恢复使能' },
  69. { kind: 'input', name: '故障恢复时间' }
  70. ]
  71. ]
  72. },
  73. {
  74. key: 'hardwareCurrent',
  75. title: '硬件过流',
  76. rows: [
  77. [
  78. { kind: 'input', name: '硬件过流值', label: '硬件过流' }
  79. ]
  80. ]
  81. },
  82. {
  83. key: 'current',
  84. title: '电流',
  85. rows: [
  86. [
  87. { kind: 'switch', name: '电流保护使能' },
  88. { kind: 'input', name: '软件过流值' }
  89. ]
  90. ]
  91. },
  92. {
  93. key: 'phase',
  94. title: '缺相',
  95. rows: [
  96. [
  97. { kind: 'switch', name: '缺相保护使能' }
  98. ]
  99. ]
  100. },
  101. {
  102. key: 'voltage',
  103. title: '电压',
  104. rows: [
  105. [
  106. { kind: 'switch', name: '电压保护使能' }
  107. ],
  108. [
  109. { kind: 'input', name: '过压保护值', label: '过压值' },
  110. { kind: 'input', name: '欠压保护值', label: '欠压值' }
  111. ],
  112. [
  113. { kind: 'input', name: '过压恢复值' },
  114. { kind: 'input', name: '欠压恢复值' }
  115. ]
  116. ]
  117. },
  118. {
  119. key: 'stall',
  120. title: '堵转',
  121. rows: [
  122. [
  123. { kind: 'switch', name: '堵转保护使能' }
  124. ],
  125. [
  126. { kind: 'input', name: '速度限制最大值' },
  127. { kind: 'input', name: '速度限制最小值' }
  128. ],
  129. [
  130. { kind: 'input', name: '反电动势低阈值' },
  131. { kind: 'input', name: '反电动势高阈值' }
  132. ],
  133. [
  134. { kind: 'input', name: '速度中间值' }
  135. ]
  136. ]
  137. },
  138. {
  139. key: 'power',
  140. title: '功率',
  141. rows: [
  142. [
  143. { kind: 'switch', name: '功率保护使能' },
  144. { kind: 'input', name: '功率保护值' },
  145. { kind: 'input', name: '功率保护时间' }
  146. ]
  147. ]
  148. },
  149. {
  150. key: 'temperature',
  151. title: '温度',
  152. rows: [
  153. [
  154. { kind: 'switch', name: '温度保护使能' }
  155. ],
  156. [
  157. { kind: 'input', name: '温度保护值' },
  158. { kind: 'input', name: '温度恢复值' },
  159. { kind: 'input', name: '温度保护时间' }
  160. ]
  161. ]
  162. },
  163. {
  164. key: 'serial',
  165. title: '串口',
  166. rows: [
  167. [
  168. { kind: 'switch', name: '串口保护使能' },
  169. { kind: 'input', name: '串口丢失检测时间' }
  170. ]
  171. ]
  172. },
  173. {
  174. key: 'pwm',
  175. title: 'PWM',
  176. rows: [
  177. [
  178. { kind: 'switch', name: 'PWM丢失保护使能' }
  179. ]
  180. ]
  181. }
  182. ]
  183. function formatInputValue(item, value) {
  184. if (value === '' || value === undefined || value === null) return '--'
  185. const numberValue = toFiniteNumber(value, NaN)
  186. if (!Number.isFinite(numberValue)) return value
  187. if (item.type === 'uint8_t' && (numberValue < 0 || numberValue > 0xFF)) return '--'
  188. if (item.type === 'float') return formatFixedValue(numberValue, 2)
  189. return String(Math.round(numberValue))
  190. }
  191. function getInputValues(registers) {
  192. return mergeInputValues(registers)
  193. }
  194. function getSharedParameterValues(registers, extraRegisters = []) {
  195. return {
  196. ...getSharedInputValues(),
  197. ...getInputValues(registers.concat(extraRegisters))
  198. }
  199. }
  200. function updateAtoBandwidthValues(registers, inputValues) {
  201. return registers.map((item) => ({
  202. ...item,
  203. ...calculateAtoGainWriteValues(item.inputValue, inputValues)
  204. }))
  205. }
  206. function updateInputWriteValues(registers) {
  207. const inputValues = getInputValues(registers)
  208. return registers.map((item) => ({
  209. ...item,
  210. writeValue: calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  211. }))
  212. }
  213. function updateSpeedLoopExtraValues(registers, inputValues) {
  214. return registers.map((item) => {
  215. const writeValue = calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  216. return {
  217. ...item,
  218. actualText: '',
  219. writeValue
  220. }
  221. })
  222. }
  223. function updateOilParameterValues(registers, inputValues) {
  224. return registers.map((item) => ({
  225. ...item,
  226. writeValue: calculateParameterInputWriteValue(item, item.inputValue, inputValues)
  227. }))
  228. }
  229. function updateSpeedSlope(register, inputValues) {
  230. const speedSlope = calculateSpeedSlope(inputValues)
  231. return {
  232. ...register,
  233. writeValue: speedSlope === null ? '--' : formatFixedValue(speedSlope, 2)
  234. }
  235. }
  236. function addSourceIndex(registers) {
  237. return registers.map((item, index) => ({
  238. ...item,
  239. sourceIndex: index
  240. }))
  241. }
  242. function isNameIn(names, item) {
  243. return names.includes(item.name)
  244. }
  245. function isTailwindAtoRegister(item) {
  246. return item.suffix === 'TAILWIND'
  247. }
  248. function isTailwindCalculatedRegister(item) {
  249. return isNameIn(TAILWIND_CALCULATED_NAMES, item)
  250. }
  251. function isEstimatorCalculatedRegister(item) {
  252. if (isTailwindCalculatedRegister(item)) return false
  253. return ESTIMATOR_CALCULATED_PREFIXES.some((prefix) => item.name.startsWith(prefix))
  254. }
  255. function sortByNameOrder(registers, nameOrder) {
  256. return registers
  257. .filter((item) => nameOrder.includes(item.name))
  258. .slice()
  259. .sort((left, right) => nameOrder.indexOf(left.name) - nameOrder.indexOf(right.name))
  260. }
  261. function mapByName(registers) {
  262. return registers.reduce((result, item) => {
  263. result[item.name] = item
  264. return result
  265. }, {})
  266. }
  267. function buildProtectionField(definition, registerMap, switchMap) {
  268. const source = definition.kind === 'switch'
  269. ? switchMap[definition.name]
  270. : registerMap[definition.name]
  271. if (!source) return null
  272. return {
  273. ...source,
  274. kind: definition.kind,
  275. label: definition.label || source.name,
  276. metaValue: source.writeValue === 0 ? '0' : (source.writeValue || '--')
  277. }
  278. }
  279. function buildProtectionGroups(registers, switches = []) {
  280. const protectionDisplayRegisters = addSourceIndex(registers)
  281. const protectionSwitchDisplayRegisters = addSourceIndex(switches)
  282. const registerMap = mapByName(protectionDisplayRegisters)
  283. const switchMap = mapByName(protectionSwitchDisplayRegisters)
  284. return {
  285. protectionDisplayRegisters,
  286. protectionSections: PROTECTION_SECTION_DEFINITIONS.map((section) => ({
  287. ...section,
  288. rows: section.rows
  289. .map((row, rowIndex) => {
  290. const fields = row
  291. .map((definition) => buildProtectionField(definition, registerMap, switchMap))
  292. .filter(Boolean)
  293. return fields.length
  294. ? {
  295. fields,
  296. key: `${section.key}-${rowIndex}`
  297. }
  298. : null
  299. })
  300. .filter(Boolean)
  301. })).filter((section) => section.rows.length)
  302. }
  303. }
  304. function getWord(readValues, address) {
  305. return readValues.words[toAddressKey(address)]
  306. }
  307. function getRegisterReadValue(item, readValues) {
  308. if (item.area && item.area.key === 'coil') {
  309. const coilValue = readValues.coils[toAddressKey(item.address)]
  310. return coilValue === undefined ? null : coilValue
  311. }
  312. const firstWord = getWord(readValues, item.address)
  313. if (!Number.isInteger(firstWord)) return null
  314. if (item.type === 'uint8_t' && item.bytePosition) {
  315. return getByteRegisterValue(item, firstWord)
  316. }
  317. if (item.type === 'float') {
  318. const nextAddress = (parseHexInteger(item.address) + 1).toString(16).toUpperCase()
  319. return wordsToFloat(firstWord, getWord(readValues, nextAddress))
  320. }
  321. return firstWord
  322. }
  323. function formatReadValue(item, value) {
  324. if (value === null || value === undefined || !Number.isFinite(Number(value))) return null
  325. const numberValue = Number(value)
  326. if (item.type === 'float') return formatFixedValue(numberValue, 2)
  327. return String(Math.round(numberValue))
  328. }
  329. function getReadInputValue(item, readValue, readText, options = {}) {
  330. let inputText = readText
  331. if (options.useCalculatedInputValue) {
  332. const calculatedValue = calculateParameterReadValue(item, readValue, options.inputValues || {})
  333. if (calculatedValue !== null) {
  334. inputText = formatFixedValue(calculatedValue, 2)
  335. }
  336. }
  337. return appendInputUnit(item, inputText)
  338. }
  339. function getDqReadInputValue(item, rawText) {
  340. const rawValue = Number(rawText)
  341. if (!Number.isFinite(rawValue)) return ''
  342. if (item.gainType === 'kp') return formatFixedValue(rawValue / 4095, 2)
  343. if (item.gainType === 'ki') return formatFixedValue(rawValue / 32767, 2)
  344. return rawText
  345. }
  346. function applyReadValuesToRegisters(registers, readValues, options = {}) {
  347. return registers.map((item) => {
  348. const readValue = getRegisterReadValue(item, readValues)
  349. const readText = formatReadValue(item, readValue)
  350. if (readText === null) return item
  351. const nextItem = {
  352. ...item,
  353. isDirty: false,
  354. writeValue: readText
  355. }
  356. if (Object.prototype.hasOwnProperty.call(item, 'value')) {
  357. nextItem.value = Number(readValue) !== 0
  358. }
  359. if (options.updateInputValue) {
  360. nextItem.inputValue = getReadInputValue(item, readValue, readText, options)
  361. }
  362. if (options.updateDqInputValue) {
  363. nextItem.inputValue = getDqReadInputValue(item, readText)
  364. }
  365. return nextItem
  366. })
  367. }
  368. function applyReadValuesToAtoRegisters(registers, readValues) {
  369. return registers.map((item) => {
  370. const kpWord = getWord(readValues, item.kpAddress)
  371. const kiWord = getWord(readValues, item.kiAddress)
  372. if (!Number.isInteger(kpWord) && !Number.isInteger(kiWord)) return item
  373. return {
  374. ...item,
  375. isDirty: false,
  376. kpWriteValue: Number.isInteger(kpWord) ? String(kpWord) : item.kpWriteValue,
  377. kiWriteValue: Number.isInteger(kiWord) ? String(kiWord) : item.kiWriteValue
  378. }
  379. })
  380. }
  381. function clearDirty(registers = [], matcher = () => true) {
  382. return registers.map((item) => (
  383. matcher(item)
  384. ? {
  385. ...item,
  386. isDirty: false
  387. }
  388. : item
  389. ))
  390. }
  391. function clearGroupDirty(data, groupKey) {
  392. const nextState = {
  393. ...data
  394. }
  395. if (groupKey === 'estimator') {
  396. nextState.atoBandwidthInputRegisters = clearDirty(data.atoBandwidthInputRegisters)
  397. nextState.calculatedParameterRegisters = clearDirty(data.calculatedParameterRegisters, isEstimatorCalculatedRegister)
  398. }
  399. if (groupKey === 'dq') nextState.dqGainInputRegisters = clearDirty(data.dqGainInputRegisters)
  400. if (groupKey === 'tailwind') {
  401. nextState.tailwindSwitchRegisters = clearDirty(data.tailwindSwitchRegisters, (item) => item.name !== '预定位启用')
  402. nextState.atoBandwidthInputRegisters = clearDirty(data.atoBandwidthInputRegisters, isTailwindAtoRegister)
  403. nextState.calculatedParameterRegisters = clearDirty(data.calculatedParameterRegisters, isTailwindCalculatedRegister)
  404. }
  405. if (groupKey === 'preposition') {
  406. nextState.tailwindSwitchRegisters = clearDirty(data.tailwindSwitchRegisters, (item) => item.name === '预定位启用')
  407. nextState.prepositionParameterInputRegisters = clearDirty(data.prepositionParameterInputRegisters)
  408. }
  409. if (groupKey === 'speedLoop') {
  410. nextState.parameterInputRegisters = clearDirty(data.parameterInputRegisters, (item) => (
  411. SPEED_LOOP_INPUT_ORDER.includes(item.name)
  412. ))
  413. nextState.speedLoopExtraRegisters = clearDirty(data.speedLoopExtraRegisters)
  414. }
  415. if (groupKey === 'vsp') {
  416. nextState.parameterInputRegisters = clearDirty(data.parameterInputRegisters, (item) => VSP_CURVE_ORDER.includes(item.name))
  417. nextState.speedSlopeRegister = {
  418. ...data.speedSlopeRegister,
  419. isDirty: false
  420. }
  421. }
  422. if (groupKey === 'oil') nextState.oilParameterInputRegisters = clearDirty(data.oilParameterInputRegisters)
  423. if (groupKey === 'protection') {
  424. nextState.protectionRegisters = clearDirty(data.protectionRegisters)
  425. nextState.protectionSwitchRegisters = clearDirty(data.protectionSwitchRegisters)
  426. }
  427. return {
  428. ...nextState,
  429. ...buildViewState(nextState)
  430. }
  431. }
  432. function clearRegisterDirty(registers = [], index) {
  433. return registers.map((item, currentIndex) => (
  434. currentIndex === index
  435. ? {
  436. ...item,
  437. isDirty: false
  438. }
  439. : item
  440. ))
  441. }
  442. function clearTailwindSwitchDirty(data, index) {
  443. const nextState = {
  444. ...data,
  445. tailwindSwitchRegisters: clearRegisterDirty(data.tailwindSwitchRegisters, index)
  446. }
  447. return {
  448. ...nextState,
  449. ...buildViewState(nextState)
  450. }
  451. }
  452. function clearProtectionSwitchDirty(data, index) {
  453. const nextState = {
  454. ...data,
  455. protectionSwitchRegisters: clearRegisterDirty(data.protectionSwitchRegisters, index)
  456. }
  457. return {
  458. ...nextState,
  459. ...buildViewState(nextState)
  460. }
  461. }
  462. function buildViewState(state) {
  463. const inputRegisters = addSourceIndex(state.parameterInputRegisters)
  464. const atoRegisters = addSourceIndex(state.atoBandwidthInputRegisters)
  465. const dqRegisters = addSourceIndex(state.dqGainInputRegisters)
  466. const calculatedRegisters = addSourceIndex(state.calculatedParameterRegisters)
  467. const speedLoopExtras = addSourceIndex(state.speedLoopExtraRegisters)
  468. const tailwindSwitches = addSourceIndex(state.tailwindSwitchRegisters)
  469. return {
  470. vspCurveRegisters: sortByNameOrder(inputRegisters, VSP_CURVE_ORDER),
  471. speedLoopInputDisplayRegisters: sortByNameOrder(inputRegisters, SPEED_LOOP_INPUT_ORDER),
  472. speedLoopExtraDisplayRegisters: speedLoopExtras,
  473. atoBandwidthDisplayRegisters: atoRegisters.filter((item) => !isTailwindAtoRegister(item)),
  474. tailwindAtoBandwidthDisplayRegisters: atoRegisters.filter(isTailwindAtoRegister),
  475. tailwindCalculatedDisplayRegisters: calculatedRegisters.filter(isTailwindCalculatedRegister),
  476. tailwindControlRegisters: tailwindSwitches.filter((item) => item.name !== '预定位启用'),
  477. prepositionSwitchRegisters: tailwindSwitches.filter((item) => item.name === '预定位启用'),
  478. prepositionParameterDisplayRegisters: addSourceIndex(state.prepositionParameterInputRegisters),
  479. dqGainDisplayRegisters: dqRegisters,
  480. estimatorCalculatedDisplayRegisters: calculatedRegisters.filter(isEstimatorCalculatedRegister),
  481. ...buildProtectionGroups(state.protectionRegisters, state.protectionSwitchRegisters)
  482. }
  483. }
  484. function createInitialState() {
  485. const state = {
  486. atoBandwidthInputRegisters,
  487. atoBandwidthDisplayRegisters: [],
  488. calculatedParameterRegisters,
  489. dqGainInputRegisters,
  490. dqGainDisplayRegisters: [],
  491. estimatorCalculatedDisplayRegisters: [],
  492. oilParameterInputRegisters,
  493. parameterInputRegisters,
  494. prepositionParameterDisplayRegisters: [],
  495. prepositionParameterInputRegisters,
  496. prepositionSwitchRegisters: [],
  497. protectionDisplayRegisters: [],
  498. protectionSections: [],
  499. protectionRegisters,
  500. protectionSwitchRegisters,
  501. speedLoopExtraRegisters,
  502. speedLoopExtraDisplayRegisters: [],
  503. speedLoopInputDisplayRegisters: [],
  504. speedSlopeRegister,
  505. tailwindAtoBandwidthDisplayRegisters: [],
  506. tailwindCalculatedDisplayRegisters: [],
  507. tailwindControlRegisters: [],
  508. tailwindSwitchRegisters,
  509. vspCurveRegisters: []
  510. }
  511. return {
  512. ...state,
  513. ...buildViewState(state)
  514. }
  515. }
  516. function refreshState(data) {
  517. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  518. const nextSpeedLoopExtraRegisters = updateSpeedLoopExtraValues(data.speedLoopExtraRegisters, inputValues)
  519. const nextState = {
  520. ...data,
  521. atoBandwidthInputRegisters: updateAtoBandwidthValues(data.atoBandwidthInputRegisters, inputValues),
  522. oilParameterInputRegisters: updateOilParameterValues(data.oilParameterInputRegisters, inputValues),
  523. speedLoopExtraRegisters: nextSpeedLoopExtraRegisters,
  524. speedSlopeRegister: updateSpeedSlope(data.speedSlopeRegister, inputValues)
  525. }
  526. return {
  527. ...nextState,
  528. ...buildViewState(nextState)
  529. }
  530. }
  531. function applyReadValues(data, readValues) {
  532. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  533. const nextState = {
  534. ...data,
  535. atoBandwidthInputRegisters: applyReadValuesToAtoRegisters(data.atoBandwidthInputRegisters, readValues),
  536. calculatedParameterRegisters: applyReadValuesToRegisters(data.calculatedParameterRegisters, readValues),
  537. dqGainInputRegisters: applyReadValuesToRegisters(data.dqGainInputRegisters, readValues, {
  538. updateDqInputValue: true
  539. }),
  540. oilParameterInputRegisters: applyReadValuesToRegisters(data.oilParameterInputRegisters, readValues, {
  541. inputValues,
  542. updateInputValue: true,
  543. useCalculatedInputValue: true
  544. }),
  545. parameterInputRegisters: applyReadValuesToRegisters(data.parameterInputRegisters, readValues, {
  546. inputValues,
  547. updateInputValue: true,
  548. useCalculatedInputValue: true
  549. }),
  550. prepositionParameterInputRegisters: applyReadValuesToRegisters(data.prepositionParameterInputRegisters, readValues, {
  551. updateInputValue: true
  552. }),
  553. protectionRegisters: applyReadValuesToRegisters(data.protectionRegisters, readValues, {
  554. inputValues,
  555. updateInputValue: true,
  556. useCalculatedInputValue: true
  557. }),
  558. protectionSwitchRegisters: applyReadValuesToRegisters(data.protectionSwitchRegisters, readValues),
  559. speedLoopExtraRegisters: applyReadValuesToRegisters(data.speedLoopExtraRegisters, readValues, {
  560. updateInputValue: true
  561. }),
  562. speedSlopeRegister: applyReadValuesToRegisters([data.speedSlopeRegister], readValues)[0],
  563. tailwindSwitchRegisters: applyReadValuesToRegisters(data.tailwindSwitchRegisters, readValues)
  564. }
  565. return {
  566. ...nextState,
  567. ...buildViewState(nextState)
  568. }
  569. }
  570. function applyParameterInput(data, index, value) {
  571. const changedRegisters = data.parameterInputRegisters.map((item, currentIndex) => {
  572. if (currentIndex !== index) return item
  573. return {
  574. ...item,
  575. isDirty: true,
  576. inputValue: value,
  577. writeValue: formatInputValue(item, value)
  578. }
  579. })
  580. const nextRegisters = updateInputWriteValues(changedRegisters)
  581. const inputValues = getSharedParameterValues(nextRegisters, data.speedLoopExtraRegisters)
  582. const nextState = {
  583. ...data,
  584. parameterInputRegisters: nextRegisters,
  585. atoBandwidthInputRegisters: updateAtoBandwidthValues(data.atoBandwidthInputRegisters, inputValues),
  586. speedLoopExtraRegisters: updateSpeedLoopExtraValues(data.speedLoopExtraRegisters, inputValues),
  587. speedSlopeRegister: updateSpeedSlope(data.speedSlopeRegister, inputValues)
  588. }
  589. return {
  590. ...nextState,
  591. ...buildViewState(nextState)
  592. }
  593. }
  594. function applyAtoBandwidthInput(data, index, value) {
  595. const inputValues = getSharedParameterValues(data.parameterInputRegisters)
  596. const nextRegisters = data.atoBandwidthInputRegisters.map((item, currentIndex) => {
  597. if (currentIndex !== index) return item
  598. return {
  599. ...item,
  600. isDirty: true,
  601. inputValue: value,
  602. ...calculateAtoGainWriteValues(value, inputValues)
  603. }
  604. })
  605. const nextState = {
  606. ...data,
  607. atoBandwidthInputRegisters: nextRegisters
  608. }
  609. return {
  610. ...nextState,
  611. ...buildViewState(nextState)
  612. }
  613. }
  614. function applyDqGainInput(data, index, value) {
  615. const nextRegisters = data.dqGainInputRegisters.map((item, currentIndex) => {
  616. if (currentIndex !== index) return item
  617. return {
  618. ...item,
  619. isDirty: true,
  620. inputValue: value,
  621. writeValue: calculateDqGainWriteValue(item, value)
  622. }
  623. })
  624. const nextState = {
  625. ...data,
  626. dqGainInputRegisters: nextRegisters
  627. }
  628. return {
  629. ...nextState,
  630. ...buildViewState(nextState)
  631. }
  632. }
  633. function applySpeedLoopExtraInput(data, index, value) {
  634. const changedRegisters = data.speedLoopExtraRegisters.map((item, currentIndex) => {
  635. if (currentIndex !== index) return item
  636. return {
  637. ...item,
  638. isDirty: true,
  639. inputValue: value
  640. }
  641. })
  642. const inputValues = getSharedParameterValues(data.parameterInputRegisters, changedRegisters)
  643. const nextState = {
  644. ...data,
  645. speedLoopExtraRegisters: updateSpeedLoopExtraValues(changedRegisters, inputValues)
  646. }
  647. return {
  648. ...nextState,
  649. ...buildViewState(nextState)
  650. }
  651. }
  652. function applyOilParameterInput(data, index, value) {
  653. const changedRegisters = data.oilParameterInputRegisters.map((item, currentIndex) => {
  654. if (currentIndex !== index) return item
  655. return {
  656. ...item,
  657. isDirty: true,
  658. inputValue: value
  659. }
  660. })
  661. const inputValues = getSharedParameterValues(data.parameterInputRegisters, data.speedLoopExtraRegisters)
  662. const nextState = {
  663. ...data,
  664. oilParameterInputRegisters: updateOilParameterValues(changedRegisters, inputValues)
  665. }
  666. return nextState
  667. }
  668. function applyPrepositionParameterInput(data, index, value) {
  669. const nextState = {
  670. ...data,
  671. prepositionParameterInputRegisters: data.prepositionParameterInputRegisters.map((item, currentIndex) => {
  672. if (currentIndex !== index) return item
  673. return {
  674. ...item,
  675. isDirty: true,
  676. inputValue: value,
  677. writeValue: formatInputValue(item, value)
  678. }
  679. })
  680. }
  681. return {
  682. ...nextState,
  683. ...buildViewState(nextState)
  684. }
  685. }
  686. function applyTailwindSwitchChange(data, index, checked) {
  687. const nextState = {
  688. ...data,
  689. tailwindSwitchRegisters: data.tailwindSwitchRegisters.map((item, currentIndex) => {
  690. if (currentIndex !== index) return item
  691. return {
  692. ...item,
  693. isDirty: true,
  694. value: checked,
  695. writeValue: checked ? 1 : 0
  696. }
  697. })
  698. }
  699. return {
  700. ...nextState,
  701. ...buildViewState(nextState)
  702. }
  703. }
  704. function applyProtectionSwitchChange(data, index, checked) {
  705. const nextState = {
  706. ...data,
  707. protectionSwitchRegisters: data.protectionSwitchRegisters.map((item, currentIndex) => {
  708. if (currentIndex !== index) return item
  709. return {
  710. ...item,
  711. isDirty: true,
  712. value: checked,
  713. writeValue: checked ? 1 : 0
  714. }
  715. })
  716. }
  717. return {
  718. ...nextState,
  719. ...buildViewState(nextState)
  720. }
  721. }
  722. function applyProtectionInput(data, index, value) {
  723. const nextRegisters = data.protectionRegisters.map((item, currentIndex) => {
  724. if (currentIndex !== index) return item
  725. return {
  726. ...item,
  727. isDirty: true,
  728. inputValue: value,
  729. writeValue: value === '' ? '--' : calculateProtectionWriteValue(item, toFiniteNumber(value, NaN))
  730. }
  731. })
  732. return {
  733. ...data,
  734. protectionRegisters: nextRegisters,
  735. ...buildProtectionGroups(nextRegisters, data.protectionSwitchRegisters)
  736. }
  737. }
  738. function getInputRegister(data, group, index) {
  739. if (group === 'parameter') return data.parameterInputRegisters[index]
  740. if (group === 'ato') return data.atoBandwidthInputRegisters[index]
  741. if (group === 'dq') return data.dqGainInputRegisters[index]
  742. if (group === 'speedLoopExtra') return data.speedLoopExtraRegisters[index]
  743. if (group === 'oil') return data.oilParameterInputRegisters[index]
  744. if (group === 'preposition') return data.prepositionParameterInputRegisters[index]
  745. if (group === 'protection') return data.protectionRegisters[index]
  746. return null
  747. }
  748. function applyInputBlur(data, group, index, value) {
  749. const item = getInputRegister(data, group, index)
  750. if (!item) return data
  751. const inputValue = appendInputUnit(item, value === undefined ? item.inputValue : value)
  752. if (group === 'parameter') return applyParameterInput(data, index, inputValue)
  753. if (group === 'ato') return applyAtoBandwidthInput(data, index, inputValue)
  754. if (group === 'dq') return applyDqGainInput(data, index, inputValue)
  755. if (group === 'speedLoopExtra') return applySpeedLoopExtraInput(data, index, inputValue)
  756. if (group === 'oil') return applyOilParameterInput(data, index, inputValue)
  757. if (group === 'preposition') return applyPrepositionParameterInput(data, index, inputValue)
  758. if (group === 'protection') return applyProtectionInput(data, index, inputValue)
  759. return data
  760. }
  761. module.exports = {
  762. applyAtoBandwidthInput,
  763. clearGroupDirty,
  764. clearProtectionSwitchDirty,
  765. clearTailwindSwitchDirty,
  766. applyDqGainInput,
  767. applyInputBlur,
  768. applyOilParameterInput,
  769. applyParameterInput,
  770. applyPrepositionParameterInput,
  771. applyProtectionInput,
  772. applyProtectionSwitchChange,
  773. applyReadValues,
  774. applySpeedLoopExtraInput,
  775. applyTailwindSwitchChange,
  776. createInitialState,
  777. refreshState
  778. }