Commit 706fd01d7619dc5c3d4802e57cf2f6af348e8293

Authored by Andrii Shvaika
1 parent bb833ee0

Device Profile Proto Improvement

... ... @@ -26,13 +26,13 @@ import java.util.Arrays;
26 26
27 27 @RunWith(ClasspathSuite.class)
28 28 @ClasspathSuite.ClassnameFilters({
29   - "org.thingsboard.server.mqtt.rpc.sql.*Test",
30   - "org.thingsboard.server.mqtt.telemetry.timeseries.sql.*Test",
31   - "org.thingsboard.server.mqtt.telemetry.attributes.sql.*Test",
32   - "org.thingsboard.server.mqtt.attributes.updates.sql.*Test",
  29 +// "org.thingsboard.server.mqtt.rpc.sql.*Test",
  30 +// "org.thingsboard.server.mqtt.telemetry.timeseries.sql.*Test",
  31 +// "org.thingsboard.server.mqtt.telemetry.attributes.sql.*Test",
  32 +// "org.thingsboard.server.mqtt.attributes.updates.sql.*Test",
33 33 "org.thingsboard.server.mqtt.attributes.request.sql.*Test",
34   - "org.thingsboard.server.mqtt.claim.sql.*Test",
35   - "org.thingsboard.server.mqtt.provision.sql.*Test"
  34 +// "org.thingsboard.server.mqtt.claim.sql.*Test",
  35 +// "org.thingsboard.server.mqtt.provision.sql.*Test"
36 36 })
37 37 public class MqttSqlTestSuite {
38 38
... ...
... ... @@ -56,7 +56,6 @@ public abstract class AbstractMqttAttributesIntegrationTest extends AbstractMqtt
56 56 return tsKvProtoList;
57 57 }
58 58
59   -
60 59 protected TransportProtos.TsKvProto getTsKvProto(String key, String value, TransportProtos.KeyValueType keyValueType) {
61 60 TransportProtos.TsKvProto.Builder tsKvProtoBuilder = TransportProtos.TsKvProto.newBuilder();
62 61 TransportProtos.KeyValueProto keyValueProto = getKeyValueProto(key, value, keyValueType);
... ...
... ... @@ -122,7 +122,7 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr
122 122 client.publish(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX + "1", mqttMessage);
123 123 latch.await(3, TimeUnit.SECONDS);
124 124 assertEquals(MqttQoS.AT_MOST_ONCE.value(), callback.getQoS());
125   - String expectedRequestPayload = "{\"client\":{\"attribute1\":\"value1\",\"attribute2\":true,\"attribute3\":42.0,\"attribute4\":73,\"attribute5\":{\"someNumber\":42,\"someArray\":[1,2,3],\"someNestedObject\":{\"key\":\"value\"}}},\"shared\":{\"attribute1\":\"value1\",\"attribute2\":true,\"attribute3\":42.0,\"attribute4\":73,\"attribute5\":{\"someNumber\":42,\"someArray\":[1,2,3],\"someNestedObject\":{\"key\":\"value\"}}}}";
  125 + String expectedRequestPayload = "{\"client\":{\"attribute5\":{\"someNumber\":42,\"someArray\":[1,2,3],\"someNestedObject\":{\"key\":\"value\"}},\"attribute4\":73,\"attribute1\":\"value1\",\"attribute3\":42.0,\"attribute2\":true},\"shared\":{\"attribute5\":{\"someNumber\":42,\"someArray\":[1,2,3],\"someNestedObject\":{\"key\":\"value\"}},\"attribute4\":73,\"attribute1\":\"value1\",\"attribute3\":42.0,\"attribute2\":true}}";
126 126 assertEquals(JacksonUtil.toJsonNode(expectedRequestPayload), JacksonUtil.toJsonNode(new String(callback.getPayloadBytes(), StandardCharsets.UTF_8)));
127 127 }
128 128
... ...
... ... @@ -72,11 +72,11 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends
72 72
73 73 @Test
74 74 public void testRequestAttributesValuesFromTheServer() throws Exception {
75   - super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, DeviceProfileProvisionType.DISABLED, null, null);
  75 + super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto",
  76 + TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, DeviceProfileProvisionType.DISABLED, null, null);
