Search in sources :

Example 11 with ProtoTransportPayloadConfiguration

use of org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration in project thingsboard by thingsboard.

the class AbstractMqttServerSideRpcIntegrationTest method processProtoMessageArrived.

protected MqttMessage processProtoMessageArrived(String requestTopic, MqttMessage mqttMessage) throws MqttException, InvalidProtocolBufferException {
    MqttMessage message = new MqttMessage();
    if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC) || requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC_V2)) {
        ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = getProtoTransportPayloadConfiguration();
        ProtoFileElement rpcRequestProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(RPC_REQUEST_PROTO_SCHEMA);
        DynamicSchema rpcRequestProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(rpcRequestProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_REQUEST_PROTO_SCHEMA);
        byte[] requestPayload = mqttMessage.getPayload();
        DynamicMessage.Builder rpcRequestMsg = rpcRequestProtoSchema.newMessageBuilder("RpcRequestMsg");
        Descriptors.Descriptor rpcRequestMsgDescriptor = rpcRequestMsg.getDescriptorForType();
        assertNotNull(rpcRequestMsgDescriptor);
        try {
            DynamicMessage dynamicMessage = DynamicMessage.parseFrom(rpcRequestMsgDescriptor, requestPayload);
            List<Descriptors.FieldDescriptor> fields = rpcRequestMsgDescriptor.getFields();
            for (Descriptors.FieldDescriptor fieldDescriptor : fields) {
                assertTrue(dynamicMessage.hasField(fieldDescriptor));
            }
            ProtoFileElement transportProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_RPC_RESPONSE_PROTO_SCHEMA);
            DynamicSchema rpcResponseProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_RESPONSE_PROTO_SCHEMA);
            DynamicMessage.Builder rpcResponseBuilder = rpcResponseProtoSchema.newMessageBuilder("RpcResponseMsg");
            Descriptors.Descriptor rpcResponseMsgDescriptor = rpcResponseBuilder.getDescriptorForType();
            assertNotNull(rpcResponseMsgDescriptor);
            DynamicMessage rpcResponseMsg = rpcResponseBuilder.setField(rpcResponseMsgDescriptor.findFieldByName("payload"), DEVICE_RESPONSE).build();
            message.setPayload(rpcResponseMsg.toByteArray());
        } catch (InvalidProtocolBufferException e) {
            log.warn("Command Response Ack Error, Invalid response received: ", e);
        }
    } else {
        TransportApiProtos.GatewayDeviceRpcRequestMsg msg = TransportApiProtos.GatewayDeviceRpcRequestMsg.parseFrom(mqttMessage.getPayload());
        String deviceName = msg.getDeviceName();
        int requestId = msg.getRpcRequestMsg().getRequestId();
        TransportApiProtos.GatewayRpcResponseMsg gatewayRpcResponseMsg = TransportApiProtos.GatewayRpcResponseMsg.newBuilder().setDeviceName(deviceName).setId(requestId).setData("{\"success\": true}").build();
        message.setPayload(gatewayRpcResponseMsg.toByteArray());
    }
    return message;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) TransportApiProtos(org.thingsboard.server.gen.transport.TransportApiProtos) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) DynamicMessage(com.google.protobuf.DynamicMessage) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) Descriptors(com.google.protobuf.Descriptors)

Example 12 with ProtoTransportPayloadConfiguration

use of org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration in project thingsboard by thingsboard.

the class AbstractMqttIntegrationTest method createMqttDeviceProfile.

