status-state.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const {
  2. statusRegisters
  3. } = require('./registers.js')
  4. const {
  5. MAX_USER_STATUS_COUNT,
  6. getUserStatusCount
  7. } = require('./control-state.js')
  8. const {
  9. formatStatusRegisters
  10. } = require('./status-format.js')
  11. const STATUS_SUMMARY_METRICS = [
  12. { key: 'speed', name: '估算速度', unit: 'RPM', decimals: 0 },
  13. { key: 'voltage', name: '母线电压', unit: 'V', decimals: 1 },
  14. { key: 'power', name: '估算功率', unit: 'W', decimals: 1 },
  15. { key: 'temperature', name: 'NTC 温度', unit: '℃', decimals: 0 }
  16. ]
  17. const STATUS_GROUPS = [
  18. {
  19. key: 'system',
  20. title: '状态机 / 故障码',
  21. names: ['状态机', '故障码']
  22. },
  23. {
  24. key: 'dqVoltageCurrent',
  25. title: 'DQ 电压电流',
  26. names: ['UD', 'UQ', 'ID', 'IQ']
  27. },
  28. {
  29. key: 'phaseCurrent',
  30. title: '相电流',
  31. names: ['A 相电流', 'B 相电流', 'C 相电流', '相电流最大值', '相电流最小值']
  32. },
  33. {
  34. key: 'estimator',
  35. title: '估算器状态',
  36. names: ['估算速度', '估算反电动势']
  37. },
  38. {
  39. key: 'bus',
  40. title: '母线 / 温度',
  41. names: ['母线电压', '母线电流', '估算功率', 'NTC 温度']
  42. },
  43. {
  44. key: 'inputPwm',
  45. title: '输入 / PWM',
  46. names: ['模拟输入电压', '占空比', '频率']
  47. }
  48. ]
  49. function getVisibleStatusRegisters(userStatusCount) {
  50. const count = getUserStatusCount(userStatusCount)
  51. return statusRegisters.filter((item) => (
  52. item.name.indexOf('用户状态字') !== 0 ||
  53. Number(item.name.replace('用户状态字 ', '')) <= count
  54. ))
  55. }
  56. function getStatusPageState(userStatusCount) {
  57. const registers = formatStatusRegisters(getVisibleStatusRegisters(userStatusCount))
  58. const registerMap = registers.reduce((result, item) => {
  59. result[item.name] = item
  60. return result
  61. }, {})
  62. return {
  63. maxUserStatusCount: MAX_USER_STATUS_COUNT,
  64. statusRegisterGroups: STATUS_GROUPS.map((group) => ({
  65. key: group.key,
  66. title: group.title,
  67. registers: group.names.map((name) => registerMap[name]).filter(Boolean)
  68. })).filter((group) => group.registers.length),
  69. statusRegisters: registers,
  70. userStatusRegisters: registers.filter((item) => item.name.indexOf('用户状态字') === 0)
  71. }
  72. }
  73. function getFormattedStatusMap() {
  74. const formattedRegisters = formatStatusRegisters(statusRegisters)
  75. return formattedRegisters.reduce((result, item) => {
  76. result[item.name] = item
  77. return result
  78. }, {})
  79. }
  80. function formatMetricText(item, unit, decimals) {
  81. if (!item || item.displayValue === undefined || item.displayValue === '--') return '--'
  82. const numberValue = Number(item.displayValue)
  83. const displayValue = Number.isFinite(numberValue)
  84. ? numberValue.toFixed(decimals)
  85. : String(item.displayValue)
  86. return `${displayValue}${unit}`
  87. }
  88. function getStatusSummaryState() {
  89. const registerMap = getFormattedStatusMap()
  90. const stateValue = registerMap['状态机'] && registerMap['状态机'].displayValue
  91. const faultValue = registerMap['故障码'] && registerMap['故障码'].displayValue
  92. const faultText = faultValue || '--'
  93. const isFault = faultText !== '--' && faultText !== '无故障'
  94. return {
  95. faultClass: isFault ? 'is-warning' : '',
  96. faultText,
  97. metrics: STATUS_SUMMARY_METRICS.map((config) => ({
  98. key: config.key,
  99. displayText: formatMetricText(registerMap[config.name], config.unit, config.decimals)
  100. })),
  101. stateText: stateValue || '--'
  102. }
  103. }
  104. module.exports = {
  105. getStatusPageState,
  106. getStatusSummaryState
  107. }