1
|
1
|
import { defineStore } from "pinia";
|
2
|
|
-import { KeyBoundComponentList, SocketComponentRecord, SocketReceiveMessageType, SocketSendMessageItemType, SocketSendMessageType, SocketStoreType } from '@/store/external/modules/socketStore.d'
|
|
2
|
+import { KeyBoundComponentList, SocketComponentRecord, SocketReceiveMessageType, SocketSendMessageItemType, SocketSendMessageType, SocketStoreType, UnsubscribePoolType } from '@/store/external/modules/socketStore.d'
|
3
|
3
|
import { CreateComponentType } from "@/packages/index.d";
|
4
|
4
|
import { RequestContentTypeEnum } from "@/enums/external/httpEnum";
|
5
|
5
|
import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore";
|
...
|
...
|
@@ -16,7 +16,8 @@ export const useSocketStore = defineStore({ |
16
|
16
|
connectionPool: {},
|
17
|
17
|
subscribePool: [],
|
18
|
18
|
cacheMessage: {},
|
19
|
|
- currentSubscribeId: 0
|
|
19
|
+ currentSubscribeId: 0,
|
|
20
|
+ unsubscribePool: []
|
20
|
21
|
}),
|
21
|
22
|
getters: {
|
22
|
23
|
/**
|
...
|
...
|
@@ -39,7 +40,16 @@ export const useSocketStore = defineStore({ |
39
|
40
|
},
|
40
|
41
|
actions: {
|
41
|
42
|
getSubscribeId() {
|
42
|
|
- return this.currentSubscribeId += this.currentSubscribeId
|
|
43
|
+ return this.currentSubscribeId++
|
|
44
|
+ },
|
|
45
|
+
|
|
46
|
+ setUnsubscribePool(message: UnsubscribePoolType) {
|
|
47
|
+ this.unsubscribePool.push(message)
|
|
48
|
+ },
|
|
49
|
+
|
|
50
|
+ removeUnsubscribePool(message: SocketReceiveMessageType) {
|
|
51
|
+ const index = this.unsubscribePool.findIndex(item => item.subscribeId === message.subscriptionId)
|
|
52
|
+ this.unsubscribePool.splice(index, 1)
|
43
|
53
|
},
|
44
|
54
|
|
45
|
55
|
/**
|
...
|
...
|
@@ -76,12 +86,27 @@ export const useSocketStore = defineStore({ |
76
|
86
|
*/
|
77
|
87
|
refreshSubscribedMessage(entityId: string) {
|
78
|
88
|
const isExist = this.subscribePool.findIndex(item => item.entityId === entityId)
|
|
89
|
+ const needUnsubscribe = !!~isExist
|
|
90
|
+
|
|
91
|
+ const newSubscribeId = this.getSubscribeId()
|
|
92
|
+ let oldSubscribeId: number
|
|
93
|
+ // 订阅设备不存在时
|
79
|
94
|
if (!~isExist) {
|
80
|
|
- const subscribeId = this.getSubscribeId()
|
81
|
|
- this.subscribePool.push({ subscribeId, entityId })
|
|
95
|
+
|
|
96
|
+ this.subscribePool.push({ subscribeId: newSubscribeId, entityId })
|
|
97
|
+ } else {
|
|
98
|
+ oldSubscribeId = this.subscribePool.findIndex(item => item.entityId === entityId)
|
|
99
|
+ this.subscribePool.splice(oldSubscribeId, 1)
|
|
100
|
+ this.subscribePool.push({ subscribeId: newSubscribeId, entityId })
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ const unSubscribeMessage = needUnsubscribe ? this.createUnSubscribeMessage(oldSubscribeId!, entityId) : null
|
|
104
|
+ const subscribeMessage = this.createMessage(newSubscribeId, entityId)
|
|
105
|
+
|
|
106
|
+ return {
|
|
107
|
+ unSubscribeMessage,
|
|
108
|
+ subscribeMessage
|
82
|
109
|
}
|
83
|
|
- const subscribeId = this.subscribePool.find(item => item.entityId === entityId)!.subscribeId!
|
84
|
|
- return this.createMessage(subscribeId, entityId, !!~isExist)
|
85
|
110
|
},
|
86
|
111
|
|
87
|
112
|
/**
|
...
|
...
|
@@ -90,18 +115,32 @@ export const useSocketStore = defineStore({ |
90
|
115
|
* @param entityId
|
91
|
116
|
* @returns
|
92
|
117
|
*/
|
93
|
|
- createMessage(subscribeId: number, entityId: string, needUnsubscribe: boolean): SocketSendMessageType {
|
|
118
|
+ createMessage(subscribeId: number, entityId: string): SocketSendMessageType {
|
94
|
119
|
const keys = Object.keys(Reflect.get(this.connectionPool, entityId)).join(',')
|
95
|
120
|
const messageInfo = { entityType: 'DEVICE', entityId: entityId, scope: "LATEST_TELEMETRY", cmdId: subscribeId, keys }
|
96
|
121
|
return {
|
97
|
122
|
tsSubCmds: [
|
98
|
|
- ...(needUnsubscribe ? [{ ...messageInfo, unsubscribe: true }] as SocketSendMessageItemType[] : []),
|
99
|
|
- messageInfo,
|
|
123
|
+ messageInfo
|
100
|
124
|
]
|
101
|
125
|
}
|
102
|
126
|
},
|
103
|
127
|
|
104
|
128
|
/**
|
|
129
|
+ * @description 创建取消订阅的消息
|
|
130
|
+ * @param subscribeId
|
|
131
|
+ * @param entityId
|
|
132
|
+ */
|
|
133
|
+ createUnSubscribeMessage(subscribeId: number, entityId: string): SocketSendMessageType {
|
|
134
|
+ const keys = Object.keys(Reflect.get(this.connectionPool, entityId)).join(',')
|
|
135
|
+ const messageInfo = { entityType: 'DEVICE', entityId: entityId, scope: "LATEST_TELEMETRY", cmdId: subscribeId, keys, unsubscribe: true } as SocketSendMessageItemType
|
|
136
|
+ const message = {
|
|
137
|
+ tsSubCmds: [messageInfo]
|
|
138
|
+ }
|
|
139
|
+ this.setUnsubscribePool({ subscribeId, message })
|
|
140
|
+ return message
|
|
141
|
+ },
|
|
142
|
+
|
|
143
|
+ /**
|
105
|
144
|
* @description 订阅
|
106
|
145
|
* @param targetComponent
|
107
|
146
|
*/
|
...
|
...
|
@@ -138,6 +177,7 @@ export const useSocketStore = defineStore({ |
138
|
177
|
*/
|
139
|
178
|
getNeedUpdateComponentsIdBySubscribeId(subscribeId: number, keys: string[]) {
|
140
|
179
|
const entityId = this.subscribePool.find(item => item.subscribeId === subscribeId)?.entityId
|
|
180
|
+
|
141
|
181
|
if (entityId) {
|
142
|
182
|
const keysRecord = Reflect.get(this.connectionPool, entityId)
|
143
|
183
|
const needUpdateComponents = keys.map(key => keysRecord[key])
|
...
|
...
|
@@ -160,14 +200,14 @@ export const useSocketStore = defineStore({ |
160
|
200
|
const { keys = '' } = Params
|
161
|
201
|
const targetComponentBindKeys = keys.split(KEYS_SEPARATOR)
|
162
|
202
|
|
163
|
|
- const _value = cloneDeep(value)
|
|
203
|
+ const _value = cloneDeep(value) || { data: {}, latestValues: {} }
|
164
|
204
|
_value.data = targetComponentBindKeys.reduce((prev, next) => {
|
165
|
|
- return { ...prev, [next]: value.data[next] }
|
|
205
|
+ return { ...prev, [next]: _value.data[next] }
|
166
|
206
|
}, {})
|
167
|
207
|
_value.latestValues = targetComponentBindKeys.reduce((prev, next) => {
|
168
|
|
- return { ...prev, [next]: value.latestValues[next] }
|
|
208
|
+ return { ...prev, [next]: _value.latestValues[next] }
|
169
|
209
|
}, {})
|
170
|
|
-
|
|
210
|
+
|
171
|
211
|
return _value
|
172
|
212
|
},
|
173
|
213
|
|
...
|
...
|
@@ -198,10 +238,23 @@ export const useSocketStore = defineStore({ |
198
|
238
|
const { subscriptionId, data } = value
|
199
|
239
|
const keys = Object.keys(data)
|
200
|
240
|
const componentIds = this.getNeedUpdateComponentsIdBySubscribeId(subscriptionId, keys)
|
|
241
|
+ console.log(componentIds)
|
201
|
242
|
componentIds?.forEach((targetComponentId) => {
|
202
|
|
- this.updateComponentById(targetComponentId as string , value)
|
|
243
|
+ this.updateComponentById(targetComponentId as string, value)
|
203
|
244
|
})
|
|
245
|
+ },
|
204
|
246
|
|
|
247
|
+ /**
|
|
248
|
+ * @description socket接受到消息后,从需要取消的订阅池中取消订阅消息
|
|
249
|
+ */
|
|
250
|
+ unsubscribe(message: SocketReceiveMessageType, unsubscribeFn: Fn) {
|
|
251
|
+ const { subscriptionId } = message
|
|
252
|
+ if (subscriptionId === undefined) return
|
|
253
|
+ const index = this.unsubscribePool.findIndex(item => item.subscribeId === subscriptionId)
|
|
254
|
+ if (!~index) return
|
|
255
|
+ const sendMessage = this.unsubscribePool[index].message
|
|
256
|
+ unsubscribeFn(JSON.stringify(sendMessage))
|
|
257
|
+ this.removeUnsubscribePool(message)
|
205
|
258
|
}
|
206
|
259
|
}
|
207
|
260
|
}) |
...
|
...
|
|