protected DeviceProfile createMqttDeviceProfile(TransportPayloadType transportPayloadType, String telemetryTopic, String attributesTopic, String telemetryProtoSchema, String attributesProtoSchema, String rpcResponseProtoSchema, String rpcRequestProtoSchema, String provisionKey, String provisionSecret, DeviceProfileProvisionType provisionType, boolean enableCompatibilityWithJsonPayloadFormat, boolean useJsonPayloadFormatForDefaultDownlinkTopics) {
    DeviceProfile deviceProfile = new DeviceProfile();
    deviceProfile.setName(transportPayloadType.name());
    deviceProfile.setType(DeviceProfileType.DEFAULT);
    deviceProfile.setTransportType(DeviceTransportType.MQTT);
    deviceProfile.setProvisionType(provisionType);
    deviceProfile.setProvisionDeviceKey(provisionKey);
    deviceProfile.setDescription(transportPayloadType.name() + " Test");
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
    MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = new MqttDeviceProfileTransportConfiguration();
    if (!StringUtils.isEmpty(telemetryTopic)) {
        mqttDeviceProfileTransportConfiguration.setDeviceTelemetryTopic(telemetryTopic);
    }
    if (!StringUtils.isEmpty(attributesTopic)) {
        mqttDeviceProfileTransportConfiguration.setDeviceAttributesTopic(attributesTopic);
    }
    TransportPayloadTypeConfiguration transportPayloadTypeConfiguration;
    if (TransportPayloadType.JSON.equals(transportPayloadType)) {
        transportPayloadTypeConfiguration = new JsonTransportPayloadConfiguration();
    } else {
        ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = new ProtoTransportPayloadConfiguration();
        if (StringUtils.isEmpty(telemetryProtoSchema)) {
            telemetryProtoSchema = DEVICE_TELEMETRY_PROTO_SCHEMA;
        }
        if (StringUtils.isEmpty(attributesProtoSchema)) {
            attributesProtoSchema = DEVICE_ATTRIBUTES_PROTO_SCHEMA;
        }
        if (StringUtils.isEmpty(rpcResponseProtoSchema)) {
            rpcResponseProtoSchema = DEVICE_RPC_RESPONSE_PROTO_SCHEMA;
        }
        if (StringUtils.isEmpty(rpcRequestProtoSchema)) {
            rpcRequestProtoSchema = DEVICE_RPC_REQUEST_PROTO_SCHEMA;
        }
        protoTransportPayloadConfiguration.setDeviceTelemetryProtoSchema(telemetryProtoSchema);
        protoTransportPayloadConfiguration.setDeviceAttributesProtoSchema(attributesProtoSchema);
        protoTransportPayloadConfiguration.setDeviceRpcResponseProtoSchema(rpcResponseProtoSchema);
        protoTransportPayloadConfiguration.setDeviceRpcRequestProtoSchema(rpcRequestProtoSchema);
        protoTransportPayloadConfiguration.setEnableCompatibilityWithJsonPayloadFormat(enableCompatibilityWithJsonPayloadFormat);
        protoTransportPayloadConfiguration.setUseJsonPayloadFormatForDefaultDownlinkTopics(enableCompatibilityWithJsonPayloadFormat && useJsonPayloadFormatForDefaultDownlinkTopics);
        transportPayloadTypeConfiguration = protoTransportPayloadConfiguration;
    }
    mqttDeviceProfileTransportConfiguration.setTransportPayloadTypeConfiguration(transportPayloadTypeConfiguration);
    deviceProfileData.setTransportConfiguration(mqttDeviceProfileTransportConfiguration);
    DeviceProfileProvisionConfiguration provisionConfiguration;
    switch(provisionType) {
        case ALLOW_CREATE_NEW_DEVICES:
            provisionConfiguration = new AllowCreateNewDevicesDeviceProfileProvisionConfiguration(provisionSecret);
            break;
        case CHECK_PRE_PROVISIONED_DEVICES:
            provisionConfiguration = new CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration(provisionSecret);
            break;
        case DISABLED:
        default:
            provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(provisionSecret);
            break;
    }
    deviceProfileData.setProvisionConfiguration(provisionConfiguration);
    deviceProfileData.setConfiguration(configuration);
    deviceProfile.setProfileData(deviceProfileData);
    deviceProfile.setDefault(false);
    deviceProfile.setDefaultRuleChainId(null);
    return deviceProfile;
}
Also used : DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration) AllowCreateNewDevicesDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration) DeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileProvisionConfiguration) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration) AllowCreateNewDevicesDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration) CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) JsonTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.JsonTransportPayloadConfiguration) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration)

Example 13 with ProtoTransportPayloadConfiguration

use of org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration in project thingsboard by thingsboard.

the class DeviceSessionCtx method updateDeviceSessionConfiguration.

