Search in sources :

Example 21 with ProtoTransportPayloadConfiguration

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

the class AbstractCoapTimeseriesProtoIntegrationTest method testPushTelemetryWithTsAndNoPresenceFields.

@Test
public void testPushTelemetryWithTsAndNoPresenceFields() throws Exception {
    String schemaStr = "syntax =\"proto3\";\n" + "\n" + "package test;\n" + "\n" + "message PostTelemetry {\n" + "  optional int64 ts = 1;\n" + "  Values values = 2;\n" + "  \n" + "  message Values {\n" + "    string key1 = 3;\n" + "    bool key2 = 4;\n" + "    double key3 = 5;\n" + "    int32 key4 = 6;\n" + "    JsonObject key5 = 7;\n" + "  }\n" + "  \n" + "  message JsonObject {\n" + "    optional int32 someNumber = 8;\n" + "    repeated int32 someArray = 9;\n" + "    NestedJsonObject someNestedObject = 10;\n" + "    message NestedJsonObject {\n" + "       optional string key = 11;\n" + "    }\n" + "  }\n" + "}";
    processBeforeTest("Test Post Telemetry device proto payload", CoapDeviceType.DEFAULT, TransportPayloadType.PROTOBUF, schemaStr, null, null, null, null, null, DeviceProfileProvisionType.DISABLED);
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    assertTrue(transportConfiguration instanceof CoapDeviceProfileTransportConfiguration);
    CoapDeviceProfileTransportConfiguration coapDeviceProfileTransportConfiguration = (CoapDeviceProfileTransportConfiguration) transportConfiguration;
    CoapDeviceTypeConfiguration coapDeviceTypeConfiguration = coapDeviceProfileTransportConfiguration.getCoapDeviceTypeConfiguration();
    assertTrue(coapDeviceTypeConfiguration instanceof DefaultCoapDeviceTypeConfiguration);
    DefaultCoapDeviceTypeConfiguration defaultCoapDeviceTypeConfiguration = (DefaultCoapDeviceTypeConfiguration) coapDeviceTypeConfiguration;
    TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = defaultCoapDeviceTypeConfiguration.getTransportPayloadTypeConfiguration();
    assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration);
    ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
    ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(schemaStr);
    DynamicSchema telemetrySchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema");
    DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject");
    Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType();
    assertNotNull(nestedJsonObjectBuilderDescriptor);
    DynamicMessage nestedJsonObject = nestedJsonObjectBuilder.setField(nestedJsonObjectBuilderDescriptor.findFieldByName("key"), "value").build();
    DynamicMessage.Builder jsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject");
    Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType();
    assertNotNull(jsonObjectBuilderDescriptor);
    DynamicMessage jsonObject = jsonObjectBuilder.addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1).addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2).addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3).setField(jsonObjectBuilderDescriptor.findFieldByName("someNestedObject"), nestedJsonObject).build();
    DynamicMessage.Builder valuesBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.Values");
    Descriptors.Descriptor valuesDescriptor = valuesBuilder.getDescriptorForType();
    assertNotNull(valuesDescriptor);
    DynamicMessage valuesMsg = valuesBuilder.setField(valuesDescriptor.findFieldByName("key5"), jsonObject).build();
    DynamicMessage.Builder postTelemetryBuilder = telemetrySchema.newMessageBuilder("PostTelemetry");
    Descriptors.Descriptor postTelemetryMsgDescriptor = postTelemetryBuilder.getDescriptorForType();
    assertNotNull(postTelemetryMsgDescriptor);
    DynamicMessage postTelemetryMsg = postTelemetryBuilder.setField(postTelemetryMsgDescriptor.findFieldByName("ts"), 10000L).setField(postTelemetryMsgDescriptor.findFieldByName("values"), valuesMsg).build();
    processTestPostTelemetry(postTelemetryMsg.toByteArray(), Arrays.asList("key1", "key2", "key3", "key4", "key5"), true, true);
}
Also used : DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) DynamicMessage(com.google.protobuf.DynamicMessage) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration) DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) CoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceTypeConfiguration) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.Test)

Example 22 with ProtoTransportPayloadConfiguration

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

