messageHandler.ts
2.86 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
import { isNull } from 'lodash-es'
import type { App } from 'vue'
import type { SubscriptionUpdateMsg } from '../type/message'
import { DataDynamicEffectHandler } from './dataDynamicEffectHandler'
import { useObjectModelValue } from './useObjectModelValue'
import type { CommandSource, LightboxModeWebsocketService } from '.'
import type { ActTypeEnum } from '@/enums/datasource'
import { DataSourceTypeEnum } from '@/enums/datasource'
import { getAppInstanceId } from '@/utils/draw'
import type { RenderComponentExposeType } from '@/core/Library/types'
import { useLatestMessageValue } from '@/core/Library/hook/useLatestMessageValue'
import type { NodeDataDataSourceJsonType } from '@/api/node/model'
export class MessageHandler {
constructor(public service: LightboxModeWebsocketService) {
}
get nodeUtils() {
return this.service.nodeUtils
}
distribute(message: SubscriptionUpdateMsg) {
const { subscriptionId } = message
const telemetrySubscriber = this.service.subscribersMap.get(subscriptionId)
if (!telemetrySubscriber) return
const { commandSource } = telemetrySubscriber
const { dataType } = commandSource
if (dataType === DataSourceTypeEnum.DATASOURCE)
this.updateDataSource(commandSource, message)
else if (dataType === DataSourceTypeEnum.ACT)
this.updateDataDynamicEffect(commandSource, message)
}
updateDataSource(commandSource: CommandSource, message: SubscriptionUpdateMsg) {
try {
const { node } = commandSource
const cell = this.nodeUtils.getCellById(node)
if (!cell) return
const instanceId = getAppInstanceId(cell)
const instance = window?.VueInstanceMap?.[instanceId] as App
if (instance && (instance._instance?.exposed as RenderComponentExposeType)?.onMessage)
(instance._instance?.exposed as RenderComponentExposeType)?.onMessage?.(commandSource, message)
else
this.defaultHandler(commandSource, message)
}
catch (error) {
console.error(error)
}
}
defaultHandler(commandSource: CommandSource, message: SubscriptionUpdateMsg) {
const { data, node } = commandSource
const { attr, deviceProfileId } = data as NodeDataDataSourceJsonType
let { latestValue } = useLatestMessageValue(message.data, attr as string)
if (isNull(latestValue)) return
latestValue = useObjectModelValue(deviceProfileId, attr as string, latestValue)
const cell = this.nodeUtils.getCellById(node)
const cellValue = cell.getValue() as Element
cellValue.setAttribute('label', latestValue)
this.nodeUtils.updateCellValue(cell, cellValue)
}
updateDataDynamicEffect(commandSource: CommandSource, message: SubscriptionUpdateMsg) {
const { type } = commandSource
const handler = new DataDynamicEffectHandler(this.service)
if (!type) return
if (handler[type as ActTypeEnum])
handler[type as ActTypeEnum]?.(commandSource, message)
}
}