config.ts 3.89 KB
import { StructJSON } from '/@/api/device/model/modelOfMatterModel';
import { FormSchema } from '/@/components/Form';
import { validateTCPCustomCommand } from '/@/components/Form/src/components/ThingsModelForm';
import { DataTypeEnum } from '/@/enums/objectModelEnum';

const InsertString = (t, c, n) => {
  const r: string | number[] = [];

  for (let i = 0; i * 2 < t.length; i++) {
    r.push(t.substr(i * 2, n));
  }
  return r.join(c);
};
const FillString = (t, c, n, b) => {
  if (t == '' || c.length != 1 || n <= t.length) {
    return t;
  }
  const l = t.length;

  for (let i = 0; i < n - l; i++) {
    if (b == true) {
      t = c + t;
    } else {
      t += c;
    }
  }
  return t;
};
const SingleToHex = (t) => {
  if (t == '') {
    return '';
  }
  t = parseFloat(t);

  if (isNaN(t) == true) {
    return 'Error';
  }
  if (t == 0) {
    return '00000000';
  }
  let s, e, m;

  if (t > 0) {
    s = 0;
  } else {
    s = 1;

    t = 0 - t;
  }
  m = t.toString(2);

  if (m >= 1) {
    if (m.indexOf('.') == -1) {
      m = m + '.0';
    }
    e = m.indexOf('.') - 1;
  } else {
    e = 1 - m.indexOf('1');
  }
  if (e >= 0) {
    m = m.replace('.', '');
  } else {
    m = m.substring(m.indexOf('1'));
  }
  if (m.length > 24) {
    m = m.substr(0, 24);
  } else {
    m = FillString(m, '0', 24, false);
  }
  m = m.substring(1);

  e = (e + 127).toString(2);

  e = FillString(e, '0', 8, true);

  let r = parseInt(s + e + m, 2).toString(16);

  r = FillString(r, '0', 8, true);

  return InsertString(r, ' ', 2).toUpperCase();
};

const FormatHex = (t, n, ie) => {
  const r: string[] = [];

  let s = '';

  let c = 0;

  for (let i = 0; i < t.length; i++) {
    if (t.charAt(i) != ' ') {
      s += t.charAt(i);

      c += 1;

      if (c == n) {
        r.push(s);

        s = '';

        c = 0;
      }
    }
    if (ie == false) {
      if (i == t.length - 1 && s != '') {
        r.push(s);
      }
    }
  }
  return r.join('\n');
};
const FormatHexBatch = (t, n, ie) => {
  const a = t.split('\n');

  const r: string[] = [];

  for (let i = 0; i < a.length; i++) {
    r[i] = FormatHex(a[i], n, ie);
  }
  return r.join('\n');
};
const SingleToHexBatch = (t) => {
  const a = t.split('\n');

  const r: string[] = [];

  for (let i = 0; i < a.length; i++) {
    r[i] = SingleToHex(a[i]);
  }
  return r.join('\r\n');
};

const formSchemasConfig = (schemas: StructJSON, actionType: string): FormSchema[] => {
  const { identifier, functionName, dataType } = schemas;

  if (dataType?.type === DataTypeEnum.STRING) {
    return [
      {
        field: identifier,
        label: functionName!,
        component: 'Input',
        rules: [{ required: true, validator: validateTCPCustomCommand }],
        componentProps: {
          placeholder: `请输入${functionName}`,
        },
      },
    ];
  }

  if (actionType == '06') {
    return [
      {
        field: identifier,
        label: functionName!,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入正数' }],
        componentProps: {
          min: 0,
          precision: 2,
          placeholder: `请输入正数`,
        },
      },
    ];
  } else if (actionType == '05') {
    return [
      {
        field: identifier,
        label: functionName!,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入值' }],
        componentProps: {
          min: 0,
          max: 1,
          precision: 0,
          placeholder: `请输入0或1`,
        },
      },
    ];
  } else {
    return [
      {
        field: identifier,
        label: functionName!,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入值' }],
        componentProps: {
          placeholder: `请输入数字`,
          precision: 2,
        },
      },
    ];
  }
};

export {
  InsertString,
  FillString,
  SingleToHex,
  FormatHex,
  FormatHexBatch,
  SingleToHexBatch,
  formSchemasConfig,
};