Browse Source

Merge pull request #14641 from MazurenkoNick/fix-device-profile-edge-sync

Fix device profile synchronization issue for edges with versions below 4.3
pull/14650/head
Viacheslav Klimov 10 months ago
committed by GitHub
parent
commit
43d90d282b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 34
      application/src/main/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtils.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceEdgeProcessor.java
  3. 3
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/profile/DeviceProfileEdgeProcessor.java
  4. 3
      common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java
  5. 2
      dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java

34
application/src/main/java/org/thingsboard/server/service/edge/EdgeMsgConstructorUtils.java

@ -51,6 +51,8 @@ import org.thingsboard.server.common.data.alarm.AlarmComment;
import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.asset.AssetProfile;
import org.thingsboard.server.common.data.cf.CalculatedField; import org.thingsboard.server.common.data.cf.CalculatedField;
import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.domain.DomainInfo; import org.thingsboard.server.common.data.domain.DomainInfo;
import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.edge.EdgeEvent; import org.thingsboard.server.common.data.edge.EdgeEvent;
@ -261,12 +263,40 @@ public class EdgeMsgConstructorUtils {
return DeviceCredentialsUpdateMsg.newBuilder().setEntity(JacksonUtil.toString(deviceCredentials)).build(); return DeviceCredentialsUpdateMsg.newBuilder().setEntity(JacksonUtil.toString(deviceCredentials)).build();
} }
public static DeviceProfileUpdateMsg constructDeviceProfileUpdatedMsg(UpdateMsgType msgType, DeviceProfile deviceProfile) { public static DeviceProfileUpdateMsg constructDeviceProfileUpdatedMsg(UpdateMsgType msgType, DeviceProfile deviceProfile, EdgeVersion edgeVersion) {
return DeviceProfileUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(deviceProfile)) String entity = getEntityAndFixLwm2mBootstrapShortServerId(deviceProfile, edgeVersion);
return DeviceProfileUpdateMsg.newBuilder().setMsgType(msgType).setEntity(entity)
.setIdMSB(deviceProfile.getId().getId().getMostSignificantBits()) .setIdMSB(deviceProfile.getId().getId().getMostSignificantBits())
.setIdLSB(deviceProfile.getId().getId().getLeastSignificantBits()).build(); .setIdLSB(deviceProfile.getId().getId().getLeastSignificantBits()).build();
} }
public static String getEntityAndFixLwm2mBootstrapShortServerId(DeviceProfile deviceProfile, EdgeVersion edgeVersion) {
DeviceProfileTransportConfiguration transportConfiguration = deviceProfile.getProfileData().getTransportConfiguration();
if (!(transportConfiguration instanceof Lwm2mDeviceProfileTransportConfiguration) || edgeVersion.getNumber() >= EdgeVersion.V_4_3_0.getNumber()) {
return JacksonUtil.toString(deviceProfile);
}
JsonNode jsonNode = JacksonUtil.valueToTree(deviceProfile);
JsonNode profileDataNode = jsonNode.get("profileData");
if (profileDataNode != null && profileDataNode.has("transportConfiguration")) {
JsonNode transportConfigNode = profileDataNode.get("transportConfiguration");
JsonNode bootstrapNode = transportConfigNode.get("bootstrap");
if (bootstrapNode != null && bootstrapNode.isArray()) {
for (JsonNode bootstrapServerNode : bootstrapNode) {
if (bootstrapServerNode.isObject()) {
ObjectNode serverObjectNode = (ObjectNode) bootstrapServerNode;
JsonNode isBootstrapNode = serverObjectNode.get("bootstrapServerIs");
boolean isBootstrapServer = isBootstrapNode != null && isBootstrapNode.asBoolean(false);
JsonNode shortServerIdNode = serverObjectNode.get("shortServerId");
if (isBootstrapServer && (shortServerIdNode == null || shortServerIdNode.isNull())) {
serverObjectNode.put("shortServerId", 0);
}
}
}
}
}
return JacksonUtil.toString(jsonNode);
}
public static DeviceProfileUpdateMsg constructDeviceProfileDeleteMsg(DeviceProfileId deviceProfileId) { public static DeviceProfileUpdateMsg constructDeviceProfileDeleteMsg(DeviceProfileId deviceProfileId) {
return DeviceProfileUpdateMsg.newBuilder() return DeviceProfileUpdateMsg.newBuilder()
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE) .setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE)