the class BaseDeviceProfileControllerTest method getDynamicSchema.

private DynamicSchema getDynamicSchema(String schema) throws Exception {
    DeviceProfile deviceProfile = testSaveDeviceProfileWithProtoPayloadType(schema);
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration);
    MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
    TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttDeviceProfileTransportConfiguration.getTransportPayloadTypeConfiguration();
    assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration);
    ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
    ProtoFileElement protoFile = protoTransportPayloadConfiguration.getTransportProtoSchema(schema);
    return protoTransportPayloadConfiguration.getDynamicSchema(protoFile, ProtoTransportPayloadConfiguration.ATTRIBUTES_PROTO_SCHEMA);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) 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)

Example 23 with ProtoTransportPayloadConfiguration

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

the class DeviceProfileDataValidator method validateDataImpl.

@Override
protected void validateDataImpl(TenantId tenantId, DeviceProfile deviceProfile) {
    if (org.thingsboard.server.common.data.StringUtils.isEmpty(deviceProfile.getName())) {
        throw new DataValidationException("Device profile name should be specified!");
    }
    if (deviceProfile.getType() == null) {
        throw new DataValidationException("Device profile type should be specified!");
    }
    if (deviceProfile.getTransportType() == null) {
        throw new DataValidationException("Device profile transport type should be specified!");
    }
    if (deviceProfile.getTenantId() == null) {
        throw new DataValidationException("Device profile should be assigned to tenant!");
    } else {
        Tenant tenant = tenantDao.findById(deviceProfile.getTenantId(), deviceProfile.getTenantId().getId());
        if (tenant == null) {
            throw new DataValidationException("Device profile is referencing to non-existent tenant!");
        }
    }
    if (deviceProfile.isDefault()) {
        DeviceProfile defaultDeviceProfile = deviceProfileService.findDefaultDeviceProfile(tenantId);
        if (defaultDeviceProfile != null && !defaultDeviceProfile.getId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Another default device profile is present in scope of current tenant!");
        }
    }
    if (!org.thingsboard.server.common.data.StringUtils.isEmpty(deviceProfile.getDefaultQueueName()) && queueService != null) {
        if (!queueService.getQueuesByServiceType(ServiceType.TB_RULE_ENGINE).contains(deviceProfile.getDefaultQueueName())) {
            throw new DataValidationException("Device profile is referencing to non-existent queue!");
        }
    }
    if (deviceProfile.getProvisionType() == null) {
        deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    }
    DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
    transportConfiguration.validate();
    if (transportConfiguration instanceof MqttDeviceProfileTransportConfiguration) {
        MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
        if (mqttTransportConfiguration.getTransportPayloadTypeConfiguration() instanceof ProtoTransportPayloadConfiguration) {
            ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) mqttTransportConfiguration.getTransportPayloadTypeConfiguration();
            validateProtoSchemas(protoTransportPayloadConfiguration);
            validateTelemetryDynamicMessageFields(protoTransportPayloadConfiguration);
            validateRpcRequestDynamicMessageFields(protoTransportPayloadConfiguration);
        }
    } else if (transportConfiguration instanceof CoapDeviceProfileTransportConfiguration) {
        CoapDeviceProfileTransportConfiguration coapDeviceProfileTransportConfiguration = (CoapDeviceProfileTransportConfiguration) transportConfiguration;
        CoapDeviceTypeConfiguration coapDeviceTypeConfiguration = coapDeviceProfileTransportConfiguration.getCoapDeviceTypeConfiguration();
        if (coapDeviceTypeConfiguration instanceof DefaultCoapDeviceTypeConfiguration) {
            DefaultCoapDeviceTypeConfiguration defaultCoapDeviceTypeConfiguration = (DefaultCoapDeviceTypeConfiguration) coapDeviceTypeConfiguration;
            TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = defaultCoapDeviceTypeConfiguration.getTransportPayloadTypeConfiguration();
            if (transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration) {
                ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration;
                validateProtoSchemas(protoTransportPayloadConfiguration);
                validateTelemetryDynamicMessageFields(protoTransportPayloadConfiguration);
                validateRpcRequestDynamicMessageFields(protoTransportPayloadConfiguration);
            }
        }
    } else if (transportConfiguration instanceof Lwm2mDeviceProfileTransportConfiguration) {
        List<LwM2MBootstrapServerCredential> lwM2MBootstrapServersConfigurations = ((Lwm2mDeviceProfileTransportConfiguration) transportConfiguration).getBootstrap();
        if (lwM2MBootstrapServersConfigurations != null) {
            validateLwm2mServersConfigOfBootstrapForClient(lwM2MBootstrapServersConfigurations, ((Lwm2mDeviceProfileTransportConfiguration) transportConfiguration).isBootstrapServerUpdateEnable());
            for (LwM2MBootstrapServerCredential bootstrapServerCredential : lwM2MBootstrapServersConfigurations) {
                validateLwm2mServersCredentialOfBootstrapForClient(bootstrapServerCredential);
            }
        }
    }
    List<DeviceProfileAlarm> profileAlarms = deviceProfile.getProfileData().getAlarms();
    if (!CollectionUtils.isEmpty(profileAlarms)) {
        Set<String> alarmTypes = new HashSet<>();
        for (DeviceProfileAlarm alarm : profileAlarms) {
            String alarmType = alarm.getAlarmType();
            if (StringUtils.isEmpty(alarmType)) {
                throw new DataValidationException("Alarm rule type should be specified!");
            }
            if (!alarmTypes.add(alarmType)) {
                throw new DataValidationException(String.format("Can't create device profile with the same alarm rule types: \"%s\"!", alarmType));
            }
        }
    }
    if (deviceProfile.getDefaultRuleChainId() != null) {
        RuleChain ruleChain = ruleChainService.findRuleChainById(tenantId, deviceProfile.getDefaultRuleChainId());
        if (ruleChain == null) {
            throw new DataValidationException("Can't assign non-existent rule chain!");
        }
    }
    if (deviceProfile.getDefaultDashboardId() != null) {
        DashboardInfo dashboard = dashboardService.findDashboardInfoById(tenantId, deviceProfile.getDefaultDashboardId());
        if (dashboard == null) {
            throw new DataValidationException("Can't assign non-existent dashboard!");
        }
    }
    if (deviceProfile.getFirmwareId() != null) {
        OtaPackage firmware = otaPackageService.findOtaPackageById(tenantId, deviceProfile.getFirmwareId());
        if (firmware == null) {
            throw new DataValidationException("Can't assign non-existent firmware!");
        }
        if (!firmware.getType().equals(OtaPackageType.FIRMWARE)) {
            throw new DataValidationException("Can't assign firmware with type: " + firmware.getType());
        }
        if (firmware.getData() == null && !firmware.hasUrl()) {
            throw new DataValidationException("Can't assign firmware with empty data!");
        }
        if (!firmware.getDeviceProfileId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Can't assign firmware with different deviceProfile!");
        }
    }
    if (deviceProfile.getSoftwareId() != null) {
        OtaPackage software = otaPackageService.findOtaPackageById(tenantId, deviceProfile.getSoftwareId());
        if (software == null) {
            throw new DataValidationException("Can't assign non-existent software!");
        }
        if (!software.getType().equals(OtaPackageType.SOFTWARE)) {
            throw new DataValidationException("Can't assign software with type: " + software.getType());
        }
        if (software.getData() == null && !software.hasUrl()) {
            throw new DataValidationException("Can't assign software with empty data!");
        }
        if (!software.getDeviceProfileId().equals(deviceProfile.getId())) {
            throw new DataValidationException("Can't assign firmware with different deviceProfile!");
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) MqttDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) AbstractLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.AbstractLwM2MBootstrapServerCredential) RPKLwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.RPKLwM2MBootstrapServerCredential) LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.LwM2MBootstrapServerCredential) X509LwM2MBootstrapServerCredential(org.thingsboard.server.common.data.device.profile.lwm2m.bootstrap.X509LwM2MBootstrapServerCredential) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ProtoTransportPayloadConfiguration(org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) TransportPayloadTypeConfiguration(org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration) DefaultCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.DefaultCoapDeviceTypeConfiguration) CoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceTypeConfiguration) HashSet(java.util.HashSet)

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