| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- const {
- createGenericGroupConfig,
- createGenericGroupDialogState,
- createGenericModbusDialogState,
- createGenericRegisterChangedData,
- createGenericRegisterDialogState,
- findGenericGroup,
- findGenericRegister,
- getActiveGenericGroup,
- getGenericDialogDataTypeState,
- getGenericOption,
- getPageState,
- getSettingsPageState,
- getTransportPageState,
- getVisiblePageState,
- resolveActiveParamView
- } = require('../../features/private-protocol/params-view-model.js')
- const {
- createGenericModbusPoller,
- genericModbusService
- } = require('../../features/generic-modbus/index.js')
- const settingsService = require('../../store/settings-store.js')
- const themeService = require('../../store/theme-store.js')
- const transport = require('../../transport/ble-core.js')
- const {
- createPageToast
- } = require('../../utils/page-toast.js')
- const GENERIC_REGISTER_DRAG_THRESHOLD_PX = 12
- const GENERIC_REGISTER_ROW_HEIGHT_RPX = 112
- function clampIndex(value, min, max) {
- return Math.min(Math.max(value, min), max)
- }
- function getWindowWidth() {
- try {
- if (typeof wx !== 'undefined' && wx && typeof wx.getWindowInfo === 'function') {
- const info = wx.getWindowInfo()
- if (info && Number.isFinite(info.windowWidth)) return info.windowWidth
- }
- } catch (error) {}
- try {
- if (typeof wx !== 'undefined' && wx && typeof wx.getSystemInfoSync === 'function') {
- const info = wx.getSystemInfoSync()
- if (info && Number.isFinite(info.windowWidth)) return info.windowWidth
- }
- } catch (error) {}
- return 375
- }
- function rpxToPx(rpx, windowWidth) {
- return Math.round(Number(rpx || 0) * Number(windowWidth || 375) / 750)
- }
- function getFallbackDragRowOffsetPx(windowWidth) {
- return Math.max(44, rpxToPx(GENERIC_REGISTER_ROW_HEIGHT_RPX, windowWidth))
- }
- function resolveDragTargetIndex(drag, currentY, totalCount) {
- if (!drag || !Number.isInteger(totalCount) || totalCount <= 0) return 0
- const sourceIndex = clampIndex(Number(drag.index) || 0, 0, totalCount - 1)
- const rowCenters = Array.isArray(drag.rowCenters) ? drag.rowCenters : []
- const sourceCenter = Number(rowCenters[sourceIndex])
- if (rowCenters.length === totalCount && Number.isFinite(sourceCenter)) {
- const currentCenter = sourceCenter + (Number(currentY) - Number(drag.startY || currentY))
- let targetIndex = sourceIndex
- if (currentCenter >= sourceCenter) {
- for (let index = sourceIndex + 1; index < totalCount; index += 1) {
- if (currentCenter > Number(rowCenters[index])) targetIndex = index
- }
- } else {
- for (let index = sourceIndex - 1; index >= 0; index -= 1) {
- if (currentCenter < Number(rowCenters[index])) targetIndex = index
- }
- }
- return clampIndex(targetIndex, 0, totalCount - 1)
- }
- const rowOffset = Math.max(1, Number(drag.rowOffset) || 1)
- const step = Math.round((Number(currentY) - Number(drag.startY || currentY)) / rowOffset)
- return clampIndex(sourceIndex + step, 0, totalCount - 1)
- }
- function buildActiveGenericRegisterRows(group, dragState) {
- if (!group || !Array.isArray(group.registers)) return []
- const drag = dragState && dragState.groupId === group.id ? dragState : null
- const activeIndex = drag ? clampIndex(Number(drag.index) || 0, 0, group.registers.length - 1) : -1
- const targetIndex = drag ? clampIndex(Number(drag.targetIndex) || activeIndex, 0, group.registers.length - 1) : -1
- const rowOffset = drag ? Math.max(1, Math.round(Number(drag.rowOffset) || 0)) : 0
- const translateY = drag ? Math.round(Number(drag.translateY) || 0) : 0
- return group.registers.map((register, index) => {
- const row = {
- ...register,
- sourceIndex: index,
- dragClass: '',
- dragHandleClass: '',
- dragStyle: ''
- }
- if (!drag) return row
- const isActive = index === activeIndex
- let shiftY = 0
- if (drag.moved && rowOffset) {
- if (activeIndex < targetIndex && index > activeIndex && index <= targetIndex) {
- shiftY = -rowOffset
- } else if (activeIndex > targetIndex && index >= targetIndex && index < activeIndex) {
- shiftY = rowOffset
- }
- }
- if (isActive) {
- row.dragClass = drag.moved ? 'is-dragging' : 'is-drag-armed'
- row.dragHandleClass = drag.moved ? 'is-dragging' : 'is-drag-armed'
- row.dragStyle = drag.moved
- ? `transform: translate3d(0, ${translateY}px, 0) scale(1.02); z-index: 8;`
- : 'z-index: 3;'
- return row
- }
- if (shiftY) {
- row.dragClass = shiftY > 0 ? 'is-shift-down' : 'is-shift-up'
- row.dragStyle = `transform: translate3d(0, ${shiftY}px, 0);`
- }
- return row
- })
- }
- Page({
- data: {
- ...getPageState(),
- activeParamView: 'genericModbus',
- activeGenericGroupId: '',
- activeGenericRegisterRows: [],
- genericModbusDialog: createGenericModbusDialogState()
- },
- onTabItemTap() {
- this.backToParamsHome()
- },
- onLoad() {
- this.pageToast = createPageToast(this, this.data)
- this.genericModbusPoller = createGenericModbusPoller(() => this.data)
- this.genericModbusTouchStarts = {}
- this.genericWindowWidth = getWindowWidth()
- genericModbusService.init()
- themeService.init()
- settingsService.init()
- this.unsubscribeTheme = themeService.subscribe((themeState) => {
- this.setData(themeState)
- })
- this.unsubscribeTransport = transport.subscribe((transportState) => {
- this.setData(getTransportPageState(transportState))
- if (transportState.connectedDevice) {
- setTimeout(() => this.scheduleVisibleGenericAutoReads(), 0)
- } else {
- this.clearGenericAutoTimers()
- }
- })
- this.unsubscribeGenericModbus = genericModbusService.subscribe((genericState) => {
- const activeGenericGroup = getActiveGenericGroup(genericState.genericModbusGroups, this.data.activeGenericGroupId)
- this.setData({
- ...genericState,
- activeGenericGroup,
- activeGenericRegisterRows: buildActiveGenericRegisterRows(activeGenericGroup, this.genericModbusRegisterDrag),
- activeParamView: this.data.activeParamView === 'genericModbusGroup' && !activeGenericGroup
- ? 'genericModbus'
- : this.data.activeParamView
- })
- })
- this.unsubscribeSettings = settingsService.subscribe((settingsState) => {
- const nextState = getSettingsPageState(this.data, settingsState)
- const activeParamView = nextState.activeParamView
- const activeGenericGroup = getActiveGenericGroup(this.data.genericModbusGroups, this.data.activeGenericGroupId)
- const safeActiveView = activeParamView === 'genericModbusGroup' && !activeGenericGroup
- ? 'genericModbus'
- : activeParamView
- this.setData({
- ...nextState,
- activeParamView: safeActiveView,
- activeGenericGroup,
- activeGenericRegisterRows: buildActiveGenericRegisterRows(activeGenericGroup, this.genericModbusRegisterDrag)
- })
- if (safeActiveView === 'genericModbus' || safeActiveView === 'genericModbusGroup') {
- setTimeout(() => this.scheduleVisibleGenericAutoReads(), 0)
- } else {
- this.clearGenericAutoTimers()
- }
- })
- },
- onShow() {
- if (this.pageToast) {
- this.pageToast.setActive(true)
- }
- const pageState = getVisiblePageState(this.data)
- this.setData({
- ...pageState,
- activeGenericGroup: getActiveGenericGroup(pageState.genericModbusGroups, this.data.activeGenericGroupId),
- activeGenericRegisterRows: buildActiveGenericRegisterRows(
- getActiveGenericGroup(pageState.genericModbusGroups, this.data.activeGenericGroupId),
- this.genericModbusRegisterDrag
- )
- })
- this.pageToast.showFromState(pageState)
- this.scheduleVisibleGenericAutoReads()
- },
- onHide() {
- if (this.pageToast) {
- this.pageToast.setActive(false)
- }
- this.clearGenericRegisterDrag()
- this.clearGenericAutoTimers()
- },
- onUnload() {
- if (this.pageToast) {
- this.pageToast.destroy()
- this.pageToast = null
- }
- if (this.unsubscribeTheme) {
- this.unsubscribeTheme()
- this.unsubscribeTheme = null
- }
- if (this.unsubscribeTransport) {
- this.unsubscribeTransport()
- this.unsubscribeTransport = null
- }
- if (this.unsubscribeGenericModbus) {
- this.unsubscribeGenericModbus()
- this.unsubscribeGenericModbus = null
- }
- if (this.unsubscribeSettings) {
- this.unsubscribeSettings()
- this.unsubscribeSettings = null
- }
- this.clearGenericAutoTimers()
- },
- openParamView(event) {
- if (this.pageToast) this.pageToast.clear()
- this.closeGenericModbusDraft()
- this.clearGenericRegisterDrag()
- const activeParamView = event.currentTarget.dataset.view
- if (!activeParamView) return
- this.setData({
- activeParamView
- })
- if (activeParamView === 'genericModbus') {
- setTimeout(() => this.scheduleVisibleGenericAutoReads(), 0)
- }
- },
- openGenericModbusGroup(event) {
- const groupId = event.currentTarget.dataset.groupId
- const group = findGenericGroup(this.data.genericModbusGroups, groupId)
- if (!group) return
- if (this.pageToast) this.pageToast.clear()
- this.closeGenericModbusDraft()
- this.setData({
- activeGenericGroup: group,
- activeGenericGroupId: groupId,
- activeParamView: 'genericModbusGroup',
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, this.genericModbusRegisterDrag)
- })
- },
- backToParamsHome() {
- if (this.pageToast) this.pageToast.clear()
- this.closeGenericModbusDraft()
- this.clearGenericRegisterDrag()
- this.clearGenericAutoTimers()
- const activeParamView = resolveActiveParamView('', this.data)
- this.setData({
- activeGenericGroup: null,
- activeGenericGroupId: '',
- activeParamView,
- activeGenericRegisterRows: []
- })
- if (activeParamView === 'genericModbus') {
- setTimeout(() => this.scheduleVisibleGenericAutoReads(), 0)
- }
- },
- noop() {},
- updateGenericModbusDialog(changedData) {
- this.setData({
- genericModbusDialog: {
- ...this.data.genericModbusDialog,
- ...changedData
- }
- })
- },
- openGenericModbusDraft(event) {
- const groupId = event && event.currentTarget && event.currentTarget.dataset
- ? event.currentTarget.dataset.groupId
- : ''
- const group = groupId ? findGenericGroup(this.data.genericModbusGroups, groupId) : null
- this.updateGenericModbusDialog(createGenericGroupDialogState(group))
- },
- closeGenericModbusDraft() {
- this.genericModbusGroupLongPressGuard = ''
- this.genericModbusRegisterLongPressGuard = ''
- this.updateGenericModbusDialog(createGenericModbusDialogState())
- },
- onGenericDraftInput(event) {
- const field = event.currentTarget.dataset.field
- if (!field) return
- const value = event.detail.value
- this.updateGenericModbusDialog({
- [field]: value,
- ...(field === 'structDefinition' ? {
- parsedStructRegisters: [],
- structParsedSummary: ''
- } : {})
- })
- },
- parseGenericStructDefinition() {
- const dialog = this.data.genericModbusDialog || createGenericModbusDialogState()
- const sourceText = dialog.structDefinition || ''
- if (!sourceText.trim()) {
- if (this.pageToast) this.pageToast.show('请先粘贴结构体定义', 'error')
- return
- }
- const registerType = getGenericOption(this.data.genericModbusRegisterTypeOptions, dialog.registerTypeIndex)
- if (registerType.key === 'coil' || registerType.key === 'discrete') {
- if (this.pageToast) this.pageToast.show('结构体解析仅支持寄存器类型', 'error')
- return
- }
- try {
- const parsed = genericModbusService.parseStructDefinition(sourceText)
- const inputRegisterIndex = Math.max(
- 0,
- this.data.genericModbusRegisterTypeOptions.findIndex((item) => item.key === 'input')
- )
- const inputRegisterType = getGenericOption(this.data.genericModbusRegisterTypeOptions, inputRegisterIndex)
- this.updateGenericModbusDialog({
- groupName: parsed.name || dialog.groupName,
- parsedStructRegisters: parsed.registers,
- quantity: String(parsed.registers.length),
- registerTypeIndex: inputRegisterIndex,
- registerTypeText: inputRegisterType.label || '',
- structParsedSummary: `${parsed.structName} · ${parsed.registers.length} 个字段`
- })
- if (this.pageToast) this.pageToast.show('结构体解析完成')
- } catch (error) {
- if (this.pageToast) this.pageToast.show(error.message || '结构体解析失败', 'error')
- }
- },
- onGenericDraftTypeChange(event) {
- const registerTypeIndex = Number(event.detail.value)
- const registerType = getGenericOption(this.data.genericModbusRegisterTypeOptions, registerTypeIndex)
- const clearParsedStruct = registerType.key === 'coil' || registerType.key === 'discrete'
- this.updateGenericModbusDialog({
- ...(clearParsedStruct ? {
- parsedStructRegisters: [],
- structParsedSummary: ''
- } : {}),
- registerTypeIndex,
- registerTypeText: registerType.label || ''
- })
- },
- onGenericDialogDataTypeChange(event) {
- const dataTypeIndex = Number(event.detail.value)
- this.updateGenericModbusDialog(getGenericDialogDataTypeState(
- this.data.genericModbusDialog,
- this.data.genericModbusDataTypeOptions,
- dataTypeIndex
- ))
- },
- openGenericGroupEdit(event) {
- const groupId = event.currentTarget.dataset.groupId
- const group = findGenericGroup(this.data.genericModbusGroups, groupId)
- if (!group) return
- this.genericModbusGroupLongPressGuard = groupId
- this.updateGenericModbusDialog(createGenericGroupDialogState(group))
- },
- openGenericRegisterInfo(event) {
- const groupId = event.currentTarget.dataset.groupId
- const registerIndex = Number(event.currentTarget.dataset.index)
- const registerKey = `${groupId}:${registerIndex}`
- if (this.genericModbusRegisterLongPressGuard === registerKey) {
- this.genericModbusRegisterLongPressGuard = ''
- return
- }
- const {
- group,
- register
- } = findGenericRegister(this.data.genericModbusGroups, groupId, registerIndex)
- if (!register) return
- this.updateGenericModbusDialog(createGenericRegisterDialogState('viewRegister', group, register, registerIndex))
- },
- openGenericRegisterEdit(event) {
- const groupId = event.currentTarget.dataset.groupId
- const registerIndex = Number(event.currentTarget.dataset.index)
- const {
- group,
- register
- } = findGenericRegister(this.data.genericModbusGroups, groupId, registerIndex)
- if (!register) return
- this.genericModbusRegisterLongPressGuard = `${groupId}:${registerIndex}`
- this.updateGenericModbusDialog(createGenericRegisterDialogState('editRegister', group, register, registerIndex))
- },
- async confirmGenericModbusDialog() {
- const dialog = this.data.genericModbusDialog || createGenericModbusDialogState()
- const mode = dialog.mode
- if (mode === 'createGroup') {
- const group = genericModbusService.addGroupFromConfig(createGenericGroupConfig(dialog))
- if (group) {
- if (this.pageToast) this.pageToast.show(`${group.name}已添加`)
- this.closeGenericModbusDraft()
- this.setData({
- activeGenericGroup: group,
- activeGenericGroupId: group.id,
- activeParamView: 'genericModbusGroup',
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, this.genericModbusRegisterDrag)
- })
- }
- return
- }
- if (mode === 'editGroup') {
- const group = genericModbusService.updateGroupConfig(dialog.groupId, createGenericGroupConfig(dialog))
- if (group) {
- if (this.pageToast) this.pageToast.show(`${group.name}已更新`)
- this.closeGenericModbusDraft()
- if (this.data.activeGenericGroupId === group.id) {
- this.setData({
- activeGenericGroup: group,
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, this.genericModbusRegisterDrag)
- })
- }
- }
- return
- }
- if (mode === 'editRegister') {
- const changedData = createGenericRegisterChangedData(dialog, this.data.genericModbusDataTypeOptions)
- genericModbusService.updateRegister(dialog.groupId, dialog.registerIndex, changedData)
- if (this.pageToast) this.pageToast.show(`${dialog.name || '寄存器'}已更新`)
- this.closeGenericModbusDraft()
- }
- },
- async importGenericModbusJson() {
- const count = await genericModbusService.importJsonFromMessageFile()
- if (count && this.pageToast) this.pageToast.show(`已导入 ${count} 个寄存器组`)
- },
- async syncGenericModbusGroups() {
- if (this.data.isGenericProtocol) return
- if (!this.data.connectedDevice) return
- const result = await genericModbusService.queryCodeInfoBlock({
- maxPacketLength: this.data.genericModbusMaxPacketLength
- })
- if (result && result.ok && this.pageToast) {
- const addressText = Number(result.address || 0).toString(16).toUpperCase().padStart(4, '0')
- const addedCount = Number(result.addedGroups || 0) + Number(result.addedRegisters || 0)
- const updatedCount = Number(result.updatedGroups || 0) + Number(result.updatedRegisters || 0)
- const changedText = [
- addedCount ? `新增 ${addedCount}` : '',
- updatedCount ? `更新 ${updatedCount}` : ''
- ].filter(Boolean).join(',')
- this.pageToast.show(`同步完成 0x${addressText},${result.structCount} 项${changedText ? `,${changedText}` : ''}`)
- }
- },
- async completeGenericModbusStructs() {
- const result = await genericModbusService.completeStructInstanceGroupsWithStructFile()
- if (result && result.completedCount && this.pageToast) {
- this.pageToast.show(`已补全 ${result.completedCount} 个寄存器组`)
- }
- },
- toggleGenericModbusPolling() {
- if (this.data.isPrivateProtocol && !this.data.connectedDevice) return
- const enabled = !this.data.genericModbusAutoPollEnabled
- settingsService.setGenericModbusAutoPollEnabled(enabled)
- if (enabled) {
- this.scheduleGenericAutoPoll(0)
- } else {
- this.clearGenericAutoTimers()
- }
- },
- async saveGenericModbusJson() {
- const count = await genericModbusService.saveJsonToChat()
- if (count && this.pageToast) this.pageToast.show(`已保存 ${count} 个寄存器组`)
- },
- toggleGenericModbusGroup(event) {
- const groupId = event.currentTarget.dataset.groupId
- if (this.genericModbusGroupLongPressGuard === groupId) {
- this.genericModbusGroupLongPressGuard = ''
- return
- }
- const group = findGenericGroup(this.data.genericModbusGroups, groupId)
- if (!group) return
- genericModbusService.setGroupExpanded(groupId, !group.expanded)
- },
- onGenericRegisterValueInput(event) {
- genericModbusService.updateRegisterValue(
- event.currentTarget.dataset.groupId,
- Number(event.currentTarget.dataset.index),
- event.detail.value
- )
- },
- async onGenericRegisterValueBlur(event) {
- const groupId = event.currentTarget.dataset.groupId
- const registerIndex = Number(event.currentTarget.dataset.index)
- try {
- genericModbusService.validateRegisterInputValue(groupId, registerIndex, event.detail.value)
- } catch (error) {
- if (this.pageToast) this.pageToast.show(error.message || '输入值无效', 'error')
- return
- }
- if (!this.data.isPrivateProtocol || !this.data.connectedDevice) return
- const group = findGenericGroup(this.data.genericModbusGroups, groupId)
- const register = group && group.registers ? group.registers[registerIndex] : null
- if (!group || !register || !register.isDirty || !group.writable || group.addressOverflow) return
- this.clearGenericAutoTimers()
- const ok = await genericModbusService.writeRegister(groupId, registerIndex)
- if (this.data.genericModbusAutoPollEnabled) {
- this.scheduleGenericAutoPoll(this.data.genericModbusPollInterval || 100)
- }
- if (ok && this.pageToast) {
- this.pageToast.show(`${register.name || '变量'}已写入`)
- }
- },
- async readGenericModbusGroup(event) {
- if (!this.data.connectedDevice) return
- const groupId = event.currentTarget.dataset.groupId
- const ok = await genericModbusService.readGroup(groupId, {
- maxPacketLength: this.data.genericModbusMaxPacketLength
- })
- if (ok && this.pageToast) this.pageToast.show('通用Modbus读取完成')
- },
- async writeGenericModbusGroup(event) {
- if (!this.data.connectedDevice) return
- const groupId = event.currentTarget.dataset.groupId
- const ok = await genericModbusService.writeGroup(groupId)
- if (ok && this.pageToast) this.pageToast.show('通用Modbus写入完成')
- },
- onGenericGroupTouchStart(event) {
- const groupId = event.currentTarget.dataset.groupId
- const touch = (event.changedTouches || [])[0]
- if (!groupId || !touch) return
- this.genericModbusTouchStarts[groupId] = touch.clientX
- },
- onGenericGroupTouchEnd(event) {
- const groupId = event.currentTarget.dataset.groupId
- const group = findGenericGroup(this.data.genericModbusGroups, groupId)
- const touch = (event.changedTouches || [])[0]
- const startX = this.genericModbusTouchStarts[groupId]
- if (!groupId || !group || group.expanded || !touch || !Number.isFinite(startX)) return
- const deltaX = touch.clientX - startX
- if (deltaX > 42) {
- genericModbusService.setGroupDeleteVisible(groupId, true)
- } else if (deltaX < -24) {
- genericModbusService.setGroupDeleteVisible(groupId, false)
- }
- },
- onGenericRegisterDragStart(event) {
- const touch = (event.changedTouches || [])[0]
- if (!touch) return
- const groupId = event.currentTarget.dataset.groupId
- const index = Number(event.currentTarget.dataset.index)
- const activeGenericGroup = findGenericGroup(this.data.genericModbusGroups, groupId)
- if (!groupId || !activeGenericGroup || !Number.isInteger(index)) return
- this.genericModbusRegisterDrag = {
- groupId,
- index,
- moved: false,
- rowCenters: [],
- rowOffset: getFallbackDragRowOffsetPx(this.genericWindowWidth),
- startY: touch.clientY,
- targetIndex: index,
- translateY: 0
- }
- if (this.data.activeGenericGroupId === groupId) {
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(activeGenericGroup, this.genericModbusRegisterDrag)
- })
- }
- this.measureGenericRegisterRows(this.genericModbusRegisterDrag)
- },
- onGenericRegisterDragMove(event) {
- const touch = (event.changedTouches || [])[0]
- if (!touch || !this.genericModbusRegisterDrag) return
- const drag = this.genericModbusRegisterDrag
- const group = findGenericGroup(this.data.genericModbusGroups, drag.groupId)
- if (!group) return
- const translateY = Math.round(touch.clientY - drag.startY)
- const moved = Math.abs(translateY) > GENERIC_REGISTER_DRAG_THRESHOLD_PX
- const targetIndex = moved
- ? resolveDragTargetIndex(drag, touch.clientY, group.registers.length)
- : drag.index
- if (
- drag.translateY === translateY
- && drag.moved === moved
- && drag.targetIndex === targetIndex
- ) {
- return
- }
- drag.translateY = translateY
- drag.moved = moved
- drag.targetIndex = targetIndex
- if (this.data.activeGenericGroupId === group.id) {
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, drag)
- })
- }
- },
- onGenericRegisterDragEnd(event) {
- const drag = this.genericModbusRegisterDrag
- this.genericModbusRegisterDrag = null
- if (!drag || !drag.groupId) return
- const group = findGenericGroup(this.data.genericModbusGroups, drag.groupId)
- if (!group) return
- if (this.data.activeGenericGroupId === group.id) {
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, null)
- })
- }
- if (!drag.moved) return
- const targetIndex = clampIndex(
- Number(drag.targetIndex) || drag.index,
- 0,
- group.registers.length - 1
- )
- if (targetIndex === drag.index) return
- const updatedGroup = genericModbusService.reorderRegister(drag.groupId, drag.index, targetIndex)
- if (!updatedGroup) return
- this.genericModbusRegisterLongPressGuard = `${drag.groupId}:${targetIndex}`
- setTimeout(() => {
- if (this.genericModbusRegisterLongPressGuard === `${drag.groupId}:${targetIndex}`) {
- this.genericModbusRegisterLongPressGuard = ''
- }
- }, 260)
- if (this.data.activeGenericGroupId === updatedGroup.id) {
- this.setData({
- activeGenericGroup: updatedGroup,
- activeGenericRegisterRows: buildActiveGenericRegisterRows(updatedGroup, null)
- })
- }
- },
- onGenericRegisterDragCancel() {
- const drag = this.genericModbusRegisterDrag
- this.genericModbusRegisterDrag = null
- if (!drag || !drag.groupId) return
- const group = findGenericGroup(this.data.genericModbusGroups, drag.groupId)
- if (!group || this.data.activeGenericGroupId !== group.id) return
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, null)
- })
- },
- deleteGenericModbusGroup(event) {
- const groupId = event.currentTarget.dataset.groupId
- this.clearGenericAutoTimer(groupId)
- genericModbusService.removeGroup(groupId)
- if (this.data.activeGenericGroupId === groupId) {
- this.setData({
- activeGenericGroup: null,
- activeGenericGroupId: '',
- activeParamView: 'genericModbus'
- })
- }
- if (this.pageToast) this.pageToast.show('寄存器组已删除')
- },
- clearGenericAutoTimer(groupId) {
- if (this.genericModbusPoller) this.genericModbusPoller.clearTimer(groupId)
- },
- clearGenericAutoTimers() {
- if (this.genericModbusPoller) this.genericModbusPoller.clearAll()
- },
- scheduleVisibleGenericAutoReads() {
- if (this.genericModbusPoller) this.genericModbusPoller.scheduleVisible()
- },
- scheduleGenericAutoPoll(delay) {
- if (this.genericModbusPoller) this.genericModbusPoller.schedule(delay)
- },
- clearGenericRegisterDrag() {
- if (!this.genericModbusRegisterDrag) return
- const drag = this.genericModbusRegisterDrag
- this.genericModbusRegisterDrag = null
- const group = findGenericGroup(this.data.genericModbusGroups, drag.groupId)
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, null)
- })
- },
- measureGenericRegisterRows(dragReference) {
- const query = this.createSelectorQuery()
- query.selectAll('.generic-register-row').boundingClientRect((rects) => {
- if (!this.genericModbusRegisterDrag || this.genericModbusRegisterDrag !== dragReference) return
- if (!Array.isArray(rects) || !rects.length) return
- dragReference.rowCenters = rects.map((rect) => Number(rect.top || 0) + Number(rect.height || 0) / 2)
- dragReference.rowOffset = Math.max(
- 1,
- Math.round(Number((rects[dragReference.index] || {}).height) || dragReference.rowOffset || 0)
- )
- const group = findGenericGroup(this.data.genericModbusGroups, dragReference.groupId)
- if (!group || this.data.activeGenericGroupId !== group.id) return
- this.setData({
- activeGenericRegisterRows: buildActiveGenericRegisterRows(group, dragReference)
- })
- }).exec()
- }
- })
|