Search in sources :

Example 6 with WsClient

use of org.thingsboard.server.msa.WsClient in project thingsboard by thingsboard.

the class MqttGatewayClientTest method requestAttributeValuesFromServer.

@Test
public void requestAttributeValuesFromServer() throws Exception {
    WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "CLIENT_SCOPE", CmdsType.ATTR_SUB_CMDS);
    // Add a new client attribute
    JsonObject clientAttributes = new JsonObject();
    String clientAttributeValue = RandomStringUtils.randomAlphanumeric(8);
    clientAttributes.addProperty("clientAttr", clientAttributeValue);
    JsonObject gatewayClientAttributes = new JsonObject();
    gatewayClientAttributes.add(createdDevice.getName(), clientAttributes);
    mqttClient.publish("v1/gateway/attributes", Unpooled.wrappedBuffer(gatewayClientAttributes.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);
    // Subscribe for attribute update event
    mqttClient.on("v1/gateway/attributes", listener, MqttQoS.AT_LEAST_ONCE).get();
    ResponseEntity sharedAttributesResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/plugins/telemetry/DEVICE/{deviceId}/SHARED_SCOPE", mapper.readTree(sharedAttributes.toString()), ResponseEntity.class, createdDevice.getId());
    Assert.assertTrue(sharedAttributesResponse.getStatusCode().is2xxSuccessful());
    MqttEvent sharedAttributeEvent = listener.getEvents().poll(10, TimeUnit.SECONDS);
    // Catch attribute update event
    Assert.assertNotNull(sharedAttributeEvent);
    Assert.assertEquals("v1/gateway/attributes", sharedAttributeEvent.getTopic());
    // Subscribe to attributes response
    mqttClient.on("v1/gateway/attributes/response", listener, MqttQoS.AT_LEAST_ONCE).get();
    // Wait until subscription is processed
    TimeUnit.SECONDS.sleep(3);
    checkAttribute(true, clientAttributeValue);
    checkAttribute(false, sharedAttributeValue);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) WsTelemetryResponse(org.thingsboard.server.msa.mapper.WsTelemetryResponse) JsonObject(com.google.gson.JsonObject) WsClient(org.thingsboard.server.msa.WsClient) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest) Test(org.junit.Test)

Example 7 with WsClient

use of org.thingsboard.server.msa.WsClient in project thingsboard by thingsboard.

the class MqttGatewayClientTest method telemetryUploadWithTs.

@Test
public void telemetryUploadWithTs() throws Exception {
    long ts = 1451649600512L;
    restClient.login("tenant@thingsboard.org", "tenant");
    WsClient wsClient = subscribeToWebSocket(createdDevice.getId(), "LATEST_TELEMETRY", CmdsType.TS_SUB_CMDS);
    mqttClient.publish("v1/gateway/telemetry", Unpooled.wrappedBuffer(createGatewayPayload(createdDevice.getName(), ts).toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(4, actualLatestTelemetry.getData().size());
    Assert.assertEquals(getExpectedLatestValues(ts), actualLatestTelemetry.getLatestValues());
    Assert.assertTrue(verify(actualLatestTelemetry, "booleanKey", ts, Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "stringKey", ts, "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", ts, Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "longKey", ts, 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 8 with WsClient

use of org.thingsboard.server.msa.WsClient in project thingsboard by thingsboard.

the class MqttClientTest method telemetryUploadWithTs.

@Test
public void telemetryUploadWithTs() throws Exception {
    long ts = 1451649600512L;
    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(ts).toString().getBytes())).get();
    WsTelemetryResponse actualLatestTelemetry = wsClient.getLastMessage();
    log.info("Received telemetry: {}", actualLatestTelemetry);
    wsClient.closeBlocking();
    Assert.assertEquals(4, actualLatestTelemetry.getData().size());
    Assert.assertEquals(getExpectedLatestValues(ts), actualLatestTelemetry.getLatestValues());
    Assert.assertTrue(verify(actualLatestTelemetry, "booleanKey", ts, Boolean.TRUE.toString()));
    Assert.assertTrue(verify(actualLatestTelemetry, "stringKey", ts, "value1"));
    Assert.assertTrue(verify(actualLatestTelemetry, "doubleKey", ts, Double.toString(42.0)));
    Assert.assertTrue(verify(actualLatestTelemetry, "longKey", ts, 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 9 with WsClient

use of org.thingsboard.server.msa.WsClient 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

AbstractContainerTest (org.thingsboard.server.msa.AbstractContainerTest)9 WsClient (org.thingsboard.server.msa.WsClient)9 WsTelemetryResponse (org.thingsboard.server.msa.mapper.WsTelemetryResponse)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 AttributesResponse (org.thingsboard.server.msa.mapper.AttributesResponse)1