use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.
the class DeviceAttributesFilterTest method basicClientAttributesTest.
@Test
public void basicClientAttributesTest() {
DeviceAttributesFilter filter = new DeviceAttributesFilter();
filter.init(wrap("doubleValue == 1.0 && booleanValue == false"));
List<AttributeKvEntry> clientAttributes = new ArrayList<>();
clientAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
DeviceAttributes attributes = new DeviceAttributes(clientAttributes, new ArrayList<>(), new ArrayList<>());
Mockito.when(ruleCtx.getDeviceMetaData()).thenReturn(new DeviceMetaData(new DeviceId(UUID.randomUUID()), "A", "A", attributes));
Assert.assertTrue(filter.filter(ruleCtx, null));
filter.stop();
}
use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.
the class DeviceAttributesFilterTest method basicClientAttributesStressTest.
@Test(timeout = 30000)
public void basicClientAttributesStressTest() {
DeviceAttributesFilter filter = new DeviceAttributesFilter();
filter.init(wrap("doubleValue == 1.0 && booleanValue == false"));
List<AttributeKvEntry> clientAttributes = new ArrayList<>();
clientAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
DeviceAttributes attributes = new DeviceAttributes(clientAttributes, new ArrayList<>(), new ArrayList<>());
Mockito.when(ruleCtx.getDeviceMetaData()).thenReturn(new DeviceMetaData(new DeviceId(UUID.randomUUID()), "A", "A", attributes));
for (int i = 0; i < 10000; i++) {
Assert.assertTrue(filter.filter(ruleCtx, null));
}
filter.stop();
}
use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.
the class RpcRuleMsgHandler method handle.
private void handle(final PluginContext ctx, TenantId tenantId, RuleId ruleId, ServerSideRpcCallActionMsg msg) {
DeviceId deviceId = new DeviceId(UUID.fromString(msg.getDeviceId()));
ctx.checkAccess(deviceId, new PluginCallback<Void>() {
@Override
public void onSuccess(PluginContext dummy, Void value) {
try {
List<EntityId> deviceIds;
if (StringUtils.isEmpty(msg.getFromDeviceRelation()) && StringUtils.isEmpty(msg.getToDeviceRelation())) {
deviceIds = Collections.singletonList(deviceId);
} else if (!StringUtils.isEmpty(msg.getFromDeviceRelation())) {
List<EntityRelation> relations = ctx.findByFromAndType(deviceId, msg.getFromDeviceRelation()).get();
deviceIds = relations.stream().map(EntityRelation::getTo).collect(Collectors.toList());
} else {
List<EntityRelation> relations = ctx.findByToAndType(deviceId, msg.getFromDeviceRelation()).get();
deviceIds = relations.stream().map(EntityRelation::getFrom).collect(Collectors.toList());
}
ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(msg.getRpcCallMethod(), msg.getRpcCallBody());
long expirationTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(msg.getRpcCallTimeoutInSec());
for (EntityId address : deviceIds) {
DeviceId tmpId = new DeviceId(address.getId());
ctx.checkAccess(tmpId, new PluginCallback<Void>() {
@Override
public void onSuccess(PluginContext ctx, Void value) {
ctx.sendRpcRequest(new ToDeviceRpcRequest(UUID.randomUUID(), null, tenantId, tmpId, true, expirationTime, body));
log.trace("[{}] Sent RPC Call Action msg", tmpId);
}
@Override
public void onFailure(PluginContext ctx, Exception e) {
log.info("[{}] Failed to process RPC Call Action msg", tmpId, e);
}
});
}
} catch (Exception e) {
log.info("Failed to process RPC Call Action msg", e);
}
}
@Override
public void onFailure(PluginContext dummy, Exception e) {
log.info("[{}] Failed to process RPC Call Action msg", deviceId, e);
}
});
}
use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.
the class DeviceActorMessageProcessor method processAttributesUpdate.
void processAttributesUpdate(ActorContext context, DeviceAttributesEventNotificationMsg msg) {
refreshAttributes(msg);
if (attributeSubscriptions.size() > 0) {
ToDeviceMsg notification = null;
if (msg.isDeleted()) {
List<AttributeKey> sharedKeys = msg.getDeletedKeys().stream().filter(key -> DataConstants.SHARED_SCOPE.equals(key.getScope())).collect(Collectors.toList());
notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromDeleted(sharedKeys));
} else {
if (DataConstants.SHARED_SCOPE.equals(msg.getScope())) {
List<AttributeKvEntry> attributes = new ArrayList<>(msg.getValues());
if (attributes.size() > 0) {
notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromShared(attributes));
} else {
logger.debug("[{}] No public server side attributes changed!", deviceId);
}
}
}
if (notification != null) {
ToDeviceMsg finalNotification = notification;
attributeSubscriptions.entrySet().forEach(sub -> {
ToDeviceSessionActorMsg response = new BasicToDeviceSessionActorMsg(finalNotification, sub.getKey());
sendMsgToSessionActor(response, sub.getValue().getServer());
});
}
} else {
logger.debug("[{}] No registered attributes subscriptions to process!", deviceId);
}
}
use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.
the class BasicRpcSessionListener method deserialize.
private static ToDeviceRpcRequestPluginMsg deserialize(ServerAddress serverAddress, ClusterAPIProtos.ToDeviceRpcRequestRpcMessage msg) {
ClusterAPIProtos.PluginAddress address = msg.getAddress();
TenantId pluginTenantId = new TenantId(toUUID(address.getTenantId()));
PluginId pluginId = new PluginId(toUUID(address.getPluginId()));
TenantId deviceTenantId = new TenantId(toUUID(msg.getDeviceTenantId()));
DeviceId deviceId = new DeviceId(toUUID(msg.getDeviceId()));
ToDeviceRpcRequestBody requestBody = new ToDeviceRpcRequestBody(msg.getMethod(), msg.getParams());
ToDeviceRpcRequest request = new ToDeviceRpcRequest(toUUID(msg.getMsgId()), null, deviceTenantId, deviceId, msg.getOneway(), msg.getExpTime(), requestBody);
return new ToDeviceRpcRequestPluginMsg(serverAddress, pluginId, pluginTenantId, request);
}
Aggregations