Search in sources :

Example 11 with MqttClient

use of org.thingsboard.mqtt.MqttClient in project thingsboard by thingsboard.

the class MqttClientTest method publishAttributeUpdateToServer.

@Test
public void publishAttributeUpdateToServer() throws Exception {
    restClient.login("tenant@thingsboard.org", "tenant");
    Device device = createDevice("mqtt_");
    DeviceCredentials deviceCredentials = restClient.getDeviceCredentialsByDeviceId(device.getId()).get();
    WsClient wsClient = subscribeToWebSocket(device.getId(), "CLIENT_SCOPE", CmdsType.ATTR_SUB_CMDS);
    MqttMessageListener listener = new MqttMessageListener();
    MqttClient mqttClient = getMqttClient(deviceCredentials, listener);
    JsonObject clientAttributes = new JsonObject();
    clientAttributes.addProperty("attr1", "value1");
    clientAttributes.addProperty("attr2", true);
    clientAttributes.addProperty("attr3", 42.0);
    clientAttributes.addProperty("attr4", 73);
    mqttClient.publish("v1/devices/me/attributes", Unpooled.wrappedBuffer(clientAttributes.toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(4, actualLatestTelemetry.getData().size());
    Assert.assertEquals(Sets.newHashSet("attr1", "attr2", "attr3", "attr4"), actualLatestTelemetry.getLatestValues().keySet());
    Assert.assertTrue(verify(actualLatestTelemetry, "attr1", "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "attr2", Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "attr3", Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "attr4", Long.toString(73)));
    restClient.getRestTemplate().delete(HTTPS_URL + "/api/device/" + device.getId());
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) Device(org.thingsboard.server.common.data.Device) WsTelemetryResponse(org.thingsboard.server.msa.mapper.WsTelemetryResponse) JsonObject(com.google.gson.JsonObject) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) WsClient(org.thingsboard.server.msa.WsClient) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest)

Aggregations

MqttClient (org.thingsboard.mqtt.MqttClient)11 Device (org.thingsboard.server.common.data.Device)8 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)8 AbstractContainerTest (org.thingsboard.server.msa.AbstractContainerTest)8 JsonObject (com.google.gson.JsonObject)5 WsClient (org.thingsboard.server.msa.WsClient)4 WsTelemetryResponse (org.thingsboard.server.msa.mapper.WsTelemetryResponse)4 ResponseEntity (org.springframework.http.ResponseEntity)3 MqttClientConfig (org.thingsboard.mqtt.MqttClientConfig)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 MqttConnectResult (org.thingsboard.mqtt.MqttConnectResult)1 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)1 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)1 AttributesResponse (org.thingsboard.server.msa.mapper.AttributesResponse)1