1
0

communication.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. const settingsService = require('../../store/settings-store.js')
  2. const themeService = require('../../store/theme-store.js')
  3. const transport = require('../../transport/ble-core.js')
  4. const {
  5. createPageToast
  6. } = require('../../utils/page-toast.js')
  7. const {
  8. parameterGroupService
  9. } = require('../../features/parameter-groups/index.js')
  10. const {
  11. LOG_MODE_OPTIONS,
  12. decorateLogs,
  13. executeStorageAccessProtocol,
  14. executeStorageAccessSpecialCommand,
  15. getLogModeToggleText,
  16. getManualStatePayload,
  17. getNextLogMode,
  18. getNextSerialMode,
  19. getProtocolModeState,
  20. getStorageAccessAreaOptions,
  21. manualRtuService,
  22. sendSerialFrame,
  23. normalizeStorageAccessSpecialState,
  24. normalizeStorageAccessState,
  25. normalizeSerialState
  26. } = require('../../features/communication/index.js')
  27. const INITIAL_SERIAL_STATE = normalizeSerialState({
  28. serialInputText: '',
  29. serialMode: 'hex'
  30. })
  31. Page({
  32. data: {
  33. ...themeService.getState(),
  34. ...settingsService.getState(),
  35. ...getProtocolModeState(settingsService.getState()),
  36. ...transport.getState(),
  37. ...parameterGroupService.getState(),
  38. ...manualRtuService.getState(),
  39. logs: decorateLogs(transport.getState().logs, 'hex'),
  40. logMode: 'hex',
  41. logModeToggleText: getLogModeToggleText('hex'),
  42. logModeOptions: LOG_MODE_OPTIONS,
  43. storageAccessAreaOptions: getStorageAccessAreaOptions(),
  44. ...normalizeStorageAccessState({
  45. storageAccessAreaIndex: 0,
  46. storageAccessAddress: '',
  47. storageAccessCommandIndex: 0,
  48. storageAccessDataText: '',
  49. storageAccessLength: ''
  50. }),
  51. ...normalizeStorageAccessSpecialState(),
  52. serialTitleText: '串口发送',
  53. ...INITIAL_SERIAL_STATE,
  54. toastText: '',
  55. toastType: ''
  56. },
  57. onLoad() {
  58. this.pageToast = createPageToast(this, this.data)
  59. this.lastSyncedSlaveAddress = ''
  60. transport.init()
  61. parameterGroupService.init()
  62. settingsService.init()
  63. themeService.init()
  64. const settingsState = settingsService.getState()
  65. manualRtuService.setProtocolInput({
  66. slaveAddress: settingsState.modbusSlaveAddress
  67. })
  68. this.lastSyncedSlaveAddress = settingsState.modbusSlaveAddress
  69. this.unsubscribeTheme = themeService.subscribe((themeState) => {
  70. this.setData(themeState)
  71. })
  72. this.unsubscribeSettings = settingsService.subscribe((settingsState) => {
  73. this.setData({
  74. ...settingsState,
  75. ...getProtocolModeState(settingsState)
  76. })
  77. if (settingsState.modbusSlaveAddress !== this.lastSyncedSlaveAddress) {
  78. this.lastSyncedSlaveAddress = settingsState.modbusSlaveAddress
  79. manualRtuService.setProtocolInput({
  80. slaveAddress: settingsState.modbusSlaveAddress
  81. })
  82. }
  83. })
  84. this.unsubscribeTransport = transport.subscribe((transportState) => {
  85. this.setData({
  86. ...transportState,
  87. logs: decorateLogs(transportState.logs, this.data.logMode)
  88. })
  89. })
  90. this.unsubscribeParameterGroups = parameterGroupService.subscribe((parameterState) => {
  91. this.setData({
  92. ...parameterState,
  93. ...normalizeStorageAccessState(this.data, {
  94. storageAccessCommandIndex: this.data.storageAccessCommandIndex,
  95. storageAccessAreaIndex: this.data.storageAccessAreaIndex
  96. })
  97. })
  98. })
  99. this.unsubscribeManualRtu = manualRtuService.subscribe((manualState) => {
  100. this.setData(getManualStatePayload(manualState))
  101. })
  102. },
  103. onShow() {
  104. if (this.pageToast) {
  105. this.pageToast.setActive(true)
  106. }
  107. },
  108. onHide() {
  109. if (this.pageToast) {
  110. this.pageToast.setActive(false)
  111. }
  112. },
  113. onUnload() {
  114. if (this.pageToast) {
  115. this.pageToast.destroy()
  116. this.pageToast = null
  117. }
  118. if (this.unsubscribeTheme) {
  119. this.unsubscribeTheme()
  120. this.unsubscribeTheme = null
  121. }
  122. if (this.unsubscribeSettings) {
  123. this.unsubscribeSettings()
  124. this.unsubscribeSettings = null
  125. }
  126. if (this.unsubscribeTransport) {
  127. this.unsubscribeTransport()
  128. this.unsubscribeTransport = null
  129. }
  130. if (this.unsubscribeParameterGroups) {
  131. this.unsubscribeParameterGroups()
  132. this.unsubscribeParameterGroups = null
  133. }
  134. if (this.unsubscribeManualRtu) {
  135. this.unsubscribeManualRtu()
  136. this.unsubscribeManualRtu = null
  137. }
  138. },
  139. noop() {},
  140. switchSerialMode() {
  141. this.setData(normalizeSerialState(this.data, {
  142. serialMode: getNextSerialMode(this.data.serialMode)
  143. }))
  144. },
  145. switchLogMode(event) {
  146. const requestedMode = event && event.currentTarget && event.currentTarget.dataset
  147. ? event.currentTarget.dataset.mode
  148. : ''
  149. const mode = requestedMode || getNextLogMode(this.data.logMode)
  150. this.setData({
  151. logMode: mode,
  152. logModeToggleText: getLogModeToggleText(mode),
  153. logs: decorateLogs(transport.getState().logs, mode)
  154. })
  155. },
  156. clearCommunicationLogs() {
  157. transport.clearLogs()
  158. },
  159. onSerialInput(event) {
  160. this.setData(normalizeSerialState(this.data, {
  161. serialInputText: event.detail.value
  162. }))
  163. },
  164. clearSerialInput() {
  165. this.setData(normalizeSerialState(this.data, {
  166. serialInputText: ''
  167. }))
  168. },
  169. async sendSerialFrame() {
  170. try {
  171. const result = await sendSerialFrame(this.data)
  172. if (!result.ok) {
  173. if (this.pageToast) this.pageToast.show(result.errorText || '发送失败', 'error')
  174. return
  175. }
  176. if (this.pageToast) this.pageToast.show('已发送')
  177. this.setData({
  178. ...(result.serialState || {}),
  179. serialPreviewHex: result.previewHex || '',
  180. serialPreviewLengthText: `${(result.bytes || []).length} bytes`
  181. })
  182. } catch (error) {
  183. if (this.pageToast) this.pageToast.show(error.message || '发送失败', 'error')
  184. }
  185. },
  186. onProtocolCommandChange(event) {
  187. manualRtuService.setCommandIndex(Number(event.detail.value))
  188. },
  189. onProtocolInput(event) {
  190. const field = event.currentTarget.dataset.field
  191. if (!field) return
  192. manualRtuService.setProtocolInput({
  193. [field]: event.detail.value
  194. })
  195. },
  196. onProtocolCoilChange(event) {
  197. manualRtuService.setProtocolInput({
  198. coilEnabled: !!event.detail.value
  199. })
  200. },
  201. openProtocolMultipleDialog() {
  202. manualRtuService.openProtocolMultipleDialog()
  203. },
  204. closeProtocolMultipleDialog() {
  205. manualRtuService.closeProtocolMultipleDialog()
  206. },
  207. toggleProtocolMultipleExpanded() {
  208. manualRtuService.toggleProtocolMultipleExpanded()
  209. },
  210. onProtocolMultipleQuantityChange(event) {
  211. manualRtuService.setProtocolMultipleQuantity(event.detail.value)
  212. },
  213. onProtocolMultipleValueInput(event) {
  214. const index = Number(event.currentTarget.dataset.index)
  215. manualRtuService.setProtocolMultipleValue(index, event.detail.value)
  216. },
  217. onProtocolMultipleTypeChange(event) {
  218. const index = Number(event.currentTarget.dataset.index)
  219. manualRtuService.setProtocolMultipleType(index, Number(event.detail.value))
  220. },
  221. onProtocolMultipleTextLengthInput(event) {
  222. const index = Number(event.currentTarget.dataset.index)
  223. manualRtuService.setProtocolMultipleTextLength(index, event.detail.value)
  224. },
  225. async sendProtocolFrame(event) {
  226. const source = event && event.currentTarget && event.currentTarget.dataset
  227. ? event.currentTarget.dataset.source
  228. : ''
  229. if (this.data.protocolMultipleDialog && this.data.protocolMultipleDialog.visible && source !== 'dialog') {
  230. if (this.pageToast) this.pageToast.show('请在多值窗口内发送', 'error')
  231. return
  232. }
  233. if (!this.data.connectedDevice) {
  234. if (this.pageToast) this.pageToast.show('请先连接蓝牙设备', 'error')
  235. return
  236. }
  237. const response = await manualRtuService.sendGeneratedFrame()
  238. if (response && this.pageToast) {
  239. this.pageToast.show(response === true ? '已发送' : '已收到回复')
  240. } else if (this.pageToast) {
  241. const manualState = manualRtuService.getState()
  242. this.pageToast.show(manualState.protocolStatusText || '发送失败或超时未回复', 'error')
  243. }
  244. },
  245. switchStorageAccessCommandMode() {
  246. const currentIndex = Number(this.data.storageAccessCommandIndex) || 0
  247. const nextState = normalizeStorageAccessState(this.data, {
  248. storageAccessCommandIndex: currentIndex === 0 ? 1 : 0
  249. })
  250. this.setData(nextState)
  251. },
  252. onStorageAccessInput(event) {
  253. const field = event.currentTarget.dataset.field
  254. if (!field) return
  255. const changed = {
  256. storageAccessCommandIndex: this.data.storageAccessCommandIndex,
  257. [field]: event.detail.value
  258. }
  259. const nextState = normalizeStorageAccessState(this.data, changed)
  260. this.setData(nextState)
  261. },
  262. onStorageAccessAreaChange(event) {
  263. const storageAccessAreaIndex = Number(event.detail.value)
  264. const nextState = normalizeStorageAccessState(this.data, {
  265. storageAccessCommandIndex: this.data.storageAccessCommandIndex,
  266. storageAccessAreaIndex
  267. })
  268. this.setData(nextState)
  269. },
  270. async sendStorageAccessProtocolFrame() {
  271. try {
  272. if (!this.data.connectedDevice) {
  273. if (this.pageToast) this.pageToast.show('请先连接蓝牙设备', 'error')
  274. return
  275. }
  276. const result = await executeStorageAccessProtocol(this.data)
  277. if (!result.ok) {
  278. if (this.pageToast) this.pageToast.show(result.errorText || '操作失败', 'error')
  279. return
  280. }
  281. if (!this.data.storageAccessShowWriteData) {
  282. this.setData({
  283. storageAccessPreviewHexText: result.previewHex || ''
  284. })
  285. if (this.pageToast) this.pageToast.show(`读取完成 ${(result.bytes || []).length} bytes`)
  286. return
  287. }
  288. if (this.pageToast) this.pageToast.show('写入完成')
  289. } catch (error) {
  290. if (this.pageToast) this.pageToast.show(error.message || '操作失败', 'error')
  291. }
  292. },
  293. async syncStorageAccessCodeInfo() {
  294. if (!this.data.isStorageAccessProtocol) return
  295. if (!this.data.connectedDevice) {
  296. if (this.pageToast) this.pageToast.show('请先连接蓝牙设备', 'error')
  297. return
  298. }
  299. const result = await parameterGroupService.syncFromStorageAccessCodeInfo({
  300. maxPacketLength: this.data.parameterMaxPacketLength
  301. })
  302. if (result && result.ok && this.pageToast) {
  303. this.setData(normalizeStorageAccessState(this.data, {
  304. storageAccessCommandIndex: this.data.storageAccessCommandIndex,
  305. storageAccessAreaIndex: 0
  306. }))
  307. const codeInfoAddressText = result.codeInfoAddressText || Number(result.codeInfoAddress || 0).toString(16).toUpperCase()
  308. const codeInfoByteLengthText = result.codeInfoByteLengthText || Number(result.codeInfoByteLength || 0).toString(16).toUpperCase()
  309. const addedCount = Number(result.addedGroups || 0) + Number(result.addedRegisters || 0)
  310. const updatedCount = Number(result.updatedGroups || 0) + Number(result.updatedRegisters || 0)
  311. const changedText = [
  312. addedCount ? `新增 ${addedCount}` : '',
  313. updatedCount ? `更新 ${updatedCount}` : ''
  314. ].filter(Boolean).join(',')
  315. this.pageToast.show(`同步完成 codeInfo 0x${codeInfoAddressText} / 0x${codeInfoByteLengthText},${result.structCount} 项${changedText ? `,${changedText}` : ''}`)
  316. }
  317. },
  318. async sendStorageAccessSpecialCommand(event) {
  319. const commandKey = event && event.currentTarget && event.currentTarget.dataset
  320. ? event.currentTarget.dataset.command
  321. : ''
  322. const command = (this.data.storageAccessSpecialCommands || []).find((item) => item.key === commandKey)
  323. try {
  324. const result = await executeStorageAccessSpecialCommand(command, this.data)
  325. if (!result.ok) {
  326. if (this.pageToast) this.pageToast.show(result.errorText || '指令执行失败', 'error')
  327. return
  328. }
  329. if (this.pageToast) this.pageToast.show(`${command.label || '特殊指令'}已执行`)
  330. } catch (error) {
  331. if (this.pageToast) this.pageToast.show(error.message || '指令执行失败', 'error')
  332. }
  333. }
  334. })