Was added new rule node

This commit is contained in:
viktorbasanets 2018-09-14 15:35:02 +03:00
commit 06d59ed62a
12 changed files with 197 additions and 13 deletions

View File

@ -48,6 +48,7 @@ import org.thingsboard.server.dao.attributes.AttributesService;
import org.thingsboard.server.dao.audit.AuditLogService;
import org.thingsboard.server.dao.customer.CustomerService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.event.EventService;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.rule.RuleChainService;
@ -159,6 +160,10 @@ public class ActorSystemContext {
@Getter
private AuditLogService auditLogService;
@Autowired
@Getter
private EntityViewService entityViewService;
@Autowired
@Getter
private TelemetrySubscriptionService tsSubService;

View File

@ -41,6 +41,7 @@ import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.dao.attributes.AttributesService;
import org.thingsboard.server.dao.customer.CustomerService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.dao.tenant.TenantService;
@ -212,6 +213,11 @@ class DefaultTbContext implements TbContext {
return mainCtx.getRelationService();
}
@Override
public EntityViewService getEntityViewService() {
return mainCtx.getEntityViewService();
}
@Override
public MailService getMailService() {
if (mainCtx.isAllowSystemMailService()) {

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.dao.entityview;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.Select;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntitySubtype;
@ -105,4 +106,10 @@ public class CassandraEntityViewDao extends CassandraAbstractSearchTextDao<Entit
public List<EntityView> findEntityViewsByTenantIdAndCustomerIdAndEntityId(UUID tenantId, UUID customerId, UUID entityId, TextPageLink pageLink) {
return null;
}
@Override
public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId) {
// TODO: implement this
return null;
}
}

View File

@ -15,6 +15,8 @@
*/
package org.thingsboard.server.dao.entityview;
import com.google.common.util.concurrent.ListenableFuture;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.page.TextPageLink;
import org.thingsboard.server.dao.Dao;
@ -91,4 +93,6 @@ public interface EntityViewDao extends Dao<EntityView> {
UUID customerId,
UUID entityId,
TextPageLink pageLink);
ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId);
}

View File

@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntitySubtype;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.device.DeviceSearchQuery;
import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery;
import org.thingsboard.server.common.data.id.*;
@ -65,4 +66,6 @@ public interface EntityViewService {
ListenableFuture<EntityView> findEntityViewByIdAsync(EntityViewId entityViewId);
ListenableFuture<List<EntityView>> findEntityViewsByQuery(EntityViewSearchQuery query);
ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId);
}

View File

@ -279,6 +279,14 @@ public class EntityViewServiceImpl extends AbstractEntityService implements Enti
return entityViews;
}
@Override
public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(TenantId tenantId, EntityId entityId) {
log.trace("Executing findEntityViewsByTenantIdAndEntityIdAsync, tenantId [{}], entityId [{}]", tenantId, entityId);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
validateId(entityId.getId(), "Incorrect entityId" + entityId);
return entityViewDao.findEntityViewsByTenantIdAndEntityIdAsync(tenantId.getId(), entityId.getId());
}
private DataValidator<EntityView> entityViewValidator =
new DataValidator<EntityView>() {

View File

@ -19,6 +19,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.dao.model.sql.EntityViewEntity;
import org.thingsboard.server.dao.util.SqlDao;
@ -78,4 +79,6 @@ public interface EntityViewRepository extends CrudRepository<EntityViewEntity, S
List<String> entityViewsIds);
List<EntityViewEntity> findAllByTenantIdAndIdIn(String tenantId, List<String> entityViewsIds);
List<EntityViewEntity> findAllByTenantIdAndEntityId(String tenantId, String entityId);
}

View File

