config.ts 4.63 KB
import { validateTCPCustomCommand } from '.'
import type { Specs, StructJSON } from '@/api/device/model'
import { type FormSchema } from '@/components/Form'
import { ComponentEnum } from '@/components/Form/src/enum'
import { TransportTypeEnum } from '@/enums/deviceEnum'
import { DataTypeEnum } from '@/enums/objectModelEnum'

export enum FormFieldsEnum {
  SERVICE_COMMAND = 'serviceCommand',
}

export const getFormSchemas = ({ structJSON: structJson, required, transportType }: { structJSON: StructJSON[]; required?: boolean; transportType?: string }): FormSchema[] => {
  const createInputNumber = ({
    identifier,
    functionName,
    dataType,
  }: StructJSON): FormSchema => {
    const { specs, type } = dataType || {}
    const { valueRange } = specs! as Specs
    const { max = Number.MAX_SAFE_INTEGER, min = Number.MIN_SAFE_INTEGER } = valueRange || {}
    return {
      field: identifier,
      label: functionName,
      component: ComponentEnum.INPUT_NUMBER,
      rules: [
        {
          required,
          message: `${functionName}是必填项`,
        },
      ],
      componentProps: {
        max,
        min,
        precision: type === DataTypeEnum.NUMBER_INT ? 0 : 2,
      },
    } as FormSchema
  }

  const createInput = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
    const { specs } = dataType || {}
    const { length = 10240 } = specs! as Partial<Specs>
    return {
      field: identifier,
      label: functionName,
      component: ComponentEnum.INPUT,
      rules: [
        {
          required,
          message: `${functionName}是必填项`,
        },
        {
          type: 'string',
          trigger: 'change',
          validator: (_rule, value) => {
            if ((value?.length || 0) > length)
              return Promise.reject(new Error(`${functionName}数据长度应该小于${length}`))

            return Promise.resolve(value)
          },
        },
      ],
      componentProps: {
        maxLength: length,
      },
    } as FormSchema
  }

  const createSelect = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
    const { specs } = dataType || {}
    const { boolClose, boolOpen } = specs! as Partial<Specs>
    return {
      field: identifier,
      label: functionName,
      component: ComponentEnum.SELECT,
      rules: [
        {
          required,
          message: `${functionName}是必填项`,
          type: 'number',
        },
      ],
      componentProps: {
        options: [
          { label: `${boolClose}-0`, value: 0 },
          { label: `${boolOpen}-1`, value: 1 },
        ],
      },
    }
  }

  const createEnumSelect = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
    const { specsList } = dataType || {}

    return {
      field: identifier,
      label: functionName,
      component: ComponentEnum.SELECT,
      rules: [
        {
          required,
          message: `${functionName}是必填项`,
          type: 'number',
        },
      ],
      componentProps: {
        options: specsList,
        fieldNames: { label: 'name', value: 'value' },
      },
    }
  }

  const createStructJson = ({ identifier, functionName, dataType }: StructJSON): FormSchema => {
    return {
      field: identifier,
      label: functionName,
      component: ComponentEnum.THINGS_MODEL_FORM,
      changeEvent: 'update:value',
      componentProps: () => {
        return {
          inputData: dataType?.specs || [],
        }
      },
      colSlot: identifier,
    }
  }

  const createTCPServiceCommandInput = ({ serviceCommand }: StructJSON): FormSchema => {
    return {
      field: FormFieldsEnum.SERVICE_COMMAND,
      label: '服务命令',
      component: ComponentEnum.INPUT,
      required,
      rules: [{ validator: validateTCPCustomCommand }],
      componentProps: {
        defaultValue: serviceCommand,
      },
    }
  }

  const schemas: FormSchema[] = []
  for (const item of structJson) {
    const { dataType } = item
    const { type } = dataType || {}

    if (transportType === TransportTypeEnum.TCP) {
      item.serviceCommand && schemas.push(createTCPServiceCommandInput(item))
      break
    }

    if (type === DataTypeEnum.BOOL)
      schemas.push(createSelect(item))
    else if (type === DataTypeEnum.ENUM)
      schemas.push(createEnumSelect(item))
    else if (type === DataTypeEnum.NUMBER_INT)
      schemas.push(createInputNumber(item))
    else if (type === DataTypeEnum.NUMBER_DOUBLE)
      schemas.push(createInputNumber(item))
    else if (type === DataTypeEnum.STRING)
      schemas.push(createInput(item))
    else if (type === DataTypeEnum.STRUCT)
      schemas.push(createStructJson(item))
  }
  return schemas
}