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

enum EnumMQTT {
  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 === EnumMQTT.IS_PROTOBUF;
};

const { t } = useI18n();
export const MqttSchemas: FormSchema[] = [
  {
    field: 'deviceTelemetryTopic',
    component: 'Input',
    label: t('deviceManagement.product.telemetryDataSubjectFilterText'),
    required: true,
    defaultValue: 'v1/devices/me/telemetry',

    colProps: { span: 23 },
  },
  {
    field: 'deviceAttributesTopic',
    component: 'Input',
    required: true,
    label: t('deviceManagement.product.attributeTopicFilterText'),
    defaultValue: 'v1/devices/me/attributes',
    colProps: { span: 23 },
  },
  {
    field: 'desc',
    component: 'InputTextArea',
    label: '',
    slot: 'descSlot',
  },
  {
    field: 'transportPayloadType',
    component: 'Select',
    label: t('deviceManagement.product.deviceMessagePayloadText', { deviceType: 'MQTT' }),
    defaultValue: 'JSON',
    componentProps: {
      options: [
        { label: 'JSON', value: 'JSON' },
        { label: 'PROTOBUF', value: 'PROTOBUF' },
      ],
    },
    colProps: { span: 11 },
  },
  {
    field: 'useJsonPayloadFormatForDefaultDownlinkTopics',
    label: '',
    colProps: { span: 23 },
    defaultValue: false,
    component: 'Checkbox',
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
    renderComponentContent: t('deviceManagement.product.enablePayloadCompatibilityText'),
  },
  {
    field: 'desc1',
    component: 'InputTextArea',
    label: '',
    slot: 'desc1',
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
  {
    field: 'enableCompatibilityWithJsonPayloadFormat',
    label: '',
    colProps: { span: 23 },
    defaultValue: false,
    component: 'Checkbox',
    renderComponentContent: t('deviceManagement.product.defaultDownlinkTopicUseDefaultTypeText'),
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) &&
      !!values.useJsonPayloadFormatForDefaultDownlinkTopics,
  },
  {
    field: 'desc2',
    component: 'InputTextArea',
    label: '',
    slot: 'desc2',
    ifShow: ({ values }) =>
      isProtobuf(values.transportPayloadType) &&
      !!values.useJsonPayloadFormatForDefaultDownlinkTopics,
  },
  {
    field: 'deviceTelemetryProtoSchema',
    label: t('deviceManagement.product.deviceTelemetryProtoSchemaText'),
    colProps: { span: 23 },
    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),
  },
  {
    field: 'deviceAttributesProtoSchema',
    label: t('deviceManagement.product.deviceAttributesProtoSchemaText'),
    colProps: { span: 23 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package attributes;

    message SensorConfiguration {
      optional string firmwareVersion = 1;
      optional string serialNumber = 2;
    }
    `,
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
  {
    field: 'deviceRpcRequestProtoSchema',
    label: t('deviceManagement.product.deviceRpcRequestProtoSchemaText'),
    colProps: { span: 23 },
    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),
  },
  {
    field: 'deviceRpcResponseProtoSchema',
    label: t('deviceManagement.product.deviceRpcResponseProtoSchemaText'),
    colProps: { span: 23 },
    component: 'InputTextArea',
    componentProps: {
      rows: 11,
    },
    defaultValue: `
    syntax ="proto3";
    package rpc;
    
    message RpcResponseMsg {
      optional string payload = 1;
    }
    `,
    ifShow: ({ values }) => isProtobuf(values.transportPayloadType),
  },
];

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