ModelOfMatter.config.ts 2.2 KB
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import { DataType, Specs } from '/@/api/device/model/modelOfMatterModel';
import { DataTypeEnum } from '/@/enums/objectModelEnum';
import { isArray } from '/@/utils/is';

export interface StructValueItemType extends BaseAdditionalInfo {
  name: string;
  key: string;
  value?: string | number | boolean;
}

export interface BaseAdditionalInfo {
  unit?: string;
  boolClose?: string;
  boolOpen?: string;
  unitName?: string;
}

export interface SocketInfoDataSourceItemType extends BaseAdditionalInfo {
  accessMode: string;
  key: string;
  name: string;
  type: DataTypeEnum;
  detail: DeviceModelOfMatterAttrs;
  time?: number;
  value?: string | number | boolean | Record<string, StructValueItemType>;
  expand?: boolean;
  showHistoryDataButton?: boolean;
  rawValue?: any;
}

export function buildTableDataSourceByObjectModel(
  models: DeviceModelOfMatterAttrs[]
): SocketInfoDataSourceItemType[] {
  function getAdditionalInfoByDataType(dataType?: DataType) {
    const { specs } = dataType || {};
    if (isArray(specs)) return {};
    const { unit, boolClose, boolOpen, unitName } = (specs as Partial<Specs>) || {};
    return { unit, boolClose, boolOpen, unitName };
  }

  return models.map((item) => {
    const { accessMode, identifier, name, detail } = item;
    const { dataType } = detail;
    const { type, specs } = dataType || {};

    const res = {
      accessMode,
      name,
      key: identifier,
      type: type!,
      detail: item,
    };

    if (isArray(specs) && type === DataTypeEnum.STRUCT) {
      const value: Record<string, StructValueItemType> = specs
        .filter((item) => item.dataType?.type !== DataTypeEnum.STRUCT)
        .reduce((prev, next) => {
          const { identifier, functionName, dataType } = next;
          return {
            ...prev,
            [identifier]: {
              key: identifier!,
              name: functionName!,
              type: dataType?.type,
              ...getAdditionalInfoByDataType(dataType),
            },
          };
        }, {});

      Object.assign(res, { value });
    } else {
      Object.assign(res, getAdditionalInfoByDataType(dataType));
    }

    return res;
  });
}