useNodeEvent.ts
6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { h, render } from 'vue'
import { ControlComponentEnum } from '../packages/Control'
import { doCommandDelivery, getDeviceActive, getDeviceInfo } from '@/api/device'
import type { MouseUpEventDataType, NodeDataDataSourceJsonType, NodeDataEventJsonType, SingleClickEventDataType } from '@/api/node/model'
import { CommandWayEnum } from '@/enums/commandEnum'
import { ActRangListItemTypeEnum, EventActionTypeEnum, TransportTypeEnum } from '@/enums/datasource'
import { useMessage } from '@/hooks/web/useMessage'
import { AttributeDeliverModal, CommandDeliveryConfirmModal, CommandDeliveryModal, ConfirmModal } from '@/core/Library/components/PublicForm/components/DataEvents/CommandDeliveryModal'
import type { MxCell } from '@/fitCore/types'
import { NodeUtils } from '@/hooks/business/useNodeUtils'
import { useContentDataStoreWithOut } from '@/store/modules/contentData'
import type { RpcCommandType } from '@/api/device/model'
export function useNodeEvent(eventJson: NodeDataEventJsonType, dataSourceJson: NodeDataDataSourceJsonType, cell: MxCell) {
const { createMessage } = useMessage()
const handlerCommandDelivery = async (data: MouseUpEventDataType) => {
try {
const promiseList: (() => Promise<any>)[] = []
const { commandList = [], enable } = data || {}
if (!enable) return
for (const item of commandList) {
const { command, way, deviceId } = item
promiseList.push(async () => {
if (way === CommandWayEnum.TWO_WAY) {
const [record] = await getDeviceActive(deviceId)
if (!record.value) {
createMessage.warning('设备不在线')
return
}
}
await doCommandDelivery({
way,
deviceId,
command: {
additionalInfo: { cmdType: 'API' },
method: 'methodThingskit',
params: command,
persistent: true,
},
})
createMessage.success('下发成功')
})
}
await Promise.all(promiseList.map(fn => fn()))
}
catch (error) {
createMessage.error(error as string)
}
}
const handleOpenLink = (data: SingleClickEventDataType) => {
const { openLink } = data
window.open(openLink)
}
const handleSwitchPage = (data: SingleClickEventDataType) => {
const { openPage } = data
const pages = window.DrawApp.pages
const openPageFile = pages.find(item => item.getId() === openPage)
if (openPageFile)
window.DrawApp.selectPage(openPageFile)
}
const handleParamsSetting = async (_data: SingleClickEventDataType) => {
const deviceInfo = dataSourceJson
const instance = h(CommandDeliveryModal)
render(instance, document.body);
(instance.component?.exposed as InstanceType<typeof CommandDeliveryModal>)?.open({ ..._data, deviceInfo, operationPassword: eventJson?.OPERATION_PASSWORD })
}
const handleAttributeDelivery = async (data: SingleClickEventDataType) => {
try {
const nodeUtils = new NodeUtils()
const { way = CommandWayEnum.ONE_WAY } = data
const { attr, deviceId, attrInfo } = dataSourceJson
const { transportType, alias, name: deviceName } = await getDeviceInfo(dataSourceJson.deviceId) || {}// 设备信息
const contentDataStore = useContentDataStoreWithOut()
const currentData = contentDataStore.contentData.find(item => item.id === cell.getId())
if (!currentData) return
const { actJson, eventJson } = currentData
const { value: operationPassword, checked: operationPasswordEnable } = eventJson.OPERATION_PASSWORD
const command: RpcCommandType = {
additionalInfo: { cmdType: 'API' },
method: 'methodThingskit',
params: {},
persistent: true,
}
if (nodeUtils.getNodeComponentKey(cell) === ControlComponentEnum.SWITCH) {
const { rangeList } = actJson.STATUS_SETTING
const status = cell.getAttribute('SWITCH_STATUS')
const res = rangeList.find(item => item.type === (status === ActRangListItemTypeEnum.OPEN ? ActRangListItemTypeEnum.CLOSE : ActRangListItemTypeEnum.OPEN))
if (!res) return
const { statusValue } = res
command.params = transportType === TransportTypeEnum.TCP
? statusValue!
: {
[attr]: statusValue,
}
if (operationPasswordEnable) {
const instance = h(CommandDeliveryConfirmModal)
render(instance, document.body)
await (instance.component?.exposed as InstanceType<typeof CommandDeliveryConfirmModal>)?.open(operationPassword)
}
else {
const instance = h(ConfirmModal)
render(instance, document.body)
await (instance.component?.exposed as InstanceType<typeof ConfirmModal>)?.open()
}
await doCommandDelivery({ way, command, deviceId })
createMessage.success('命令下发成功')
}
else {
const instance = h(AttributeDeliverModal)
render(instance, document.body)
const value = await (instance.component?.exposed as InstanceType<typeof AttributeDeliverModal>)?.open({ title: `${alias || deviceName}-${attrInfo.name}`, operationPassword }) as string
command.params = transportType === TransportTypeEnum.TCP ? value : { [attr]: value }
await doCommandDelivery({ way, command, deviceId })
createMessage.success('命令下发成功')
}
}
catch (error) {
console.error(error)
}
}
const handlerMouseClickEvent = (data: SingleClickEventDataType) => {
const { actionType, enable } = data
if (!enable) return
if (actionType === EventActionTypeEnum.OPEN_LINK)
handleOpenLink(data)
else if (actionType === EventActionTypeEnum.OPEN_PAGE)
handleSwitchPage(data)
else if (actionType === EventActionTypeEnum.PARAMS_SETTING)
handleParamsSetting(data)
else if (actionType === EventActionTypeEnum.ATTRIBUTE_DELIVERY)
handleAttributeDelivery(data)
}
const handlerMouseDown = async () => {
eventJson.DOWN && handlerCommandDelivery(eventJson.DOWN)
}
const handlerMouseUp = () => {
eventJson.UP && handlerCommandDelivery(eventJson.UP)
}
const handlerMouseClick = () => {
eventJson.SINGLE && handlerMouseClickEvent(eventJson.SINGLE)
}
const handlerMouseDoubleClick = () => {
eventJson.DOUBLE && handlerMouseClickEvent(eventJson.DOUBLE)
}
return {
handlerMouseUp,
handlerMouseDown,
handlerMouseClick,
handlerMouseDoubleClick,
}
}