view-model.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const storageAccessManualCommand = require('../storage-access/manual-command.js')
  2. const settingsService = require('../../store/settings-store.js')
  3. const {
  4. bytesToHex,
  5. bytesToUtf8Text,
  6. stringToUtf8Bytes
  7. } = require('../../utils/binary-utils.js')
  8. const {
  9. parseHexBytes,
  10. validateHexText
  11. } = require('../../utils/validation.js')
  12. const LOG_MODE_OPTIONS = [
  13. { key: 'hex', label: 'HEX' },
  14. { key: 'text', label: '文本' }
  15. ]
  16. function getStorageAccessAreaOptions() {
  17. return storageAccessManualCommand.getMemoryAreaOptions()
  18. }
  19. function getBinaryModeLabel(mode) {
  20. return mode === 'text' ? '文本' : 'HEX'
  21. }
  22. function getNextBinaryMode(mode) {
  23. return mode === 'hex' ? 'text' : 'hex'
  24. }
  25. function getBinaryModeToggleText(mode) {
  26. return getBinaryModeLabel(getNextBinaryMode(mode))
  27. }
  28. function normalizeSerialState(current = {}, changed = {}) {
  29. const next = {
  30. serialInputText: current.serialInputText || '',
  31. serialMode: current.serialMode || 'hex',
  32. ...changed
  33. }
  34. const mode = next.serialMode === 'text' ? 'text' : 'hex'
  35. const inputText = String(next.serialInputText || '')
  36. let errorText = ''
  37. let bytes = []
  38. let previewHex = ''
  39. if (inputText.trim()) {
  40. if (mode === 'hex') {
  41. errorText = validateHexText(inputText)
  42. if (!errorText) {
  43. bytes = parseHexBytes(inputText)
  44. previewHex = bytesToHex(bytes, ' ')
  45. }
  46. } else {
  47. bytes = stringToUtf8Bytes(inputText)
  48. previewHex = bytesToHex(bytes, ' ')
  49. }
  50. }
  51. return {
  52. serialErrorText: errorText,
  53. serialInputText: inputText,
  54. serialMode: mode,
  55. serialModeLabel: getBinaryModeLabel(mode),
  56. serialModeToggleText: getBinaryModeToggleText(mode),
  57. serialPreviewHex: previewHex,
  58. serialPreviewLengthText: bytes.length ? `${bytes.length} bytes` : '--'
  59. }
  60. }
  61. function normalizeStorageAccessState(current = {}, changed = {}) {
  62. return storageAccessManualCommand.normalizeMemoryCommandState(current, changed)
  63. }
  64. function getLogPayloadText(log, mode) {
  65. if (!log) return '--'
  66. if (mode === 'text') {
  67. const payloadText = String(log.payloadText || '').trim()
  68. if (payloadText) return payloadText
  69. const bytes = Array.isArray(log.payloadBytes) ? log.payloadBytes : []
  70. return bytesToUtf8Text(bytes) || String(log.payload || '').trim() || '--'
  71. }
  72. if (typeof log.payload === 'string' && log.payload) {
  73. return log.payload
  74. }
  75. const bytes = Array.isArray(log.payloadBytes) ? log.payloadBytes : []
  76. return bytes.length ? bytesToHex(bytes, ' ') : '--'
  77. }
  78. function decorateLogs(logs, mode) {
  79. return (logs || []).map((item) => ({
  80. ...item,
  81. displayText: getLogPayloadText(item, mode)
  82. }))
  83. }
  84. function getProtocolModeState(settingsState = {}) {
  85. const isNoProtocol = settingsService.isNoProtocol(settingsState.protocolMode)
  86. const isModbusProtocol = settingsService.isModbusProtocol(settingsState.protocolMode)
  87. const isStorageAccessProtocol = settingsService.isStorageAccessProtocol(settingsState.protocolMode)
  88. return {
  89. isNoProtocol,
  90. isModbusProtocol,
  91. isStorageAccessProtocol
  92. }
  93. }
  94. function getManualStatePayload(manualState = {}) {
  95. const commands = Array.isArray(manualState.protocolCommands) ? manualState.protocolCommands : []
  96. const command = commands[Number(manualState.commandIndex) || 0] || commands[0] || {}
  97. return {
  98. ...manualState,
  99. protocolCommandLabel: command.label || '指令',
  100. protocolResponseText: manualState.protocolResponseText || '',
  101. protocolSendLabel: '发送',
  102. showProtocolMultipleButton: command.inputMode === 'multiple',
  103. protocolTitleText: '标准 Modbus'
  104. }
  105. }
  106. module.exports = {
  107. LOG_MODE_OPTIONS,
  108. decorateLogs,
  109. getStorageAccessAreaOptions,
  110. getLogModeToggleText: getBinaryModeToggleText,
  111. getManualStatePayload,
  112. getNextLogMode: getNextBinaryMode,
  113. getNextSerialMode: getNextBinaryMode,
  114. getProtocolModeState,
  115. normalizeSerialState,
  116. normalizeStorageAccessState
  117. }