create.config.ts 5.1 KB
import { DirectionEnum, RelationTypeEnum, RelationTypeNameEnum } from '../../../enum/form';
import { useRuleChainI18n } from '../../../hook/useRuleChainI18n';

export enum RelatedDeviceAttributeFieldsEnum {
  DEVICE_RELATIONS_QUERY = 'deviceRelationsQuery',
  TELL_FAILURE_IF_ABSENT = 'tellFailureIfAbsent',
  CLIENT_ATTRIBUTE_NAMES = 'clientAttributeNames',
  SHARED_ATTRIBUTE_NAMES = 'sharedAttributeNames',
  SERVER_ATTRIBUTE_NAMES = 'serverAttributeNames',
  LATEST_TS_KEY_NAMES = 'latestTsKeyNames',
  GET_LATEST_VALUE_WITH_TS = 'getLatestValueWithTs',
  FETCH_LAST_LEVEL_ONLY = 'fetchLastLevelOnly',

  // DEVICE_RELATIONS_QUERY
  DIRECTION = 'direction',
  MAX_LEVEL = 'maxLevel',
  RELATION_TYPE = 'relationType',
  DEVICE_TYPES = 'deviceTypes',
}

import { getDeviceTypes } from '/@/api/ruleChainDesigner';
import { FormSchema } from '/@/components/Form';

const { tLabel, tPlaceholder, t } = useRuleChainI18n('enrichment', 'relatedDeviceAttributes');

export interface ValueType {
  deviceRelationsQuery: DeviceRelationsQuery;
  tellFailureIfAbsent: boolean;
  clientAttributeNames: string[];
  sharedAttributeNames: string[];
  serverAttributeNames: string[];
  latestTsKeyNames: string[];
  getLatestValueWithTs: boolean;
}

export interface DeviceRelationsQuery {
  fetchLastLevelOnly: boolean;
  direction: string;
  maxLevel: number;
  relationType: string;
  deviceTypes: string[];
}

export const formSchemas: FormSchema[] = [
  {
    field: RelatedDeviceAttributeFieldsEnum.FETCH_LAST_LEVEL_ONLY,
    component: 'Checkbox',
    label: '',
    renderComponentContent: () => ({
      default: () => tLabel(RelatedDeviceAttributeFieldsEnum.FETCH_LAST_LEVEL_ONLY),
    }),
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.DIRECTION,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.DIRECTION),
    colProps: { span: 12 },
    required: true,
    componentProps: {
      options: Object.keys(DirectionEnum).map((value) => ({
        label: t(`enum.direction.${value}`),
        value,
      })),
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.DIRECTION, 'choose'),
      getPopupContainer: () => document.body,
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.MAX_LEVEL,
    component: 'InputNumber',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.MAX_LEVEL),
    colProps: { span: 12 },
    componentProps: {
      min: 1,
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.MAX_LEVEL),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.RELATION_TYPE,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.RELATION_TYPE),
    componentProps: {
      options: Object.keys(RelationTypeEnum).map((value) => ({
        label: RelationTypeNameEnum[value],
        value,
      })),
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.RELATION_TYPE, 'choose'),
      getPopupContainer: () => document.body,
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.DEVICE_TYPES,
    component: 'ApiSelect',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.DEVICE_TYPES),
    componentProps: {
      mode: 'multiple',
      api: getDeviceTypes,
      labelField: 'type',
      valueField: 'type',
      getPopupContainer: () => document.body,
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.DEVICE_TYPES, 'choose'),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.TELL_FAILURE_IF_ABSENT,
    component: 'Checkbox',
    label: '',
    renderComponentContent: () => ({
      default: () => tLabel(RelatedDeviceAttributeFieldsEnum.TELL_FAILURE_IF_ABSENT),
    }),
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.CLIENT_ATTRIBUTE_NAMES,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.CLIENT_ATTRIBUTE_NAMES),
    componentProps: {
      open: false,
      mode: 'tags',
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.CLIENT_ATTRIBUTE_NAMES),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.SHARED_ATTRIBUTE_NAMES,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.SHARED_ATTRIBUTE_NAMES),
    componentProps: {
      open: false,
      mode: 'tags',
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.SHARED_ATTRIBUTE_NAMES),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.SERVER_ATTRIBUTE_NAMES,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.SERVER_ATTRIBUTE_NAMES),
    componentProps: {
      open: false,
      mode: 'tags',
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.SERVER_ATTRIBUTE_NAMES),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.LATEST_TS_KEY_NAMES,
    component: 'Select',
    label: tLabel(RelatedDeviceAttributeFieldsEnum.LATEST_TS_KEY_NAMES),
    componentProps: {
      open: false,
      mode: 'tags',
      placeholder: tPlaceholder(RelatedDeviceAttributeFieldsEnum.LATEST_TS_KEY_NAMES),
    },
  },
  {
    field: RelatedDeviceAttributeFieldsEnum.GET_LATEST_VALUE_WITH_TS,
    component: 'Checkbox',
    label: '',
    renderComponentContent: () => ({
      default: () => tLabel(RelatedDeviceAttributeFieldsEnum.GET_LATEST_VALUE_WITH_TS),
    }),
  },
];