useCommon.ts 4.05 KB
import useParitalValid from './useParitalValid';
import { findDictItemByCode } from '/@/api/system/dict';
import { buildUUID } from '/@/utils/uuid';

///根据不同数据类型得到不同表单数据(模拟的阿里云数据结构)
type TForm = {
  dataType: string;
  max?: string;
  min?: string;
  step?: string;
  unit?: string;
  boolClose?: string;
  boolOpen?: string;
  length?: string;
  valueRange?: {
    min: string;
    max: string;
  };
};

export default () => {
  const { validateValueRangeAndStep, validateValueBool } = useParitalValid();

  const useChangeTypeGetTypeForm = (type, options: TForm) => {
    switch (type) {
      //INT和DOUBLE收集表单一样
      case 'INT':
        validateValueRangeAndStep(
          Number(options?.valueRange?.min),
          Number(options?.step),
          Number(options?.valueRange?.max)
        );
        return {
          dataType: options?.dataType,
          max: options?.valueRange?.max,
          min: options?.valueRange?.min,
          step: options?.step,
          unit: options?.unit,
        };
      case 'DOUBLE':
        validateValueRangeAndStep(
          Number(options?.valueRange?.min),
          Number(options?.step),
          Number(options?.valueRange?.max)
        );
        return {
          dataType: options?.dataType,
          max: options?.valueRange?.max,
          min: options?.valueRange?.min,
          step: options?.step,
          unit: options?.unit,
        };
      case 'BOOL':
        validateValueBool(Number(options?.boolClose), Number(options?.boolOpen));
        return [
          {
            dataType: options?.dataType,
            name: options?.boolClose,
            value: 0,
          },
          {
            dataType: options?.dataType,
            name: options?.boolOpen,
            value: 1,
          },
        ];
      case 'TEXT':
        return {
          dataType: options?.dataType,
          length: Number(options?.length),
        };
    }
  };

  //是否排除结构体
  const useUpdateFormExcludeStruct = async (F, U) => {
    const res: any = await findDictItemByCode({ dictCode: 'data_type' });
    const optionTrue = res
      .map((m) => {
        if (F) {
          if (m.itemValue !== 'STRUCT') {
            return {
              value: m.itemValue,
              label: m.itemText,
            };
          }
        } else {
          return {
            value: m.itemValue,
            label: m.itemText,
          };
        }
      })
      .filter(Boolean);
    U({
      field: 'dataType',
      componentProps: {
        options: optionTrue,
      },
    });
  };

  //获取输入参数或者输出参数弹窗数据
  const useGetInOrOutData = (d, f, inputData, outputData) => {
    if (f == 'input') {
      if (d.id !== null) {
        const findIndex = inputData.findIndex((f) => f.id == d.id);
        if (findIndex !== -1) inputData.splice(findIndex, 1, d);
      } else {
        inputData.push({ ...d, id: buildUUID() });
      }
    } else {
      if (d.id !== null) {
        const findIndex = outputData.findIndex((f) => f.id == d.id);
        if (findIndex !== -1) outputData.splice(findIndex, 1, d);
      } else {
        outputData.push({ ...d, id: buildUUID() });
      }
    }
  };

  const useBlockContent = `物模型是对设备在云端的功能描述,包括设备的属性、服务和事件。物联网平台通过定义一种物的描述语言来描述物模型,称之为 TSL(即 Thing 
  Specification Language),采用 JSON 格式,您可以根据 TSL 组装上报设备的数据。您可以导出完整物模型,用于云端应用开发;您也可以只导出
  精简物模型,配合设备端 SDK 实现设备开发。`;

  const useBlockPhysicalContent = `属性一般是设备的运行状态,如当前温度等;服务是设备可被调用的方法,支持定义参数,如执行某项任务;事件则是设备上报的
  通知,如告警,需要被及时处理。`;

  return {
    useChangeTypeGetTypeForm,
    useUpdateFormExcludeStruct,
    useGetInOrOutData,
    useBlockContent,
    useBlockPhysicalContent,
  };
};