useNodeEvent.ts
5.55 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
import { h, render, toRaw, unref } from 'vue'
import { ControlComponentEnum } from '../packages/Control'
import { doCommandDelivery, getDeviceActive } from '@/api/device'
import type { MouseUpEventDataType, NodeDataDataSourceJsonType, NodeDataEventJsonType, SingleClickEventDataType } from '@/api/node/model'
import { CommandMethodEnum, CommandTypeEnum, CommandWayEnum } from '@/enums/commandEnum'
import { ActRangListItemTypeEnum, EventActionTypeEnum } from '@/enums/datasource'
import { useMessage } from '@/hooks/web/useMessage'
import { AttributeDeliverModal, CommandDeliveryModal, SwitchCommandDeliveryModal } 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'
export function useNodeEvent(eventJson: NodeDataEventJsonType, dataSourceJson: NodeDataDataSourceJsonType, cell: MxCell) {
const { createMessage } = useMessage()
/**
* @description 多命令下发
* @param data
* @returns
*/
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: CommandTypeEnum.API },
method: CommandMethodEnum.THINGSKIT,
params: command,
persistent: true,
},
})
createMessage.success('下发成功')
})
}
await Promise.all(promiseList.map(fn => fn()))
}
catch (error) {
createMessage.error(error as string)
}
}
/**
* @description 打开链接
* @param data
*/
const handleOpenLink = (data: SingleClickEventDataType) => {
const { openLink } = data
window.open(openLink)
}
/**
* @description 跳转页面
* @param data
*/
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 instance = h(CommandDeliveryModal)
render(instance, document.body);
(instance.component?.exposed as InstanceType<typeof CommandDeliveryModal>)?.open({ dataSource: toRaw(unref(dataSourceJson)), eventBindData: data, operationPasswordInfo: eventJson.OPERATION_PASSWORD })
}
const handleAttributeDelivery = async (data: SingleClickEventDataType) => {
try {
const nodeUtils = new NodeUtils()
const operationPasswordInfo = toRaw(unref(eventJson.OPERATION_PASSWORD))
if (nodeUtils.getNodeComponentKey(cell) === ControlComponentEnum.SWITCH) {
const contentDataStore = useContentDataStoreWithOut()
const currentData = contentDataStore.contentData.find(item => item.configurationNodeId === cell.getId())
if (!currentData) return
const { actJson } = currentData
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
const instance = h(SwitchCommandDeliveryModal)
render(instance, document.body)
await (instance.component?.exposed as InstanceType<typeof SwitchCommandDeliveryModal>)?.open({ operationPasswordInfo, eventBindData: toRaw(unref(data)), dataSourceJson: toRaw(unref(dataSourceJson)), sendValue: statusValue }!)
}
else {
const instance = h(AttributeDeliverModal)
render(instance, document.body);
(instance.component?.exposed as InstanceType<typeof AttributeDeliverModal>)?.open({ dataSourceJson, operationPasswordInfo, eventBindData: data })
}
}
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,
}
}