Commit 026fd0f4fe1ddfa65ef757594919edd18d403fcd
1 parent
4e663db6
perf: visual board socket subscribe message
Showing
1 changed file
with
56 additions
and
38 deletions
@@ -17,12 +17,13 @@ interface SocketMessageItem { | @@ -17,12 +17,13 @@ interface SocketMessageItem { | ||
17 | keys: string; | 17 | keys: string; |
18 | } | 18 | } |
19 | 19 | ||
20 | -interface CmdMapping { | ||
21 | - componentId: string; | ||
22 | - deviceId: string; | 20 | +interface GroupMappingRecord { |
21 | + id: string; | ||
23 | recordIndex: number; | 22 | recordIndex: number; |
24 | dataSourceIndex: number; | 23 | dataSourceIndex: number; |
25 | attribute: string; | 24 | attribute: string; |
25 | + deviceId: string; | ||
26 | + slaveDeviceId: string; | ||
26 | } | 27 | } |
27 | 28 | ||
28 | interface ResponseMessage { | 29 | interface ResponseMessage { |
@@ -50,7 +51,9 @@ const generateMessage = (deviceId: string, cmdId: number, attr: string): SocketM | @@ -50,7 +51,9 @@ const generateMessage = (deviceId: string, cmdId: number, attr: string): SocketM | ||
50 | export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | 51 | export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { |
51 | const token = getAuthCache(JWT_TOKEN_KEY); | 52 | const token = getAuthCache(JWT_TOKEN_KEY); |
52 | 53 | ||
53 | - const cmdIdMapping = new Map<number, CmdMapping>(); | 54 | + const cmdIdMapping = new Map<number, GroupMappingRecord[]>(); |
55 | + | ||
56 | + const groupMapping = new Map<string, GroupMappingRecord[]>(); | ||
54 | 57 | ||
55 | const waitSendQueue: string[] = []; | 58 | const waitSendQueue: string[] = []; |
56 | 59 | ||
@@ -64,6 +67,44 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | @@ -64,6 +67,44 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | ||
64 | return unref(dataSourceRef)[recordIndex].record.dataSource[dataSourceIndex]; | 67 | return unref(dataSourceRef)[recordIndex].record.dataSource[dataSourceIndex]; |
65 | }; | 68 | }; |
66 | 69 | ||
70 | + const mergeGroup = (dataSourceRef: Ref<DataBoardLayoutInfo[]>) => { | ||
71 | + for (let recordIndex = 0; recordIndex < unref(dataSourceRef).length; recordIndex++) { | ||
72 | + const record = unref(dataSourceRef).at(recordIndex)!; | ||
73 | + const dataSource = record?.record.dataSource; | ||
74 | + for (let dataSourceIndex = 0; dataSourceIndex < dataSource.length; dataSourceIndex++) { | ||
75 | + const dataDatum = dataSource.at(dataSourceIndex)!; | ||
76 | + const { deviceId, slaveDeviceId, attribute } = dataDatum; | ||
77 | + const groupMappingRecord: GroupMappingRecord = { | ||
78 | + id: record.record.id, | ||
79 | + recordIndex, | ||
80 | + dataSourceIndex, | ||
81 | + attribute, | ||
82 | + deviceId, | ||
83 | + slaveDeviceId, | ||
84 | + }; | ||
85 | + if (groupMapping.has(slaveDeviceId || deviceId)) { | ||
86 | + const group = groupMapping.get(slaveDeviceId || deviceId); | ||
87 | + group?.push(groupMappingRecord); | ||
88 | + } else { | ||
89 | + groupMapping.set(slaveDeviceId || deviceId, [groupMappingRecord]); | ||
90 | + } | ||
91 | + } | ||
92 | + } | ||
93 | + }; | ||
94 | + | ||
95 | + function generateGroupMessage() { | ||
96 | + const messageList: SocketMessageItem[] = []; | ||
97 | + let cmdId = 0; | ||
98 | + groupMapping.forEach((value, key) => { | ||
99 | + const message = generateMessage(key, cmdId, value.map((item) => item.attribute).join(',')); | ||
100 | + messageList.push(message); | ||
101 | + setCmdId(cmdId, value); | ||
102 | + cmdId++; | ||
103 | + }); | ||
104 | + console.log(cmdIdMapping); | ||
105 | + return messageList; | ||
106 | + } | ||
107 | + | ||
67 | const { close, send, open, status } = useWebSocket(config.server, { | 108 | const { close, send, open, status } = useWebSocket(config.server, { |
68 | onConnected() { | 109 | onConnected() { |
69 | if (waitSendQueue.length) { | 110 | if (waitSendQueue.length) { |
@@ -80,11 +121,14 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | @@ -80,11 +121,14 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | ||
80 | if (isNullAndUnDef(subscriptionId)) return; | 121 | if (isNullAndUnDef(subscriptionId)) return; |
81 | const mappingRecord = cmdIdMapping.get(subscriptionId); | 122 | const mappingRecord = cmdIdMapping.get(subscriptionId); |
82 | if (!mappingRecord) return; | 123 | if (!mappingRecord) return; |
83 | - const { attribute, recordIndex, dataSourceIndex } = mappingRecord; | ||
84 | - const [[timespan, value]] = data[attribute]; | ||
85 | - const record = getNeedUpdateValueByIndex(recordIndex, dataSourceIndex); | ||
86 | - record.componentInfo.value = value; | ||
87 | - record.componentInfo.updateTime = timespan; | 124 | + mappingRecord.forEach((item) => { |
125 | + const { attribute, recordIndex, dataSourceIndex } = item; | ||
126 | + const [[timespan, value]] = data[attribute]; | ||
127 | + const record = getNeedUpdateValueByIndex(recordIndex, dataSourceIndex); | ||
128 | + record.componentInfo.value = value; | ||
129 | + record.componentInfo.updateTime = timespan; | ||
130 | + }); | ||
131 | + return; | ||
88 | } catch (error) { | 132 | } catch (error) { |
89 | throw Error(error as string); | 133 | throw Error(error as string); |
90 | } | 134 | } |
@@ -94,40 +138,14 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | @@ -94,40 +138,14 @@ export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { | ||
94 | // }, | 138 | // }, |
95 | }); | 139 | }); |
96 | 140 | ||
97 | - const setCmdId = (cmdId: number, record: CmdMapping) => { | 141 | + const setCmdId = (cmdId: number, record: GroupMappingRecord[]) => { |
98 | cmdIdMapping.set(cmdId, record); | 142 | cmdIdMapping.set(cmdId, record); |
99 | }; | 143 | }; |
100 | 144 | ||
101 | const transformSocketMessageItem = () => { | 145 | const transformSocketMessageItem = () => { |
102 | - const messageList: SocketMessageItem[] = []; | ||
103 | - let index = 0; | ||
104 | - unref(dataSourceRef).forEach((record, recordIndex) => { | ||
105 | - const componentId = record.record.id; | ||
106 | - for ( | ||
107 | - let dataSourceIndex = 0; | ||
108 | - dataSourceIndex < record.record.dataSource.length; | ||
109 | - dataSourceIndex++ | ||
110 | - ) { | ||
111 | - const dataSource = record.record.dataSource[dataSourceIndex]; | ||
112 | - const { deviceId, attribute, slaveDeviceId, gatewayDevice } = dataSource; | ||
113 | - if (!attribute) continue; | ||
114 | - const cmdId = index; | ||
115 | - index++; | ||
116 | - setCmdId(cmdId, { | ||
117 | - componentId, | ||
118 | - deviceId: gatewayDevice ? deviceId : slaveDeviceId, | ||
119 | - recordIndex, | ||
120 | - dataSourceIndex, | ||
121 | - attribute, | ||
122 | - }); | ||
123 | - | ||
124 | - messageList.push( | ||
125 | - generateMessage(gatewayDevice ? slaveDeviceId : deviceId, cmdId, attribute) | ||
126 | - ); | ||
127 | - } | ||
128 | - }); | 146 | + mergeGroup(dataSourceRef); |
129 | return { | 147 | return { |
130 | - tsSubCmds: messageList, | 148 | + tsSubCmds: generateGroupMessage(), |
131 | } as SocketMessage; | 149 | } as SocketMessage; |
132 | }; | 150 | }; |
133 | 151 |