useSendCommand.ts
1.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
import { ControlComponentValue } from './control.config';
import { getDeviceProfile } from '/@/api/alarm/position';
import { sendCommandOneway } from '/@/api/dataBoard';
import { getModelServices } from '/@/api/device/modelOfMatter';
import { useMessage } from '/@/hooks/web/useMessage';
import { isString } from '/@/utils/is';
const { createMessage } = useMessage();
export function useSendCommand() {
const sendCommand = async (record: ControlComponentValue, value: any) => {
const { deviceId, deviceProfileId, attribute } = record;
if (!deviceId) return;
try {
const list = await getDeviceProfile();
const deviceProfile = list.find((item) => item.id === deviceProfileId);
if (!deviceProfile) return;
let params: string | Recordable = {
[attribute!]: Number(value),
};
if (deviceProfile.transportType === 'TCP') {
const serviceList = await getModelServices({ deviceProfileId: deviceProfileId! });
const record = serviceList.find((item) => item.identifier === attribute);
const sendCommand = record?.functionJson.inputData?.at(0)?.serviceCommand || '';
params = isString(sendCommand) ? sendCommand : JSON.stringify(sendCommand);
}
await sendCommandOneway({
deviceId,
value: {
params: params,
persistent: true,
additionalInfo: {
cmdType: 'API',
},
method: 'methodThingskit',
},
});
createMessage.success('命令下发成功');
} catch (error) {}
};
return {
sendCommand,
};
}