status-format.js 2.7 KB

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