ModelOfMatter.config.ts 3.08 KB
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import { EdgeDeviceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { DataType, Specs } from '/@/api/device/model/modelOfMatterModel';
import { TCPProtocolTypeEnum, TransportTypeEnum } from '/@/enums/deviceEnum';
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;
  enum?: Record<string, string>;
}

export function buildTableDataSourceByObjectModel(
  models: DeviceModelOfMatterAttrs[],
  deviceDetail: EdgeDeviceItemType
): SocketInfoDataSourceItemType[] {
  const isTCPTransportType =
    deviceDetail.deviceData.transportConfiguration.type === TransportTypeEnum.TCP;

  const isModbusDevice =
    isTCPTransportType &&
    deviceDetail?.deviceProfile?.profileData?.transportConfiguration?.protocol ===
      TCPProtocolTypeEnum.MODBUS_RTU;

  function getAdditionalInfoByDataType(dataType?: DataType) {
    const { specs, specsList, type } = dataType || {};
    if (isArray(specs)) return {};
    const { unit, boolClose, boolOpen, unitName } = (specs as Partial<Specs>) || {};
    const result = { unit, boolClose, boolOpen, unitName };
    if ((type == DataTypeEnum.ENUM && specsList && specsList.length) || isModbusDevice) {
      Reflect.set(
        result,
        'enum',
        (specsList || []).reduce((prev, next) => ({ ...prev, [next.value!]: next.name }), {})
      );
    }

    if (isModbusDevice) {
      result.boolClose = '关';
      result.boolOpen = '开';
    }

    return result;
  }

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