useSendCommand.ts
2.34 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
import { ControlComponentValue } from './control.config';
import { getDeviceProfile } from '/@/api/alarm/position';
import { getDeviceRelation, sendCommandOneway } from '/@/api/dataBoard';
import { DeviceTypeEnum } from '/@/api/device/model/deviceModel';
import { getModelServices } from '/@/api/device/modelOfMatter';
import { useMessage } from '/@/hooks/web/useMessage';
import { isString } from '/@/utils/is';
import { TransportTypeEnum } from '/@/views/device/profiles/components/TransportDescript/const';
const { createMessage } = useMessage();
export function useSendCommand() {
const error = () => {
createMessage.error('下发指令失败');
return false;
};
const sendCommand = async (record: ControlComponentValue, value: any) => {
if (!record) return error();
const { deviceProfileId, attribute, deviceType } = record;
let { deviceId } = record;
if (!deviceId) return error();
try {
const list = await getDeviceProfile();
const deviceProfile = list.find((item) => item.id === deviceProfileId);
if (!deviceProfile) return error();
let params: string | Recordable = {
[attribute!]: Number(value),
};
// 如果是TCP设备从物模型中获取下发命令(TCP网关子设备无物模型服务与事件)
if (deviceProfile!.transportType === TransportTypeEnum.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);
}
if (deviceType === DeviceTypeEnum.SENSOR) {
deviceId = await getDeviceRelation({
deviceId,
isSlave: deviceType === DeviceTypeEnum.SENSOR,
});
}
// 控制按钮下发命令为0 或 1
await sendCommandOneway({
deviceId,
value: {
params: params,
persistent: true,
additionalInfo: {
cmdType: 'API',
},
method: 'methodThingskit',
},
});
createMessage.success('命令下发成功');
return true;
} catch (msg) {
console.log('enter');
return error();
}
};
return {
sendCommand,
};
}