27 changed files with 501 additions and 71 deletions
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.rule.engine.data; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Data |
|||
public class DeviceRelationsQuery { |
|||
private EntitySearchDirection direction; |
|||
private int maxLevel = 1; |
|||
private String relationType; |
|||
private List<String> deviceTypes; |
|||
} |
|||
@ -0,0 +1,103 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.rule.engine.metadata; |
|||
|
|||
import com.google.common.base.Function; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.apache.commons.collections.CollectionUtils; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.api.TbNode; |
|||
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.TbNodeException; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|||
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
|
|||
import java.util.List; |
|||
|
|||
import static org.thingsboard.rule.engine.DonAsynchron.withCallback; |
|||
import static org.thingsboard.rule.engine.api.TbRelationTypes.FAILURE; |
|||
import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS; |
|||
import static org.thingsboard.server.common.data.DataConstants.*; |
|||
|
|||
public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> implements TbNode { |
|||
|
|||
protected C config; |
|||
|
|||
@Override |
|||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
|||
this.config = loadGetAttributesNodeConfig(configuration); |
|||
} |
|||
|
|||
protected abstract C loadGetAttributesNodeConfig(TbNodeConfiguration configuration) throws TbNodeException; |
|||
|
|||
@Override |
|||
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException { |
|||
try { |
|||
withCallback( |
|||
findEntityAsync(ctx, msg.getOriginator()), |
|||
entityId -> safePutAttributes(ctx, msg, entityId), |
|||
t -> ctx.tellError(msg, t), ctx.getDbCallbackExecutor()); |
|||
} catch (Throwable th) { |
|||
ctx.tellError(msg, th); |
|||
} |
|||
} |
|||
|
|||
private void safePutAttributes(TbContext ctx, TbMsg msg, T entityId) { |
|||
if(entityId == null || entityId.isNullUid()) { |
|||
ctx.tellNext(msg, FAILURE); |
|||
return; |
|||
} |
|||
ListenableFuture<List<Void>> allFutures = Futures.allAsList( |
|||
putLatestTelemetry(ctx, entityId, msg, config.getLatestTsKeyNames()), |
|||
putAttrAsync(ctx, entityId, msg, CLIENT_SCOPE, config.getClientAttributeNames(), "cs_"), |
|||
putAttrAsync(ctx, entityId, msg, SHARED_SCOPE, config.getSharedAttributeNames(), "shared_"), |
|||
putAttrAsync(ctx, entityId, msg, SERVER_SCOPE, config.getServerAttributeNames(), "ss_") |
|||
); |
|||
withCallback(allFutures, i -> ctx.tellNext(msg, SUCCESS), t -> ctx.tellError(msg, t)); |
|||
} |
|||
|
|||
private ListenableFuture<Void> putAttrAsync(TbContext ctx, EntityId entityId, TbMsg msg, String scope, List<String> keys, String prefix) { |
|||
if (CollectionUtils.isEmpty(keys)) { |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
ListenableFuture<List<AttributeKvEntry>> latest = ctx.getAttributesService().find(entityId, scope, keys); |
|||
return Futures.transform(latest, (Function<? super List<AttributeKvEntry>, Void>) l -> { |
|||
l.forEach(r -> msg.getMetaData().putValue(prefix + r.getKey(), r.getValueAsString())); |
|||
return null; |
|||
}); |
|||
} |
|||
|
|||
private ListenableFuture<Void> putLatestTelemetry(TbContext ctx, EntityId entityId, TbMsg msg, List<String> keys) { |
|||
if (CollectionUtils.isEmpty(keys)) { |
|||
return Futures.immediateFuture(null); |
|||
} |
|||
ListenableFuture<List<TsKvEntry>> latest = ctx.getTimeseriesService().findLatest(entityId, keys); |
|||
return Futures.transform(latest, (Function<? super List<TsKvEntry>, Void>) l -> { |
|||
l.forEach(r -> msg.getMetaData().putValue(r.getKey(), r.getValueAsString())); |
|||
return null; |
|||
}); |
|||
} |
|||
|
|||
@Override |
|||
public void destroy() { |
|||
|
|||
} |
|||
|
|||
protected abstract ListenableFuture<T> findEntityAsync(TbContext ctx, EntityId originator); |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.rule.engine.metadata; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.rule.engine.TbNodeUtils; |
|||
import org.thingsboard.rule.engine.api.RuleNode; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.TbNodeException; |
|||
import org.thingsboard.rule.engine.util.EntitiesRelatedDeviceIdAsyncLoader; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.plugin.ComponentType; |
|||
|
|||
@Slf4j |
|||
@RuleNode(type = ComponentType.ENRICHMENT, |
|||
name = "device attributes", |
|||
configClazz = TbGetDeviceAttrNodeConfiguration.class, |
|||
nodeDescription = "Add Originators Related Device Attributes or Latest Telemetry into Message Metadata", |
|||
nodeDetails = "If Attributes enrichment configured, <b>CLIENT/SHARED/SERVER</b> attributes are added into Message metadata " + |
|||
"with specific prefix: <i>cs/shared/ss</i>. Latest telemetry value added into metadata without prefix. " + |
|||
"To access those attributes in other nodes this template can be used " + |
|||
"<code>metadata.cs_temperature</code> or <code>metadata.shared_limit</code> ", |
|||
uiResources = {"static/rulenode/rulenode-core-config.js"}, |
|||
configDirective = "tbEnrichmentNodeDeviceAttributesConfig") |
|||
public class TbGetDeviceAttrNode extends TbAbstractGetAttributesNode<TbGetDeviceAttrNodeConfiguration, DeviceId> { |
|||
|
|||
@Override |
|||
protected TbGetDeviceAttrNodeConfiguration loadGetAttributesNodeConfig(TbNodeConfiguration configuration) throws TbNodeException { |
|||
return TbNodeUtils.convert(configuration, TbGetDeviceAttrNodeConfiguration.class); |
|||
} |
|||
|
|||
@Override |
|||
protected ListenableFuture<DeviceId> findEntityAsync(TbContext ctx, EntityId originator) { |
|||
return EntitiesRelatedDeviceIdAsyncLoader.findDeviceAsync(ctx, originator, config.getDeviceRelationsQuery()); |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.rule.engine.metadata; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.rule.engine.data.DeviceRelationsQuery; |
|||
import org.thingsboard.server.common.data.relation.EntityRelation; |
|||
import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|||
|
|||
import java.util.Collections; |
|||
|
|||
@Data |
|||
public class TbGetDeviceAttrNodeConfiguration extends TbGetAttributesNodeConfiguration { |
|||
|
|||
private DeviceRelationsQuery deviceRelationsQuery; |
|||
|
|||
@Override |
|||
public TbGetDeviceAttrNodeConfiguration defaultConfiguration() { |
|||
TbGetDeviceAttrNodeConfiguration configuration = new TbGetDeviceAttrNodeConfiguration(); |
|||
configuration.setClientAttributeNames(Collections.emptyList()); |
|||
configuration.setSharedAttributeNames(Collections.emptyList()); |
|||
configuration.setServerAttributeNames(Collections.emptyList()); |
|||
configuration.setLatestTsKeyNames(Collections.emptyList()); |
|||
|
|||
DeviceRelationsQuery deviceRelationsQuery = new DeviceRelationsQuery(); |
|||
deviceRelationsQuery.setDirection(EntitySearchDirection.FROM); |
|||
deviceRelationsQuery.setMaxLevel(1); |
|||
deviceRelationsQuery.setRelationType(EntityRelation.CONTAINS_TYPE); |
|||
deviceRelationsQuery.setDeviceTypes(Collections.singletonList("default")); |
|||
|
|||
configuration.setDeviceRelationsQuery(deviceRelationsQuery); |
|||
|
|||
return configuration; |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
/** |
|||
* Copyright © 2016-2018 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.rule.engine.util; |
|||
|
|||
import com.google.common.util.concurrent.AsyncFunction; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.apache.commons.collections.CollectionUtils; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.data.DeviceRelationsQuery; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.device.DeviceSearchQuery; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.relation.RelationsSearchParameters; |
|||
import org.thingsboard.server.dao.device.DeviceService; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class EntitiesRelatedDeviceIdAsyncLoader { |
|||
|
|||
public static ListenableFuture<DeviceId> findDeviceAsync(TbContext ctx, EntityId originator, |
|||
DeviceRelationsQuery deviceRelationsQuery) { |
|||
DeviceService deviceService = ctx.getDeviceService(); |
|||
DeviceSearchQuery query = buildQuery(originator, deviceRelationsQuery); |
|||
|
|||
ListenableFuture<List<Device>> asyncDevices = deviceService.findDevicesByQuery(query); |
|||
|
|||
return Futures.transform(asyncDevices, (AsyncFunction<List<Device>, DeviceId>) |
|||
d -> CollectionUtils.isNotEmpty(d) ? Futures.immediateFuture(d.get(0).getId()) |
|||
: Futures.immediateFuture(null)); |
|||
} |
|||
|
|||
private static DeviceSearchQuery buildQuery(EntityId originator, DeviceRelationsQuery deviceRelationsQuery) { |
|||
DeviceSearchQuery query = new DeviceSearchQuery(); |
|||
RelationsSearchParameters parameters = new RelationsSearchParameters(originator, |
|||
deviceRelationsQuery.getDirection(), deviceRelationsQuery.getMaxLevel()); |
|||
query.setParameters(parameters); |
|||
query.setRelationType(deviceRelationsQuery.getRelationType()); |
|||
query.setDeviceTypes(deviceRelationsQuery.getDeviceTypes()); |
|||
return query; |
|||
} |
|||
} |
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue