1
0

view-model.js 4.2 KB

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