status-format.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const {
  2. getFaultText
  3. } = require('./calculation-context.js')
  4. const {
  5. parseHexInteger
  6. } = require('../../utils/base-utils.js')
  7. const {
  8. calculateStatusValue,
  9. formatFixedValue
  10. } = require('./conversions.js')
  11. const {
  12. getByteRegisterValue
  13. } = require('./registers.js')
  14. const statusWordValues = {}
  15. const statusRawValues = {}
  16. const SYS_STATE_TEXT = {
  17. 0: '就绪状态',
  18. 1: '初始化',
  19. 2: '检测偏置电压',
  20. 3: '预充电',
  21. 4: '风向检测',
  22. 5: '初始位置检测',
  23. 6: '预定位',
  24. 7: '电机启动',
  25. 8: '电机运行',
  26. 9: '电机停止',
  27. 10: '刹车',
  28. 11: '故障'
  29. }
  30. const FORMULA_STATUS_NAMES = [
  31. '母线电压',
  32. '模拟输入电压',
  33. '母线电流',
  34. 'NTC 温度',
  35. '估算速度',
  36. '估算功率',
  37. '频率',
  38. '占空比'
  39. ]
  40. function formatStatusRegister(item) {
  41. const rawValue = item.type === 'uint8_t' && item.bytePosition && statusWordValues[item.address] !== undefined
  42. ? getByteRegisterValue(item, statusWordValues[item.address])
  43. : statusRawValues[item.name]
  44. const hasFormula = FORMULA_STATUS_NAMES.includes(item.name)
  45. let displayValue = rawValue === undefined ? '--' : String(rawValue)
  46. if (item.name === '故障码' && rawValue !== undefined) {
  47. displayValue = getFaultText(rawValue)
  48. }
  49. if (item.name === '状态机' && rawValue !== undefined) {
  50. displayValue = SYS_STATE_TEXT[rawValue] || `未知状态 ${rawValue}`
  51. }
  52. if (hasFormula && rawValue !== undefined) {
  53. const calculatedValue = calculateStatusValue(item.name, rawValue)
  54. displayValue = item.name === '频率' || item.name === '占空比'
  55. ? formatFixedValue(calculatedValue, 1)
  56. : formatFixedValue(calculatedValue, 2)
  57. }
  58. return {
  59. ...item,
  60. rawValue: rawValue === undefined ? '--' : rawValue,
  61. displayUnit: displayValue === '--' ? '' : item.displayUnit || item.unit || '',
  62. displayValue
  63. }
  64. }
  65. function formatStatusRegisters(registers) {
  66. return registers.map(formatStatusRegister)
  67. }
  68. function toSigned16(value) {
  69. const wordValue = Number(value) & 0xFFFF
  70. return wordValue > 0x7FFF ? wordValue - 0x10000 : wordValue
  71. }
  72. function updateStatusRegisterWords(registers, startAddress, words) {
  73. const start = Number(startAddress)
  74. registers.forEach((item) => {
  75. const offset = parseHexInteger(item.address) - start
  76. if (offset < 0 || offset >= words.length) return
  77. const wordValue = Number(words[offset]) & 0xFFFF
  78. statusWordValues[item.address] = wordValue
  79. if (item.type === 'uint8_t' && item.bytePosition) {
  80. statusRawValues[item.name] = getByteRegisterValue(item, wordValue)
  81. return
  82. }
  83. statusRawValues[item.name] = item.type === 'int16_t' ? toSigned16(wordValue) : wordValue
  84. })
  85. return formatStatusRegisters(registers)
  86. }
  87. module.exports = {
  88. formatStatusRegisters,
  89. updateStatusRegisterWords
  90. }