config.ts 1.94 KB
import { ref, unref } from 'vue';
import { TransportTypeEnum } from '/@/views/device/profiles/components/TransportDescript/const';
import { CommandTypeEnum } from '/@/views/rule/linkedge/config/config.data';
import { TaskTypeEnum } from '/@/views/task/center/config';
import { genModbusCommand } from '/@/api/task';

export const getSendValues = async (option, getDesign, checked) => {
  const values: any = option;
  const isModbusCommand = ref<boolean>(false); //判断是否是TCP设备-> 且设备标识符是modeBUs,切选择是下发命令类型是属性
  const { extensionDesc, commandType, codeType, deviceCode, customCommand } = getDesign || {};
  const { registerAddress, actionType } = extensionDesc || {};
  const { openCommand, closeCommand, transportType } = customCommand || {};
  const modBUS = ref<any>({});
  const sendValue = ref();
  //判断是不是TCP类型设备
  if (transportType === TransportTypeEnum.TCP) {
    if (
      //判断TCP下发类型是否是自定义还是服务
      commandType === CommandTypeEnum.CUSTOM.toString() ||
      commandType == CommandTypeEnum.SERVICE.toString()
    ) {
      values.customCommand.command = checked ? openCommand : closeCommand;
    }
    if (
      //判断命令下发类型是不是属性
      commandType === CommandTypeEnum.ATTRIBUTE.toString() &&
      codeType === TaskTypeEnum.MODBUS_RTU
    ) {
      isModbusCommand.value = true;
      modBUS.value = {
        crc: 'CRC_16_LOWER',
        deviceCode: deviceCode,
        method: actionType == '16' ? '10' : actionType,
        registerAddress,
        registerNumber: 1,
        registerValues: [Number(checked)],
      };
      sendValue.value = await genModbusCommand(unref(modBUS));
    }
  }

  return { values, sendValue: unref(sendValue), isModbusCommand: unref(isModbusCommand) };
};

export const CommandTypeEnumLIst = {
  '0': { CUSTOM: 0, name: '自定义' },
  '1': { CUSTOM: 0, name: '服务' },
  '2': { CUSTOM: 0, name: '属性' },
};