Commit 845d8247dd185a4dbae3cc43a6fa10ae3b033ea7
Committed by
GitHub
1 parent
08c6a80d
[3.3.2] MQTT short topics support (#4967)
* added mqtt short topics support * remove volatile keyword from topic types parameters * added new tests for mqtt short topics * fix compilation error after merge * improvements/typo-fixes after pull request review
Showing
23 changed files
with
709 additions
and
260 deletions
@@ -52,7 +52,17 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | @@ -52,7 +52,17 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | ||
52 | 52 | ||
53 | @Test | 53 | @Test |
54 | public void testRequestAttributesValuesFromTheServer() throws Exception { | 54 | public void testRequestAttributesValuesFromTheServer() throws Exception { |
55 | - processTestRequestAttributesValuesFromTheServer(); | 55 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX); |
56 | + } | ||
57 | + | ||
58 | + @Test | ||
59 | + public void testRequestAttributesValuesFromTheServerOnShortTopic() throws Exception { | ||
60 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX); | ||
61 | + } | ||
62 | + | ||
63 | + @Test | ||
64 | + public void testRequestAttributesValuesFromTheServerOnShortJsonTopic() throws Exception { | ||
65 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_JSON_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_JSON_TOPIC_PREFIX); | ||
56 | } | 66 | } |
57 | 67 | ||
58 | @Test | 68 | @Test |
@@ -60,18 +70,18 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | @@ -60,18 +70,18 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | ||
60 | processTestGatewayRequestAttributesValuesFromTheServer(); | 70 | processTestGatewayRequestAttributesValuesFromTheServer(); |
61 | } | 71 | } |
62 | 72 | ||
63 | - protected void processTestRequestAttributesValuesFromTheServer() throws Exception { | 73 | + protected void processTestRequestAttributesValuesFromTheServer(String attrPubTopic, String attrSubTopic, String attrReqTopicPrefix) throws Exception { |
64 | 74 | ||
65 | MqttAsyncClient client = getMqttAsyncClient(accessToken); | 75 | MqttAsyncClient client = getMqttAsyncClient(accessToken); |
66 | 76 | ||
67 | - postAttributesAndSubscribeToTopic(savedDevice, client); | 77 | + postAttributesAndSubscribeToTopic(savedDevice, client, attrPubTopic, attrSubTopic); |
68 | 78 | ||
69 | Thread.sleep(5000); | 79 | Thread.sleep(5000); |
70 | 80 | ||
71 | TestMqttCallback callback = getTestMqttCallback(); | 81 | TestMqttCallback callback = getTestMqttCallback(); |
72 | client.setCallback(callback); | 82 | client.setCallback(callback); |
73 | 83 | ||
74 | - validateResponse(client, callback.getLatch(), callback); | 84 | + validateResponse(client, callback.getLatch(), callback, attrReqTopicPrefix); |
75 | } | 85 | } |
76 | 86 | ||
77 | protected void processTestGatewayRequestAttributesValuesFromTheServer() throws Exception { | 87 | protected void processTestGatewayRequestAttributesValuesFromTheServer() throws Exception { |
@@ -103,10 +113,10 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | @@ -103,10 +113,10 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | ||
103 | validateSharedResponseGateway(client, sharedAttributesCallback); | 113 | validateSharedResponseGateway(client, sharedAttributesCallback); |
104 | } | 114 | } |
105 | 115 | ||
106 | - protected void postAttributesAndSubscribeToTopic(Device savedDevice, MqttAsyncClient client) throws Exception { | 116 | + protected void postAttributesAndSubscribeToTopic(Device savedDevice, MqttAsyncClient client, String attrPubTopic, String attrSubTopic) throws Exception { |
107 | doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", POST_ATTRIBUTES_PAYLOAD, String.class, status().isOk()); | 117 | doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", POST_ATTRIBUTES_PAYLOAD, String.class, status().isOk()); |
108 | - client.publish(MqttTopics.DEVICE_ATTRIBUTES_TOPIC, new MqttMessage(POST_ATTRIBUTES_PAYLOAD.getBytes())).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); | ||
109 | - client.subscribe(MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC, MqttQoS.AT_MOST_ONCE.value()).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); | 118 | + client.publish(attrPubTopic, new MqttMessage(POST_ATTRIBUTES_PAYLOAD.getBytes())).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); |
119 | + client.subscribe(attrSubTopic, MqttQoS.AT_MOST_ONCE.value()).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); | ||
110 | } | 120 | } |
111 | 121 | ||
112 | protected void postGatewayDeviceClientAttributes(MqttAsyncClient client) throws Exception { | 122 | protected void postGatewayDeviceClientAttributes(MqttAsyncClient client) throws Exception { |
@@ -114,12 +124,12 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | @@ -114,12 +124,12 @@ public abstract class AbstractMqttAttributesRequestIntegrationTest extends Abstr | ||
114 | client.publish(MqttTopics.GATEWAY_ATTRIBUTES_TOPIC, new MqttMessage(postClientAttributes.getBytes())).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); | 124 | client.publish(MqttTopics.GATEWAY_ATTRIBUTES_TOPIC, new MqttMessage(postClientAttributes.getBytes())).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); |
115 | } | 125 | } |
116 | 126 | ||
117 | - protected void validateResponse(MqttAsyncClient client, CountDownLatch latch, TestMqttCallback callback) throws MqttException, InterruptedException, InvalidProtocolBufferException { | 127 | + protected void validateResponse(MqttAsyncClient client, CountDownLatch latch, TestMqttCallback callback, String attrReqTopicPrefix) throws MqttException, InterruptedException, InvalidProtocolBufferException { |
118 | String keys = "attribute1,attribute2,attribute3,attribute4,attribute5"; | 128 | String keys = "attribute1,attribute2,attribute3,attribute4,attribute5"; |
119 | String payloadStr = "{\"clientKeys\":\"" + keys + "\", \"sharedKeys\":\"" + keys + "\"}"; | 129 | String payloadStr = "{\"clientKeys\":\"" + keys + "\", \"sharedKeys\":\"" + keys + "\"}"; |
120 | MqttMessage mqttMessage = new MqttMessage(); | 130 | MqttMessage mqttMessage = new MqttMessage(); |
121 | mqttMessage.setPayload(payloadStr.getBytes()); | 131 | mqttMessage.setPayload(payloadStr.getBytes()); |
122 | - client.publish(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX + "1", mqttMessage).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); | 132 | + client.publish(attrReqTopicPrefix + "1", mqttMessage).waitForCompletion(TimeUnit.MINUTES.toMillis(1)); |
123 | latch.await(1, TimeUnit.MINUTES); | 133 | latch.await(1, TimeUnit.MINUTES); |
124 | assertEquals(MqttQoS.AT_MOST_ONCE.value(), callback.getQoS()); | 134 | 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\"}}}}"; | 135 | 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\"}}}}"; |
@@ -20,6 +20,7 @@ import org.junit.After; | @@ -20,6 +20,7 @@ import org.junit.After; | ||
20 | import org.junit.Before; | 20 | import org.junit.Before; |
21 | import org.junit.Test; | 21 | import org.junit.Test; |
22 | import org.thingsboard.server.common.data.TransportPayloadType; | 22 | import org.thingsboard.server.common.data.TransportPayloadType; |
23 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
23 | 24 | ||
24 | @Slf4j | 25 | @Slf4j |
25 | public abstract class AbstractMqttAttributesRequestJsonIntegrationTest extends AbstractMqttAttributesRequestIntegrationTest { | 26 | public abstract class AbstractMqttAttributesRequestJsonIntegrationTest extends AbstractMqttAttributesRequestIntegrationTest { |
@@ -36,7 +37,17 @@ public abstract class AbstractMqttAttributesRequestJsonIntegrationTest extends A | @@ -36,7 +37,17 @@ public abstract class AbstractMqttAttributesRequestJsonIntegrationTest extends A | ||
36 | 37 | ||
37 | @Test | 38 | @Test |
38 | public void testRequestAttributesValuesFromTheServer() throws Exception { | 39 | public void testRequestAttributesValuesFromTheServer() throws Exception { |
39 | - processTestRequestAttributesValuesFromTheServer(); | 40 | + super.testRequestAttributesValuesFromTheServer(); |
41 | + } | ||
42 | + | ||
43 | + @Test | ||
44 | + public void testRequestAttributesValuesFromTheServerOnShortTopic() throws Exception { | ||
45 | + super.testRequestAttributesValuesFromTheServerOnShortTopic(); | ||
46 | + } | ||
47 | + | ||
48 | + @Test | ||
49 | + public void testRequestAttributesValuesFromTheServerOnShortJsonTopic() throws Exception { | ||
50 | + super.testRequestAttributesValuesFromTheServerOnShortJsonTopic(); | ||
40 | } | 51 | } |
41 | 52 | ||
42 | @Test | 53 | @Test |
@@ -84,7 +84,21 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | @@ -84,7 +84,21 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | ||
84 | public void testRequestAttributesValuesFromTheServer() throws Exception { | 84 | public void testRequestAttributesValuesFromTheServer() throws Exception { |
85 | super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", | 85 | super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", |
86 | TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, null, null, null, null, DeviceProfileProvisionType.DISABLED); | 86 | TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, null, null, null, null, DeviceProfileProvisionType.DISABLED); |
87 | - processTestRequestAttributesValuesFromTheServer(); | 87 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX); |
88 | + } | ||
89 | + | ||
90 | + @Test | ||
91 | + public void testRequestAttributesValuesFromTheServerOnShortTopic() throws Exception { | ||
92 | + super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", | ||
93 | + TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, null, null, null, null, DeviceProfileProvisionType.DISABLED); | ||
94 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX); | ||
95 | + } | ||
96 | + | ||
97 | + @Test | ||
98 | + public void testRequestAttributesValuesFromTheServerOnShortProtoTopic() throws Exception { | ||
99 | + super.processBeforeTest("Test Request attribute values from the server proto", "Gateway Test Request attribute values from the server proto", | ||
100 | + TransportPayloadType.PROTOBUF, null, null, null, ATTRIBUTES_SCHEMA_STR, null, null, null, null, DeviceProfileProvisionType.DISABLED); | ||
101 | + processTestRequestAttributesValuesFromTheServer(MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_PROTO_TOPIC, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_PROTO_TOPIC_PREFIX); | ||
88 | } | 102 | } |
89 | 103 | ||
90 | @Test | 104 | @Test |
@@ -93,7 +107,10 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | @@ -93,7 +107,10 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | ||
93 | processTestGatewayRequestAttributesValuesFromTheServer(); | 107 | processTestGatewayRequestAttributesValuesFromTheServer(); |
94 | } | 108 | } |
95 | 109 | ||
96 | - protected void postAttributesAndSubscribeToTopic(Device savedDevice, MqttAsyncClient client) throws Exception { | 110 | + @Test |
111 | + public void testRequestAttributesValuesFromTheServerOnShortJsonTopic() throws Exception { } | ||
112 | + | ||
113 | + protected void postAttributesAndSubscribeToTopic(Device savedDevice, MqttAsyncClient client, String attrPubTopic, String attrSubTopic) throws Exception { | ||
97 | doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", AbstractMqttAttributesIntegrationTest.POST_ATTRIBUTES_PAYLOAD, String.class, status().isOk()); | 114 | doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", AbstractMqttAttributesIntegrationTest.POST_ATTRIBUTES_PAYLOAD, String.class, status().isOk()); |
98 | DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | 115 | DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); |
99 | assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | 116 | assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); |
@@ -131,8 +148,8 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | @@ -131,8 +148,8 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | ||
131 | .setField(postAttributesMsgDescriptor.findFieldByName("attribute5"), jsonObject) | 148 | .setField(postAttributesMsgDescriptor.findFieldByName("attribute5"), jsonObject) |
132 | .build(); | 149 | .build(); |
133 | byte[] payload = postAttributesMsg.toByteArray(); | 150 | byte[] payload = postAttributesMsg.toByteArray(); |
134 | - client.publish(MqttTopics.DEVICE_ATTRIBUTES_TOPIC, new MqttMessage(payload)); | ||
135 | - client.subscribe(MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC, MqttQoS.AT_MOST_ONCE.value()); | 151 | + client.publish(attrPubTopic, new MqttMessage(payload)); |
152 | + client.subscribe(attrSubTopic, MqttQoS.AT_MOST_ONCE.value()); | ||
136 | } | 153 | } |
137 | 154 | ||
138 | protected void postGatewayDeviceClientAttributes(MqttAsyncClient client) throws Exception { | 155 | protected void postGatewayDeviceClientAttributes(MqttAsyncClient client) throws Exception { |
@@ -149,7 +166,7 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | @@ -149,7 +166,7 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | ||
149 | client.publish(MqttTopics.GATEWAY_ATTRIBUTES_TOPIC, new MqttMessage(bytes)); | 166 | client.publish(MqttTopics.GATEWAY_ATTRIBUTES_TOPIC, new MqttMessage(bytes)); |
150 | } | 167 | } |
151 | 168 | ||
152 | - protected void validateResponse(MqttAsyncClient client, CountDownLatch latch, AbstractMqttAttributesIntegrationTest.TestMqttCallback callback) throws MqttException, InterruptedException, InvalidProtocolBufferException { | 169 | + protected void validateResponse(MqttAsyncClient client, CountDownLatch latch, TestMqttCallback callback, String attrReqTopic) throws MqttException, InterruptedException, InvalidProtocolBufferException { |
153 | String keys = "attribute1,attribute2,attribute3,attribute4,attribute5"; | 170 | String keys = "attribute1,attribute2,attribute3,attribute4,attribute5"; |
154 | TransportApiProtos.AttributesRequest.Builder attributesRequestBuilder = TransportApiProtos.AttributesRequest.newBuilder(); | 171 | TransportApiProtos.AttributesRequest.Builder attributesRequestBuilder = TransportApiProtos.AttributesRequest.newBuilder(); |
155 | attributesRequestBuilder.setClientKeys(keys); | 172 | attributesRequestBuilder.setClientKeys(keys); |
@@ -157,7 +174,7 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | @@ -157,7 +174,7 @@ public abstract class AbstractMqttAttributesRequestProtoIntegrationTest extends | ||
157 | TransportApiProtos.AttributesRequest attributesRequest = attributesRequestBuilder.build(); | 174 | TransportApiProtos.AttributesRequest attributesRequest = attributesRequestBuilder.build(); |
158 | MqttMessage mqttMessage = new MqttMessage(); | 175 | MqttMessage mqttMessage = new MqttMessage(); |
159 | mqttMessage.setPayload(attributesRequest.toByteArray()); | 176 | mqttMessage.setPayload(attributesRequest.toByteArray()); |
160 | - client.publish(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX + "1", mqttMessage); | 177 | + client.publish(attrReqTopic + "1", mqttMessage); |
161 | latch.await(3, TimeUnit.SECONDS); | 178 | latch.await(3, TimeUnit.SECONDS); |
162 | assertEquals(MqttQoS.AT_MOST_ONCE.value(), callback.getQoS()); | 179 | assertEquals(MqttQoS.AT_MOST_ONCE.value(), callback.getQoS()); |
163 | TransportProtos.GetAttributeResponseMsg expectedAttributesResponse = getExpectedAttributeResponseMsg(); | 180 | TransportProtos.GetAttributeResponseMsg expectedAttributesResponse = getExpectedAttributeResponseMsg(); |
@@ -61,7 +61,17 @@ public abstract class AbstractMqttAttributesUpdatesIntegrationTest extends Abstr | @@ -61,7 +61,17 @@ public abstract class AbstractMqttAttributesUpdatesIntegrationTest extends Abstr | ||
61 | 61 | ||
62 | @Test | 62 | @Test |
63 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { | 63 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { |
64 | - processTestSubscribeToAttributesUpdates(); | 64 | + processTestSubscribeToAttributesUpdates(MqttTopics.DEVICE_ATTRIBUTES_TOPIC); |
65 | + } | ||
66 | + | ||
67 | + @Test | ||
68 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortTopic() throws Exception { | ||
69 | + processTestSubscribeToAttributesUpdates(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC); | ||
70 | + } | ||
71 | + | ||
72 | + @Test | ||
73 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortJsonTopic() throws Exception { | ||
74 | + processTestSubscribeToAttributesUpdates(MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC); | ||
65 | } | 75 | } |
66 | 76 | ||
67 | @Test | 77 | @Test |
@@ -69,14 +79,14 @@ public abstract class AbstractMqttAttributesUpdatesIntegrationTest extends Abstr | @@ -69,14 +79,14 @@ public abstract class AbstractMqttAttributesUpdatesIntegrationTest extends Abstr | ||
69 | processGatewayTestSubscribeToAttributesUpdates(); | 79 | processGatewayTestSubscribeToAttributesUpdates(); |
70 | } | 80 | } |
71 | 81 | ||
72 | - protected void processTestSubscribeToAttributesUpdates() throws Exception { | 82 | + protected void processTestSubscribeToAttributesUpdates(String attrSubTopic) throws Exception { |
73 | 83 | ||
74 | MqttAsyncClient client = getMqttAsyncClient(accessToken); | 84 | MqttAsyncClient client = getMqttAsyncClient(accessToken); |
75 | 85 | ||
76 | TestMqttCallback onUpdateCallback = getTestMqttCallback(); | 86 | TestMqttCallback onUpdateCallback = getTestMqttCallback(); |
77 | client.setCallback(onUpdateCallback); | 87 | client.setCallback(onUpdateCallback); |
78 | 88 | ||
79 | - client.subscribe(MqttTopics.DEVICE_ATTRIBUTES_TOPIC, MqttQoS.AT_MOST_ONCE.value()); | 89 | + client.subscribe(attrSubTopic, MqttQoS.AT_MOST_ONCE.value()); |
80 | 90 | ||
81 | Thread.sleep(1000); | 91 | Thread.sleep(1000); |
82 | 92 |
@@ -36,7 +36,17 @@ public abstract class AbstractMqttAttributesUpdatesJsonIntegrationTest extends A | @@ -36,7 +36,17 @@ public abstract class AbstractMqttAttributesUpdatesJsonIntegrationTest extends A | ||
36 | 36 | ||
37 | @Test | 37 | @Test |
38 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { | 38 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { |
39 | - processTestSubscribeToAttributesUpdates(); | 39 | + super.testSubscribeToAttributesUpdatesFromTheServer(); |
40 | + } | ||
41 | + | ||
42 | + @Test | ||
43 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortTopic() throws Exception { | ||
44 | + super.testSubscribeToAttributesUpdatesFromTheServerOnShortTopic(); | ||
45 | + } | ||
46 | + | ||
47 | + @Test | ||
48 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortJsonTopic() throws Exception { | ||
49 | + super.testSubscribeToAttributesUpdatesFromTheServerOnShortJsonTopic(); | ||
40 | } | 50 | } |
41 | 51 | ||
42 | @Test | 52 | @Test |
@@ -21,6 +21,7 @@ import org.junit.After; | @@ -21,6 +21,7 @@ import org.junit.After; | ||
21 | import org.junit.Before; | 21 | import org.junit.Before; |
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | import org.thingsboard.server.common.data.TransportPayloadType; | 23 | import org.thingsboard.server.common.data.TransportPayloadType; |
24 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
24 | import org.thingsboard.server.gen.transport.TransportApiProtos; | 25 | import org.thingsboard.server.gen.transport.TransportApiProtos; |
25 | import org.thingsboard.server.gen.transport.TransportProtos; | 26 | import org.thingsboard.server.gen.transport.TransportProtos; |
26 | 27 | ||
@@ -46,7 +47,20 @@ public abstract class AbstractMqttAttributesUpdatesProtoIntegrationTest extends | @@ -46,7 +47,20 @@ public abstract class AbstractMqttAttributesUpdatesProtoIntegrationTest extends | ||
46 | 47 | ||
47 | @Test | 48 | @Test |
48 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { | 49 | public void testSubscribeToAttributesUpdatesFromTheServer() throws Exception { |
49 | - processTestSubscribeToAttributesUpdates(); | 50 | + super.testSubscribeToAttributesUpdatesFromTheServer(); |
51 | + } | ||
52 | + | ||
53 | + @Test | ||
54 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortTopic() throws Exception { | ||
55 | + super.testSubscribeToAttributesUpdatesFromTheServerOnShortTopic(); | ||
56 | + } | ||
57 | + | ||
58 | + @Test | ||
59 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortJsonTopic() throws Exception {} | ||
60 | + | ||
61 | + @Test | ||
62 | + public void testSubscribeToAttributesUpdatesFromTheServerOnShortProtoTopic() throws Exception { | ||
63 | + processTestSubscribeToAttributesUpdates(MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC); | ||
50 | } | 64 | } |
51 | 65 | ||
52 | @Test | 66 | @Test |
@@ -21,6 +21,7 @@ import org.junit.After; | @@ -21,6 +21,7 @@ import org.junit.After; | ||
21 | import org.junit.Assert; | 21 | import org.junit.Assert; |
22 | import org.junit.Before; | 22 | import org.junit.Before; |
23 | import org.junit.Test; | 23 | import org.junit.Test; |
24 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
24 | import org.thingsboard.server.service.security.AccessValidator; | 25 | import org.thingsboard.server.service.security.AccessValidator; |
25 | 26 | ||
26 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | 27 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
@@ -81,12 +82,32 @@ public abstract class AbstractMqttServerSideRpcDefaultIntegrationTest extends Ab | @@ -81,12 +82,32 @@ public abstract class AbstractMqttServerSideRpcDefaultIntegrationTest extends Ab | ||
81 | 82 | ||
82 | @Test | 83 | @Test |
83 | public void testServerMqttOneWayRpc() throws Exception { | 84 | public void testServerMqttOneWayRpc() throws Exception { |
84 | - processOneWayRpcTest(); | 85 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
86 | + } | ||
87 | + | ||
88 | + @Test | ||
89 | + public void testServerMqttOneWayRpcOnShortTopic() throws Exception { | ||
90 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
91 | + } | ||
92 | + | ||
93 | + @Test | ||
94 | + public void testServerMqttOneWayRpcOnShortJsonTopic() throws Exception { | ||
95 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC); | ||
85 | } | 96 | } |
86 | 97 | ||
87 | @Test | 98 | @Test |
88 | public void testServerMqttTwoWayRpc() throws Exception { | 99 | public void testServerMqttTwoWayRpc() throws Exception { |
89 | - processTwoWayRpcTest(); | 100 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
101 | + } | ||
102 | + | ||
103 | + @Test | ||
104 | + public void testServerMqttTwoWayRpcOnShortTopic() throws Exception { | ||
105 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
106 | + } | ||
107 | + | ||
108 | + @Test | ||
109 | + public void testServerMqttTwoWayRpcOnShortJsonTopic() throws Exception { | ||
110 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC); | ||
90 | } | 111 | } |
91 | 112 | ||
92 | @Test | 113 | @Test |
@@ -60,14 +60,14 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | @@ -60,14 +60,14 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | ||
60 | asyncContextTimeoutToUseRpcPlugin = 10000L; | 60 | asyncContextTimeoutToUseRpcPlugin = 10000L; |
61 | } | 61 | } |
62 | 62 | ||
63 | - protected void processOneWayRpcTest() throws Exception { | 63 | + protected void processOneWayRpcTest(String rpcSubTopic) throws Exception { |
64 | MqttAsyncClient client = getMqttAsyncClient(accessToken); | 64 | MqttAsyncClient client = getMqttAsyncClient(accessToken); |
65 | 65 | ||
66 | CountDownLatch latch = new CountDownLatch(1); | 66 | CountDownLatch latch = new CountDownLatch(1); |
67 | TestMqttCallback callback = new TestMqttCallback(client, latch); | 67 | TestMqttCallback callback = new TestMqttCallback(client, latch); |
68 | client.setCallback(callback); | 68 | client.setCallback(callback); |
69 | 69 | ||
70 | - client.subscribe(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC, MqttQoS.AT_MOST_ONCE.value()); | 70 | + client.subscribe(rpcSubTopic, MqttQoS.AT_MOST_ONCE.value()); |
71 | 71 | ||
72 | Thread.sleep(1000); | 72 | Thread.sleep(1000); |
73 | 73 | ||
@@ -86,9 +86,9 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | @@ -86,9 +86,9 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | ||
86 | validateOneWayRpcGatewayResponse(deviceName, client, payloadBytes); | 86 | validateOneWayRpcGatewayResponse(deviceName, client, payloadBytes); |
87 | } | 87 | } |
88 | 88 | ||
89 | - protected void processTwoWayRpcTest() throws Exception { | 89 | + protected void processTwoWayRpcTest(String rpcSubTopic) throws Exception { |
90 | MqttAsyncClient client = getMqttAsyncClient(accessToken); | 90 | MqttAsyncClient client = getMqttAsyncClient(accessToken); |
91 | - client.subscribe(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC, 1); | 91 | + client.subscribe(rpcSubTopic, 1); |
92 | 92 | ||
93 | CountDownLatch latch = new CountDownLatch(1); | 93 | CountDownLatch latch = new CountDownLatch(1); |
94 | TestMqttCallback callback = new TestMqttCallback(client, latch); | 94 | TestMqttCallback callback = new TestMqttCallback(client, latch); |
@@ -199,7 +199,7 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | @@ -199,7 +199,7 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | ||
199 | 199 | ||
200 | protected MqttMessage processMessageArrived(String requestTopic, MqttMessage mqttMessage) throws MqttException, InvalidProtocolBufferException { | 200 | protected MqttMessage processMessageArrived(String requestTopic, MqttMessage mqttMessage) throws MqttException, InvalidProtocolBufferException { |
201 | MqttMessage message = new MqttMessage(); | 201 | MqttMessage message = new MqttMessage(); |
202 | - if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC)) { | 202 | + if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC) || requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC_V2)) { |
203 | message.setPayload(DEVICE_RESPONSE.getBytes(StandardCharset.UTF_8)); | 203 | message.setPayload(DEVICE_RESPONSE.getBytes(StandardCharset.UTF_8)); |
204 | } else { | 204 | } else { |
205 | JsonNode requestMsgNode = JacksonUtil.toJsonNode(new String(mqttMessage.getPayload(), StandardCharset.UTF_8)); | 205 | JsonNode requestMsgNode = JacksonUtil.toJsonNode(new String(mqttMessage.getPayload(), StandardCharset.UTF_8)); |
@@ -232,7 +232,12 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | @@ -232,7 +232,12 @@ public abstract class AbstractMqttServerSideRpcIntegrationTest extends AbstractM | ||
232 | @Override | 232 | @Override |
233 | public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception { | 233 | public void messageArrived(String requestTopic, MqttMessage mqttMessage) throws Exception { |
234 | log.info("Message Arrived: " + Arrays.toString(mqttMessage.getPayload())); | 234 | log.info("Message Arrived: " + Arrays.toString(mqttMessage.getPayload())); |
235 | - String responseTopic = requestTopic.replace("request", "response"); | 235 | + String responseTopic; |
236 | + if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC_V2)) { | ||
237 | + responseTopic = requestTopic.replace("req", "res"); | ||
238 | + } else { | ||
239 | + responseTopic = requestTopic.replace("request", "response"); | ||
240 | + } | ||
236 | qoS = mqttMessage.getQos(); | 241 | qoS = mqttMessage.getQos(); |
237 | client.publish(responseTopic, processMessageArrived(requestTopic, mqttMessage)); | 242 | client.publish(responseTopic, processMessageArrived(requestTopic, mqttMessage)); |
238 | latch.countDown(); | 243 | latch.countDown(); |
@@ -21,6 +21,7 @@ import org.junit.After; | @@ -21,6 +21,7 @@ import org.junit.After; | ||
21 | import org.junit.Before; | 21 | import org.junit.Before; |
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | import org.thingsboard.server.common.data.TransportPayloadType; | 23 | import org.thingsboard.server.common.data.TransportPayloadType; |
24 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
24 | 25 | ||
25 | @Slf4j | 26 | @Slf4j |
26 | public abstract class AbstractMqttServerSideRpcJsonIntegrationTest extends AbstractMqttServerSideRpcIntegrationTest { | 27 | public abstract class AbstractMqttServerSideRpcJsonIntegrationTest extends AbstractMqttServerSideRpcIntegrationTest { |
@@ -37,12 +38,32 @@ public abstract class AbstractMqttServerSideRpcJsonIntegrationTest extends Abstr | @@ -37,12 +38,32 @@ public abstract class AbstractMqttServerSideRpcJsonIntegrationTest extends Abstr | ||
37 | 38 | ||
38 | @Test | 39 | @Test |
39 | public void testServerMqttOneWayRpc() throws Exception { | 40 | public void testServerMqttOneWayRpc() throws Exception { |
40 | - processOneWayRpcTest(); | 41 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
42 | + } | ||
43 | + | ||
44 | + @Test | ||
45 | + public void testServerMqttOneWayRpcOnShortTopic() throws Exception { | ||
46 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
47 | + } | ||
48 | + | ||
49 | + @Test | ||
50 | + public void testServerMqttOneWayRpcOnShortJsonTopic() throws Exception { | ||
51 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC); | ||
41 | } | 52 | } |
42 | 53 | ||
43 | @Test | 54 | @Test |
44 | public void testServerMqttTwoWayRpc() throws Exception { | 55 | public void testServerMqttTwoWayRpc() throws Exception { |
45 | - processTwoWayRpcTest(); | 56 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
57 | + } | ||
58 | + | ||
59 | + @Test | ||
60 | + public void testServerMqttTwoWayRpcOnShortTopic() throws Exception { | ||
61 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
62 | + } | ||
63 | + | ||
64 | + @Test | ||
65 | + public void testServerMqttTwoWayRpcOnShortJsonTopic() throws Exception { | ||
66 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC); | ||
46 | } | 67 | } |
47 | 68 | ||
48 | @Test | 69 | @Test |
@@ -78,12 +78,32 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | @@ -78,12 +78,32 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | ||
78 | 78 | ||
79 | @Test | 79 | @Test |
80 | public void testServerMqttOneWayRpc() throws Exception { | 80 | public void testServerMqttOneWayRpc() throws Exception { |
81 | - processOneWayRpcTest(); | 81 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
82 | + } | ||
83 | + | ||
84 | + @Test | ||
85 | + public void testServerMqttOneWayRpcOnShortTopic() throws Exception { | ||
86 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
87 | + } | ||
88 | + | ||
89 | + @Test | ||
90 | + public void testServerMqttOneWayRpcOnShortProtoTopic() throws Exception { | ||
91 | + processOneWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_PROTO_TOPIC); | ||
82 | } | 92 | } |
83 | 93 | ||
84 | @Test | 94 | @Test |
85 | public void testServerMqttTwoWayRpc() throws Exception { | 95 | public void testServerMqttTwoWayRpc() throws Exception { |
86 | - processTwoWayRpcTest(); | 96 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC); |
97 | + } | ||
98 | + | ||
99 | + @Test | ||
100 | + public void testServerMqttTwoWayRpcOnShortTopic() throws Exception { | ||
101 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC); | ||
102 | + } | ||
103 | + | ||
104 | + @Test | ||
105 | + public void testServerMqttTwoWayRpcOnShortProtoTopic() throws Exception { | ||
106 | + processTwoWayRpcTest(MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_PROTO_TOPIC); | ||
87 | } | 107 | } |
88 | 108 | ||
89 | @Test | 109 | @Test |
@@ -118,9 +138,9 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | @@ -118,9 +138,9 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | ||
118 | return builder.build(); | 138 | return builder.build(); |
119 | } | 139 | } |
120 | 140 | ||
121 | - protected void processTwoWayRpcTest() throws Exception { | 141 | + protected void processTwoWayRpcTest(String rpcSubTopic) throws Exception { |
122 | MqttAsyncClient client = getMqttAsyncClient(accessToken); | 142 | MqttAsyncClient client = getMqttAsyncClient(accessToken); |
123 | - client.subscribe(MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC, 1); | 143 | + client.subscribe(rpcSubTopic, 1); |
124 | 144 | ||
125 | CountDownLatch latch = new CountDownLatch(1); | 145 | CountDownLatch latch = new CountDownLatch(1); |
126 | TestMqttCallback callback = new TestMqttCallback(client, latch); | 146 | TestMqttCallback callback = new TestMqttCallback(client, latch); |
@@ -139,7 +159,7 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | @@ -139,7 +159,7 @@ public abstract class AbstractMqttServerSideRpcProtoIntegrationTest extends Abst | ||
139 | 159 | ||
140 | protected MqttMessage processMessageArrived(String requestTopic, MqttMessage mqttMessage) throws MqttException, InvalidProtocolBufferException { | 160 | protected MqttMessage processMessageArrived(String requestTopic, MqttMessage mqttMessage) throws MqttException, InvalidProtocolBufferException { |
141 | MqttMessage message = new MqttMessage(); | 161 | MqttMessage message = new MqttMessage(); |
142 | - if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC)) { | 162 | + if (requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC) || requestTopic.startsWith(MqttTopics.BASE_DEVICE_API_TOPIC_V2)) { |
143 | ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = getProtoTransportPayloadConfiguration(); | 163 | ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = getProtoTransportPayloadConfiguration(); |
144 | ProtoFileElement rpcRequestProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(RPC_REQUEST_PROTO_SCHEMA); | 164 | ProtoFileElement rpcRequestProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(RPC_REQUEST_PROTO_SCHEMA); |
145 | DynamicSchema rpcRequestProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(rpcRequestProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_REQUEST_PROTO_SCHEMA); | 165 | DynamicSchema rpcRequestProtoSchema = protoTransportPayloadConfiguration.getDynamicSchema(rpcRequestProtoSchemaFile, ProtoTransportPayloadConfiguration.RPC_REQUEST_PROTO_SCHEMA); |
@@ -61,6 +61,18 @@ public abstract class AbstractMqttAttributesIntegrationTest extends AbstractMqtt | @@ -61,6 +61,18 @@ public abstract class AbstractMqttAttributesIntegrationTest extends AbstractMqtt | ||
61 | } | 61 | } |
62 | 62 | ||
63 | @Test | 63 | @Test |
64 | + public void testPushAttributesOnShortTopic() throws Exception { | ||
65 | + List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
66 | + processJsonPayloadAttributesTest(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC, expectedKeys, PAYLOAD_VALUES_STR.getBytes()); | ||
67 | + } | ||
68 | + | ||
69 | + @Test | ||
70 | + public void testPushAttributesOnShortJsonTopic() throws Exception { | ||
71 | + List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
72 | + processJsonPayloadAttributesTest(MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC, expectedKeys, PAYLOAD_VALUES_STR.getBytes()); | ||
73 | + } | ||
74 | + | ||
75 | + @Test | ||
64 | public void testPushAttributesGateway() throws Exception { | 76 | public void testPushAttributesGateway() throws Exception { |
65 | List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | 77 | List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); |
66 | String deviceName1 = "Device A"; | 78 | String deviceName1 = "Device A"; |
@@ -20,6 +20,7 @@ import org.junit.After; | @@ -20,6 +20,7 @@ import org.junit.After; | ||
20 | import org.junit.Before; | 20 | import org.junit.Before; |
21 | import org.junit.Test; | 21 | import org.junit.Test; |
22 | import org.thingsboard.server.common.data.TransportPayloadType; | 22 | import org.thingsboard.server.common.data.TransportPayloadType; |
23 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
23 | 24 | ||
24 | import java.util.Arrays; | 25 | import java.util.Arrays; |
25 | import java.util.List; | 26 | import java.util.List; |
@@ -46,11 +47,17 @@ public abstract class AbstractMqttAttributesJsonIntegrationTest extends Abstract | @@ -46,11 +47,17 @@ public abstract class AbstractMqttAttributesJsonIntegrationTest extends Abstract | ||
46 | } | 47 | } |
47 | 48 | ||
48 | @Test | 49 | @Test |
50 | + public void testPushAttributesOnShortTopic() throws Exception { | ||
51 | + super.testPushAttributesOnShortTopic(); | ||
52 | + } | ||
53 | + | ||
54 | + @Test | ||
55 | + public void testPushAttributesOnShortJsonTopic() throws Exception { | ||
56 | + super.testPushAttributesOnShortJsonTopic(); | ||
57 | + } | ||
58 | + | ||
59 | + @Test | ||
49 | public void testPushAttributesGateway() throws Exception { | 60 | public void testPushAttributesGateway() throws Exception { |
50 | - List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
51 | - String deviceName1 = "Device A"; | ||
52 | - String deviceName2 = "Device B"; | ||
53 | - String payload = getGatewayAttributesJsonPayload(deviceName1, deviceName2); | ||
54 | - processGatewayAttributesTest(expectedKeys, payload.getBytes(), deviceName1, deviceName2); | 61 | + super.testPushAttributesGateway(); |
55 | } | 62 | } |
56 | } | 63 | } |
@@ -25,6 +25,7 @@ import org.junit.Test; | @@ -25,6 +25,7 @@ import org.junit.Test; | ||
25 | import org.thingsboard.server.common.data.TransportPayloadType; | 25 | import org.thingsboard.server.common.data.TransportPayloadType; |
26 | import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration; | 26 | import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration; |
27 | import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; | 27 | import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; |
28 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | ||
28 | import org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration; | 29 | import org.thingsboard.server.common.data.device.profile.ProtoTransportPayloadConfiguration; |
29 | import org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration; | 30 | import org.thingsboard.server.common.data.device.profile.TransportPayloadTypeConfiguration; |
30 | import org.thingsboard.server.gen.transport.TransportApiProtos; | 31 | import org.thingsboard.server.gen.transport.TransportApiProtos; |
@@ -49,14 +50,14 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -49,14 +50,14 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
49 | @Test | 50 | @Test |
50 | public void testPushAttributes() throws Exception { | 51 | public void testPushAttributes() throws Exception { |
51 | super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); | 52 | super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); |
52 | - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
53 | - assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
54 | - MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
55 | - TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
56 | - assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
57 | - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
58 | - ProtoFileElement transportProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_ATTRIBUTES_PROTO_SCHEMA); | ||
59 | - DynamicSchema attributesSchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchemaFile, ProtoTransportPayloadConfiguration.ATTRIBUTES_PROTO_SCHEMA); | 53 | + DynamicMessage postAttributesMsg = getDefaultDynamicMessage(); |
54 | + processAttributesTest(POST_DATA_ATTRIBUTES_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postAttributesMsg.toByteArray(), false); | ||
55 | + } | ||
56 | + | ||
57 | + @Test | ||
58 | + public void testPushAttributesWithExplicitPresenceProtoKeys() throws Exception { | ||
59 | + super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); | ||
60 | + DynamicSchema attributesSchema = getDynamicSchema(); | ||
60 | 61 | ||
61 | DynamicMessage.Builder nestedJsonObjectBuilder = attributesSchema.newMessageBuilder("PostAttributes.JsonObject.NestedJsonObject"); | 62 | DynamicMessage.Builder nestedJsonObjectBuilder = attributesSchema.newMessageBuilder("PostAttributes.JsonObject.NestedJsonObject"); |
62 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | 63 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); |
@@ -67,7 +68,6 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -67,7 +68,6 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
67 | Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); | 68 | Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); |
68 | assertNotNull(jsonObjectBuilderDescriptor); | 69 | assertNotNull(jsonObjectBuilderDescriptor); |
69 | DynamicMessage jsonObject = jsonObjectBuilder | 70 | DynamicMessage jsonObject = jsonObjectBuilder |
70 | - .setField(jsonObjectBuilderDescriptor.findFieldByName("someNumber"), 42) | ||
71 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) | 71 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) |
72 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) | 72 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) |
73 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) | 73 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) |
@@ -78,18 +78,50 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -78,18 +78,50 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
78 | Descriptors.Descriptor postAttributesMsgDescriptor = postAttributesBuilder.getDescriptorForType(); | 78 | Descriptors.Descriptor postAttributesMsgDescriptor = postAttributesBuilder.getDescriptorForType(); |
79 | assertNotNull(postAttributesMsgDescriptor); | 79 | assertNotNull(postAttributesMsgDescriptor); |
80 | DynamicMessage postAttributesMsg = postAttributesBuilder | 80 | DynamicMessage postAttributesMsg = postAttributesBuilder |
81 | - .setField(postAttributesMsgDescriptor.findFieldByName("key1"), "value1") | ||
82 | - .setField(postAttributesMsgDescriptor.findFieldByName("key2"), true) | ||
83 | - .setField(postAttributesMsgDescriptor.findFieldByName("key3"), 3.0) | ||
84 | - .setField(postAttributesMsgDescriptor.findFieldByName("key4"), 4) | 81 | + .setField(postAttributesMsgDescriptor.findFieldByName("key1"), "") |
82 | + .setField(postAttributesMsgDescriptor.findFieldByName("key2"), false) | ||
83 | + .setField(postAttributesMsgDescriptor.findFieldByName("key3"), 0.0) | ||
84 | + .setField(postAttributesMsgDescriptor.findFieldByName("key4"), 0) | ||
85 | .setField(postAttributesMsgDescriptor.findFieldByName("key5"), jsonObject) | 85 | .setField(postAttributesMsgDescriptor.findFieldByName("key5"), jsonObject) |
86 | .build(); | 86 | .build(); |
87 | - processAttributesTest(POST_DATA_ATTRIBUTES_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postAttributesMsg.toByteArray(), false); | 87 | + processAttributesTest(POST_DATA_ATTRIBUTES_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postAttributesMsg.toByteArray(), true); |
88 | } | 88 | } |
89 | 89 | ||
90 | @Test | 90 | @Test |
91 | - public void testPushAttributesWithExplicitPresenceProtoKeys() throws Exception { | 91 | + public void testPushAttributesOnShortTopic() throws Exception { |
92 | super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); | 92 | super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); |
93 | + DynamicMessage postAttributesMsg = getDefaultDynamicMessage(); | ||
94 | + processAttributesTest(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postAttributesMsg.toByteArray(), false); | ||
95 | + } | ||
96 | + | ||
97 | + @Test | ||
98 | + public void testPushAttributesOnShortJsonTopic() throws Exception { | ||
99 | + super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); | ||
100 | + processJsonPayloadAttributesTest(MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), PAYLOAD_VALUES_STR.getBytes()); | ||
101 | + } | ||
102 | + | ||
103 | + @Test | ||
104 | + public void testPushAttributesOnShortProtoTopic() throws Exception { | ||
105 | + super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, POST_DATA_ATTRIBUTES_TOPIC); | ||
106 | + DynamicMessage postAttributesMsg = getDefaultDynamicMessage(); | ||
107 | + processAttributesTest(MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postAttributesMsg.toByteArray(), false); | ||
108 | + } | ||
109 | + | ||
110 | + @Test | ||
111 | + public void testPushAttributesGateway() throws Exception { | ||
112 | + super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, null); | ||
113 | + TransportApiProtos.GatewayAttributesMsg.Builder gatewayAttributesMsgProtoBuilder = TransportApiProtos.GatewayAttributesMsg.newBuilder(); | ||
114 | + List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
115 | + String deviceName1 = "Device A"; | ||
116 | + String deviceName2 = "Device B"; | ||
117 | + TransportApiProtos.AttributesMsg firstDeviceAttributesMsgProto = getDeviceAttributesMsgProto(deviceName1, expectedKeys); | ||
118 | + TransportApiProtos.AttributesMsg secondDeviceAttributesMsgProto = getDeviceAttributesMsgProto(deviceName2, expectedKeys); | ||
119 | + gatewayAttributesMsgProtoBuilder.addAllMsg(Arrays.asList(firstDeviceAttributesMsgProto, secondDeviceAttributesMsgProto)); | ||
120 | + TransportApiProtos.GatewayAttributesMsg gatewayAttributesMsg = gatewayAttributesMsgProtoBuilder.build(); | ||
121 | + processGatewayAttributesTest(expectedKeys, gatewayAttributesMsg.toByteArray(), deviceName1, deviceName2); | ||
122 | + } | ||
123 | + | ||
124 | + private DynamicSchema getDynamicSchema() { | ||
93 | DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | 125 | DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); |
94 | assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | 126 | assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); |
95 | MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | 127 | MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; |
@@ -97,7 +129,11 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -97,7 +129,11 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
97 | assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | 129 | assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); |
98 | ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | 130 | ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; |
99 | ProtoFileElement transportProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_ATTRIBUTES_PROTO_SCHEMA); | 131 | ProtoFileElement transportProtoSchemaFile = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_ATTRIBUTES_PROTO_SCHEMA); |
100 | - DynamicSchema attributesSchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchemaFile, ProtoTransportPayloadConfiguration.ATTRIBUTES_PROTO_SCHEMA); | 132 | + return protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchemaFile, ProtoTransportPayloadConfiguration.ATTRIBUTES_PROTO_SCHEMA); |
133 | + } | ||
134 | + | ||
135 | + private DynamicMessage getDefaultDynamicMessage() { | ||
136 | + DynamicSchema attributesSchema = getDynamicSchema(); | ||
101 | 137 | ||
102 | DynamicMessage.Builder nestedJsonObjectBuilder = attributesSchema.newMessageBuilder("PostAttributes.JsonObject.NestedJsonObject"); | 138 | DynamicMessage.Builder nestedJsonObjectBuilder = attributesSchema.newMessageBuilder("PostAttributes.JsonObject.NestedJsonObject"); |
103 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | 139 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); |
@@ -108,6 +144,7 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -108,6 +144,7 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
108 | Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); | 144 | Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); |
109 | assertNotNull(jsonObjectBuilderDescriptor); | 145 | assertNotNull(jsonObjectBuilderDescriptor); |
110 | DynamicMessage jsonObject = jsonObjectBuilder | 146 | DynamicMessage jsonObject = jsonObjectBuilder |
147 | + .setField(jsonObjectBuilderDescriptor.findFieldByName("someNumber"), 42) | ||
111 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) | 148 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) |
112 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) | 149 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) |
113 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) | 150 | .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) |
@@ -117,25 +154,13 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | @@ -117,25 +154,13 @@ public abstract class AbstractMqttAttributesProtoIntegrationTest extends Abstrac | ||
117 | DynamicMessage.Builder postAttributesBuilder = attributesSchema.newMessageBuilder("PostAttributes"); | 154 | DynamicMessage.Builder postAttributesBuilder = attributesSchema.newMessageBuilder("PostAttributes"); |
118 | Descriptors.Descriptor postAttributesMsgDescriptor = postAttributesBuilder.getDescriptorForType(); | 155 | Descriptors.Descriptor postAttributesMsgDescriptor = postAttributesBuilder.getDescriptorForType(); |
119 | assertNotNull(postAttributesMsgDescriptor); | 156 | assertNotNull(postAttributesMsgDescriptor); |
120 | - DynamicMessage postAttributesMsg = postAttributesBuilder | ||
121 | - .setField(postAttributesMsgDescriptor.findFieldByName("key1"), "") | 157 | + return postAttributesBuilder |
158 | + .setField(postAttributesMsgDescriptor.findFieldByName("key1"), "value1") | ||
159 | + .setField(postAttributesMsgDescriptor.findFieldByName("key2"), true) | ||
160 | + .setField(postAttributesMsgDescriptor.findFieldByName("key3"), 3.0) | ||
161 | + .setField(postAttributesMsgDescriptor.findFieldByName("key4"), 4) | ||
122 | .setField(postAttributesMsgDescriptor.findFieldByName("key5"), jsonObject) | 162 | .setField(postAttributesMsgDescriptor.findFieldByName("key5"), jsonObject) |
123 | .build(); | 163 | .build(); |
124 | - processAttributesTest(POST_DATA_ATTRIBUTES_TOPIC, Arrays.asList("key1", "key5"), postAttributesMsg.toByteArray(), true); | ||
125 | - } | ||
126 | - | ||
127 | - @Test | ||
128 | - public void testPushAttributesGateway() throws Exception { | ||
129 | - super.processBeforeTest("Test Post Attributes device", "Test Post Attributes gateway", TransportPayloadType.PROTOBUF, null, null); | ||
130 | - TransportApiProtos.GatewayAttributesMsg.Builder gatewayAttributesMsgProtoBuilder = TransportApiProtos.GatewayAttributesMsg.newBuilder(); | ||
131 | - List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
132 | - String deviceName1 = "Device A"; | ||
133 | - String deviceName2 = "Device B"; | ||
134 | - TransportApiProtos.AttributesMsg firstDeviceAttributesMsgProto = getDeviceAttributesMsgProto(deviceName1, expectedKeys); | ||
135 | - TransportApiProtos.AttributesMsg secondDeviceAttributesMsgProto = getDeviceAttributesMsgProto(deviceName2, expectedKeys); | ||
136 | - gatewayAttributesMsgProtoBuilder.addAllMsg(Arrays.asList(firstDeviceAttributesMsgProto, secondDeviceAttributesMsgProto)); | ||
137 | - TransportApiProtos.GatewayAttributesMsg gatewayAttributesMsg = gatewayAttributesMsgProtoBuilder.build(); | ||
138 | - processGatewayAttributesTest(expectedKeys, gatewayAttributesMsg.toByteArray(), deviceName1, deviceName2); | ||
139 | } | 164 | } |
140 | 165 | ||
141 | private TransportApiProtos.AttributesMsg getDeviceAttributesMsgProto(String deviceName, List<String> expectedKeys) { | 166 | private TransportApiProtos.AttributesMsg getDeviceAttributesMsgProto(String deviceName, List<String> expectedKeys) { |
@@ -74,6 +74,18 @@ public abstract class AbstractMqttTimeseriesIntegrationTest extends AbstractMqtt | @@ -74,6 +74,18 @@ public abstract class AbstractMqttTimeseriesIntegrationTest extends AbstractMqtt | ||
74 | } | 74 | } |
75 | 75 | ||
76 | @Test | 76 | @Test |
77 | + public void testPushTelemetryOnShortTopic() throws Exception { | ||
78 | + List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
79 | + processJsonPayloadTelemetryTest(MqttTopics.DEVICE_TELEMETRY_SHORT_TOPIC, expectedKeys, PAYLOAD_VALUES_STR.getBytes(), false); | ||
80 | + } | ||
81 | + | ||
82 | + @Test | ||
83 | + public void testPushTelemetryOnShortJsonTopic() throws Exception { | ||
84 | + List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
85 | + processJsonPayloadTelemetryTest(MqttTopics.DEVICE_TELEMETRY_SHORT_JSON_TOPIC, expectedKeys, PAYLOAD_VALUES_STR.getBytes(), false); | ||
86 | + } | ||
87 | + | ||
88 | + @Test | ||
77 | public void testPushTelemetryGateway() throws Exception { | 89 | public void testPushTelemetryGateway() throws Exception { |
78 | List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | 90 | List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); |
79 | String deviceName1 = "Device A"; | 91 | String deviceName1 = "Device A"; |
@@ -58,24 +58,22 @@ public abstract class AbstractMqttTimeseriesJsonIntegrationTest extends Abstract | @@ -58,24 +58,22 @@ public abstract class AbstractMqttTimeseriesJsonIntegrationTest extends Abstract | ||
58 | } | 58 | } |
59 | 59 | ||
60 | @Test | 60 | @Test |
61 | + public void testPushTelemetryOnShortTopic() throws Exception { | ||
62 | + super.testPushTelemetryOnShortTopic(); | ||
63 | + } | ||
64 | + | ||
65 | + @Test | ||
66 | + public void testPushTelemetryWithTsOnShortJsonTopic() throws Exception { | ||
67 | + super.testPushTelemetryOnShortJsonTopic(); | ||
68 | + } | ||
69 | + | ||
70 | + @Test | ||
61 | public void testPushTelemetryGateway() throws Exception { | 71 | public void testPushTelemetryGateway() throws Exception { |
62 | - List<String> expectedKeys = Arrays.asList("key1", "key2", "key3", "key4", "key5"); | ||
63 | - String deviceName1 = "Device A"; | ||
64 | - String deviceName2 = "Device B"; | ||
65 | - String payload = getGatewayTelemetryJsonPayload(deviceName1, deviceName2, "10000", "20000"); | ||
66 | - processGatewayTelemetryTest(MqttTopics.GATEWAY_TELEMETRY_TOPIC, expectedKeys, payload.getBytes(), deviceName1, deviceName2); | 72 | + super.testPushTelemetryGateway(); |
67 | } | 73 | } |
68 | 74 | ||
69 | @Test | 75 | @Test |
70 | public void testGatewayConnect() throws Exception { | 76 | public void testGatewayConnect() throws Exception { |
71 | - String payload = "{\"device\":\"Device A\", \"type\": \"" + TransportPayloadType.JSON.name() + "\"}"; | ||
72 | - MqttAsyncClient client = getMqttAsyncClient(gatewayAccessToken); | ||
73 | - publishMqttMsg(client, payload.getBytes(), MqttTopics.GATEWAY_CONNECT_TOPIC); | ||
74 | - | ||
75 | - String deviceName = "Device A"; | ||
76 | - Device device = doExecuteWithRetriesAndInterval(() -> doGet("/api/tenant/devices?deviceName=" + deviceName, Device.class), | ||
77 | - 20, | ||
78 | - 100); | ||
79 | - assertNotNull(device); | 77 | + super.testGatewayConnect(); |
80 | } | 78 | } |
81 | } | 79 | } |
@@ -21,6 +21,7 @@ import com.google.protobuf.DynamicMessage; | @@ -21,6 +21,7 @@ import com.google.protobuf.DynamicMessage; | ||
21 | import com.squareup.wire.schema.internal.parser.ProtoFileElement; | 21 | import com.squareup.wire.schema.internal.parser.ProtoFileElement; |
22 | import lombok.extern.slf4j.Slf4j; | 22 | import lombok.extern.slf4j.Slf4j; |
23 | import org.eclipse.paho.client.mqttv3.MqttAsyncClient; | 23 | import org.eclipse.paho.client.mqttv3.MqttAsyncClient; |
24 | +import org.jetbrains.annotations.NotNull; | ||
24 | import org.junit.After; | 25 | import org.junit.After; |
25 | import org.junit.Ignore; | 26 | import org.junit.Ignore; |
26 | import org.junit.Test; | 27 | import org.junit.Test; |
@@ -55,41 +56,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -55,41 +56,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
55 | @Test | 56 | @Test |
56 | public void testPushTelemetry() throws Exception { | 57 | public void testPushTelemetry() throws Exception { |
57 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); | 58 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); |
58 | - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
59 | - assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
60 | - MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
61 | - TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
62 | - assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
63 | - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
64 | - ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_TELEMETRY_PROTO_SCHEMA); | ||
65 | - DynamicSchema telemetrySchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema"); | ||
66 | - | ||
67 | - DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); | ||
68 | - Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | ||
69 | - assertNotNull(nestedJsonObjectBuilderDescriptor); | ||
70 | - DynamicMessage nestedJsonObject = nestedJsonObjectBuilder.setField(nestedJsonObjectBuilderDescriptor.findFieldByName("key"), "value").build(); | ||
71 | - | ||
72 | - DynamicMessage.Builder jsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject"); | ||
73 | - Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); | ||
74 | - assertNotNull(jsonObjectBuilderDescriptor); | ||
75 | - DynamicMessage jsonObject = jsonObjectBuilder | ||
76 | - .setField(jsonObjectBuilderDescriptor.findFieldByName("someNumber"), 42) | ||
77 | - .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) | ||
78 | - .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) | ||
79 | - .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) | ||
80 | - .setField(jsonObjectBuilderDescriptor.findFieldByName("someNestedObject"), nestedJsonObject) | ||
81 | - .build(); | ||
82 | - | ||
83 | - DynamicMessage.Builder postTelemetryBuilder = telemetrySchema.newMessageBuilder("PostTelemetry"); | ||
84 | - Descriptors.Descriptor postTelemetryMsgDescriptor = postTelemetryBuilder.getDescriptorForType(); | ||
85 | - assertNotNull(postTelemetryMsgDescriptor); | ||
86 | - DynamicMessage postTelemetryMsg = postTelemetryBuilder | ||
87 | - .setField(postTelemetryMsgDescriptor.findFieldByName("key1"), "value1") | ||
88 | - .setField(postTelemetryMsgDescriptor.findFieldByName("key2"), true) | ||
89 | - .setField(postTelemetryMsgDescriptor.findFieldByName("key3"), 3.0) | ||
90 | - .setField(postTelemetryMsgDescriptor.findFieldByName("key4"), 4) | ||
91 | - .setField(postTelemetryMsgDescriptor.findFieldByName("key5"), jsonObject) | ||
92 | - .build(); | 59 | + DynamicMessage postTelemetryMsg = getDefaultDynamicMessage(); |
93 | processTelemetryTest(POST_DATA_TELEMETRY_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postTelemetryMsg.toByteArray(), false, false); | 60 | processTelemetryTest(POST_DATA_TELEMETRY_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postTelemetryMsg.toByteArray(), false, false); |
94 | } | 61 | } |
95 | 62 | ||
@@ -121,14 +88,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -121,14 +88,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
121 | " }\n" + | 88 | " }\n" + |
122 | "}"; | 89 | "}"; |
123 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null, schemaStr, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); | 90 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null, schemaStr, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); |
124 | - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
125 | - assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
126 | - MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
127 | - TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
128 | - assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
129 | - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
130 | - ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(schemaStr); | ||
131 | - DynamicSchema telemetrySchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema"); | 91 | + DynamicSchema telemetrySchema = getDynamicSchema(schemaStr); |
132 | 92 | ||
133 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); | 93 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); |
134 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | 94 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); |
@@ -173,14 +133,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -173,14 +133,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
173 | @Test | 133 | @Test |
174 | public void testPushTelemetryWithExplicitPresenceProtoKeys() throws Exception { | 134 | public void testPushTelemetryWithExplicitPresenceProtoKeys() throws Exception { |
175 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); | 135 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); |
176 | - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
177 | - assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
178 | - MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
179 | - TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
180 | - assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
181 | - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
182 | - ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(DEVICE_TELEMETRY_PROTO_SCHEMA); | ||
183 | - DynamicSchema telemetrySchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema"); | 136 | + DynamicSchema telemetrySchema = getDynamicSchema(DEVICE_TELEMETRY_PROTO_SCHEMA); |
184 | 137 | ||
185 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); | 138 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); |
186 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | 139 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); |
@@ -237,14 +190,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -237,14 +190,7 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
237 | " }\n" + | 190 | " }\n" + |
238 | "}"; | 191 | "}"; |
239 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null, schemaStr, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); | 192 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null, schemaStr, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); |
240 | - DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
241 | - assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
242 | - MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
243 | - TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
244 | - assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
245 | - ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
246 | - ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(schemaStr); | ||
247 | - DynamicSchema telemetrySchema = protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema"); | 193 | + DynamicSchema telemetrySchema = getDynamicSchema(schemaStr); |
248 | 194 | ||
249 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); | 195 | DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); |
250 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | 196 | Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); |
@@ -282,6 +228,26 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -282,6 +228,26 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
282 | } | 228 | } |
283 | 229 | ||
284 | @Test | 230 | @Test |
231 | + public void testPushTelemetryOnShortTopic() throws Exception { | ||
232 | + super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); | ||
233 | + DynamicMessage postTelemetryMsg = getDefaultDynamicMessage(); | ||
234 | + processTelemetryTest(MqttTopics.DEVICE_TELEMETRY_SHORT_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postTelemetryMsg.toByteArray(), false, false); | ||
235 | + } | ||
236 | + | ||
237 | + @Test | ||
238 | + public void testPushTelemetryOnShortJsonTopic() throws Exception { | ||
239 | + super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); | ||
240 | + processJsonPayloadTelemetryTest(MqttTopics.DEVICE_TELEMETRY_SHORT_JSON_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), PAYLOAD_VALUES_STR.getBytes(), false); | ||
241 | + } | ||
242 | + | ||
243 | + @Test | ||
244 | + public void testPushTelemetryOnShortProtoTopic() throws Exception { | ||
245 | + super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, POST_DATA_TELEMETRY_TOPIC, null); | ||
246 | + DynamicMessage postTelemetryMsg = getDefaultDynamicMessage(); | ||
247 | + processTelemetryTest(MqttTopics.DEVICE_TELEMETRY_SHORT_PROTO_TOPIC, Arrays.asList("key1", "key2", "key3", "key4", "key5"), postTelemetryMsg.toByteArray(), false, false); | ||
248 | + } | ||
249 | + | ||
250 | + @Test | ||
285 | public void testPushTelemetryGateway() throws Exception { | 251 | public void testPushTelemetryGateway() throws Exception { |
286 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, null, null, null, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); | 252 | super.processBeforeTest("Test Post Telemetry device proto payload", "Test Post Telemetry gateway proto payload", TransportPayloadType.PROTOBUF, null, null, null, null, null, null, null, null, DeviceProfileProvisionType.DISABLED); |
287 | TransportApiProtos.GatewayTelemetryMsg.Builder gatewayTelemetryMsgProtoBuilder = TransportApiProtos.GatewayTelemetryMsg.newBuilder(); | 253 | TransportApiProtos.GatewayTelemetryMsg.Builder gatewayTelemetryMsgProtoBuilder = TransportApiProtos.GatewayTelemetryMsg.newBuilder(); |
@@ -310,6 +276,48 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | @@ -310,6 +276,48 @@ public abstract class AbstractMqttTimeseriesProtoIntegrationTest extends Abstrac | ||
310 | assertNotNull(device); | 276 | assertNotNull(device); |
311 | } | 277 | } |
312 | 278 | ||
279 | + private DynamicSchema getDynamicSchema(String deviceTelemetryProtoSchema) { | ||
280 | + DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration(); | ||
281 | + assertTrue(transportConfiguration instanceof MqttDeviceProfileTransportConfiguration); | ||
282 | + MqttDeviceProfileTransportConfiguration mqttTransportConfiguration = (MqttDeviceProfileTransportConfiguration) transportConfiguration; | ||
283 | + TransportPayloadTypeConfiguration transportPayloadTypeConfiguration = mqttTransportConfiguration.getTransportPayloadTypeConfiguration(); | ||
284 | + assertTrue(transportPayloadTypeConfiguration instanceof ProtoTransportPayloadConfiguration); | ||
285 | + ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = (ProtoTransportPayloadConfiguration) transportPayloadTypeConfiguration; | ||
286 | + ProtoFileElement transportProtoSchema = protoTransportPayloadConfiguration.getTransportProtoSchema(deviceTelemetryProtoSchema); | ||
287 | + return protoTransportPayloadConfiguration.getDynamicSchema(transportProtoSchema, "telemetrySchema"); | ||
288 | + } | ||
289 | + | ||
290 | + private DynamicMessage getDefaultDynamicMessage() { | ||
291 | + DynamicSchema telemetrySchema = getDynamicSchema(DEVICE_TELEMETRY_PROTO_SCHEMA); | ||
292 | + | ||
293 | + DynamicMessage.Builder nestedJsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject.NestedJsonObject"); | ||
294 | + Descriptors.Descriptor nestedJsonObjectBuilderDescriptor = nestedJsonObjectBuilder.getDescriptorForType(); | ||
295 | + assertNotNull(nestedJsonObjectBuilderDescriptor); | ||
296 | + DynamicMessage nestedJsonObject = nestedJsonObjectBuilder.setField(nestedJsonObjectBuilderDescriptor.findFieldByName("key"), "value").build(); | ||
297 | + | ||
298 | + DynamicMessage.Builder jsonObjectBuilder = telemetrySchema.newMessageBuilder("PostTelemetry.JsonObject"); | ||
299 | + Descriptors.Descriptor jsonObjectBuilderDescriptor = jsonObjectBuilder.getDescriptorForType(); | ||
300 | + assertNotNull(jsonObjectBuilderDescriptor); | ||
301 | + DynamicMessage jsonObject = jsonObjectBuilder | ||
302 | + .setField(jsonObjectBuilderDescriptor.findFieldByName("someNumber"), 42) | ||
303 | + .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 1) | ||
304 | + .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 2) | ||
305 | + .addRepeatedField(jsonObjectBuilderDescriptor.findFieldByName("someArray"), 3) | ||
306 | + .setField(jsonObjectBuilderDescriptor.findFieldByName("someNestedObject"), nestedJsonObject) | ||
307 | + .build(); | ||
308 | + | ||
309 | + DynamicMessage.Builder postTelemetryBuilder = telemetrySchema.newMessageBuilder("PostTelemetry"); | ||
310 | + Descriptors.Descriptor postTelemetryMsgDescriptor = postTelemetryBuilder.getDescriptorForType(); | ||
311 | + assertNotNull(postTelemetryMsgDescriptor); | ||
312 | + return postTelemetryBuilder | ||
313 | + .setField(postTelemetryMsgDescriptor.findFieldByName("key1"), "value1") | ||
314 | + .setField(postTelemetryMsgDescriptor.findFieldByName("key2"), true) | ||
315 | + .setField(postTelemetryMsgDescriptor.findFieldByName("key3"), 3.0) | ||
316 | + .setField(postTelemetryMsgDescriptor.findFieldByName("key4"), 4) | ||
317 | + .setField(postTelemetryMsgDescriptor.findFieldByName("key5"), jsonObject) | ||
318 | + .build(); | ||
319 | + } | ||
320 | + | ||
313 | private TransportApiProtos.ConnectMsg getConnectProto(String deviceName) { | 321 | private TransportApiProtos.ConnectMsg getConnectProto(String deviceName) { |
314 | TransportApiProtos.ConnectMsg.Builder builder = TransportApiProtos.ConnectMsg.newBuilder(); | 322 | TransportApiProtos.ConnectMsg.Builder builder = TransportApiProtos.ConnectMsg.newBuilder(); |
315 | builder.setDeviceName(deviceName); | 323 | builder.setDeviceName(deviceName); |
@@ -34,20 +34,25 @@ public class MqttTopics { | @@ -34,20 +34,25 @@ public class MqttTopics { | ||
34 | private static final String SOFTWARE = "/sw"; | 34 | private static final String SOFTWARE = "/sw"; |
35 | private static final String CHUNK = "/chunk/"; | 35 | private static final String CHUNK = "/chunk/"; |
36 | private static final String ERROR = "/error"; | 36 | private static final String ERROR = "/error"; |
37 | - | 37 | + private static final String TELEMETRY_SHORT = "/t"; |
38 | + private static final String ATTRIBUTES_SHORT = "/a"; | ||
39 | + private static final String RPC_SHORT = "/r"; | ||
40 | + private static final String REQUEST_SHORT = "/req"; | ||
41 | + private static final String RESPONSE_SHORT = "/res"; | ||
42 | + private static final String JSON_SHORT = "j"; | ||
43 | + private static final String PROTO_SHORT = "p"; | ||
38 | private static final String ATTRIBUTES_RESPONSE = ATTRIBUTES + RESPONSE; | 44 | private static final String ATTRIBUTES_RESPONSE = ATTRIBUTES + RESPONSE; |
39 | private static final String ATTRIBUTES_REQUEST = ATTRIBUTES + REQUEST; | 45 | private static final String ATTRIBUTES_REQUEST = ATTRIBUTES + REQUEST; |
40 | - | 46 | + private static final String ATTRIBUTES_RESPONSE_SHORT = ATTRIBUTES_SHORT + RESPONSE_SHORT + "/"; |
47 | + private static final String ATTRIBUTES_REQUEST_SHORT = ATTRIBUTES_SHORT + REQUEST_SHORT + "/"; | ||
41 | private static final String DEVICE_RPC_RESPONSE = RPC + RESPONSE + "/"; | 48 | private static final String DEVICE_RPC_RESPONSE = RPC + RESPONSE + "/"; |
42 | private static final String DEVICE_RPC_REQUEST = RPC + REQUEST + "/"; | 49 | private static final String DEVICE_RPC_REQUEST = RPC + REQUEST + "/"; |
43 | - | 50 | + private static final String DEVICE_RPC_RESPONSE_SHORT = RPC_SHORT + RESPONSE_SHORT + "/"; |
51 | + private static final String DEVICE_RPC_REQUEST_SHORT = RPC_SHORT + REQUEST_SHORT + "/"; | ||
44 | private static final String DEVICE_ATTRIBUTES_RESPONSE = ATTRIBUTES_RESPONSE + "/"; | 52 | private static final String DEVICE_ATTRIBUTES_RESPONSE = ATTRIBUTES_RESPONSE + "/"; |
45 | private static final String DEVICE_ATTRIBUTES_REQUEST = ATTRIBUTES_REQUEST + "/"; | 53 | private static final String DEVICE_ATTRIBUTES_REQUEST = ATTRIBUTES_REQUEST + "/"; |
46 | - | ||
47 | - // V1_JSON topics | ||
48 | - | 54 | + // v1 topics |
49 | public static final String BASE_DEVICE_API_TOPIC = "v1/devices/me"; | 55 | public static final String BASE_DEVICE_API_TOPIC = "v1/devices/me"; |
50 | - | ||
51 | public static final String DEVICE_RPC_RESPONSE_TOPIC = BASE_DEVICE_API_TOPIC + DEVICE_RPC_RESPONSE; | 56 | public static final String DEVICE_RPC_RESPONSE_TOPIC = BASE_DEVICE_API_TOPIC + DEVICE_RPC_RESPONSE; |
52 | public static final String DEVICE_RPC_RESPONSE_SUB_TOPIC = DEVICE_RPC_RESPONSE_TOPIC + SUB_TOPIC; | 57 | public static final String DEVICE_RPC_RESPONSE_SUB_TOPIC = DEVICE_RPC_RESPONSE_TOPIC + SUB_TOPIC; |
53 | public static final String DEVICE_RPC_REQUESTS_TOPIC = BASE_DEVICE_API_TOPIC + DEVICE_RPC_REQUEST; | 58 | public static final String DEVICE_RPC_REQUESTS_TOPIC = BASE_DEVICE_API_TOPIC + DEVICE_RPC_REQUEST; |
@@ -60,9 +65,7 @@ public class MqttTopics { | @@ -60,9 +65,7 @@ public class MqttTopics { | ||
60 | public static final String DEVICE_ATTRIBUTES_TOPIC = BASE_DEVICE_API_TOPIC + ATTRIBUTES; | 65 | public static final String DEVICE_ATTRIBUTES_TOPIC = BASE_DEVICE_API_TOPIC + ATTRIBUTES; |
61 | public static final String DEVICE_PROVISION_REQUEST_TOPIC = PROVISION + REQUEST; | 66 | public static final String DEVICE_PROVISION_REQUEST_TOPIC = PROVISION + REQUEST; |
62 | public static final String DEVICE_PROVISION_RESPONSE_TOPIC = PROVISION + RESPONSE; | 67 | public static final String DEVICE_PROVISION_RESPONSE_TOPIC = PROVISION + RESPONSE; |
63 | - | ||
64 | - // V1_JSON gateway topics | ||
65 | - | 68 | + // v1 gateway topics |
66 | public static final String BASE_GATEWAY_API_TOPIC = "v1/gateway"; | 69 | public static final String BASE_GATEWAY_API_TOPIC = "v1/gateway"; |
67 | public static final String GATEWAY_CONNECT_TOPIC = BASE_GATEWAY_API_TOPIC + CONNECT; | 70 | public static final String GATEWAY_CONNECT_TOPIC = BASE_GATEWAY_API_TOPIC + CONNECT; |
68 | public static final String GATEWAY_DISCONNECT_TOPIC = BASE_GATEWAY_API_TOPIC + DISCONNECT; | 71 | public static final String GATEWAY_DISCONNECT_TOPIC = BASE_GATEWAY_API_TOPIC + DISCONNECT; |
@@ -72,22 +75,44 @@ public class MqttTopics { | @@ -72,22 +75,44 @@ public class MqttTopics { | ||
72 | public static final String GATEWAY_RPC_TOPIC = BASE_GATEWAY_API_TOPIC + RPC; | 75 | public static final String GATEWAY_RPC_TOPIC = BASE_GATEWAY_API_TOPIC + RPC; |
73 | public static final String GATEWAY_ATTRIBUTES_REQUEST_TOPIC = BASE_GATEWAY_API_TOPIC + ATTRIBUTES_REQUEST; | 76 | public static final String GATEWAY_ATTRIBUTES_REQUEST_TOPIC = BASE_GATEWAY_API_TOPIC + ATTRIBUTES_REQUEST; |
74 | public static final String GATEWAY_ATTRIBUTES_RESPONSE_TOPIC = BASE_GATEWAY_API_TOPIC + ATTRIBUTES_RESPONSE; | 77 | public static final String GATEWAY_ATTRIBUTES_RESPONSE_TOPIC = BASE_GATEWAY_API_TOPIC + ATTRIBUTES_RESPONSE; |
75 | - | ||
76 | // v2 topics | 78 | // v2 topics |
77 | public static final String BASE_DEVICE_API_TOPIC_V2 = "v2"; | 79 | public static final String BASE_DEVICE_API_TOPIC_V2 = "v2"; |
78 | - | ||
79 | public static final String REQUEST_ID_PATTERN = "(?<requestId>\\d+)"; | 80 | public static final String REQUEST_ID_PATTERN = "(?<requestId>\\d+)"; |
80 | public static final String CHUNK_PATTERN = "(?<chunk>\\d+)"; | 81 | public static final String CHUNK_PATTERN = "(?<chunk>\\d+)"; |
81 | - | ||
82 | public static final String DEVICE_FIRMWARE_REQUEST_TOPIC_PATTERN = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + REQUEST + "/" + REQUEST_ID_PATTERN + CHUNK + CHUNK_PATTERN; | 82 | public static final String DEVICE_FIRMWARE_REQUEST_TOPIC_PATTERN = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + REQUEST + "/" + REQUEST_ID_PATTERN + CHUNK + CHUNK_PATTERN; |
83 | public static final String DEVICE_FIRMWARE_RESPONSES_TOPIC = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + RESPONSE + "/" + SUB_TOPIC + CHUNK + SUB_TOPIC; | 83 | public static final String DEVICE_FIRMWARE_RESPONSES_TOPIC = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + RESPONSE + "/" + SUB_TOPIC + CHUNK + SUB_TOPIC; |
84 | public static final String DEVICE_FIRMWARE_ERROR_TOPIC = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + ERROR; | 84 | public static final String DEVICE_FIRMWARE_ERROR_TOPIC = BASE_DEVICE_API_TOPIC_V2 + FIRMWARE + ERROR; |
85 | - | ||
86 | public static final String DEVICE_SOFTWARE_FIRMWARE_RESPONSES_TOPIC_FORMAT = BASE_DEVICE_API_TOPIC_V2 + "/%s" + RESPONSE + "/%s" + CHUNK + "%d"; | 85 | public static final String DEVICE_SOFTWARE_FIRMWARE_RESPONSES_TOPIC_FORMAT = BASE_DEVICE_API_TOPIC_V2 + "/%s" + RESPONSE + "/%s" + CHUNK + "%d"; |
87 | - | ||
88 | public static final String DEVICE_SOFTWARE_REQUEST_TOPIC_PATTERN = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + REQUEST + "/" + REQUEST_ID_PATTERN + CHUNK + CHUNK_PATTERN; | 86 | public static final String DEVICE_SOFTWARE_REQUEST_TOPIC_PATTERN = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + REQUEST + "/" + REQUEST_ID_PATTERN + CHUNK + CHUNK_PATTERN; |
89 | public static final String DEVICE_SOFTWARE_RESPONSES_TOPIC = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + RESPONSE + "/" + SUB_TOPIC + CHUNK + SUB_TOPIC; | 87 | public static final String DEVICE_SOFTWARE_RESPONSES_TOPIC = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + RESPONSE + "/" + SUB_TOPIC + CHUNK + SUB_TOPIC; |
90 | public static final String DEVICE_SOFTWARE_ERROR_TOPIC = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + ERROR; | 88 | public static final String DEVICE_SOFTWARE_ERROR_TOPIC = BASE_DEVICE_API_TOPIC_V2 + SOFTWARE + ERROR; |
89 | + public static final String DEVICE_ATTRIBUTES_SHORT_TOPIC = BASE_DEVICE_API_TOPIC_V2 + ATTRIBUTES_SHORT; | ||
90 | + public static final String DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC = BASE_DEVICE_API_TOPIC_V2 + ATTRIBUTES_SHORT + "/" + JSON_SHORT; | ||
91 | + public static final String DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC = BASE_DEVICE_API_TOPIC_V2 + ATTRIBUTES_SHORT + "/" + PROTO_SHORT; | ||
92 | + public static final String DEVICE_TELEMETRY_SHORT_TOPIC = BASE_DEVICE_API_TOPIC_V2 + TELEMETRY_SHORT; | ||
93 | + public static final String DEVICE_TELEMETRY_SHORT_JSON_TOPIC = BASE_DEVICE_API_TOPIC_V2 + TELEMETRY_SHORT + "/" + JSON_SHORT; | ||
94 | + public static final String DEVICE_TELEMETRY_SHORT_PROTO_TOPIC = BASE_DEVICE_API_TOPIC_V2 + TELEMETRY_SHORT + "/" + PROTO_SHORT; | ||
95 | + public static final String DEVICE_RPC_RESPONSE_SHORT_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_RESPONSE_SHORT; | ||
96 | + public static final String DEVICE_RPC_RESPONSE_SHORT_JSON_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_RESPONSE_SHORT + JSON_SHORT + "/"; | ||
97 | + public static final String DEVICE_RPC_RESPONSE_SHORT_PROTO_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_RESPONSE_SHORT + PROTO_SHORT + "/"; | ||
98 | + public static final String DEVICE_RPC_RESPONSE_SUB_SHORT_TOPIC = DEVICE_RPC_RESPONSE_SHORT_TOPIC + SUB_TOPIC; | ||
99 | + public static final String DEVICE_RPC_RESPONSE_SUB_SHORT_JSON_TOPIC = DEVICE_RPC_RESPONSE_SHORT_TOPIC + JSON_SHORT + "/" + SUB_TOPIC; | ||
100 | + public static final String DEVICE_RPC_RESPONSE_SUB_SHORT_PROTO_TOPIC = DEVICE_RPC_RESPONSE_SHORT_TOPIC + PROTO_SHORT + "/" + SUB_TOPIC; | ||
101 | + public static final String DEVICE_RPC_REQUESTS_SHORT_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_REQUEST_SHORT; | ||
102 | + public static final String DEVICE_RPC_REQUESTS_SHORT_JSON_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_REQUEST_SHORT + JSON_SHORT + "/"; | ||
103 | + public static final String DEVICE_RPC_REQUESTS_SHORT_PROTO_TOPIC = BASE_DEVICE_API_TOPIC_V2 + DEVICE_RPC_REQUEST_SHORT + PROTO_SHORT + "/"; | ||
104 | + public static final String DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC = DEVICE_RPC_REQUESTS_SHORT_TOPIC + SUB_TOPIC; | ||
105 | + public static final String DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC = DEVICE_RPC_REQUESTS_SHORT_TOPIC + JSON_SHORT + "/" + SUB_TOPIC; | ||
106 | + public static final String DEVICE_RPC_REQUESTS_SUB_SHORT_PROTO_TOPIC = DEVICE_RPC_REQUESTS_SHORT_TOPIC + PROTO_SHORT + "/" + SUB_TOPIC; | ||
107 | + public static final String DEVICE_ATTRIBUTES_RESPONSE_SHORT_TOPIC_PREFIX = BASE_DEVICE_API_TOPIC_V2 + ATTRIBUTES_RESPONSE_SHORT; | ||
108 | + public static final String DEVICE_ATTRIBUTES_RESPONSES_SHORT_TOPIC = DEVICE_ATTRIBUTES_RESPONSE_SHORT_TOPIC_PREFIX + SUB_TOPIC; | ||
109 | + public static final String DEVICE_ATTRIBUTES_RESPONSE_SHORT_JSON_TOPIC_PREFIX = DEVICE_ATTRIBUTES_RESPONSE_SHORT_TOPIC_PREFIX + JSON_SHORT + "/"; | ||
110 | + public static final String DEVICE_ATTRIBUTES_RESPONSES_SHORT_JSON_TOPIC = DEVICE_ATTRIBUTES_RESPONSE_SHORT_JSON_TOPIC_PREFIX + SUB_TOPIC; | ||
111 | + public static final String DEVICE_ATTRIBUTES_RESPONSE_SHORT_PROTO_TOPIC_PREFIX = DEVICE_ATTRIBUTES_RESPONSE_SHORT_TOPIC_PREFIX + PROTO_SHORT + "/"; | ||
112 | + public static final String DEVICE_ATTRIBUTES_RESPONSES_SHORT_PROTO_TOPIC = DEVICE_ATTRIBUTES_RESPONSE_SHORT_PROTO_TOPIC_PREFIX + SUB_TOPIC; | ||
113 | + public static final String DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX = BASE_DEVICE_API_TOPIC_V2 + ATTRIBUTES_REQUEST_SHORT; | ||
114 | + public static final String DEVICE_ATTRIBUTES_REQUEST_SHORT_JSON_TOPIC_PREFIX = DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX + JSON_SHORT + "/"; | ||
115 | + public static final String DEVICE_ATTRIBUTES_REQUEST_SHORT_PROTO_TOPIC_PREFIX = DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX + PROTO_SHORT + "/"; | ||
91 | 116 | ||
92 | private MqttTopics() { | 117 | private MqttTopics() { |
93 | } | 118 | } |
@@ -20,7 +20,6 @@ import com.google.gson.JsonParser; | @@ -20,7 +20,6 @@ import com.google.gson.JsonParser; | ||
20 | import com.google.protobuf.Descriptors; | 20 | import com.google.protobuf.Descriptors; |
21 | import com.google.protobuf.DynamicMessage; | 21 | import com.google.protobuf.DynamicMessage; |
22 | import com.google.protobuf.InvalidProtocolBufferException; | 22 | import com.google.protobuf.InvalidProtocolBufferException; |
23 | -import com.google.protobuf.util.JsonFormat; | ||
24 | import lombok.extern.slf4j.Slf4j; | 23 | import lombok.extern.slf4j.Slf4j; |
25 | import org.eclipse.californium.core.coap.CoAP; | 24 | import org.eclipse.californium.core.coap.CoAP; |
26 | import org.eclipse.californium.core.coap.MediaTypeRegistry; | 25 | import org.eclipse.californium.core.coap.MediaTypeRegistry; |
@@ -32,6 +31,7 @@ import org.thingsboard.server.common.data.id.DeviceId; | @@ -32,6 +31,7 @@ import org.thingsboard.server.common.data.id.DeviceId; | ||
32 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; | 31 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
33 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; | 32 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
34 | import org.thingsboard.server.common.transport.adaptor.ProtoConverter; | 33 | import org.thingsboard.server.common.transport.adaptor.ProtoConverter; |
34 | +import org.thingsboard.server.gen.transport.TransportApiProtos; | ||
35 | import org.thingsboard.server.gen.transport.TransportProtos; | 35 | import org.thingsboard.server.gen.transport.TransportProtos; |
36 | import org.thingsboard.server.transport.coap.CoapTransportResource; | 36 | import org.thingsboard.server.transport.coap.CoapTransportResource; |
37 | 37 | ||
@@ -44,8 +44,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | @@ -44,8 +44,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | ||
44 | 44 | ||
45 | @Override | 45 | @Override |
46 | public TransportProtos.PostTelemetryMsg convertToPostTelemetry(UUID sessionId, Request inbound, Descriptors.Descriptor telemetryMsgDescriptor) throws AdaptorException { | 46 | public TransportProtos.PostTelemetryMsg convertToPostTelemetry(UUID sessionId, Request inbound, Descriptors.Descriptor telemetryMsgDescriptor) throws AdaptorException { |
47 | + ProtoConverter.validateDescriptor(telemetryMsgDescriptor); | ||
47 | try { | 48 | try { |
48 | - return JsonConverter.convertToTelemetryProto(new JsonParser().parse(dynamicMsgToJson(inbound.getPayload(), telemetryMsgDescriptor))); | 49 | + return JsonConverter.convertToTelemetryProto(new JsonParser().parse(ProtoConverter.dynamicMsgToJson(inbound.getPayload(), telemetryMsgDescriptor))); |
49 | } catch (Exception e) { | 50 | } catch (Exception e) { |
50 | throw new AdaptorException(e); | 51 | throw new AdaptorException(e); |
51 | } | 52 | } |
@@ -53,8 +54,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | @@ -53,8 +54,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | ||
53 | 54 | ||
54 | @Override | 55 | @Override |
55 | public TransportProtos.PostAttributeMsg convertToPostAttributes(UUID sessionId, Request inbound, Descriptors.Descriptor attributesMsgDescriptor) throws AdaptorException { | 56 | public TransportProtos.PostAttributeMsg convertToPostAttributes(UUID sessionId, Request inbound, Descriptors.Descriptor attributesMsgDescriptor) throws AdaptorException { |
57 | + ProtoConverter.validateDescriptor(attributesMsgDescriptor); | ||
56 | try { | 58 | try { |
57 | - return JsonConverter.convertToAttributesProto(new JsonParser().parse(dynamicMsgToJson(inbound.getPayload(), attributesMsgDescriptor))); | 59 | + return JsonConverter.convertToAttributesProto(new JsonParser().parse(ProtoConverter.dynamicMsgToJson(inbound.getPayload(), attributesMsgDescriptor))); |
58 | } catch (Exception e) { | 60 | } catch (Exception e) { |
59 | throw new AdaptorException(e); | 61 | throw new AdaptorException(e); |
60 | } | 62 | } |
@@ -71,8 +73,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | @@ -71,8 +73,9 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | ||
71 | if (requestId.isEmpty()) { | 73 | if (requestId.isEmpty()) { |
72 | throw new AdaptorException("Request id is missing!"); | 74 | throw new AdaptorException("Request id is missing!"); |
73 | } else { | 75 | } else { |
76 | + ProtoConverter.validateDescriptor(rpcResponseMsgDescriptor); | ||
74 | try { | 77 | try { |
75 | - JsonElement response = new JsonParser().parse(dynamicMsgToJson(inbound.getPayload(), rpcResponseMsgDescriptor)); | 78 | + JsonElement response = new JsonParser().parse(ProtoConverter.dynamicMsgToJson(inbound.getPayload(), rpcResponseMsgDescriptor)); |
76 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId.orElseThrow(() -> new AdaptorException("Request id is missing!"))) | 79 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId.orElseThrow(() -> new AdaptorException("Request id is missing!"))) |
77 | .setPayload(response.toString()).build(); | 80 | .setPayload(response.toString()).build(); |
78 | } catch (Exception e) { | 81 | } catch (Exception e) { |
@@ -158,11 +161,6 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | @@ -158,11 +161,6 @@ public class ProtoCoapAdaptor implements CoapTransportAdaptor { | ||
158 | return response; | 161 | return response; |
159 | } | 162 | } |
160 | 163 | ||
161 | - private String dynamicMsgToJson(byte[] bytes, Descriptors.Descriptor descriptor) throws InvalidProtocolBufferException { | ||
162 | - DynamicMessage dynamicMessage = DynamicMessage.parseFrom(descriptor, bytes); | ||
163 | - return JsonFormat.printer().includingDefaultValueFields().print(dynamicMessage); | ||
164 | - } | ||
165 | - | ||
166 | @Override | 164 | @Override |
167 | public int getContentFormat() { | 165 | public int getContentFormat() { |
168 | return MediaTypeRegistry.APPLICATION_OCTET_STREAM; | 166 | return MediaTypeRegistry.APPLICATION_OCTET_STREAM; |
@@ -101,8 +101,6 @@ import static io.netty.handler.codec.mqtt.MqttMessageType.UNSUBACK; | @@ -101,8 +101,6 @@ import static io.netty.handler.codec.mqtt.MqttMessageType.UNSUBACK; | ||
101 | import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE; | 101 | import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE; |
102 | import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE; | 102 | import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE; |
103 | import static io.netty.handler.codec.mqtt.MqttQoS.FAILURE; | 103 | import static io.netty.handler.codec.mqtt.MqttQoS.FAILURE; |
104 | -import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVICE_FIRMWARE_REQUEST_TOPIC_PATTERN; | ||
105 | -import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVICE_SOFTWARE_REQUEST_TOPIC_PATTERN; | ||
106 | 104 | ||
107 | /** | 105 | /** |
108 | * @author Andrew Shvayka | 106 | * @author Andrew Shvayka |
@@ -110,8 +108,8 @@ import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVIC | @@ -110,8 +108,8 @@ import static org.thingsboard.server.common.data.device.profile.MqttTopics.DEVIC | ||
110 | @Slf4j | 108 | @Slf4j |
111 | public class MqttTransportHandler extends ChannelInboundHandlerAdapter implements GenericFutureListener<Future<? super Void>>, SessionMsgListener { | 109 | public class MqttTransportHandler extends ChannelInboundHandlerAdapter implements GenericFutureListener<Future<? super Void>>, SessionMsgListener { |
112 | 110 | ||
113 | - private static final Pattern FW_REQUEST_PATTERN = Pattern.compile(DEVICE_FIRMWARE_REQUEST_TOPIC_PATTERN); | ||
114 | - private static final Pattern SW_REQUEST_PATTERN = Pattern.compile(DEVICE_SOFTWARE_REQUEST_TOPIC_PATTERN); | 111 | + private static final Pattern FW_REQUEST_PATTERN = Pattern.compile(MqttTopics.DEVICE_FIRMWARE_REQUEST_TOPIC_PATTERN); |
112 | + private static final Pattern SW_REQUEST_PATTERN = Pattern.compile(MqttTopics.DEVICE_SOFTWARE_REQUEST_TOPIC_PATTERN); | ||
115 | 113 | ||
116 | 114 | ||
117 | private static final String PAYLOAD_TOO_LARGE = "PAYLOAD_TOO_LARGE"; | 115 | private static final String PAYLOAD_TOO_LARGE = "PAYLOAD_TOO_LARGE"; |
@@ -133,6 +131,11 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -133,6 +131,11 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
133 | private final ConcurrentHashMap<String, Integer> chunkSizes; | 131 | private final ConcurrentHashMap<String, Integer> chunkSizes; |
134 | private final ConcurrentMap<Integer, TransportProtos.ToDeviceRpcRequestMsg> rpcAwaitingAck; | 132 | private final ConcurrentMap<Integer, TransportProtos.ToDeviceRpcRequestMsg> rpcAwaitingAck; |
135 | 133 | ||
134 | + private TopicType attrSubTopicType; | ||
135 | + private TopicType rpcSubTopicType; | ||
136 | + private TopicType attrReqTopicType; | ||
137 | + private TopicType toServerRpcSubTopicType; | ||
138 | + | ||
136 | MqttTransportHandler(MqttTransportContext context, SslHandler sslHandler) { | 139 | MqttTransportHandler(MqttTransportContext context, SslHandler sslHandler) { |
137 | this.sessionId = UUID.randomUUID(); | 140 | this.sessionId = UUID.randomUUID(); |
138 | this.context = context; | 141 | this.context = context; |
@@ -355,14 +358,16 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -355,14 +358,16 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
355 | TransportProtos.PostTelemetryMsg postTelemetryMsg = payloadAdaptor.convertToPostTelemetry(deviceSessionCtx, mqttMsg); | 358 | TransportProtos.PostTelemetryMsg postTelemetryMsg = payloadAdaptor.convertToPostTelemetry(deviceSessionCtx, mqttMsg); |
356 | transportService.process(deviceSessionCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(ctx, msgId, postTelemetryMsg)); | 359 | transportService.process(deviceSessionCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(ctx, msgId, postTelemetryMsg)); |
357 | } else if (topicName.startsWith(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX)) { | 360 | } else if (topicName.startsWith(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX)) { |
358 | - TransportProtos.GetAttributeRequestMsg getAttributeMsg = payloadAdaptor.convertToGetAttributes(deviceSessionCtx, mqttMsg); | 361 | + TransportProtos.GetAttributeRequestMsg getAttributeMsg = payloadAdaptor.convertToGetAttributes(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX); |
359 | transportService.process(deviceSessionCtx.getSessionInfo(), getAttributeMsg, getPubAckCallback(ctx, msgId, getAttributeMsg)); | 362 | transportService.process(deviceSessionCtx.getSessionInfo(), getAttributeMsg, getPubAckCallback(ctx, msgId, getAttributeMsg)); |
363 | + attrReqTopicType = TopicType.V1; | ||
360 | } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_RESPONSE_TOPIC)) { | 364 | } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_RESPONSE_TOPIC)) { |
361 | - TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = payloadAdaptor.convertToDeviceRpcResponse(deviceSessionCtx, mqttMsg); | 365 | + TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = payloadAdaptor.convertToDeviceRpcResponse(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_RESPONSE_TOPIC); |
362 | transportService.process(deviceSessionCtx.getSessionInfo(), rpcResponseMsg, getPubAckCallback(ctx, msgId, rpcResponseMsg)); | 366 | transportService.process(deviceSessionCtx.getSessionInfo(), rpcResponseMsg, getPubAckCallback(ctx, msgId, rpcResponseMsg)); |
363 | } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_REQUESTS_TOPIC)) { | 367 | } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_REQUESTS_TOPIC)) { |
364 | - TransportProtos.ToServerRpcRequestMsg rpcRequestMsg = payloadAdaptor.convertToServerRpcRequest(deviceSessionCtx, mqttMsg); | 368 | + TransportProtos.ToServerRpcRequestMsg rpcRequestMsg = payloadAdaptor.convertToServerRpcRequest(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC); |
365 | transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequestMsg, getPubAckCallback(ctx, msgId, rpcRequestMsg)); | 369 | transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequestMsg, getPubAckCallback(ctx, msgId, rpcRequestMsg)); |
370 | + toServerRpcSubTopicType = TopicType.V1; | ||
366 | } else if (topicName.equals(MqttTopics.DEVICE_CLAIM_TOPIC)) { | 371 | } else if (topicName.equals(MqttTopics.DEVICE_CLAIM_TOPIC)) { |
367 | TransportProtos.ClaimDeviceMsg claimDeviceMsg = payloadAdaptor.convertToClaimDevice(deviceSessionCtx, mqttMsg); | 372 | TransportProtos.ClaimDeviceMsg claimDeviceMsg = payloadAdaptor.convertToClaimDevice(deviceSessionCtx, mqttMsg); |
368 | transportService.process(deviceSessionCtx.getSessionInfo(), claimDeviceMsg, getPubAckCallback(ctx, msgId, claimDeviceMsg)); | 373 | transportService.process(deviceSessionCtx.getSessionInfo(), claimDeviceMsg, getPubAckCallback(ctx, msgId, claimDeviceMsg)); |
@@ -370,6 +375,57 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -370,6 +375,57 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
370 | getOtaPackageCallback(ctx, mqttMsg, msgId, fwMatcher, OtaPackageType.FIRMWARE); | 375 | getOtaPackageCallback(ctx, mqttMsg, msgId, fwMatcher, OtaPackageType.FIRMWARE); |
371 | } else if ((fwMatcher = SW_REQUEST_PATTERN.matcher(topicName)).find()) { | 376 | } else if ((fwMatcher = SW_REQUEST_PATTERN.matcher(topicName)).find()) { |
372 | getOtaPackageCallback(ctx, mqttMsg, msgId, fwMatcher, OtaPackageType.SOFTWARE); | 377 | getOtaPackageCallback(ctx, mqttMsg, msgId, fwMatcher, OtaPackageType.SOFTWARE); |
378 | + } else if (topicName.equals(MqttTopics.DEVICE_TELEMETRY_SHORT_TOPIC)) { | ||
379 | + TransportProtos.PostTelemetryMsg postTelemetryMsg = payloadAdaptor.convertToPostTelemetry(deviceSessionCtx, mqttMsg); | ||
380 | + transportService.process(deviceSessionCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(ctx, msgId, postTelemetryMsg)); | ||
381 | + } else if (topicName.equals(MqttTopics.DEVICE_TELEMETRY_SHORT_JSON_TOPIC)) { | ||
382 | + TransportProtos.PostTelemetryMsg postTelemetryMsg = context.getJsonMqttAdaptor().convertToPostTelemetry(deviceSessionCtx, mqttMsg); | ||
383 | + transportService.process(deviceSessionCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(ctx, msgId, postTelemetryMsg)); | ||
384 | + } else if (topicName.equals(MqttTopics.DEVICE_TELEMETRY_SHORT_PROTO_TOPIC)) { | ||
385 | + TransportProtos.PostTelemetryMsg postTelemetryMsg = context.getProtoMqttAdaptor().convertToPostTelemetry(deviceSessionCtx, mqttMsg); | ||
386 | + transportService.process(deviceSessionCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(ctx, msgId, postTelemetryMsg)); | ||
387 | + } else if (topicName.equals(MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC)) { | ||
388 | + TransportProtos.PostAttributeMsg postAttributeMsg = payloadAdaptor.convertToPostAttributes(deviceSessionCtx, mqttMsg); | ||
389 | + transportService.process(deviceSessionCtx.getSessionInfo(), postAttributeMsg, getPubAckCallback(ctx, msgId, postAttributeMsg)); | ||
390 | + } else if (topicName.equals(MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC)) { | ||
391 | + TransportProtos.PostAttributeMsg postAttributeMsg = context.getJsonMqttAdaptor().convertToPostAttributes(deviceSessionCtx, mqttMsg); | ||
392 | + transportService.process(deviceSessionCtx.getSessionInfo(), postAttributeMsg, getPubAckCallback(ctx, msgId, postAttributeMsg)); | ||
393 | + } else if (topicName.equals(MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC)) { | ||
394 | + TransportProtos.PostAttributeMsg postAttributeMsg = context.getProtoMqttAdaptor().convertToPostAttributes(deviceSessionCtx, mqttMsg); | ||
395 | + transportService.process(deviceSessionCtx.getSessionInfo(), postAttributeMsg, getPubAckCallback(ctx, msgId, postAttributeMsg)); | ||
396 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_RESPONSE_SHORT_JSON_TOPIC)) { | ||
397 | + TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = context.getJsonMqttAdaptor().convertToDeviceRpcResponse(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_RESPONSE_SHORT_JSON_TOPIC); | ||
398 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcResponseMsg, getPubAckCallback(ctx, msgId, rpcResponseMsg)); | ||
399 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_RESPONSE_SHORT_PROTO_TOPIC)) { | ||
400 | + TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = context.getProtoMqttAdaptor().convertToDeviceRpcResponse(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_RESPONSE_SHORT_PROTO_TOPIC); | ||
401 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcResponseMsg, getPubAckCallback(ctx, msgId, rpcResponseMsg)); | ||
402 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_RESPONSE_SHORT_TOPIC)) { | ||
403 | + TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = payloadAdaptor.convertToDeviceRpcResponse(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_RESPONSE_SHORT_TOPIC); | ||
404 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcResponseMsg, getPubAckCallback(ctx, msgId, rpcResponseMsg)); | ||
405 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_REQUESTS_SHORT_JSON_TOPIC)) { | ||
406 | + TransportProtos.ToServerRpcRequestMsg rpcRequestMsg = context.getJsonMqttAdaptor().convertToServerRpcRequest(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_REQUESTS_SHORT_JSON_TOPIC); | ||
407 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequestMsg, getPubAckCallback(ctx, msgId, rpcRequestMsg)); | ||
408 | + toServerRpcSubTopicType = TopicType.V2_JSON; | ||
409 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_REQUESTS_SHORT_PROTO_TOPIC)) { | ||
410 | + TransportProtos.ToServerRpcRequestMsg rpcRequestMsg = context.getProtoMqttAdaptor().convertToServerRpcRequest(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_REQUESTS_SHORT_PROTO_TOPIC); | ||
411 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequestMsg, getPubAckCallback(ctx, msgId, rpcRequestMsg)); | ||
412 | + toServerRpcSubTopicType = TopicType.V2_PROTO; | ||
413 | + } else if (topicName.startsWith(MqttTopics.DEVICE_RPC_REQUESTS_SHORT_TOPIC)) { | ||
414 | + TransportProtos.ToServerRpcRequestMsg rpcRequestMsg = payloadAdaptor.convertToServerRpcRequest(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_RPC_REQUESTS_SHORT_TOPIC); | ||
415 | + transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequestMsg, getPubAckCallback(ctx, msgId, rpcRequestMsg)); | ||
416 | + toServerRpcSubTopicType = TopicType.V2; | ||
417 | + } else if (topicName.startsWith(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_JSON_TOPIC_PREFIX)) { | ||
418 | + TransportProtos.GetAttributeRequestMsg getAttributeMsg = context.getJsonMqttAdaptor().convertToGetAttributes(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_JSON_TOPIC_PREFIX); | ||
419 | + transportService.process(deviceSessionCtx.getSessionInfo(), getAttributeMsg, getPubAckCallback(ctx, msgId, getAttributeMsg)); | ||
420 | + attrReqTopicType = TopicType.V2_JSON; | ||
421 | + } else if (topicName.startsWith(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_PROTO_TOPIC_PREFIX)) { | ||
422 | + TransportProtos.GetAttributeRequestMsg getAttributeMsg = context.getProtoMqttAdaptor().convertToGetAttributes(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_PROTO_TOPIC_PREFIX); | ||
423 | + transportService.process(deviceSessionCtx.getSessionInfo(), getAttributeMsg, getPubAckCallback(ctx, msgId, getAttributeMsg)); | ||
424 | + attrReqTopicType = TopicType.V2_PROTO; | ||
425 | + } else if (topicName.startsWith(MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX)) { | ||
426 | + TransportProtos.GetAttributeRequestMsg getAttributeMsg = payloadAdaptor.convertToGetAttributes(deviceSessionCtx, mqttMsg, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_SHORT_TOPIC_PREFIX); | ||
427 | + transportService.process(deviceSessionCtx.getSessionInfo(), getAttributeMsg, getPubAckCallback(ctx, msgId, getAttributeMsg)); | ||
428 | + attrReqTopicType = TopicType.V2; | ||
373 | } else { | 429 | } else { |
374 | transportService.reportActivity(deviceSessionCtx.getSessionInfo()); | 430 | transportService.reportActivity(deviceSessionCtx.getSessionInfo()); |
375 | ack(ctx, msgId); | 431 | ack(ctx, msgId); |
@@ -541,19 +597,53 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -541,19 +597,53 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
541 | try { | 597 | try { |
542 | switch (topic) { | 598 | switch (topic) { |
543 | case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: { | 599 | case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: { |
544 | - transportService.process(deviceSessionCtx.getSessionInfo(), TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null); | ||
545 | - registerSubQoS(topic, grantedQoSList, reqQoS); | 600 | + processAttributesSubscribe(grantedQoSList, topic, reqQoS, TopicType.V1); |
601 | + activityReported = true; | ||
602 | + break; | ||
603 | + } | ||
604 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC: { | ||
605 | + processAttributesSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2); | ||
606 | + activityReported = true; | ||
607 | + break; | ||
608 | + } | ||
609 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC: { | ||
610 | + processAttributesSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2_JSON); | ||
611 | + activityReported = true; | ||
612 | + break; | ||
613 | + } | ||
614 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC: { | ||
615 | + processAttributesSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2_PROTO); | ||
546 | activityReported = true; | 616 | activityReported = true; |
547 | break; | 617 | break; |
548 | } | 618 | } |
549 | case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: { | 619 | case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: { |
550 | - transportService.process(deviceSessionCtx.getSessionInfo(), TransportProtos.SubscribeToRPCMsg.newBuilder().build(), null); | ||
551 | - registerSubQoS(topic, grantedQoSList, reqQoS); | 620 | + processRpcSubscribe(grantedQoSList, topic, reqQoS, TopicType.V1); |
621 | + activityReported = true; | ||
622 | + break; | ||
623 | + } | ||
624 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC: { | ||
625 | + processRpcSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2); | ||
626 | + activityReported = true; | ||
627 | + break; | ||
628 | + } | ||
629 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC: { | ||
630 | + processRpcSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2_JSON); | ||
631 | + activityReported = true; | ||
632 | + break; | ||
633 | + } | ||
634 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_PROTO_TOPIC: { | ||
635 | + processRpcSubscribe(grantedQoSList, topic, reqQoS, TopicType.V2_PROTO); | ||
552 | activityReported = true; | 636 | activityReported = true; |
553 | break; | 637 | break; |
554 | } | 638 | } |
555 | case MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC: | 639 | case MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC: |
640 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_TOPIC: | ||
641 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_JSON_TOPIC: | ||
642 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_PROTO_TOPIC: | ||
556 | case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC: | 643 | case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC: |
644 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_TOPIC: | ||
645 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_JSON_TOPIC: | ||
646 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_PROTO_TOPIC: | ||
557 | case MqttTopics.GATEWAY_ATTRIBUTES_TOPIC: | 647 | case MqttTopics.GATEWAY_ATTRIBUTES_TOPIC: |
558 | case MqttTopics.GATEWAY_RPC_TOPIC: | 648 | case MqttTopics.GATEWAY_RPC_TOPIC: |
559 | case MqttTopics.GATEWAY_ATTRIBUTES_RESPONSE_TOPIC: | 649 | case MqttTopics.GATEWAY_ATTRIBUTES_RESPONSE_TOPIC: |
@@ -580,6 +670,18 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -580,6 +670,18 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
580 | ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList)); | 670 | ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList)); |
581 | } | 671 | } |
582 | 672 | ||
673 | + private void processRpcSubscribe(List<Integer> grantedQoSList, String topic, MqttQoS reqQoS, TopicType topicType) { | ||
674 | + transportService.process(deviceSessionCtx.getSessionInfo(), TransportProtos.SubscribeToRPCMsg.newBuilder().build(), null); | ||
675 | + rpcSubTopicType = topicType; | ||
676 | + registerSubQoS(topic, grantedQoSList, reqQoS); | ||
677 | + } | ||
678 | + | ||
679 | + private void processAttributesSubscribe(List<Integer> grantedQoSList, String topic, MqttQoS reqQoS, TopicType topicType) { | ||
680 | + transportService.process(deviceSessionCtx.getSessionInfo(), TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null); | ||
681 | + attrSubTopicType = topicType; | ||
682 | + registerSubQoS(topic, grantedQoSList, reqQoS); | ||
683 | + } | ||
684 | + | ||
583 | private void registerSubQoS(String topic, List<Integer> grantedQoSList, MqttQoS reqQoS) { | 685 | private void registerSubQoS(String topic, List<Integer> grantedQoSList, MqttQoS reqQoS) { |
584 | grantedQoSList.add(getMinSupportedQos(reqQoS)); | 686 | grantedQoSList.add(getMinSupportedQos(reqQoS)); |
585 | mqttQoSMap.put(new MqttTopicMatcher(topic), getMinSupportedQos(reqQoS)); | 687 | mqttQoSMap.put(new MqttTopicMatcher(topic), getMinSupportedQos(reqQoS)); |
@@ -595,18 +697,43 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -595,18 +697,43 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
595 | mqttQoSMap.remove(new MqttTopicMatcher(topicName)); | 697 | mqttQoSMap.remove(new MqttTopicMatcher(topicName)); |
596 | try { | 698 | try { |
597 | switch (topicName) { | 699 | switch (topicName) { |
598 | - case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: { | 700 | + case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: |
701 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC: | ||
702 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC: | ||
703 | + case MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC: { | ||
599 | transportService.process(deviceSessionCtx.getSessionInfo(), | 704 | transportService.process(deviceSessionCtx.getSessionInfo(), |
600 | TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(), null); | 705 | TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(), null); |
601 | activityReported = true; | 706 | activityReported = true; |
602 | break; | 707 | break; |
603 | } | 708 | } |
604 | - case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: { | 709 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: |
710 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_TOPIC: | ||
711 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_JSON_TOPIC: | ||
712 | + case MqttTopics.DEVICE_RPC_REQUESTS_SUB_SHORT_PROTO_TOPIC: { | ||
605 | transportService.process(deviceSessionCtx.getSessionInfo(), | 713 | transportService.process(deviceSessionCtx.getSessionInfo(), |
606 | TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(), null); | 714 | TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(), null); |
607 | activityReported = true; | 715 | activityReported = true; |
608 | break; | 716 | break; |
609 | } | 717 | } |
718 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC: | ||
719 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_TOPIC: | ||
720 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_JSON_TOPIC: | ||
721 | + case MqttTopics.DEVICE_RPC_RESPONSE_SUB_SHORT_PROTO_TOPIC: | ||
722 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC: | ||
723 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_TOPIC: | ||
724 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_JSON_TOPIC: | ||
725 | + case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_SHORT_PROTO_TOPIC: | ||
726 | + case MqttTopics.GATEWAY_ATTRIBUTES_TOPIC: | ||
727 | + case MqttTopics.GATEWAY_RPC_TOPIC: | ||
728 | + case MqttTopics.GATEWAY_ATTRIBUTES_RESPONSE_TOPIC: | ||
729 | + case MqttTopics.DEVICE_PROVISION_RESPONSE_TOPIC: | ||
730 | + case MqttTopics.DEVICE_FIRMWARE_RESPONSES_TOPIC: | ||
731 | + case MqttTopics.DEVICE_FIRMWARE_ERROR_TOPIC: | ||
732 | + case MqttTopics.DEVICE_SOFTWARE_RESPONSES_TOPIC: | ||
733 | + case MqttTopics.DEVICE_SOFTWARE_ERROR_TOPIC: { | ||
734 | + activityReported = true; | ||
735 | + break; | ||
736 | + } | ||
610 | } | 737 | } |
611 | } catch (Exception e) { | 738 | } catch (Exception e) { |
612 | log.warn("[{}] Failed to process unsubscription [{}] to [{}]", sessionId, mqttMsg.variableHeader().messageId(), topicName); | 739 | log.warn("[{}] Failed to process unsubscription [{}] to [{}]", sessionId, mqttMsg.variableHeader().messageId(), topicName); |
@@ -837,8 +964,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -837,8 +964,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
837 | 964 | ||
838 | @Override | 965 | @Override |
839 | public void onGetAttributesResponse(TransportProtos.GetAttributeResponseMsg response) { | 966 | public void onGetAttributesResponse(TransportProtos.GetAttributeResponseMsg response) { |
967 | + log.trace("[{}] Received get attributes response", sessionId); | ||
968 | + String topicBase; | ||
969 | + MqttTransportAdaptor adaptor; | ||
970 | + switch (attrReqTopicType) { | ||
971 | + case V2: | ||
972 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
973 | + topicBase = MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_SHORT_TOPIC_PREFIX; | ||
974 | + break; | ||
975 | + case V2_JSON: | ||
976 | + adaptor = context.getJsonMqttAdaptor(); | ||
977 | + topicBase = MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_SHORT_JSON_TOPIC_PREFIX; | ||
978 | + break; | ||
979 | + case V2_PROTO: | ||
980 | + adaptor = context.getProtoMqttAdaptor(); | ||
981 | + topicBase = MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_SHORT_PROTO_TOPIC_PREFIX; | ||
982 | + break; | ||
983 | + default: | ||
984 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
985 | + topicBase = MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_TOPIC_PREFIX; | ||
986 | + break; | ||
987 | + } | ||
840 | try { | 988 | try { |
841 | - deviceSessionCtx.getPayloadAdaptor().convertToPublish(deviceSessionCtx, response).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); | 989 | + adaptor.convertToPublish(deviceSessionCtx, response, topicBase).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); |
842 | } catch (Exception e) { | 990 | } catch (Exception e) { |
843 | log.trace("[{}] Failed to convert device attributes response to MQTT msg", sessionId, e); | 991 | log.trace("[{}] Failed to convert device attributes response to MQTT msg", sessionId, e); |
844 | } | 992 | } |
@@ -847,8 +995,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -847,8 +995,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
847 | @Override | 995 | @Override |
848 | public void onAttributeUpdate(UUID sessionId, TransportProtos.AttributeUpdateNotificationMsg notification) { | 996 | public void onAttributeUpdate(UUID sessionId, TransportProtos.AttributeUpdateNotificationMsg notification) { |
849 | log.trace("[{}] Received attributes update notification to device", sessionId); | 997 | log.trace("[{}] Received attributes update notification to device", sessionId); |
998 | + log.info("[{}] : attrSubTopicType: {}", notification.toString(), attrSubTopicType); | ||
999 | + String topic; | ||
1000 | + MqttTransportAdaptor adaptor; | ||
1001 | + switch (attrSubTopicType) { | ||
1002 | + case V2: | ||
1003 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1004 | + topic = MqttTopics.DEVICE_ATTRIBUTES_SHORT_TOPIC; | ||
1005 | + break; | ||
1006 | + case V2_JSON: | ||
1007 | + adaptor = context.getJsonMqttAdaptor(); | ||
1008 | + topic = MqttTopics.DEVICE_ATTRIBUTES_SHORT_JSON_TOPIC; | ||
1009 | + break; | ||
1010 | + case V2_PROTO: | ||
1011 | + adaptor = context.getProtoMqttAdaptor(); | ||
1012 | + topic = MqttTopics.DEVICE_ATTRIBUTES_SHORT_PROTO_TOPIC; | ||
1013 | + break; | ||
1014 | + default: | ||
1015 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1016 | + topic = MqttTopics.DEVICE_ATTRIBUTES_TOPIC; | ||
1017 | + break; | ||
1018 | + } | ||
850 | try { | 1019 | try { |
851 | - deviceSessionCtx.getPayloadAdaptor().convertToPublish(deviceSessionCtx, notification).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); | 1020 | + adaptor.convertToPublish(deviceSessionCtx, notification, topic).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); |
852 | } catch (Exception e) { | 1021 | } catch (Exception e) { |
853 | log.trace("[{}] Failed to convert device attributes update to MQTT msg", sessionId, e); | 1022 | log.trace("[{}] Failed to convert device attributes update to MQTT msg", sessionId, e); |
854 | } | 1023 | } |
@@ -862,9 +1031,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -862,9 +1031,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
862 | 1031 | ||
863 | @Override | 1032 | @Override |
864 | public void onToDeviceRpcRequest(UUID sessionId, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) { | 1033 | public void onToDeviceRpcRequest(UUID sessionId, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) { |
865 | - log.trace("[{}] Received RPC command to device", sessionId); | 1034 | + log.info("[{}] Received RPC command to device", sessionId); |
1035 | + String baseTopic; | ||
1036 | + MqttTransportAdaptor adaptor; | ||
1037 | + switch (rpcSubTopicType) { | ||
1038 | + case V2: | ||
1039 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1040 | + baseTopic = MqttTopics.DEVICE_RPC_REQUESTS_SHORT_TOPIC; | ||
1041 | + break; | ||
1042 | + case V2_JSON: | ||
1043 | + adaptor = context.getJsonMqttAdaptor(); | ||
1044 | + baseTopic = MqttTopics.DEVICE_RPC_REQUESTS_SHORT_JSON_TOPIC; | ||
1045 | + break; | ||
1046 | + case V2_PROTO: | ||
1047 | + adaptor = context.getProtoMqttAdaptor(); | ||
1048 | + baseTopic = MqttTopics.DEVICE_RPC_REQUESTS_SHORT_PROTO_TOPIC; | ||
1049 | + break; | ||
1050 | + default: | ||
1051 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1052 | + baseTopic = MqttTopics.DEVICE_RPC_REQUESTS_TOPIC; | ||
1053 | + break; | ||
1054 | + } | ||
866 | try { | 1055 | try { |
867 | - deviceSessionCtx.getPayloadAdaptor().convertToPublish(deviceSessionCtx, rpcRequest).ifPresent(payload -> { | 1056 | + adaptor.convertToPublish(deviceSessionCtx, rpcRequest, baseTopic).ifPresent(payload -> { |
868 | int msgId = ((MqttPublishMessage) payload).variableHeader().packetId(); | 1057 | int msgId = ((MqttPublishMessage) payload).variableHeader().packetId(); |
869 | if (isAckExpected(payload)) { | 1058 | if (isAckExpected(payload)) { |
870 | rpcAwaitingAck.put(msgId, rpcRequest); | 1059 | rpcAwaitingAck.put(msgId, rpcRequest); |
@@ -898,9 +1087,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -898,9 +1087,29 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
898 | 1087 | ||
899 | @Override | 1088 | @Override |
900 | public void onToServerRpcResponse(TransportProtos.ToServerRpcResponseMsg rpcResponse) { | 1089 | public void onToServerRpcResponse(TransportProtos.ToServerRpcResponseMsg rpcResponse) { |
901 | - log.trace("[{}] Received RPC command to server", sessionId); | 1090 | + log.trace("[{}] Received RPC response from server", sessionId); |
1091 | + String baseTopic; | ||
1092 | + MqttTransportAdaptor adaptor; | ||
1093 | + switch (toServerRpcSubTopicType) { | ||
1094 | + case V2: | ||
1095 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1096 | + baseTopic = MqttTopics.DEVICE_RPC_RESPONSE_SHORT_TOPIC; | ||
1097 | + break; | ||
1098 | + case V2_JSON: | ||
1099 | + adaptor = context.getJsonMqttAdaptor(); | ||
1100 | + baseTopic = MqttTopics.DEVICE_RPC_RESPONSE_SHORT_JSON_TOPIC; | ||
1101 | + break; | ||
1102 | + case V2_PROTO: | ||
1103 | + adaptor = context.getProtoMqttAdaptor(); | ||
1104 | + baseTopic = MqttTopics.DEVICE_RPC_RESPONSE_SHORT_PROTO_TOPIC; | ||
1105 | + break; | ||
1106 | + default: | ||
1107 | + adaptor = deviceSessionCtx.getPayloadAdaptor(); | ||
1108 | + baseTopic = MqttTopics.DEVICE_RPC_RESPONSE_TOPIC; | ||
1109 | + break; | ||
1110 | + } | ||
902 | try { | 1111 | try { |
903 | - deviceSessionCtx.getPayloadAdaptor().convertToPublish(deviceSessionCtx, rpcResponse).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); | 1112 | + adaptor.convertToPublish(deviceSessionCtx, rpcResponse, baseTopic).ifPresent(deviceSessionCtx.getChannel()::writeAndFlush); |
904 | } catch (Exception e) { | 1113 | } catch (Exception e) { |
905 | log.trace("[{}] Failed to convert device RPC command to MQTT msg", sessionId, e); | 1114 | log.trace("[{}] Failed to convert device RPC command to MQTT msg", sessionId, e); |
906 | } | 1115 | } |
@@ -923,4 +1132,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | @@ -923,4 +1132,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement | ||
923 | public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { | 1132 | public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { |
924 | deviceSessionCtx.onDeviceUpdate(sessionInfo, device, deviceProfileOpt); | 1133 | deviceSessionCtx.onDeviceUpdate(sessionInfo, device, deviceProfileOpt); |
925 | } | 1134 | } |
1135 | + | ||
1136 | + private enum TopicType { | ||
1137 | + V1, V2, V2_JSON, V2_PROTO | ||
1138 | + } | ||
1139 | + | ||
926 | } | 1140 | } |
@@ -64,6 +64,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -64,6 +64,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
64 | try { | 64 | try { |
65 | return JsonConverter.convertToTelemetryProto(new JsonParser().parse(payload)); | 65 | return JsonConverter.convertToTelemetryProto(new JsonParser().parse(payload)); |
66 | } catch (IllegalStateException | JsonSyntaxException ex) { | 66 | } catch (IllegalStateException | JsonSyntaxException ex) { |
67 | + log.warn("Failed to decode post telemetry request", ex); | ||
67 | throw new AdaptorException(ex); | 68 | throw new AdaptorException(ex); |
68 | } | 69 | } |
69 | } | 70 | } |
@@ -74,6 +75,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -74,6 +75,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
74 | try { | 75 | try { |
75 | return JsonConverter.convertToAttributesProto(new JsonParser().parse(payload)); | 76 | return JsonConverter.convertToAttributesProto(new JsonParser().parse(payload)); |
76 | } catch (IllegalStateException | JsonSyntaxException ex) { | 77 | } catch (IllegalStateException | JsonSyntaxException ex) { |
78 | + log.warn("Failed to decode post attributes request", ex); | ||
77 | throw new AdaptorException(ex); | 79 | throw new AdaptorException(ex); |
78 | } | 80 | } |
79 | } | 81 | } |
@@ -84,6 +86,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -84,6 +86,7 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
84 | try { | 86 | try { |
85 | return JsonConverter.convertToClaimDeviceProto(ctx.getDeviceId(), payload); | 87 | return JsonConverter.convertToClaimDeviceProto(ctx.getDeviceId(), payload); |
86 | } catch (IllegalStateException | JsonSyntaxException ex) { | 88 | } catch (IllegalStateException | JsonSyntaxException ex) { |
89 | + log.warn("Failed to decode claim device request", ex); | ||
87 | throw new AdaptorException(ex); | 90 | throw new AdaptorException(ex); |
88 | } | 91 | } |
89 | } | 92 | } |
@@ -99,33 +102,33 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -99,33 +102,33 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
99 | } | 102 | } |
100 | 103 | ||
101 | @Override | 104 | @Override |
102 | - public TransportProtos.GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | ||
103 | - return processGetAttributeRequestMsg(inbound, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX); | 105 | + public TransportProtos.GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
106 | + return processGetAttributeRequestMsg(inbound, topicBase); | ||
104 | } | 107 | } |
105 | 108 | ||
106 | @Override | 109 | @Override |
107 | - public TransportProtos.ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | ||
108 | - return processToDeviceRpcResponseMsg(inbound, MqttTopics.DEVICE_RPC_RESPONSE_TOPIC); | 110 | + public TransportProtos.ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
111 | + return processToDeviceRpcResponseMsg(inbound, topicBase); | ||
109 | } | 112 | } |
110 | 113 | ||
111 | @Override | 114 | @Override |
112 | - public TransportProtos.ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | ||
113 | - return processToServerRpcRequestMsg(ctx, inbound, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC); | 115 | + public TransportProtos.ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
116 | + return processToServerRpcRequestMsg(ctx, inbound, topicBase); | ||
114 | } | 117 | } |
115 | 118 | ||
116 | @Override | 119 | @Override |
117 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg) throws AdaptorException { | ||
118 | - return processConvertFromAttributeResponseMsg(ctx, responseMsg, MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_TOPIC_PREFIX); | 120 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg, String topicBase) throws AdaptorException { |
121 | + return processConvertFromAttributeResponseMsg(ctx, responseMsg, topicBase); | ||
119 | } | 122 | } |
120 | 123 | ||
121 | @Override | 124 | @Override |
122 | public Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, TransportProtos.GetAttributeResponseMsg responseMsg) throws AdaptorException { | 125 | public Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, TransportProtos.GetAttributeResponseMsg responseMsg) throws AdaptorException { |
123 | - return processConvertFromGatewayAttributeResponseMsg(ctx, deviceName, responseMsg, MqttTopics.GATEWAY_ATTRIBUTES_RESPONSE_TOPIC); | 126 | + return processConvertFromGatewayAttributeResponseMsg(ctx, deviceName, responseMsg); |
124 | } | 127 | } |
125 | 128 | ||
126 | @Override | 129 | @Override |
127 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.AttributeUpdateNotificationMsg notificationMsg) { | ||
128 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_ATTRIBUTES_TOPIC, JsonConverter.toJson(notificationMsg))); | 130 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.AttributeUpdateNotificationMsg notificationMsg, String topic) { |
131 | + return Optional.of(createMqttPublishMsg(ctx, topic, JsonConverter.toJson(notificationMsg))); | ||
129 | } | 132 | } |
130 | 133 | ||
131 | @Override | 134 | @Override |
@@ -135,8 +138,8 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -135,8 +138,8 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
135 | } | 138 | } |
136 | 139 | ||
137 | @Override | 140 | @Override |
138 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) { | ||
139 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC + rpcRequest.getRequestId(), JsonConverter.toJson(rpcRequest, false))); | 141 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest, String topicBase) { |
142 | + return Optional.of(createMqttPublishMsg(ctx, topicBase + rpcRequest.getRequestId(), JsonConverter.toJson(rpcRequest, false))); | ||
140 | } | 143 | } |
141 | 144 | ||
142 | @Override | 145 | @Override |
@@ -145,8 +148,8 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -145,8 +148,8 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
145 | } | 148 | } |
146 | 149 | ||
147 | @Override | 150 | @Override |
148 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToServerRpcResponseMsg rpcResponse) { | ||
149 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_RESPONSE_TOPIC + rpcResponse.getRequestId(), JsonConverter.toJson(rpcResponse))); | 151 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToServerRpcResponseMsg rpcResponse, String topicBase) { |
152 | + return Optional.of(createMqttPublishMsg(ctx, topicBase + rpcResponse.getRequestId(), JsonConverter.toJson(rpcResponse))); | ||
150 | } | 153 | } |
151 | 154 | ||
152 | @Override | 155 | @Override |
@@ -169,11 +172,11 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -169,11 +172,11 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
169 | } | 172 | } |
170 | } | 173 | } |
171 | 174 | ||
172 | - protected TransportProtos.GetAttributeRequestMsg processGetAttributeRequestMsg(MqttPublishMessage inbound, String topic) throws AdaptorException { | 175 | + private TransportProtos.GetAttributeRequestMsg processGetAttributeRequestMsg(MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
173 | String topicName = inbound.variableHeader().topicName(); | 176 | String topicName = inbound.variableHeader().topicName(); |
174 | try { | 177 | try { |
175 | TransportProtos.GetAttributeRequestMsg.Builder result = TransportProtos.GetAttributeRequestMsg.newBuilder(); | 178 | TransportProtos.GetAttributeRequestMsg.Builder result = TransportProtos.GetAttributeRequestMsg.newBuilder(); |
176 | - result.setRequestId(getRequestId(topicName, topic)); | 179 | + result.setRequestId(getRequestId(topicName, topicBase)); |
177 | String payload = inbound.payload().toString(UTF8); | 180 | String payload = inbound.payload().toString(UTF8); |
178 | JsonElement requestBody = new JsonParser().parse(payload); | 181 | JsonElement requestBody = new JsonParser().parse(payload); |
179 | Set<String> clientKeys = toStringSet(requestBody, "clientKeys"); | 182 | Set<String> clientKeys = toStringSet(requestBody, "clientKeys"); |
@@ -191,49 +194,50 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | @@ -191,49 +194,50 @@ public class JsonMqttAdaptor implements MqttTransportAdaptor { | ||
191 | } | 194 | } |
192 | } | 195 | } |
193 | 196 | ||
194 | - protected TransportProtos.ToDeviceRpcResponseMsg processToDeviceRpcResponseMsg(MqttPublishMessage inbound, String topic) throws AdaptorException { | 197 | + private TransportProtos.ToDeviceRpcResponseMsg processToDeviceRpcResponseMsg(MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
195 | String topicName = inbound.variableHeader().topicName(); | 198 | String topicName = inbound.variableHeader().topicName(); |
196 | try { | 199 | try { |
197 | - int requestId = getRequestId(topicName, topic); | 200 | + int requestId = getRequestId(topicName, topicBase); |
198 | String payload = inbound.payload().toString(UTF8); | 201 | String payload = inbound.payload().toString(UTF8); |
199 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId).setPayload(payload).build(); | 202 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId).setPayload(payload).build(); |
200 | } catch (RuntimeException e) { | 203 | } catch (RuntimeException e) { |
201 | - log.warn("Failed to decode Rpc response", e); | 204 | + log.warn("Failed to decode rpc response", e); |
202 | throw new AdaptorException(e); | 205 | throw new AdaptorException(e); |
203 | } | 206 | } |
204 | } | 207 | } |
205 | 208 | ||
206 | - protected TransportProtos.ToServerRpcRequestMsg processToServerRpcRequestMsg(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topic) throws AdaptorException { | 209 | + private TransportProtos.ToServerRpcRequestMsg processToServerRpcRequestMsg(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
207 | String topicName = inbound.variableHeader().topicName(); | 210 | String topicName = inbound.variableHeader().topicName(); |
208 | String payload = validatePayload(ctx.getSessionId(), inbound.payload(), false); | 211 | String payload = validatePayload(ctx.getSessionId(), inbound.payload(), false); |
209 | try { | 212 | try { |
210 | - int requestId = getRequestId(topicName, topic); | 213 | + int requestId = getRequestId(topicName, topicBase); |
211 | return JsonConverter.convertToServerRpcRequest(new JsonParser().parse(payload), requestId); | 214 | return JsonConverter.convertToServerRpcRequest(new JsonParser().parse(payload), requestId); |
212 | } catch (IllegalStateException | JsonSyntaxException ex) { | 215 | } catch (IllegalStateException | JsonSyntaxException ex) { |
216 | + log.warn("Failed to decode to server rpc request", ex); | ||
213 | throw new AdaptorException(ex); | 217 | throw new AdaptorException(ex); |
214 | } | 218 | } |
215 | } | 219 | } |
216 | 220 | ||
217 | - protected Optional<MqttMessage> processConvertFromAttributeResponseMsg(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg, String topic) throws AdaptorException { | 221 | + private Optional<MqttMessage> processConvertFromAttributeResponseMsg(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg, String topicBase) throws AdaptorException { |
218 | if (!StringUtils.isEmpty(responseMsg.getError())) { | 222 | if (!StringUtils.isEmpty(responseMsg.getError())) { |
219 | throw new AdaptorException(responseMsg.getError()); | 223 | throw new AdaptorException(responseMsg.getError()); |
220 | } else { | 224 | } else { |
221 | int requestId = responseMsg.getRequestId(); | 225 | int requestId = responseMsg.getRequestId(); |
222 | if (requestId >= 0) { | 226 | if (requestId >= 0) { |
223 | return Optional.of(createMqttPublishMsg(ctx, | 227 | return Optional.of(createMqttPublishMsg(ctx, |
224 | - topic + requestId, | 228 | + topicBase + requestId, |
225 | JsonConverter.toJson(responseMsg))); | 229 | JsonConverter.toJson(responseMsg))); |
226 | } | 230 | } |
227 | return Optional.empty(); | 231 | return Optional.empty(); |
228 | } | 232 | } |
229 | } | 233 | } |
230 | 234 | ||
231 | - protected Optional<MqttMessage> processConvertFromGatewayAttributeResponseMsg(MqttDeviceAwareSessionContext ctx, String deviceName, TransportProtos.GetAttributeResponseMsg responseMsg, String topic) throws AdaptorException { | 235 | + private Optional<MqttMessage> processConvertFromGatewayAttributeResponseMsg(MqttDeviceAwareSessionContext ctx, String deviceName, TransportProtos.GetAttributeResponseMsg responseMsg) throws AdaptorException { |
232 | if (!StringUtils.isEmpty(responseMsg.getError())) { | 236 | if (!StringUtils.isEmpty(responseMsg.getError())) { |
233 | throw new AdaptorException(responseMsg.getError()); | 237 | throw new AdaptorException(responseMsg.getError()); |
234 | } else { | 238 | } else { |
235 | JsonObject result = JsonConverter.getJsonObjectForGateway(deviceName, responseMsg); | 239 | JsonObject result = JsonConverter.getJsonObjectForGateway(deviceName, responseMsg); |
236 | - return Optional.of(createMqttPublishMsg(ctx, topic, result)); | 240 | + return Optional.of(createMqttPublishMsg(ctx, MqttTopics.GATEWAY_ATTRIBUTES_RESPONSE_TOPIC, result)); |
237 | } | 241 | } |
238 | } | 242 | } |
239 | 243 |
@@ -52,27 +52,27 @@ public interface MqttTransportAdaptor { | @@ -52,27 +52,27 @@ public interface MqttTransportAdaptor { | ||
52 | 52 | ||
53 | PostAttributeMsg convertToPostAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; | 53 | PostAttributeMsg convertToPostAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; |
54 | 54 | ||
55 | - GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; | 55 | + GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException; |
56 | 56 | ||
57 | - ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg) throws AdaptorException; | 57 | + ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg, String topicBase) throws AdaptorException; |
58 | 58 | ||
59 | - ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg) throws AdaptorException; | 59 | + ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg, String topicBase) throws AdaptorException; |
60 | 60 | ||
61 | ClaimDeviceMsg convertToClaimDevice(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; | 61 | ClaimDeviceMsg convertToClaimDevice(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; |
62 | 62 | ||
63 | - Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, GetAttributeResponseMsg responseMsg) throws AdaptorException; | 63 | + Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, GetAttributeResponseMsg responseMsg, String topicBase) throws AdaptorException; |
64 | 64 | ||
65 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, GetAttributeResponseMsg responseMsg) throws AdaptorException; | 65 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, GetAttributeResponseMsg responseMsg) throws AdaptorException; |
66 | 66 | ||
67 | - Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, AttributeUpdateNotificationMsg notificationMsg) throws AdaptorException; | 67 | + Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, AttributeUpdateNotificationMsg notificationMsg, String topic) throws AdaptorException; |
68 | 68 | ||
69 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, AttributeUpdateNotificationMsg notificationMsg) throws AdaptorException; | 69 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, AttributeUpdateNotificationMsg notificationMsg) throws AdaptorException; |
70 | 70 | ||
71 | - Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, ToDeviceRpcRequestMsg rpcRequest) throws AdaptorException; | 71 | + Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, ToDeviceRpcRequestMsg rpcRequest, String topicBase) throws AdaptorException; |
72 | 72 | ||
73 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, ToDeviceRpcRequestMsg rpcRequest) throws AdaptorException; | 73 | Optional<MqttMessage> convertToGatewayPublish(MqttDeviceAwareSessionContext ctx, String deviceName, ToDeviceRpcRequestMsg rpcRequest) throws AdaptorException; |
74 | 74 | ||
75 | - Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, ToServerRpcResponseMsg rpcResponse) throws AdaptorException; | 75 | + Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, ToServerRpcResponseMsg rpcResponse, String topicBase) throws AdaptorException; |
76 | 76 | ||
77 | ProvisionDeviceRequestMsg convertToProvisionRequestMsg(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; | 77 | ProvisionDeviceRequestMsg convertToProvisionRequestMsg(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException; |
78 | 78 |
@@ -20,7 +20,6 @@ import com.google.gson.JsonParser; | @@ -20,7 +20,6 @@ import com.google.gson.JsonParser; | ||
20 | import com.google.protobuf.Descriptors; | 20 | import com.google.protobuf.Descriptors; |
21 | import com.google.protobuf.DynamicMessage; | 21 | import com.google.protobuf.DynamicMessage; |
22 | import com.google.protobuf.InvalidProtocolBufferException; | 22 | import com.google.protobuf.InvalidProtocolBufferException; |
23 | -import com.google.protobuf.util.JsonFormat; | ||
24 | import io.netty.buffer.ByteBuf; | 23 | import io.netty.buffer.ByteBuf; |
25 | import io.netty.handler.codec.mqtt.MqttMessage; | 24 | import io.netty.handler.codec.mqtt.MqttMessage; |
26 | import io.netty.handler.codec.mqtt.MqttPublishMessage; | 25 | import io.netty.handler.codec.mqtt.MqttPublishMessage; |
@@ -49,10 +48,11 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -49,10 +48,11 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
49 | public TransportProtos.PostTelemetryMsg convertToPostTelemetry(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | 48 | public TransportProtos.PostTelemetryMsg convertToPostTelemetry(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { |
50 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; | 49 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; |
51 | byte[] bytes = toBytes(inbound.payload()); | 50 | byte[] bytes = toBytes(inbound.payload()); |
52 | - Descriptors.Descriptor telemetryDynamicMsgDescriptor = getDescriptor(deviceSessionCtx.getTelemetryDynamicMsgDescriptor()); | 51 | + Descriptors.Descriptor telemetryDynamicMsgDescriptor = ProtoConverter.validateDescriptor(deviceSessionCtx.getTelemetryDynamicMsgDescriptor()); |
53 | try { | 52 | try { |
54 | - return JsonConverter.convertToTelemetryProto(new JsonParser().parse(dynamicMsgToJson(bytes, telemetryDynamicMsgDescriptor))); | 53 | + return JsonConverter.convertToTelemetryProto(new JsonParser().parse(ProtoConverter.dynamicMsgToJson(bytes, telemetryDynamicMsgDescriptor))); |
55 | } catch (Exception e) { | 54 | } catch (Exception e) { |
55 | + log.warn("Failed to decode post telemetry request", e); | ||
56 | throw new AdaptorException(e); | 56 | throw new AdaptorException(e); |
57 | } | 57 | } |
58 | } | 58 | } |
@@ -61,10 +61,11 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -61,10 +61,11 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
61 | public TransportProtos.PostAttributeMsg convertToPostAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | 61 | public TransportProtos.PostAttributeMsg convertToPostAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { |
62 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; | 62 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; |
63 | byte[] bytes = toBytes(inbound.payload()); | 63 | byte[] bytes = toBytes(inbound.payload()); |
64 | - Descriptors.Descriptor attributesDynamicMessageDescriptor = getDescriptor(deviceSessionCtx.getAttributesDynamicMessageDescriptor()); | 64 | + Descriptors.Descriptor attributesDynamicMessageDescriptor = ProtoConverter.validateDescriptor(deviceSessionCtx.getAttributesDynamicMessageDescriptor()); |
65 | try { | 65 | try { |
66 | - return JsonConverter.convertToAttributesProto(new JsonParser().parse(dynamicMsgToJson(bytes, attributesDynamicMessageDescriptor))); | 66 | + return JsonConverter.convertToAttributesProto(new JsonParser().parse(ProtoConverter.dynamicMsgToJson(bytes, attributesDynamicMessageDescriptor))); |
67 | } catch (Exception e) { | 67 | } catch (Exception e) { |
68 | + log.warn("Failed to decode post attributes request", e); | ||
68 | throw new AdaptorException(e); | 69 | throw new AdaptorException(e); |
69 | } | 70 | } |
70 | } | 71 | } |
@@ -75,16 +76,17 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -75,16 +76,17 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
75 | try { | 76 | try { |
76 | return ProtoConverter.convertToClaimDeviceProto(ctx.getDeviceId(), bytes); | 77 | return ProtoConverter.convertToClaimDeviceProto(ctx.getDeviceId(), bytes); |
77 | } catch (InvalidProtocolBufferException e) { | 78 | } catch (InvalidProtocolBufferException e) { |
79 | + log.warn("Failed to decode claim device request", e); | ||
78 | throw new AdaptorException(e); | 80 | throw new AdaptorException(e); |
79 | } | 81 | } |
80 | } | 82 | } |
81 | 83 | ||
82 | @Override | 84 | @Override |
83 | - public TransportProtos.GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { | 85 | + public TransportProtos.GetAttributeRequestMsg convertToGetAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound, String topicBase) throws AdaptorException { |
84 | byte[] bytes = toBytes(inbound.payload()); | 86 | byte[] bytes = toBytes(inbound.payload()); |
85 | String topicName = inbound.variableHeader().topicName(); | 87 | String topicName = inbound.variableHeader().topicName(); |
86 | try { | 88 | try { |
87 | - int requestId = getRequestId(topicName, MqttTopics.DEVICE_ATTRIBUTES_REQUEST_TOPIC_PREFIX); | 89 | + int requestId = getRequestId(topicName, topicBase); |
88 | return ProtoConverter.convertToGetAttributeRequestMessage(bytes, requestId); | 90 | return ProtoConverter.convertToGetAttributeRequestMessage(bytes, requestId); |
89 | } catch (InvalidProtocolBufferException e) { | 91 | } catch (InvalidProtocolBufferException e) { |
90 | log.warn("Failed to decode get attributes request", e); | 92 | log.warn("Failed to decode get attributes request", e); |
@@ -93,29 +95,30 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -93,29 +95,30 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
93 | } | 95 | } |
94 | 96 | ||
95 | @Override | 97 | @Override |
96 | - public TransportProtos.ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg) throws AdaptorException { | 98 | + public TransportProtos.ToDeviceRpcResponseMsg convertToDeviceRpcResponse(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg, String topicBase) throws AdaptorException { |
97 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; | 99 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; |
98 | String topicName = mqttMsg.variableHeader().topicName(); | 100 | String topicName = mqttMsg.variableHeader().topicName(); |
99 | byte[] bytes = toBytes(mqttMsg.payload()); | 101 | byte[] bytes = toBytes(mqttMsg.payload()); |
100 | - Descriptors.Descriptor rpcResponseDynamicMessageDescriptor = getDescriptor(deviceSessionCtx.getRpcResponseDynamicMessageDescriptor()); | 102 | + Descriptors.Descriptor rpcResponseDynamicMessageDescriptor = ProtoConverter.validateDescriptor(deviceSessionCtx.getRpcResponseDynamicMessageDescriptor()); |
101 | try { | 103 | try { |
102 | - int requestId = getRequestId(topicName, MqttTopics.DEVICE_RPC_RESPONSE_TOPIC); | ||
103 | - JsonElement response = new JsonParser().parse(dynamicMsgToJson(bytes, rpcResponseDynamicMessageDescriptor)); | 104 | + int requestId = getRequestId(topicName, topicBase); |
105 | + JsonElement response = new JsonParser().parse(ProtoConverter.dynamicMsgToJson(bytes, rpcResponseDynamicMessageDescriptor)); | ||
104 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId).setPayload(response.toString()).build(); | 106 | return TransportProtos.ToDeviceRpcResponseMsg.newBuilder().setRequestId(requestId).setPayload(response.toString()).build(); |
105 | } catch (Exception e) { | 107 | } catch (Exception e) { |
106 | - log.warn("Failed to decode Rpc response", e); | 108 | + log.warn("Failed to decode rpc response", e); |
107 | throw new AdaptorException(e); | 109 | throw new AdaptorException(e); |
108 | } | 110 | } |
109 | } | 111 | } |
110 | 112 | ||
111 | @Override | 113 | @Override |
112 | - public TransportProtos.ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg) throws AdaptorException { | 114 | + public TransportProtos.ToServerRpcRequestMsg convertToServerRpcRequest(MqttDeviceAwareSessionContext ctx, MqttPublishMessage mqttMsg, String topicBase) throws AdaptorException { |
113 | byte[] bytes = toBytes(mqttMsg.payload()); | 115 | byte[] bytes = toBytes(mqttMsg.payload()); |
114 | String topicName = mqttMsg.variableHeader().topicName(); | 116 | String topicName = mqttMsg.variableHeader().topicName(); |
115 | try { | 117 | try { |
116 | - int requestId = getRequestId(topicName, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC); | 118 | + int requestId = getRequestId(topicName, topicBase); |
117 | return ProtoConverter.convertToServerRpcRequest(bytes, requestId); | 119 | return ProtoConverter.convertToServerRpcRequest(bytes, requestId); |
118 | } catch (InvalidProtocolBufferException e) { | 120 | } catch (InvalidProtocolBufferException e) { |
121 | + log.warn("Failed to decode to server rpc request", e); | ||
119 | throw new AdaptorException(e); | 122 | throw new AdaptorException(e); |
120 | } | 123 | } |
121 | } | 124 | } |
@@ -126,40 +129,43 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -126,40 +129,43 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
126 | try { | 129 | try { |
127 | return ProtoConverter.convertToProvisionRequestMsg(bytes); | 130 | return ProtoConverter.convertToProvisionRequestMsg(bytes); |
128 | } catch (InvalidProtocolBufferException ex) { | 131 | } catch (InvalidProtocolBufferException ex) { |
132 | + log.warn("Failed to decode provision request", ex); | ||
129 | throw new AdaptorException(ex); | 133 | throw new AdaptorException(ex); |
130 | } | 134 | } |
131 | } | 135 | } |
132 | 136 | ||
133 | @Override | 137 | @Override |
134 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg) throws AdaptorException { | 138 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.GetAttributeResponseMsg responseMsg, String topicBase) throws AdaptorException { |
135 | if (!StringUtils.isEmpty(responseMsg.getError())) { | 139 | if (!StringUtils.isEmpty(responseMsg.getError())) { |
136 | throw new AdaptorException(responseMsg.getError()); | 140 | throw new AdaptorException(responseMsg.getError()); |
137 | } else { | 141 | } else { |
138 | int requestId = responseMsg.getRequestId(); | 142 | int requestId = responseMsg.getRequestId(); |
139 | if (requestId >= 0) { | 143 | if (requestId >= 0) { |
140 | - return Optional.of(createMqttPublishMsg(ctx, | ||
141 | - MqttTopics.DEVICE_ATTRIBUTES_RESPONSE_TOPIC_PREFIX + requestId, | ||
142 | - responseMsg.toByteArray())); | 144 | + return Optional.of(createMqttPublishMsg(ctx, topicBase + requestId, responseMsg.toByteArray())); |
143 | } | 145 | } |
144 | return Optional.empty(); | 146 | return Optional.empty(); |
145 | } | 147 | } |
146 | } | 148 | } |
147 | 149 | ||
148 | @Override | 150 | @Override |
149 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest) throws AdaptorException { | 151 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToDeviceRpcRequestMsg rpcRequest, String topicBase) throws AdaptorException { |
150 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; | 152 | DeviceSessionCtx deviceSessionCtx = (DeviceSessionCtx) ctx; |
151 | DynamicMessage.Builder rpcRequestDynamicMessageBuilder = deviceSessionCtx.getRpcRequestDynamicMessageBuilder(); | 153 | DynamicMessage.Builder rpcRequestDynamicMessageBuilder = deviceSessionCtx.getRpcRequestDynamicMessageBuilder(); |
152 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_REQUESTS_TOPIC + rpcRequest.getRequestId(), ProtoConverter.convertToRpcRequest(rpcRequest, rpcRequestDynamicMessageBuilder))); | 154 | + if (rpcRequestDynamicMessageBuilder == null) { |
155 | + throw new AdaptorException("Failed to get rpcRequestDynamicMessageBuilder!"); | ||
156 | + } else { | ||
157 | + return Optional.of(createMqttPublishMsg(ctx, topicBase + rpcRequest.getRequestId(), ProtoConverter.convertToRpcRequest(rpcRequest, rpcRequestDynamicMessageBuilder))); | ||
158 | + } | ||
153 | } | 159 | } |
154 | 160 | ||
155 | @Override | 161 | @Override |
156 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToServerRpcResponseMsg rpcResponse) { | ||
157 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_RPC_RESPONSE_TOPIC + rpcResponse.getRequestId(), rpcResponse.toByteArray())); | 162 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.ToServerRpcResponseMsg rpcResponse, String topicBase) { |
163 | + return Optional.of(createMqttPublishMsg(ctx, topicBase + rpcResponse.getRequestId(), rpcResponse.toByteArray())); | ||
158 | } | 164 | } |
159 | 165 | ||
160 | @Override | 166 | @Override |
161 | - public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.AttributeUpdateNotificationMsg notificationMsg) { | ||
162 | - return Optional.of(createMqttPublishMsg(ctx, MqttTopics.DEVICE_ATTRIBUTES_TOPIC, notificationMsg.toByteArray())); | 167 | + public Optional<MqttMessage> convertToPublish(MqttDeviceAwareSessionContext ctx, TransportProtos.AttributeUpdateNotificationMsg notificationMsg, String topic) { |
168 | + return Optional.of(createMqttPublishMsg(ctx, topic, notificationMsg.toByteArray())); | ||
163 | } | 169 | } |
164 | 170 | ||
165 | @Override | 171 | @Override |
@@ -213,17 +219,4 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | @@ -213,17 +219,4 @@ public class ProtoMqttAdaptor implements MqttTransportAdaptor { | ||
213 | private int getRequestId(String topicName, String topic) { | 219 | private int getRequestId(String topicName, String topic) { |
214 | return Integer.parseInt(topicName.substring(topic.length())); | 220 | return Integer.parseInt(topicName.substring(topic.length())); |
215 | } | 221 | } |
216 | - | ||
217 | - private Descriptors.Descriptor getDescriptor(Descriptors.Descriptor descriptor) throws AdaptorException { | ||
218 | - if (descriptor == null) { | ||
219 | - throw new AdaptorException("Failed to get dynamic message descriptor!"); | ||
220 | - } | ||
221 | - return descriptor; | ||
222 | - } | ||
223 | - | ||
224 | - private String dynamicMsgToJson(byte[] bytes, Descriptors.Descriptor descriptor) throws InvalidProtocolBufferException { | ||
225 | - DynamicMessage dynamicMessage = DynamicMessage.parseFrom(descriptor, bytes); | ||
226 | - return JsonFormat.printer().includingDefaultValueFields().print(dynamicMessage); | ||
227 | - } | ||
228 | - | ||
229 | } | 222 | } |
@@ -19,6 +19,7 @@ import com.google.gson.Gson; | @@ -19,6 +19,7 @@ import com.google.gson.Gson; | ||
19 | import com.google.gson.JsonElement; | 19 | import com.google.gson.JsonElement; |
20 | import com.google.gson.JsonObject; | 20 | import com.google.gson.JsonObject; |
21 | import com.google.gson.JsonParser; | 21 | import com.google.gson.JsonParser; |
22 | +import com.google.protobuf.Descriptors; | ||
22 | import com.google.protobuf.DynamicMessage; | 23 | import com.google.protobuf.DynamicMessage; |
23 | import com.google.protobuf.InvalidProtocolBufferException; | 24 | import com.google.protobuf.InvalidProtocolBufferException; |
24 | import com.google.protobuf.util.JsonFormat; | 25 | import com.google.protobuf.util.JsonFormat; |
@@ -190,4 +191,17 @@ public class ProtoConverter { | @@ -190,4 +191,17 @@ public class ProtoConverter { | ||
190 | throw new AdaptorException("Failed to convert ToDeviceRpcRequestMsg to Dynamic Rpc request message due to: ", e); | 191 | throw new AdaptorException("Failed to convert ToDeviceRpcRequestMsg to Dynamic Rpc request message due to: ", e); |
191 | } | 192 | } |
192 | } | 193 | } |
194 | + | ||
195 | + public static Descriptors.Descriptor validateDescriptor(Descriptors.Descriptor descriptor) throws AdaptorException { | ||
196 | + if (descriptor == null) { | ||
197 | + throw new AdaptorException("Failed to get dynamic message descriptor!"); | ||
198 | + } | ||
199 | + return descriptor; | ||
200 | + } | ||
201 | + | ||
202 | + public static String dynamicMsgToJson(byte[] bytes, Descriptors.Descriptor descriptor) throws InvalidProtocolBufferException { | ||
203 | + DynamicMessage dynamicMessage = DynamicMessage.parseFrom(descriptor, bytes); | ||
204 | + return JsonFormat.printer().includingDefaultValueFields().print(dynamicMessage); | ||
205 | + } | ||
206 | + | ||
193 | } | 207 | } |