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));
}
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());
}
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);
}
});
}
}
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));
}
}
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);
}
Aggregations