Browse Source

Update edge inside session

pull/15111/head
Volodymyr Babak 3 months ago
parent
commit
cfec3fbea2
  1. 5
      application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java
  2. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/edge/EdgeService.java
  3. 23
      dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java

5
application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java

@ -243,6 +243,11 @@ public abstract class EdgeGrpcSession implements Closeable {
log.debug("[{}] onConfigurationUpdate [{}]", sessionId, edge); log.debug("[{}] onConfigurationUpdate [{}]", sessionId, edge);
this.tenantId = edge.getTenantId(); this.tenantId = edge.getTenantId();
this.edge = edge; this.edge = edge;
if (!this.edge.getCustomerId().equals(edge.getCustomerId())) {
// do not send edge configuration message on customer update
// message send by separate flow from assign_to or unassing_from customer
return;
}
EdgeUpdateMsg edgeConfig = EdgeUpdateMsg.newBuilder() EdgeUpdateMsg edgeConfig = EdgeUpdateMsg.newBuilder()
.setConfiguration(EdgeMsgConstructorUtils.constructEdgeConfiguration(edge)).build(); .setConfiguration(EdgeMsgConstructorUtils.constructEdgeConfiguration(edge)).build();
ResponseMsg edgeConfigMsg = ResponseMsg.newBuilder() ResponseMsg edgeConfigMsg = ResponseMsg.newBuilder()

2
common/dao-api/src/main/java/org/thingsboard/server/dao/edge/EdgeService.java

@ -51,8 +51,6 @@ public interface EdgeService extends EntityDaoService {
Edge saveEdge(Edge edge); Edge saveEdge(Edge edge);
Edge saveEdge(Edge edge, boolean publishEvent);
Edge assignEdgeToCustomer(TenantId tenantId, EdgeId edgeId, CustomerId customerId); Edge assignEdgeToCustomer(TenantId tenantId, EdgeId edgeId, CustomerId customerId);
Edge unassignEdgeFromCustomer(TenantId tenantId, EdgeId edgeId); Edge unassignEdgeFromCustomer(TenantId tenantId, EdgeId edgeId);

23
dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -66,7 +66,6 @@ import org.thingsboard.server.dao.entity.EntityCountService;
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent; import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent;
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
import org.thingsboard.server.exception.DataValidationException;
import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
@ -75,6 +74,7 @@ import org.thingsboard.server.dao.service.Validator;
import org.thingsboard.server.dao.sql.JpaExecutorService; import org.thingsboard.server.dao.sql.JpaExecutorService;
import org.thingsboard.server.dao.timeseries.TimeseriesService; import org.thingsboard.server.dao.timeseries.TimeseriesService;
import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.user.UserService;
import org.thingsboard.server.exception.DataValidationException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -203,25 +203,18 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
@Override @Override
public Edge saveEdge(Edge edge) { public Edge saveEdge(Edge edge) {
return saveEdge(edge, true); return saveEntity(edge, () -> doSaveEdge(edge));
}
@Override
public Edge saveEdge(Edge edge, boolean publishEvent) {
return saveEntity(edge, () -> doSaveEdge(edge, publishEvent));
} }
private Edge doSaveEdge(Edge edge, boolean publishEvent) { private Edge doSaveEdge(Edge edge) {
log.trace("Executing saveEdge [{}]", edge); log.trace("Executing saveEdge [{}]", edge);
Edge oldEdge = edgeValidator.validate(edge, Edge::getTenantId); Edge oldEdge = edgeValidator.validate(edge, Edge::getTenantId);
EdgeCacheEvictEvent evictEvent = new EdgeCacheEvictEvent(edge.getTenantId(), edge.getName(), oldEdge != null ? oldEdge.getName() : null); EdgeCacheEvictEvent evictEvent = new EdgeCacheEvictEvent(edge.getTenantId(), edge.getName(), oldEdge != null ? oldEdge.getName() : null);
try { try {
Edge savedEdge = edgeDao.save(edge.getTenantId(), edge); Edge savedEdge = edgeDao.save(edge.getTenantId(), edge);
publishEvictEvent(evictEvent); publishEvictEvent(evictEvent);
if (publishEvent) { eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedEdge.getTenantId())
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedEdge.getTenantId()) .entityId(savedEdge.getId()).entity(savedEdge).created(edge.getId() == null).build());
.entityId(savedEdge.getId()).entity(savedEdge).created(edge.getId() == null).build());
}
if (edge.getId() == null) { if (edge.getId() == null) {
countService.publishCountEntityEvictEvent(savedEdge.getTenantId(), EntityType.EDGE); countService.publishCountEntityEvictEvent(savedEdge.getTenantId(), EntityType.EDGE);
} }
@ -246,7 +239,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
return edge; return edge;
} }
edge.setCustomerId(customerId); edge.setCustomerId(customerId);
Edge result = saveEdge(edge, false); Edge result = saveEdge(edge);
eventPublisher.publishEvent(ActionEntityEvent.builder().tenantId(tenantId).entityId(edgeId) eventPublisher.publishEvent(ActionEntityEvent.builder().tenantId(tenantId).entityId(edgeId)
.body(JacksonUtil.toString(customerId)).actionType(ActionType.ASSIGNED_TO_CUSTOMER).build()); .body(JacksonUtil.toString(customerId)).actionType(ActionType.ASSIGNED_TO_CUSTOMER).build());
return result; return result;
@ -261,7 +254,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
return edge; return edge;
} }
edge.setCustomerId(null); edge.setCustomerId(null);
Edge result = saveEdge(edge, false); Edge result = saveEdge(edge);
eventPublisher.publishEvent(ActionEntityEvent.builder().tenantId(tenantId).entityId(edgeId) eventPublisher.publishEvent(ActionEntityEvent.builder().tenantId(tenantId).entityId(edgeId)
.body(JacksonUtil.toString(customerId)).actionType(ActionType.UNASSIGNED_FROM_CUSTOMER).build()); .body(JacksonUtil.toString(customerId)).actionType(ActionType.UNASSIGNED_FROM_CUSTOMER).build());
return result; return result;

Loading…
Cancel
Save