useCommandDelivery.ts 3.76 KB
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,
  };
}