status-page-state.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const {
  2. statusRegisters
  3. } = require('./registers')
  4. const {
  5. MAX_USER_STATUS_COUNT,
  6. getUserStatusCount
  7. } = require('./control-page-state')
  8. const {
  9. formatStatusRegisters
  10. } = require('./status-format')
  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. function getVisibleStatusRegisters(userStatusCount) {
  18. const count = getUserStatusCount(userStatusCount)
  19. return statusRegisters.filter((item) => (
  20. item.name.indexOf('用户状态字') !== 0 ||
  21. Number(item.name.replace('用户状态字 ', '')) <= count
  22. ))
  23. }
  24. function getStatusPageState(userStatusCount) {
  25. return {
  26. maxUserStatusCount: MAX_USER_STATUS_COUNT,
  27. statusRegisters: formatStatusRegisters(getVisibleStatusRegisters(userStatusCount))
  28. }
  29. }
  30. function getFormattedStatusMap() {
  31. const formattedRegisters = formatStatusRegisters(statusRegisters)
  32. return formattedRegisters.reduce((result, item) => {
  33. result[item.name] = item
  34. return result
  35. }, {})
  36. }
  37. function formatMetricText(item, unit, decimals) {
  38. if (!item || item.displayValue === undefined || item.displayValue === '--') return '--'
  39. const numberValue = Number(item.displayValue)
  40. const displayValue = Number.isFinite(numberValue)
  41. ? numberValue.toFixed(decimals)
  42. : String(item.displayValue)
  43. return `${displayValue}${unit}`
  44. }
  45. function getStatusSummaryState() {
  46. const registerMap = getFormattedStatusMap()
  47. const stateValue = registerMap['状态机'] && registerMap['状态机'].displayValue
  48. const faultValue = registerMap['故障码'] && registerMap['故障码'].displayValue
  49. const faultText = faultValue || '--'
  50. const isFault = faultText !== '--' && faultText !== '无故障'
  51. return {
  52. faultClass: isFault ? 'is-warning' : '',
  53. faultText,
  54. metrics: STATUS_SUMMARY_METRICS.map((config) => ({
  55. key: config.key,
  56. displayText: formatMetricText(registerMap[config.name], config.unit, config.decimals)
  57. })),
  58. stateText: stateValue || '--'
  59. }
  60. }
  61. module.exports = {
  62. getStatusPageState,
  63. getStatusSummaryState
  64. }