Search in sources :

Example 1 with WsTelemetryResponse

use of org.thingsboard.server.msa.mapper.WsTelemetryResponse in project thingsboard by thingsboard.

the class MqttGatewayClientTest method telemetryUpload.

@Test
public void telemetryUpload() throws Exception {
    WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "LATEST_TELEMETRY", CmdsType.TS_SUB_CMDS);
    mqttClient.publish("v1/gateway/telemetry", Unpooled.wrappedBuffer(createGatewayPayload(createdDevice.getName(), -1).toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(4, actualLatestTelemetry.getData().size());
    Assert.assertEquals(Sets.newHashSet("booleanKey", "stringKey", "doubleKey", "longKey"), actualLatestTelemetry.getLatestValues().keySet());
    Assert.assertTrue(verify(actualLatestTelemetry, "booleanKey", Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "stringKey", "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "longKey", Long.toString(73)));
}
Also used : WsTelemetryResponse(org.thingsboard.server.msa.mapper.WsTelemetryResponse) WsClient(org.thingsboard.server.msa.WsClient) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest) Test(org.junit.Test)

Example 2 with WsTelemetryResponse

use of org.thingsboard.server.msa.mapper.WsTelemetryResponse in project thingsboard by thingsboard.

the class MqttClientTest method telemetryUpload.

@Test
public void telemetryUpload() throws Exception {
    restClient.login("tenant@thingsboard.org", "tenant");
    Device device = createDevice("mqtt_");
    DeviceCredentials deviceCredentials = restClient.getDeviceCredentialsByDeviceId(device.getId()).get();
    WsClient wsClient = subscribeToWebSocket(device.getId(), "LATEST_TELEMETRY", CmdsType.TS_SUB_CMDS);
    MqttClient mqttClient = getMqttClient(deviceCredentials, null);
    mqttClient.publish("v1/devices/me/telemetry", Unpooled.wrappedBuffer(createPayload().toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(4, actualLatestTelemetry.getData().size());
    Assert.assertEquals(Sets.newHashSet("booleanKey", "stringKey", "doubleKey", "longKey"), actualLatestTelemetry.getLatestValues().keySet());
    Assert.assertTrue(verify(actualLatestTelemetry, "booleanKey", Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "stringKey", "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "longKey", 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) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) WsClient(org.thingsboard.server.msa.WsClient) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest)

Example 3 with WsTelemetryResponse

use of org.thingsboard.server.msa.mapper.WsTelemetryResponse in project thingsboard by thingsboard.

the class MqttClientTest method requestAttributeValuesFromServer.

@Test
public void requestAttributeValuesFromServer() 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);
    // Add a new client attribute
    JsonObject clientAttributes = new JsonObject();
    String clientAttributeValue = RandomStringUtils.randomAlphanumeric(8);
    clientAttributes.addProperty("clientAttr", clientAttributeValue);
    mqttClient.publish("v1/devices/me/attributes", Unpooled.wrappedBuffer(clientAttributes.toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received ws telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(1, actualLatestTelemetry.getData().size());
    Assert.assertEquals(Sets.newHashSet("clientAttr"), actualLatestTelemetry.getLatestValues().keySet());
    Assert.assertTrue(verify(actualLatestTelemetry, "clientAttr", clientAttributeValue));
    // Add a new shared attribute
    JsonObject sharedAttributes = new JsonObject();
    String sharedAttributeValue = RandomStringUtils.randomAlphanumeric(8);
    sharedAttributes.addProperty("sharedAttr", sharedAttributeValue);
    ResponseEntity sharedAttributesResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/plugins/telemetry/DEVICE/{deviceId}/SHARED_SCOPE", mapper.readTree(sharedAttributes.toString()), ResponseEntity.class, device.getId());
    Assert.assertTrue(sharedAttributesResponse.getStatusCode().is2xxSuccessful());
    // Subscribe to attributes response
    mqttClient.on("v1/devices/me/attributes/response/+", listener, MqttQoS.AT_LEAST_ONCE).get();
    // Wait until subscription is processed
    TimeUnit.SECONDS.sleep(3);
    // Request attributes
    JsonObject request = new JsonObject();
    request.addProperty("clientKeys", "clientAttr");
    request.addProperty("sharedKeys", "sharedAttr");
    mqttClient.publish("v1/devices/me/attributes/request/" + new Random().nextInt(100), Unpooled.wrappedBuffer(request.toString().getBytes())).get();
    MqttEvent event = listener.getEvents().poll(10, TimeUnit.SECONDS);
    AttributesResponse attributes = mapper.readValue(Objects.requireNonNull(event).getMessage(), AttributesResponse.class);
    log.info("Received telemetry: {}", attributes);
    Assert.assertEquals(1, attributes.getClient().size());
    Assert.assertEquals(clientAttributeValue, attributes.getClient().get("clientAttr"));
    Assert.assertEquals(1, attributes.getShared().size());
    Assert.assertEquals(sharedAttributeValue, attributes.getShared().get("sharedAttr"));
    restClient.getRestTemplate().delete(HTTPS_URL + "/api/device/" + device.getId());
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) ResponseEntity(org.springframework.http.ResponseEntity) AttributesResponse(org.thingsboard.server.msa.mapper.AttributesResponse) 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)

Example 4 with WsTelemetryResponse

use of org.thingsboard.server.msa.mapper.WsTelemetryResponse in project thingsboard by thingsboard.

the class WsClient method onMessage.

@Override
public void onMessage(String message) {
    if (!firstReplyReceived) {
        firstReplyReceived = true;
        firstReply.countDown();
    } else {
        try {
            WsTelemetryResponse response = mapper.readValue(message, WsTelemetryResponse.class);
            if (!response.getData().isEmpty()) {
                this.message = response;
                latch.countDown();
            }
        } catch (IOException e) {
            log.error("ws message can't be read");
        }
    }
}
Also used : WsTelemetryResponse(org.thingsboard.server.msa.mapper.WsTelemetryResponse) IOException(java.io.IOException)

Example 5 with WsTelemetryResponse

use of org.thingsboard.server.msa.mapper.WsTelemetryResponse in project thingsboard by thingsboard.

the class HttpClientTest method telemetryUpload.

@Test
public void telemetryUpload() throws Exception {
    restClient.login("tenant@thingsboard.org", "tenant");
    Device device = createDevice("http_");
    DeviceCredentials deviceCredentials = restClient.getDeviceCredentialsByDeviceId(device.getId()).get();
    WsClient wsClient = subscribeToWebSocket(device.getId(), "LATEST_TELEMETRY", CmdsType.TS_SUB_CMDS);
    ResponseEntity deviceTelemetryResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/v1/{credentialsId}/telemetry", mapper.readTree(createPayload().toString()), ResponseEntity.class, deviceCredentials.getCredentialsId());
    Assert.assertTrue(deviceTelemetryResponse.getStatusCode().is2xxSuccessful());
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    wsClient.closeBlocking();
    Assert.assertEquals(Sets.newHashSet("booleanKey", "stringKey", "doubleKey", "longKey"), actualLatestTelemetry.getLatestValues().keySet());
    Assert.assertTrue(verify(actualLatestTelemetry, "booleanKey", Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "stringKey", "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "longKey", Long.toString(73)));
    restClient.deleteDevice(device.getId());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Device(org.thingsboard.server.common.data.Device) WsTelemetryResponse(org.thingsboard.server.msa.mapper.WsTelemetryResponse) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) WsClient(org.thingsboard.server.msa.WsClient) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest) Test(org.junit.Test)

Aggregations

WsTelemetryResponse (org.thingsboard.server.msa.mapper.WsTelemetryResponse)10 AbstractContainerTest (org.thingsboard.server.msa.AbstractContainerTest)9 WsClient (org.thingsboard.server.msa.WsClient)9 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)6 Test (org.junit.Test)5 Device (org.thingsboard.server.common.data.Device)5 JsonObject (com.google.gson.JsonObject)4 MqttClient (org.thingsboard.mqtt.MqttClient)4 ResponseEntity (org.springframework.http.ResponseEntity)3 IOException (java.io.IOException)1 AttributesResponse (org.thingsboard.server.msa.mapper.AttributesResponse)1