useControlCommand.ts
2.72 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
import { ref } from 'vue';
import { useDeviceProfileQueryContext } from '../../palette/hooks/useDeviceProfileQueryContext';
import {
CommandDeliveryWayEnum,
CommandTypeEnum,
ServiceCallTypeEnum,
TransportTypeEnum,
} from '/@/enums/deviceEnum';
import { useMessage } from '/@/hooks/web/useMessage';
import { useCommandDelivery } from '/@/hooks/business/useCommandDelivery';
export interface DoCommandDeliverDataSourceType {
deviceId: string;
deviceProfileId: string;
attribute: string;
commandType?: CommandTypeEnum;
openCommand?: string;
closeCommand?: string;
openService?: string;
closeService?: string;
}
export function useControlComand() {
const loading = ref(false);
const { createMessage } = useMessage();
const { getDeviceProfileTslByIdWithIdentifier } = useDeviceProfileQueryContext();
const doControlSendCommand = async (
dataSource: DoCommandDeliverDataSourceType,
value: any
): Promise<boolean> => {
try {
const { deviceId, deviceProfileId, attribute, commandType } = dataSource;
loading.value = true;
const tsl = getDeviceProfileTslByIdWithIdentifier?.(deviceProfileId, attribute);
const { doCommandDelivery } = useCommandDelivery();
await doCommandDelivery({
deviceId,
identifier: attribute,
objectModel: tsl,
value,
cmdType: commandType,
beforeFetch: (rpcCommand, { transportType, isTCPModbus }) => {
let way = CommandDeliveryWayEnum.ONE_WAY;
if (transportType === TransportTypeEnum.TCP && !isTCPModbus) {
rpcCommand.params = value;
if (commandType === CommandTypeEnum.CUSTOM) {
const { openCommand, closeCommand } = dataSource;
rpcCommand.params = !!Number(value) ? openCommand! : closeCommand!;
} else if (commandType === CommandTypeEnum.SERVICE) {
const { openService, closeService } = dataSource;
const serviceIdentifier = !!Number(value) ? openService : closeService;
const serviceTsl = getDeviceProfileTslByIdWithIdentifier?.(
deviceProfileId,
serviceIdentifier!
);
rpcCommand.params = serviceTsl?.inputData?.[0].serviceCommand as unknown as string;
way =
serviceTsl?.callType === ServiceCallTypeEnum.SYNC
? CommandDeliveryWayEnum.TWO_WAY
: CommandDeliveryWayEnum.ONE_WAY;
}
}
return { rpcCommand, way };
},
});
createMessage.success('命令下发成功');
return true;
} catch (e) {
return false;
} finally {
loading.value = false;
}
};
return {
loading,
doControlSendCommand,
};
}