useSocketConnect.ts
3.98 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
import { useWebSocket } from '@vueuse/core';
import { Ref, unref } from 'vue';
import { DataBoardLayoutInfo } from '../types/type';
import { JWT_TOKEN_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import { isNullAndUnDef } from '/@/utils/is';
interface SocketMessage {
tsSubCmds: SocketMessageItem[];
}
interface SocketMessageItem {
entityType: string;
entityId: string;
scope: string;
cmdId: number;
keys: string;
}
interface CmdMapping {
componentId: string;
deviceId: string;
recordIndex: number;
dataSourceIndex: number;
attribute: string;
}
interface ResponseMessage {
subscriptionId: number;
errorCode: number;
errorMsg: Nullable<string>;
data: {
[key: string]: [[number, string]];
};
latestValues: {
[key: string]: number;
};
}
const generateMessage = (deviceId: string, cmdId: number, attr: string): SocketMessageItem => {
return {
entityType: 'DEVICE',
entityId: deviceId,
scope: 'LATEST_TELEMETRY',
cmdId,
keys: attr,
};
};
export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) {
const token = getAuthCache(JWT_TOKEN_KEY);
const cmdIdMapping = new Map<number, CmdMapping>();
const waitSendQueue: string[] = [];
const config = {
server: `${import.meta.env.VITE_WEB_SOCKET}${token}`,
};
// const getNeedUpdateValueById = (componentId: string, deviceId: string) => {};
const getNeedUpdateValueByIndex = (recordIndex: number, dataSourceIndex: number) => {
return unref(dataSourceRef)[recordIndex].record.dataSource[dataSourceIndex];
};
const { close, send, open, status } = useWebSocket(config.server, {
onConnected() {
if (waitSendQueue.length) {
waitSendQueue.forEach((string) => {
send(string);
});
waitSendQueue.length = 0;
}
},
onMessage(_ws, message) {
try {
const res: ResponseMessage = JSON.parse(message.data);
const { subscriptionId, data = {} } = res;
if (isNullAndUnDef(subscriptionId)) return;
const mappingRecord = cmdIdMapping.get(subscriptionId);
if (!mappingRecord) return;
const { attribute, recordIndex, dataSourceIndex } = mappingRecord;
const [[timespan, value]] = data[attribute];
const record = getNeedUpdateValueByIndex(recordIndex, dataSourceIndex);
record.componentInfo.value = value;
record.componentInfo.updateTime = timespan;
} catch (error) {
throw Error(error as string);
}
},
// onDisconnected() {
// close();
// },
});
const setCmdId = (cmdId: number, record: CmdMapping) => {
cmdIdMapping.set(cmdId, record);
};
const transformSocketMessageItem = () => {
const messageList: SocketMessageItem[] = [];
let index = 0;
unref(dataSourceRef).forEach((record, recordIndex) => {
const componentId = record.record.id;
for (
let dataSourceIndex = 0;
dataSourceIndex < record.record.dataSource.length;
dataSourceIndex++
) {
const dataSource = record.record.dataSource[dataSourceIndex];
const { deviceId, attribute, slaveDeviceId, gatewayDevice } = dataSource;
if (!attribute) continue;
const cmdId = index;
index++;
setCmdId(cmdId, {
componentId,
deviceId: gatewayDevice ? deviceId : slaveDeviceId,
recordIndex,
dataSourceIndex,
attribute,
});
messageList.push(
generateMessage(gatewayDevice ? slaveDeviceId : deviceId, cmdId, attribute)
);
}
});
return {
tsSubCmds: messageList,
} as SocketMessage;
};
const beginSendMessage = () => {
// close();
cmdIdMapping.clear();
// open();
const messageList = transformSocketMessageItem();
if (unref(status) !== 'OPEN') {
waitSendQueue.push(JSON.stringify(messageList));
return;
}
send(JSON.stringify(messageList));
};
return {
close,
send,
open,
beginSendMessage,
};
}