useCommandDelivery.ts
3.76 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
import { ref } from 'vue';
import { getDeviceDetail } from '/@/api/device/deviceManager';
import { useDeviceProfileQueryContext } from '../../palette/hooks/useDeviceProfileQueryContext';
import {
  CommandTypeEnum,
  RPCCommandMethodEnum,
  ServiceCallTypeEnum,
  TransportTypeEnum,
} from '/@/enums/deviceEnum';
import { RpcCommandType } from '/@/api/device/model/deviceConfigModel';
import { useMessage } from '/@/hooks/web/useMessage';
import { useGetModbusCommand } from './useGetModbusCommand';
import { TaskTypeEnum } from '/@/views/task/center/config';
import { DataTypeEnum } from '/@/enums/objectModelEnum';
import { sendCommandOneway, sendCommandTwoway } from '/@/api/dataBoard';
import { isNullOrUnDef } from '/@/utils/is';
export interface DoCommandDeliverDataSourceType {
  deviceId: string;
  deviceProfileId: string;
  attribute: string;
  commandType?: CommandTypeEnum;
  openCommand?: string;
  closeCommand?: string;
  openService?: string;
  closeService?: string;
}
export function useCommandDelivery() {
  const loading = ref(false);
  const { createMessage } = useMessage();
  const { getDeviceProfileTslByIdWithIdentifier } = useDeviceProfileQueryContext();
  const doGetDeviceId = async (deviceId: string) => {
    return await getDeviceDetail(deviceId);
  };
  const { validateCanGetCommand, getModbusCommand } = useGetModbusCommand();
  const doCommandDeliver = async (
    dataSource: DoCommandDeliverDataSourceType,
    value: any
  ): Promise<boolean> => {
    try {
      const { deviceId, deviceProfileId, attribute, commandType } = dataSource;
      loading.value = true;
      const tsl = getDeviceProfileTslByIdWithIdentifier?.(deviceProfileId, attribute);
      const deviceDetail = await doGetDeviceId(deviceId);
      let sendCommandFn = sendCommandOneway;
      let params: string | Recordable | undefined = {
        [attribute]: value,
      };
      if (deviceDetail.transportType === TransportTypeEnum.TCP) {
        if (deviceDetail.codeType === TaskTypeEnum.MODBUS_RTU) {
          if (!validateCanGetCommand(tsl?.extensionDesc, deviceDetail.code)) return false;
          params = await getModbusCommand(value, tsl!.extensionDesc, deviceDetail.code!);
        } else if (deviceDetail.codeType === TaskTypeEnum.CUSTOM) {
          params = value;
          if (commandType === CommandTypeEnum.CUSTOM) {
            if (tsl?.specs?.dataType.type === DataTypeEnum.BOOL) {
              const { openCommand, closeCommand } = dataSource;
              params = !!Number(value) ? openCommand : closeCommand;
            }
          } else if (commandType === CommandTypeEnum.SERVICE) {
            if (tsl?.specs?.dataType.type === DataTypeEnum.BOOL) {
              const { openService, closeService } = dataSource;
              const serviceIdentifier = !!Number(value) ? openService : closeService;
              const serviceTsl = getDeviceProfileTslByIdWithIdentifier?.(
                deviceProfileId,
                serviceIdentifier!
              );
              params = serviceTsl?.inputData?.[0].serviceCommand;
              sendCommandFn =
                serviceTsl?.callType === ServiceCallTypeEnum.SYNC
                  ? sendCommandTwoway
                  : sendCommandOneway;
            }
          }
        }
      }
      if (isNullOrUnDef(params)) return false;
      const rpcCommand: RpcCommandType = {
        additionalInfo: {
          cmdType: CommandTypeEnum.API,
        },
        params,
        method: RPCCommandMethodEnum.THINGSKIT,
        persistent: true,
      };
      await sendCommandFn({ deviceId: deviceId, value: rpcCommand });
      createMessage.success('命令下发成功');
      return true;
    } catch (e) {
      return false;
    } finally {
      loading.value = false;
    }
  };
  return {
    loading,
    doCommandDeliver,
  };
}