Coap.ts 7.03 KB
import { FormSchema } from '/@/components/Form';
import { useI18n } from '/@/hooks/web/useI18n';

enum EnumType {
  IS_DEFAULT = 'DEFAULT',
  IS_MQTT = 'MQTT',
  IS_PROTOBUF = 'PROTOBUF',
  IS_COAP = 'COAP',
  IS_LWM2M = 'LWM2M',
  IS_SNMP = 'SNMP',
  IS_PSM = 'PSM',
  IS_DRX = 'E_DRX',
  IS_EFENTO_NBIOT = 'EFENTO',
}

const isProtobuf = (type: string) => {
  return type === EnumType.IS_PROTOBUF;
};

const isPsm = (type: string) => {
  return type === EnumType.IS_PSM;
};

const isDrx = (type: string) => {
  return type === EnumType.IS_DRX;
};

const isEfentoNb = (type: string) => {
  return type === EnumType.IS_EFENTO_NBIOT;
};

const { t } = useI18n();

const timeUnitOptions = [
  { label: t('deviceManagement.product.unitMsText'), value: 'Milliseconds' },
  { label: t('deviceManagement.product.unitSText'), value: 'second' },
  { label: t('deviceManagement.product.unitMText'), value: 'minute' },
  { label: t('deviceManagement.product.unitHText'), value: 'hour' },
];

export const CoapSchemas: FormSchema[] = [
  {
    field: 'coapDeviceType',
    component: 'Select',
    label: t('deviceManagement.product.coapDeviceTypeText'),
    defaultValue: 'DEFAULT',
    componentProps: {
      options: [
        { label: t('deviceManagement.product.defaultText'), value: 'DEFAULT' },
        { label: 'Efento NB-IoT', value: 'EFENTO' },
      ],
    },
    colProps: { span: 22 },
  },
  {
    field: 'transportPayloadType',
    component: 'Select',
    label: t('deviceManagement.product.coapDevicePayloadText'),
    defaultValue: 'JSON',
    componentProps: {
      options: [
        { label: 'JSON', value: 'JSON' },
        { label: 'PROTOBUF', value: 'PROTOBUF' },
      ],
    },
    colProps: { span: 22 },
    ifShow: ({ values }) => !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'powerMode',
    component: 'Select',
    label: t('deviceManagement.product.energySavingModeText'),
    defaultValue: 'DRX',
    componentProps: {
      options: [
        { label: 'Power Saving Mode (PSM)', value: 'PSM' },
        { label: 'Discontinuous Reception (DRX)', value: 'DRX' },
        {
          label: 'Extended Discontinuous Reception (eDRX)',
          value: 'E_DRX',
        },
      ],
    },
    colProps: { span: 22 },
  },
  {
    field: 'psmActivityTimer',
    component: 'InputNumber',
    label: t('deviceManagement.product.psmActivityTimerText'),
    required: true,
    defaultValue: 10,
    colProps: { span: 11 },
    ifShow: ({ values }) => isPsm(values.powerMode),
  },
  {
    field: 'unit1',
    component: 'Select',
    label: t('deviceManagement.product.timerUnitText'),
    defaultValue: 'second',
    componentProps: {
      options: timeUnitOptions,
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isPsm(values.powerMode),
  },
  {
    field: 'edrxCycle',
    component: 'InputNumber',
    label: t('deviceManagement.product.edrxCycleText'),
    required: true,
    defaultValue: 81,
    componentProps: {},
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },
  {
    field: 'unit2',
    component: 'Select',
    label: t('deviceManagement.product.timerUnitText'),
    defaultValue: 'second',
    componentProps: {
      options: timeUnitOptions,
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },
  {
    field: 'pagingTransmissionWindow',
    component: 'InputNumber',
    label: t('deviceManagement.product.pagingTransmissionWindowText'),
    required: true,
    defaultValue: 10,
    componentProps: {},
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },
  {
    field: 'unit',
    component: 'Select',
    label: t('deviceManagement.product.timerUnitText'),
    defaultValue: 'second',
    componentProps: {
      options: timeUnitOptions,
    },
    colProps: { span: 11 },
    ifShow: ({ values }) => isDrx(values.powerMode),
  },
  {
    field: 'deviceTelemetryProtoSchema',
    label: t('deviceManagement.product.deviceTelemetryProtoSchemaText'),
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 17,
    },
    defaultValue: `
    syntax ="proto3";
    package telemetry;

    message SensorDataReading {
      optional double temperature = 1;
      optional double humidity = 2;
      InnerObject innerObject = 3;
      message InnerObject {
        optional string key1 = 1;
        optional bool key2 = 2;
        optional double key3 = 3;
        optional int32 key4 = 4;
        optional string key5 = 5;
      }
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceAttributesProtoSchema',
    label: t('deviceManagement.product.deviceAttributesProtoSchemaText'),
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 9,
    },
    defaultValue: `
    syntax ="proto3";
    package attributes;

    message SensorConfiguration {
      optional string firmwareVersion = 1;
      optional string serialNumber = 2;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceRpcRequestProtoSchema',
    label: t('deviceManagement.product.deviceRpcRequestProtoSchemaText'),
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcRequestMsg {
      optional string method = 1;
      optional int32 requestId = 2;
      optional string params = 3;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
  {
    field: 'deviceRpcResponseProtoSchema',
    label: t('deviceManagement.product.deviceRpcResponseProtoSchemaText'),
    colProps: { span: 22 },
    component: 'InputTextArea',
    componentProps: {
      rows: 9,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcResponseMsg {
      optional string payload = 1;
    }
    `,
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) && !isEfentoNb(values.coapDeviceType),
  },
];

export const deviceTelemetryProtoSchemaData = `
syntax ="proto3";
package telemetry;

message SensorDataReading {
  optional double temperature = 1;
  optional double humidity = 2;
  InnerObject innerObject = 3;
  message InnerObject {
    optional string key1 = 1;
    optional bool key2 = 2;
    optional double key3 = 3;
    optional int32 key4 = 4;
    optional string key5 = 5;
  }
}
`;

export const deviceAttributesProtoSchemaData = `
syntax ="proto3";
package attributes;

message SensorConfiguration {
  optional string firmwareVersion = 1;
  optional string serialNumber = 2;
}
`;

export const deviceRpcRequestProtoSchemaData = `
syntax ="proto3";
package rpc;

message RpcRequestMsg {
  optional string method = 1;
  optional int32 requestId = 2;
  optional string params = 3;
}
`;

export const deviceRpcResponseProtoSchemaData = `
syntax ="proto3";
package rpc;

message RpcResponseMsg {
  optional string payload = 1;
}
`;