@ -23,6 +23,7 @@ import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntitySubtype;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.UUIDConverter;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.TextPageLink;
@ -40,6 +41,7 @@ import java.util.Optional;
import java.util.UUID;
import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID;
import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUIDs;
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR;
/**
@ -121,4 +123,10 @@ public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity,
new PageRequest(0, pageLink.getLimit())
));
}
@Override
public ListenableFuture<List<EntityView>> findEntityViewsByTenantIdAndEntityIdAsync(UUID tenantId, UUID entityId) {
return service.submit(() -> DaoUtil.convertDataList(
entityViewRepository.findAllByTenantIdAndEntityId(UUIDConverter.fromTimeUUID(tenantId), UUIDConverter.fromTimeUUID(entityId))));
}
}

View File

@ -26,6 +26,7 @@ import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.dao.attributes.AttributesService;
import org.thingsboard.server.dao.customer.CustomerService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.dao.tenant.TenantService;
@ -83,6 +84,8 @@ public interface TbContext {
RelationService getRelationService();
EntityViewService getEntityViewService();
ListeningExecutor getJsExecutor();
ListeningExecutor getMailExecutor();

View File

@ -0,0 +1,137 @@
/**
* 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.action;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
import org.thingsboard.rule.engine.api.RuleNode;
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.rule.engine.api.TbRelationTypes;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.session.SessionMsgType;
import org.thingsboard.server.common.transport.adaptor.JsonConverter;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import static org.thingsboard.rule.engine.api.util.DonAsynchron.withCallback;
@Slf4j
@RuleNode(
type = ComponentType.ACTION,
name = "copy attributes",
configClazz = EmptyNodeConfiguration.class,
nodeDescription = "Copy attributes from asset/device to entity view",
nodeDetails = "Copy attributes from asset/device to related entity view according to entity view configuration. \n " +
"Copy will be done only for attributes that are between start and end dates and according to attribute keys configuration",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbNodeEmptyConfig",
icon = "content_copy"
)
public class TbCopyAttributesToEntityViewNode implements TbNode {
EmptyNodeConfiguration config;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, EmptyNodeConfiguration.class);
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
if (msg.getType().equals(SessionMsgType.POST_ATTRIBUTES_REQUEST.name()) ||
msg.getType().equals(DataConstants.ATTRIBUTES_DELETED) ||
msg.getType().equals(DataConstants.ATTRIBUTES_UPDATED)) {
long now = System.currentTimeMillis();
String scope;
if (msg.getType().equals(SessionMsgType.POST_ATTRIBUTES_REQUEST.name())) {
scope = DataConstants.CLIENT_SCOPE;
} else {
scope = msg.getMetaData().getValue("scope");
}
ListenableFuture<List<EntityView>> entityViewsFuture =
ctx.getEntityViewService().findEntityViewsByTenantIdAndEntityIdAsync(ctx.getTenantId(), msg.getOriginator());
withCallback(entityViewsFuture,
entityViews -> {
List<ListenableFuture<List<Void>>> saveFutures = new ArrayList<>();
for (EntityView entityView : entityViews) {
if ((entityView.getEndTimeMs() != 0 && entityView.getEndTimeMs() > now && entityView.getStartTimeMs() < now) ||
(entityView.getEndTimeMs() == 0 && entityView.getStartTimeMs() < now)) {
Set<AttributeKvEntry> attributes = JsonConverter.convertToAttributes(new JsonParser().parse(msg.getData())).getAttributes();
List<AttributeKvEntry> filteredAttributes =
attributes.stream()
.filter(attr -> {
switch (scope) {
case DataConstants.CLIENT_SCOPE:
if (entityView.getKeys().getAttributes().getCs().isEmpty()) {
return true;
}
return entityView.getKeys().getAttributes().getCs().contains(attr.getKey());
case DataConstants.SERVER_SCOPE:
if (entityView.getKeys().getAttributes().getSs().isEmpty()) {
return true;
}
return entityView.getKeys().getAttributes().getSs().contains(attr.getKey());
case DataConstants.SHARED_SCOPE:
if (entityView.getKeys().getAttributes().getSh().isEmpty()) {
return true;
}
return entityView.getKeys().getAttributes().getSh().contains(attr.getKey());
}
return false;
})
.collect(Collectors.toList());
saveFutures.add(ctx.getAttributesService().save(entityView.getId(), scope, new ArrayList<>(filteredAttributes)));
}
}
Futures.transform(Futures.allAsList(saveFutures), new Function<List<List<Void>>, Object>() {
@Nullable
@Override
public Object apply(@Nullable List<List<Void>> lists) {
ctx.tellNext(msg, TbRelationTypes.SUCCESS);
return null;
}
});
},
t -> ctx.tellFailure(msg, t));
} else {
ctx.tellNext(msg, TbRelationTypes.FAILURE);
}
}
@Override
public void destroy() {
}
}

View File

@ -98,19 +98,19 @@
<section layout="column">
<section layout="row" layout-align="start start">
<mdp-date-picker ng-model="startTimeMs"
mdp-max-date="maxStartTimeTs"
mdp-max-date="maxStartTimeMs"
mdp-placeholder="{{ 'entity-view.start-ts' | translate }}"></mdp-date-picker>
<mdp-time-picker ng-model="startTimeMs"
mdp-max-date="maxStartTimeTs"
mdp-max-date="maxStartTimeMs"
mdp-placeholder="{{ 'entity-view.start-ts' | translate }}"
mdp-auto-switch="true"></mdp-time-picker>
</section>
<section layout="row" layout-align="start start">
<mdp-date-picker ng-model="endTimeMs"
mdp-min-date="minEndTimeTs"
mdp-min-date="minEndTimeMs"
mdp-placeholder="{{ 'entity-view.end-ts' | translate }}"></mdp-date-picker>
<mdp-time-picker ng-model="endTimeMs"
mdp-min-date="minEndTimeTs"
mdp-min-date="minEndTimeMs"
mdp-placeholder="{{ 'entity-view.end-ts' | translate }}"
mdp-auto-switch="true"></mdp-time-picker>
</section>

View File

@ -54,8 +54,8 @@ export default function EntityViewDirective($compile, $templateCache, $filter, t
if (scope.entityView.startTimeMs > 0) {
scope.startTimeMs = new Date(scope.entityView.startTimeMs);
}
if (scope.entityView.endTimeTs > 0) {
scope.endTimeTs = new Date(scope.entityView.endTimeTs);
if (scope.entityView.endTimeMs > 0) {
scope.endTimeMs = new Date(scope.entityView.endTimeMs);
}
if (!scope.entityView.keys) {
scope.entityView.keys = {};
@ -78,22 +78,22 @@ export default function EntityViewDirective($compile, $templateCache, $filter, t
}
});
scope.$watch('endTimeTs', function (newDate) {
scope.$watch('endTimeMs', function (newDate) {
if (newDate) {
if (newDate.getTime() < scope.minEndTimeTs) {
scope.endTimeTs = angular.copy(scope.minEndTimeTs);
if (newDate.getTime() < scope.minEndTimeMs) {
scope.endTimeMs = angular.copy(scope.minEndTimeMs);
}
updateMinMaxDates();
}
});
function updateMinMaxDates() {
if (scope.endTimeTs) {
scope.maxStartTimeMs = angular.copy(new Date(scope.endTimeTs.getTime()));
scope.entityView.endTimeTs = scope.endTimeTs.getTime();
if (scope.endTimeMs) {
scope.maxStartTimeMs = angular.copy(new Date(scope.endTimeMs.getTime()));
scope.entityView.endTimeMs = scope.endTimeMs.getTime();
}
if (scope.startTimeMs) {
scope.minEndTimeTs = angular.copy(new Date(scope.startTimeMs.getTime()));
scope.minEndTimeMs = angular.copy(new Date(scope.startTimeMs.getTime()));
scope.entityView.startTimeMs = scope.startTimeMs.getTime();
}
}