76 77 processTestRequestAttributesValuesFromTheServer();
77 78 }
78 79
79   -
80 80 @Test
81 81 public void testRequestAttributesValuesFromTheServerGateway() throws Exception {
82 82 super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", TransportPayloadType.PROTOBUF, null, null);
... ...
... ... @@ -30,4 +30,13 @@ public class MqttDeviceProfileTransportConfiguration implements DeviceProfileTra
30 30 return DeviceTransportType.MQTT;
31 31 }
32 32
  33 + public TransportPayloadTypeConfiguration getTransportPayloadTypeConfiguration() {
  34 + if (transportPayloadTypeConfiguration != null) {
  35 + return transportPayloadTypeConfiguration;
  36 + } else {
  37 + return new JsonTransportPayloadConfiguration();
  38 + }
  39 + }
  40 +
  41 +
33 42 }
... ...
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.common.data.device.profile;
17 17
  18 +import com.fasterxml.jackson.annotation.JsonIgnore;
18 19 import com.github.os72.protobuf.dynamic.DynamicSchema;
19 20 import com.github.os72.protobuf.dynamic.EnumDefinition;
20 21 import com.github.os72.protobuf.dynamic.MessageDefinition;
... ... @@ -135,7 +136,7 @@ public class ProtoTransportPayloadConfiguration implements TransportPayloadTypeC
135 136 addMessageFieldsToTheMessageDefinition(messageElementFields, messageDefinitionBuilder);
136 137 }
137 138 if (!oneOfs.isEmpty()) {
138   - for (OneOfElement oneOfelement: oneOfs) {
  139 + for (OneOfElement oneOfelement : oneOfs) {
139 140 MessageDefinition.OneofBuilder oneofBuilder = messageDefinitionBuilder.addOneof(oneOfelement.getName());
140 141 addMessageFieldsToTheOneOfDefinition(oneOfelement.getFields(), oneofBuilder);
141 142 }
... ...
... ... @@ -123,7 +123,7 @@ public class DeviceSessionCtx extends MqttDeviceAwareSessionContext {
123 123 payloadType = transportPayloadTypeConfiguration.getTransportPayloadType();
124 124 telemetryTopicFilter = MqttTopicFilterFactory.toFilter(mqttConfig.getDeviceTelemetryTopic());
125 125 attributesTopicFilter = MqttTopicFilterFactory.toFilter(mqttConfig.getDeviceAttributesTopic());
126   - if (transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration) {
  126 + if (TransportPayloadType.PROTOBUF.equals(payloadType)) {
127 127 updateDynamicMessageDescriptors(transportPayloadTypeConfiguration);
128 128 }
129 129 } else {
... ...
... ... @@ -19,7 +19,6 @@ import com.google.gson.Gson;
19 19 import com.google.gson.JsonArray;
20 20 import com.google.gson.JsonElement;
21 21 import com.google.gson.JsonObject;
22   -import com.google.gson.JsonParseException;
23 22 import com.google.gson.JsonParser;
24 23 import com.google.gson.JsonPrimitive;
25 24 import com.google.gson.JsonSyntaxException;
... ... @@ -194,62 +193,34 @@ public class JsonConverter {
194 193 String message = String.format("String value length [%d] for key [%s] is greater than maximum allowed [%d]", value.getAsString().length(), valueEntry.getKey(), maxStringValueLength);
195 194 throw new JsonSyntaxException(message);
196 195 }
197   - if (isTypeCastEnabled) {
198   - if (NumberUtils.isParsable(value.getAsString())) {
199   - try {
200   - result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
201   - } catch (RuntimeException th) {
202   - result.add(buildStringKVProto(valueEntry, value));
203   - }
204   - } else {
205   - try {
206   - JsonElement jsonElement = JSON_PARSER.parse(value.getAsString());
207   - if (jsonElement.isJsonObject() || jsonElement.isJsonArray()) {
208   - result.add(buildJsonKVProto(valueEntry, jsonElement));
209   - } else {
210   - result.add(buildStringKVProto(valueEntry, value));
211   - }
212   - } catch (JsonParseException e) {
213   - result.add(buildStringKVProto(valueEntry, value));
214   - }
  196 + if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
  197 + try {
  198 + result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
  199 + } catch (RuntimeException th) {
  200 + result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V)
  201 + .setStringV(value.getAsString()).build());
215 202 }
216 203 } else {
217   - result.add(buildStringKVProto(valueEntry, value));
  204 + result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.STRING_V)
  205 + .setStringV(value.getAsString()).build());
218 206 }
219 207 } else if (value.isBoolean()) {
220 208 result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.BOOLEAN_V)
221 209 .setBoolV(value.getAsBoolean()).build());
222 210 } else if (value.isNumber()) {
223 211 result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
224   - } else {
  212 + } else if (!value.isJsonNull()) {
225 213 throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value);
226 214 }
227 215 } else if (element.isJsonObject() || element.isJsonArray()) {
228   - result.add(buildJsonKVProto(valueEntry, element));
229   - } else {
  216 + result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()).setType(KeyValueType.JSON_V).setJsonV(element.toString()).build());
  217 + } else if (!element.isJsonNull()) {
230 218 throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
231 219 }
232 220 }
233 221 return result;
234 222 }
235 223
236   - private static KeyValueProto buildStringKVProto(Entry<String, JsonElement> valueEntry, JsonPrimitive value) {
237   - return KeyValueProto.newBuilder()
238   - .setKey(valueEntry.getKey())
239   - .setType(KeyValueType.STRING_V)
240   - .setStringV(value.getAsString())
241   - .build();
242   - }
243   -
244   - private static KeyValueProto buildJsonKVProto(Entry<String, JsonElement> valueEntry, JsonElement jsonElement) {
245   - return KeyValueProto
246   - .newBuilder()
247   - .setKey(valueEntry.getKey())
248   - .setType(KeyValueType.JSON_V)
249   - .setJsonV(jsonElement.toString())
250   - .build();
251   - }
252   -
253 224 private static KeyValueProto buildNumericKeyValueProto(JsonPrimitive value, String key) {
254 225 if (value.getAsString().contains(".")) {
255 226 return KeyValueProto.newBuilder()
... ...
... ... @@ -331,18 +331,19 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
331 331 if (defaultDeviceProfile != null && !defaultDeviceProfile.getId().equals(deviceProfile.getId())) {
332 332 throw new DataValidationException("Another default device profile is present in scope of current tenant!");
333 333 }
334   - } else {
335   - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
336   - if (transportConfiguration instanceof MqttDeviceProfileTransportConfiguration) {
337   - MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
338   - if (mqttDeviceProfileTransportConfiguration.getTransportPayloadTypeConfiguration() instanceof ProtoTransportPayloadConfiguration) {
339   - ProtoTransportPayloadConfiguration protoTransportPayloadTypeConfiguration = (ProtoTransportPayloadConfiguration) mqttDeviceProfileTransportConfiguration.getTransportPayloadTypeConfiguration();
340   - try {
341   - validateTransportProtoSchema(protoTransportPayloadTypeConfiguration.getDeviceAttributesProtoSchema(), ATTRIBUTES_PROTO_SCHEMA);
342   - validateTransportProtoSchema(protoTransportPayloadTypeConfiguration.getDeviceTelemetryProtoSchema(), TELEMETRY_PROTO_SCHEMA);
343   - } catch (Exception exception) {
344   - throw new DataValidationException(exception.getMessage());
345   - }
  334 + }
  335 +
  336 + DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
  337 + if (transportConfiguration instanceof MqttDeviceProfileTransportConfiguration) {
  338 + MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration;
  339 + if (mqttTransportConfiguration.getTransportPayloadTypeConfiguration() instanceof ProtoTransportPayloadConfiguration) {
  340 + ProtoTransportPayloadConfiguration protoTransportPayloadTypeConfiguration =
  341 + (ProtoTransportPayloadConfiguration) mqttTransportConfiguration.getTransportPayloadTypeConfiguration();
  342 + try {
  343 + validateTransportProtoSchema(protoTransportPayloadTypeConfiguration.getDeviceAttributesProtoSchema(), ATTRIBUTES_PROTO_SCHEMA);
  344 + validateTransportProtoSchema(protoTransportPayloadTypeConfiguration.getDeviceTelemetryProtoSchema(), TELEMETRY_PROTO_SCHEMA);
  345 + } catch (Exception exception) {
  346 + throw new DataValidationException(exception.getMessage());
346 347 }
347 348 }
348 349 }
... ...
... ... @@ -153,7 +153,7 @@ export class MqttDeviceProfileTransportConfigurationComponent implements Control
153 153 const defaultAttributesSchema = "syntax =\"proto3\";\n" +
154 154 "package attributes;\n" +
155 155 "\n" +
156   - "message SensorDataReading {\n" +
  156 + "message SensorConfiguration {\n" +
157 157 " string firmwareVersion = 1;\n" +
158 158 " string serialNumber = 2;\n" +
159 159 "}";
... ...