config.ts 3.02 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';
import { useMessage } from '/@/hooks/web/useMessage';
import { SingleToHex } from '/@/views/device/list/cpns/tabs/ObjectModelCommandDeliveryModal/config';

const getArray = (values) => {
  const str = values.replace(/\s+/g, '');
  const array: any = [];

  for (let i = 0; i < str.length; i += 4) {
    const chunk = parseInt(str.substring(i, i + 4), 16);
    array.push(chunk);
  }
  return array;
};

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();

  const { createMessage } = useMessage();
  //判断是不是TCP类型设备
  if (transportType === TransportTypeEnum.TCP) {
    if (
      //判断TCP下发类型是否是自定义还是服务
      commandType === CommandTypeEnum.CUSTOM.toString() ||
      commandType == CommandTypeEnum.SERVICE.toString()
    ) {
      values.customCommand.command = checked ? openCommand : closeCommand;
      values.customCommand.command = values.customCommand.command
        .replaceAll(/\s/g, '')
        .toUpperCase();
    }
    if (
      //判断命令下发类型是不是属性 且是modbus
      commandType === CommandTypeEnum.ATTRIBUTE.toString() &&
      codeType === TaskTypeEnum.MODBUS_RTU
    ) {
      if (!deviceCode) {
        createMessage.warning('当前设备没有设置地址码');
        return;
      }
      if (!actionType) {
        createMessage.warning('当前物模型扩展描述没有填写');
        return;
      }
      isModbusCommand.value = true;
      modBUS.value = {
        crc: 'CRC_16_LOWER',
        deviceCode: deviceCode,
        method: actionType == '16' ? '10' : actionType,
        registerAddress,
        registerNumber: 1,
        registerValues: [Number(checked)],
      };

      if (actionType == '16' || actionType == '10') {
        const newValue = Number(checked) == 0 ? [0, 0] : getArray(SingleToHex(Number(checked)));
        modBUS.value.registerValues = newValue;
        modBUS.value.registerNumber = 2;
        modBUS.value.method = '10';
      }

      sendValue.value = await genModbusCommand(unref(modBUS));
    }
  }

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

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