private void updateDeviceSessionConfiguration(DeviceProfile deviceProfile) {
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    if (transportConfiguration.getType().equals(DeviceTransportType.MQTT) && transportConfiguration instanceof MqttDeviceProfileTransportConfiguration) {
        MqttDeviceProfileTransportConfiguration mqttConfig = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
        TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttConfig.getTransportPayloadTypeConfiguration();
        payloadType = transportPayloadTypeConfiguration.getTransportPayloadType();
        telemetryTopicFilter = MqttTopicFilterFactory.toFilter(mqttConfig.getDeviceTelemetryTopic());
        attributesTopicFilter = MqttTopicFilterFactory.toFilter(mqttConfig.getDeviceAttributesTopic());
        if (TransportPayloadType.PROTOBUF.equals(payloadType)) {
            ProtoTransportPayloadConfiguration protoTransportPayloadConfig = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
            updateDynamicMessageDescriptors(protoTransportPayloadConfig);
            jsonPayloadFormatCompatibilityEnabled = protoTransportPayloadConfig.isEnableCompatibilityWithJsonPayloadFormat();
            useJsonPayloadFormatForDefaultDownlinkTopics = jsonPayloadFormatCompatibilityEnabled && protoTransportPayloadConfig.isUseJsonPayloadFormatForDefaultDownlinkTopics();
        }
    } else {
        telemetryTopicFilter = MqttTopicFilterFactory.getDefaultTelemetryFilter();
        attributesTopicFilter = MqttTopicFilterFactory.getDefaultAttributesFilter();
        payloadType = TransportPayloadType.JSON;
    }
    updateAdaptor();
}
Also used : MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration)

Example 14 with ProtoTransportPayloadConfiguration

use of org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration in project thingsboard by thingsboard.

the class AbstractWebTest method createProtoTransportPayloadConfiguration.

protected ProtoTransportPayloadConfiguration createProtoTransportPayloadConfiguration(String attributesProtoSchema, String telemetryProtoSchema, String rpcRequestProtoSchema, String rpcResponseProtoSchema) {
    ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = new ProtoTransportPayloadConfiguration();
    protoTransportPayloadConfiguration.setDeviceAttributesProtoSchema(attributesProtoSchema);
    protoTransportPayloadConfiguration.setDeviceTelemetryProtoSchema(telemetryProtoSchema);
    protoTransportPayloadConfiguration.setDeviceRpcRequestProtoSchema(rpcRequestProtoSchema);
    protoTransportPayloadConfiguration.setDeviceRpcResponseProtoSchema(rpcResponseProtoSchema);
    return protoTransportPayloadConfiguration;
}
Also used : ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration)

Example 15 with ProtoTransportPayloadConfiguration

use of org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration in project thingsboard by thingsboard.

the class AbstractMqttTimeseriesProtoIntegrationTest method getDynamicSchema.

private DynamicSchema getDynamicSchema(String deviceTelemetryProtoSchema) {
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration);
    MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
    TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration();
    assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration);
    ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
    ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(deviceTelemetryProtoSchema);
    return protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema");
}
Also used : MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement)

Aggregations

ProtoTransportPayloadConfiguration (org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration)23 TransportPayloadTypeConfiguration (org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration)17 DeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration)15 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)13 MqttDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration)11 DynamicSchema (com.github.os72.protobuf.dynamic.DynamicSchema)10 Descriptors (com.google.protobuf.Descriptors)10 DynamicMessage (com.google.protobuf.DynamicMessage)10 CoapDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration)10 CoapDeviceTypeConfiguration (org.thingsboard.server.common.data.device.profile.CoapDeviceTypeConfiguration)10 DefaultCoapDeviceTypeConfiguration (org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration)10 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)7 Test (org.junit.Test)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 CoapResponse (org.eclipse.californium.core.CoapResponse)2 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 AllowCreateNewDevicesDeviceProfileProvisionConfiguration (org.thingsboard.server.common.data.device.profile.AllowCreateNewDevicesDeviceProfileProvisionConfiguration)2 CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration (org.thingsboard.server.common.data.device.profile.CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration)2 DefaultDeviceProfileConfiguration (org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration)2 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)2