fixed bug with saving server attributes in client scope

This commit is contained in:
Bohdan Smetaniuk 2020-07-14 15:38:21 +03:00
parent 2fffa660ab
commit 2b14fa3634
3 changed files with 16 additions and 8 deletions

View File

@ -71,7 +71,9 @@ import org.thingsboard.server.gen.edge.UserCredentialsRequestMsg;
import org.thingsboard.server.service.executors.DbCallbackExecutorService; import org.thingsboard.server.service.executors.DbCallbackExecutorService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
@Service @Service
@ -289,25 +291,29 @@ public class DefaultSyncEdgeService implements SyncEdgeService {
public void onSuccess(@Nullable List<AttributeKvEntry> ssAttributes) { public void onSuccess(@Nullable List<AttributeKvEntry> ssAttributes) {
if (ssAttributes != null && !ssAttributes.isEmpty()) { if (ssAttributes != null && !ssAttributes.isEmpty()) {
try { try {
ObjectNode entityNode = mapper.createObjectNode(); Map<String, Object> entityData = new HashMap<>();
ObjectNode attributes = mapper.createObjectNode();
for (AttributeKvEntry attr : ssAttributes) { for (AttributeKvEntry attr : ssAttributes) {
if (attr.getDataType() == DataType.BOOLEAN && attr.getBooleanValue().isPresent()) { if (attr.getDataType() == DataType.BOOLEAN && attr.getBooleanValue().isPresent()) {
entityNode.put(attr.getKey(), attr.getBooleanValue().get()); attributes.put(attr.getKey(), attr.getBooleanValue().get());
} else if (attr.getDataType() == DataType.DOUBLE && attr.getDoubleValue().isPresent()) { } else if (attr.getDataType() == DataType.DOUBLE && attr.getDoubleValue().isPresent()) {
entityNode.put(attr.getKey(), attr.getDoubleValue().get()); attributes.put(attr.getKey(), attr.getDoubleValue().get());
} else if (attr.getDataType() == DataType.LONG && attr.getLongValue().isPresent()) { } else if (attr.getDataType() == DataType.LONG && attr.getLongValue().isPresent()) {
entityNode.put(attr.getKey(), attr.getLongValue().get()); attributes.put(attr.getKey(), attr.getLongValue().get());
} else { } else {
entityNode.put(attr.getKey(), attr.getValueAsString()); attributes.put(attr.getKey(), attr.getValueAsString());
} }
} }
log.debug("Sending attributes data msg, entityId [{}], attributes [{}]", entityId, entityNode); entityData.put("kv", attributes);
entityData.put("scope", DataConstants.SERVER_SCOPE);
JsonNode entityBody = mapper.valueToTree(entityData);
log.debug("Sending attributes data msg, entityId [{}], attributes [{}]", entityId, entityBody);
saveEdgeEvent(edge.getTenantId(), saveEdgeEvent(edge.getTenantId(),
edge.getId(), edge.getId(),
edgeEventType, edgeEventType,
ActionType.ATTRIBUTES_UPDATED, ActionType.ATTRIBUTES_UPDATED,
entityId, entityId,
entityNode); entityBody);
} catch (Exception e) { } catch (Exception e) {
log.error("[{}] Failed to send attribute updates to the edge", edge.getName(), e); log.error("[{}] Failed to send attribute updates to the edge", edge.getName(), e);
} }

View File

@ -115,6 +115,7 @@ message PostTelemetryMsg {
message PostAttributeMsg { message PostAttributeMsg {
repeated KeyValueProto kv = 1; repeated KeyValueProto kv = 1;
string scope = 2;
} }
message GetAttributeRequestMsg { message GetAttributeRequestMsg {

View File

@ -136,8 +136,9 @@ public class JsonConverter {
public static PostAttributeMsg convertToAttributesProto(JsonElement jsonObject) throws JsonSyntaxException { public static PostAttributeMsg convertToAttributesProto(JsonElement jsonObject) throws JsonSyntaxException {
if (jsonObject.isJsonObject()) { if (jsonObject.isJsonObject()) {
PostAttributeMsg.Builder result = PostAttributeMsg.newBuilder(); PostAttributeMsg.Builder result = PostAttributeMsg.newBuilder();
List<KeyValueProto> keyValueList = parseProtoValues(jsonObject.getAsJsonObject()); List<KeyValueProto> keyValueList = parseProtoValues(jsonObject.getAsJsonObject().getAsJsonObject("kv"));
result.addAllKv(keyValueList); result.addAllKv(keyValueList);
result.setScope(jsonObject.getAsJsonObject().getAsJsonPrimitive("scope").getAsString());
return result.build(); return result.build();
} else { } else {
throw new JsonSyntaxException(CAN_T_PARSE_VALUE + jsonObject); throw new JsonSyntaxException(CAN_T_PARSE_VALUE + jsonObject);