2
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/DeviceEdgeProcessor.java

@ -237,7 +237,7 @@ public class DeviceEdgeProcessor extends BaseDeviceProcessor implements DevicePr
if (UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE.equals(msgType)) { if (UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE.equals(msgType)) {
DeviceProfile deviceProfile = edgeCtx.getDeviceProfileService().findDeviceProfileById(edgeEvent.getTenantId(), device.getDeviceProfileId()); DeviceProfile deviceProfile = edgeCtx.getDeviceProfileService().findDeviceProfileById(edgeEvent.getTenantId(), device.getDeviceProfileId());
builder.addDeviceProfileUpdateMsg(EdgeMsgConstructorUtils.constructDeviceProfileUpdatedMsg(msgType, deviceProfile)); builder.addDeviceProfileUpdateMsg(EdgeMsgConstructorUtils.constructDeviceProfileUpdatedMsg(msgType, deviceProfile, edgeVersion));
} }
return builder.build(); return builder.build();
} }

3
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/device/profile/DeviceProfileEdgeProcessor.java

@ -102,7 +102,7 @@ public class DeviceProfileEdgeProcessor extends BaseDeviceProfileProcessor imple
DeviceProfile deviceProfile = edgeCtx.getDeviceProfileService().findDeviceProfileById(edgeEvent.getTenantId(), deviceProfileId); DeviceProfile deviceProfile = edgeCtx.getDeviceProfileService().findDeviceProfileById(edgeEvent.getTenantId(), deviceProfileId);
if (deviceProfile != null) { if (deviceProfile != null) {
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction());
DeviceProfileUpdateMsg deviceProfileUpdateMsg = EdgeMsgConstructorUtils.constructDeviceProfileUpdatedMsg(msgType, deviceProfile); DeviceProfileUpdateMsg deviceProfileUpdateMsg = EdgeMsgConstructorUtils.constructDeviceProfileUpdatedMsg(msgType, deviceProfile, edgeVersion);
return DownlinkMsg.newBuilder() return DownlinkMsg.newBuilder()
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) .setDownlinkMsgId(EdgeUtils.nextPositiveInt())
.addDeviceProfileUpdateMsg(deviceProfileUpdateMsg) .addDeviceProfileUpdateMsg(deviceProfileUpdateMsg)
@ -141,5 +141,4 @@ public class DeviceProfileEdgeProcessor extends BaseDeviceProfileProcessor imple
public EdgeEventType getEdgeEventType() { public EdgeEventType getEdgeEventType() {
return EdgeEventType.DEVICE_PROFILE; return EdgeEventType.DEVICE_PROFILE;
} }
} }

3
common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java

@ -114,6 +114,9 @@ public class DeviceProfile extends BaseData<DeviceProfileId> implements HasName,
this.description = deviceProfile.getDescription(); this.description = deviceProfile.getDescription();
this.image = deviceProfile.getImage(); this.image = deviceProfile.getImage();
this.isDefault = deviceProfile.isDefault(); this.isDefault = deviceProfile.isDefault();
this.type = deviceProfile.getType();
this.transportType = deviceProfile.getTransportType();
this.provisionType = deviceProfile.getProvisionType();
this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId(); this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId();
this.defaultDashboardId = deviceProfile.getDefaultDashboardId(); this.defaultDashboardId = deviceProfile.getDefaultDashboardId();
this.defaultQueueName = deviceProfile.getDefaultQueueName(); this.defaultQueueName = deviceProfile.getDefaultQueueName();

2
dao/src/main/java/org/thingsboard/server/dao/service/validator/DeviceProfileDataValidator.java

@ -174,6 +174,8 @@ public class DeviceProfileDataValidator extends AbstractHasOtaPackageValidator<D
for (LwM2MBootstrapServerCredential bootstrapServerCredential : lwM2MBootstrapServersConfigurations) { for (LwM2MBootstrapServerCredential bootstrapServerCredential : lwM2MBootstrapServersConfigurations) {
validateLwm2mServersCredentialOfBootstrapForClient(bootstrapServerCredential); validateLwm2mServersCredentialOfBootstrapForClient(bootstrapServerCredential);
} }
// call setProfileData after validation to ensure 'profileData' and 'profileDataBytes' fields are synchronized and ProtoUtils.toProto is not broken
deviceProfile.setProfileData(deviceProfile.getProfileData());
} }
} }

Loading…
Cancel
Save