useCommandDelivery.ts
5.53 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { ref } from 'vue';
import { commandIssuanceApi, getDeviceDetail } from '/@/api/device/deviceManager';
import { RpcCommandType } from '/@/api/device/model/deviceConfigModel';
import { DeviceProfileModel } from '/@/api/device/model/deviceModel';
import { EdgeDeviceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { Tsl } from '/@/api/device/model/modelOfMatterModel';
import { getModelTsl } from '/@/api/device/modelOfMatter';
import {
CommandDeliveryWayEnum,
CommandTypeEnum,
RPCCommandMethodEnum,
ServiceCallTypeEnum,
TCPProtocolTypeEnum,
TransportTypeEnum,
} from '/@/enums/deviceEnum';
import { FunctionTypeEnum } from '/@/enums/objectModelEnum';
import { isFunction, isNullOrUnDef } from '/@/utils/is';
import { getDeviceActiveTime } from '/@/api/alarm/position';
import { useMessage } from '/@/hooks/web/useMessage';
import { useCoverModbusCommand } from './useCoverModbusCommand';
interface SetupType {
entityId: string;
transportType: TransportTypeEnum;
isTCPModbus: boolean;
deviceCode: string | undefined;
deviceDetail: EdgeDeviceItemType;
objectModel: Tsl | undefined;
identifier: string;
}
interface DoCommandDeliverParamsType {
value: any;
deviceDetail?: EdgeDeviceItemType;
deviceId?: string;
identifier?: string;
objectModel?: Tsl;
deviceProfileId?: string;
deviceProfileDetail?: DeviceProfileModel;
way?: CommandDeliveryWayEnum;
cmdType?: CommandTypeEnum;
transportType?: TransportTypeEnum;
beforeFetch?: (
rpcCommand: RpcCommandType,
setup: SetupType
) =>
| { rpcCommand: RpcCommandType; way?: CommandDeliveryWayEnum }
| Promise<{ rpcCommand: RpcCommandType; way?: CommandDeliveryWayEnum }>;
}
export function useCommandDelivery() {
const loading = ref(false);
async function doSetup(params: DoCommandDeliverParamsType) {
let { deviceDetail, identifier, deviceProfileId, objectModel, transportType } = params;
const { deviceId } = params;
const entityId = deviceId || deviceDetail?.id?.id;
if (!entityId) {
throw new Error('not found entityId');
}
identifier = identifier || objectModel?.identifier;
if (!identifier) {
throw new Error('not found identifier');
}
transportType = transportType || (deviceDetail?.transportType as TransportTypeEnum);
if (
!transportType ||
(transportType === TransportTypeEnum.TCP && !deviceDetail) ||
!deviceDetail?.deviceProfile
) {
deviceDetail = (await getDeviceDetail(entityId)) as any as EdgeDeviceItemType;
transportType = deviceDetail?.transportType as TransportTypeEnum;
}
const isTCPModbus =
transportType === TransportTypeEnum.TCP &&
deviceDetail?.deviceProfile?.profileData?.transportConfiguration?.protocol ===
TCPProtocolTypeEnum.MODBUS_RTU;
if (isTCPModbus && !objectModel?.extensionDesc) {
// deviceProfileId = deviceDetail?.deviceProfileId || deviceProfileId || deviceProfileDetail?.id;
deviceProfileId = deviceDetail?.deviceProfileId?.id;
if (!deviceProfileId) {
throw new Error('not found deviceProfile');
}
const objectModels = await getModelTsl({ deviceProfileId });
objectModel = objectModels.find((item) => item.identifier === identifier);
}
const deviceCode = deviceDetail?.code;
return {
entityId,
transportType,
isTCPModbus,
deviceCode,
deviceDetail,
objectModel,
identifier,
};
}
async function doCommandDelivery(params: DoCommandDeliverParamsType) {
try {
loading.value = true;
const setupResult = await doSetup(params);
const { entityId, transportType, isTCPModbus, deviceCode, objectModel, identifier } =
setupResult;
let command = params.value;
if (transportType === TransportTypeEnum.TCP) {
command = params.value;
if (isTCPModbus) {
const { doCoverCommand } = useCoverModbusCommand();
command = await doCoverCommand(params.value, objectModel!, deviceCode, entityId);
}
} else {
command = {
[identifier]: command,
};
}
let rpcCommand: RpcCommandType = {
persistent: true,
method: RPCCommandMethodEnum.THINGSKIT,
additionalInfo: {
cmdType: params.cmdType ?? CommandTypeEnum.API,
},
params: command,
};
let way = params.way ?? CommandDeliveryWayEnum.ONE_WAY;
if (objectModel?.functionType === FunctionTypeEnum.SERVICE) {
rpcCommand.additionalInfo.cmdType = CommandTypeEnum.SERVICE;
way =
objectModel.callType === ServiceCallTypeEnum.ASYNC
? CommandDeliveryWayEnum.ONE_WAY
: CommandDeliveryWayEnum.TWO_WAY;
}
const sendApi = commandIssuanceApi;
if (params.beforeFetch && isFunction(params.beforeFetch)) {
const { rpcCommand: _rpcCommand, way: _way } = await params.beforeFetch(
rpcCommand,
setupResult
);
!isNullOrUnDef(rpcCommand) && (rpcCommand = _rpcCommand);
if (_way) way = _way;
}
if (way === CommandDeliveryWayEnum.TWO_WAY) {
const result = await getDeviceActiveTime(entityId);
const [firsetItem] = result || [];
if (!firsetItem.value) {
const { createMessage } = useMessage();
const message = '当前设备不在线';
createMessage.warning(message);
throw Error(message);
}
}
await sendApi(way, entityId, rpcCommand);
} finally {
loading.value = false;
}
}
return {
loading,
doCommandDelivery,
};
}