Search in sources :

Example 6 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class BaseAttributesServiceTest method findAll.

@Test
public void findAll() throws Exception {
    DeviceId deviceId = new DeviceId(UUIDs.timeBased());
    KvEntry attrAOldValue = new StringDataEntry("A", "value1");
    AttributeKvEntry attrAOld = new BaseAttributeKvEntry(attrAOldValue, 42L);
    KvEntry attrANewValue = new StringDataEntry("A", "value2");
    AttributeKvEntry attrANew = new BaseAttributeKvEntry(attrANewValue, 73L);
    KvEntry attrBNewValue = new StringDataEntry("B", "value3");
    AttributeKvEntry attrBNew = new BaseAttributeKvEntry(attrBNewValue, 73L);
    attributesService.save(deviceId, DataConstants.CLIENT_SCOPE, Collections.singletonList(attrAOld)).get();
    attributesService.save(deviceId, DataConstants.CLIENT_SCOPE, Collections.singletonList(attrANew)).get();
    attributesService.save(deviceId, DataConstants.CLIENT_SCOPE, Collections.singletonList(attrBNew)).get();
    List<AttributeKvEntry> saved = attributesService.findAll(deviceId, DataConstants.CLIENT_SCOPE).get();
    Assert.assertNotNull(saved);
    Assert.assertEquals(2, saved.size());
    Assert.assertEquals(attrANew, saved.get(0));
    Assert.assertEquals(attrBNew, saved.get(1));
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Example 7 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class BaseEventServiceTest method saveEvent.

@Test
public void saveEvent() throws Exception {
    DeviceId devId = new DeviceId(UUIDs.timeBased());
    Event event = generateEvent(null, devId, "ALARM", UUIDs.timeBased().toString());
    Event saved = eventService.save(event);
    Optional<Event> loaded = eventService.findEvent(event.getTenantId(), event.getEntityId(), event.getType(), event.getUid());
    Assert.assertTrue(loaded.isPresent());
    Assert.assertNotNull(loaded.get());
    Assert.assertEquals(saved, loaded.get());
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Event(org.thingsboard.server.common.data.Event) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Example 8 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceMessagingRuleMsgHandler method processSendMsg.

private void processSendMsg(PluginContext ctx, PendingRpcRequestMetadata requestMd, ToServerRpcRequestMsg request) {
    JsonObject params = new JsonParser().parse(request.getParams()).getAsJsonObject();
    String targetDeviceIdStr = params.get(DEVICE_ID).getAsString();
    DeviceId targetDeviceId = DeviceId.fromString(targetDeviceIdStr);
    boolean oneWay = isOneWay(params);
    long timeout = getTimeout(params);
    if (timeout <= 0) {
        replyWithError(ctx, requestMd, "Timeout can't be negative!");
    } else if (timeout > configuration.getMaxTimeout()) {
        replyWithError(ctx, requestMd, "Timeout is too large!");
    } else {
        ctx.getDevice(targetDeviceId, new PluginCallback<Device>() {

            @Override
            public void onSuccess(PluginContext ctx, Device targetDevice) {
                UUID uid = UUID.randomUUID();
                if (targetDevice == null) {
                    replyWithError(ctx, requestMd, RpcError.NOT_FOUND);
                } else if (!requestMd.getCustomerId().isNullUid() && requestMd.getTenantId().equals(targetDevice.getTenantId()) && requestMd.getCustomerId().equals(targetDevice.getCustomerId())) {
                    pendingMsgs.put(uid, requestMd);
                    log.trace("[{}] Forwarding {} to [{}]", uid, params, targetDeviceId);
                    ToDeviceRpcRequestBody requestBody = new ToDeviceRpcRequestBody(ON_MSG_METHOD_NAME, GSON.toJson(params.get("body")));
                    ctx.sendRpcRequest(new ToDeviceRpcRequest(uid, null, targetDevice.getTenantId(), targetDeviceId, oneWay, System.currentTimeMillis() + timeout, requestBody));
                } else {
                    replyWithError(ctx, requestMd, RpcError.FORBIDDEN);
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                replyWithError(ctx, requestMd, RpcError.INTERNAL);
            }
        });
    }
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) JsonObject(com.google.gson.JsonObject) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) JsonParser(com.google.gson.JsonParser) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 9 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class RpcRestMsgHandler method handleHttpPostRequest.

@Override
public void handleHttpPostRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
    boolean valid = false;
    RestRequest request = msg.getRequest();
    try {
        String[] pathParams = request.getPathParams();
        if (pathParams.length == 2) {
            String method = pathParams[0].toUpperCase();
            if (DataConstants.ONEWAY.equals(method) || DataConstants.TWOWAY.equals(method)) {
                final TenantId tenantId = ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId();
                JsonNode rpcRequestBody = jsonMapper.readTree(request.getRequestBody());
                RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), jsonMapper.writeValueAsString(rpcRequestBody.get("params")));
                if (rpcRequestBody.has("timeout")) {
                    cmd.setTimeout(rpcRequestBody.get("timeout").asLong());
                }
                boolean oneWay = DataConstants.ONEWAY.equals(method);
                DeviceId deviceId = DeviceId.fromString(pathParams[1]);
                valid = handleDeviceRPCRequest(ctx, msg, tenantId, deviceId, cmd, oneWay);
            }
        }
    } catch (IOException e) {
        log.debug("Failed to process POST request due to IO exception", e);
    } catch (RuntimeException e) {
        log.debug("Failed to process POST request due to Runtime exception", e);
    }
    if (!valid) {
        msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RpcRequest(org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 10 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class BaseTimeseriesServiceTest method testFindAllLatest.

@Test
public void testFindAllLatest() throws Exception {
    DeviceId deviceId = new DeviceId(UUIDs.timeBased());
    saveEntries(deviceId, TS - 2);
    saveEntries(deviceId, TS - 1);
    saveEntries(deviceId, TS);
    List<TsKvEntry> tsList = tsService.findAllLatest(deviceId).get();
    assertNotNull(tsList);
    assertEquals(4, tsList.size());
    for (int i = 0; i < tsList.size(); i++) {
        assertEquals(TS, tsList.get(i).getTs());
    }
    Collections.sort(tsList, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));
    List<TsKvEntry> expected = Arrays.asList(toTsEntry(TS, stringKvEntry), toTsEntry(TS, longKvEntry), toTsEntry(TS, doubleKvEntry), toTsEntry(TS, booleanKvEntry));
    Collections.sort(expected, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));
    assertEquals(expected, tsList);
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Aggregations

DeviceId (org.thingsboard.server.common.data.id.DeviceId)42 Test (org.junit.Test)21 Device (org.thingsboard.server.common.data.Device)12 TenantId (org.thingsboard.server.common.data.id.TenantId)10 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)9 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)8 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)8 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)8 ArrayList (java.util.ArrayList)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Event (org.thingsboard.server.common.data.Event)6 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 UUID (java.util.UUID)5 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 DeviceAttributes (org.thingsboard.server.extensions.api.device.DeviceAttributes)5 DeviceMetaData (org.thingsboard.server.extensions.api.device.DeviceMetaData)5 Customer (org.thingsboard.server.common.data.Customer)3 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)3