Skip to content

Commit 8315d64

Browse files
feat: only update latest value of some internal entity
1 parent 201fce6 commit 8315d64

File tree

7 files changed

+51
-28
lines changed

7 files changed

+51
-28
lines changed

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/MilesightGatewayBootstrap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void onStarted(Integration integrationConfig) {
4747
@SneakyThrows
4848
public void onEnabled(String tenantId, Integration integrationConfig) {
4949
gatewayService.syncGatewayListToAddDeviceGatewayEuiList();
50-
DeviceModelData modelData = msGwEntityService.getModelData();
50+
DeviceModelData modelData = msGwEntityService.getDeviceModelData();
5151
// init model data
5252
if (deviceCodecService.isModelDataEmpty(modelData)) {
5353
try {
@@ -58,7 +58,7 @@ public void onEnabled(String tenantId, Integration integrationConfig) {
5858
log.error("Load device codecs error: " + e.getMessage());
5959
}
6060
} else {
61-
deviceCodecService.syncDeviceModelListToAdd(msGwEntityService.getModelData());
61+
deviceCodecService.syncDeviceModelListToAdd(msGwEntityService.getDeviceModelData());
6262
}
6363
}
6464

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/entity/MsGwIntegrationEntities.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public class MsGwIntegrationEntities extends ExchangePayload {
4242

4343
public static final String MODEL_REPO_URL_KEY = Constants.INTEGRATION_ID + ".integration." + MODEL_REPO_URL_IDENTIFIER;
4444

45+
public static final String GATEWAY_DEVICE_RELATION_IDENTIFIER = "gateway-device-relation";
46+
47+
public static final String GATEWAY_DEVICE_RELATION_KEY = Constants.INTEGRATION_ID + ".integration." + GATEWAY_DEVICE_RELATION_IDENTIFIER;
48+
49+
public static final String DEVICE_MODEL_DATA_IDENTIFIER = "device-model-data";
50+
51+
public static final String DEVICE_MODEL_DATA_KEY = Constants.INTEGRATION_ID + ".integration." + DEVICE_MODEL_DATA_IDENTIFIER;
52+
4553
@Entity(type = EntityType.SERVICE, name = "Synchronize Device Codec", identifier = SYNC_DEVICE_CODEC_IDENTIFIER)
4654
private EmptyPayload syncDeviceCodec;
4755

@@ -54,10 +62,10 @@ public class MsGwIntegrationEntities extends ExchangePayload {
5462
@Entity(type = EntityType.EVENT, name = "Gateway Status Event", identifier = "gateway-status-event")
5563
private GatewayStatusEvent gatewayStatusEvent;
5664

57-
@Entity(type = EntityType.PROPERTY, name = "Gateway Device Relation", identifier = "gateway-device-relation", accessMod = AccessMod.R, visible = false)
65+
@Entity(type = EntityType.PROPERTY, name = "Gateway Device Relation", identifier = GATEWAY_DEVICE_RELATION_IDENTIFIER, accessMod = AccessMod.R, visible = false)
5866
private String gatewayDeviceRelation;
5967

60-
@Entity(type = EntityType.PROPERTY, name = "Device Model Data", identifier = "device-model-data", accessMod = AccessMod.R, visible = false)
68+
@Entity(type = EntityType.PROPERTY, name = "Device Model Data", identifier = DEVICE_MODEL_DATA_IDENTIFIER, accessMod = AccessMod.R, visible = false)
6169
private String deviceModelData;
6270

6371
@Entity(type = EntityType.PROPERTY, name = "Model Repository Url", identifier = MODEL_REPO_URL_IDENTIFIER, accessMod = AccessMod.RW, attributes = @Attribute(optional = true))

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/service/DeviceCodecService.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.milesight.beaveriot.context.integration.model.AttributeBuilder;
99
import com.milesight.beaveriot.context.integration.model.Entity;
1010
import com.milesight.beaveriot.context.integration.model.event.ExchangeEvent;
11-
import com.milesight.beaveriot.context.integration.wrapper.AnnotatedEntityWrapper;
1211
import com.milesight.beaveriot.eventbus.annotations.EventSubscribe;
1312
import com.milesight.beaveriot.eventbus.api.Event;
1413
import com.milesight.beaveriot.integrations.milesightgateway.entity.MsGwIntegrationEntities;
@@ -18,7 +17,6 @@
1817
import com.milesight.beaveriot.integrations.milesightgateway.codec.ResourceRequester;
1918
import com.milesight.beaveriot.integrations.milesightgateway.codec.ResourceString;
2019
import com.milesight.beaveriot.integrations.milesightgateway.util.LockConstants;
21-
import lombok.Data;
2220
import lombok.extern.slf4j.Slf4j;
2321
import org.springframework.beans.factory.annotation.Autowired;
2422
import org.springframework.stereotype.Component;
@@ -64,7 +62,7 @@ public void syncDeviceCodec() throws ExecutionException, InterruptedException {
6462
public void syncDeviceCodec(String url) throws ExecutionException, InterruptedException {
6563
ResourceRequester resourceRequester = new ResourceRequester(url);
6664
VersionResponse versionInfo = resourceRequester.requestCodecVersion();
67-
DeviceModelData modelData = msGwEntityService.getModelData();
65+
DeviceModelData modelData = msGwEntityService.getDeviceModelData();
6866
// Check whether version updated
6967
if (Objects.equals(modelData.getVersion(), versionInfo.getVersion()) && Objects.equals(modelData.getSource(), resourceRequester.getRepoUrl())) {
7068
log.info("Ignore update. Have been in the latest version: " + versionInfo.getVersion());
@@ -96,7 +94,6 @@ public void syncDeviceCodec(String url) throws ExecutionException, InterruptedEx
9694

9795
private void saveDeviceCodecsToEntity(String version, String repoUrl, List<Vendor> vendors, Map<String, List<DeviceModelData.DeviceInfo>> vendorDevices) {
9896
// save to deviceModelData of integration entities
99-
AnnotatedEntityWrapper<MsGwIntegrationEntities> gatewayEntitiesWrapper = new AnnotatedEntityWrapper<>();
10097
DeviceModelData deviceModelData = new DeviceModelData();
10198
deviceModelData.setVersion(version);
10299
deviceModelData.setSource(repoUrl);
@@ -108,11 +105,7 @@ private void saveDeviceCodecsToEntity(String version, String repoUrl, List<Vendo
108105
vendorInfo.setDeviceInfoList(vendorDevices.get(vendor.getId()));
109106
return vendorInfo;
110107
}).toList());
111-
try {
112-
gatewayEntitiesWrapper.saveValue(MsGwIntegrationEntities::getDeviceModelData, json.writeValueAsString(deviceModelData));
113-
} catch (Exception e) {
114-
throw ServiceException.with(ErrorCode.SERVER_ERROR.getErrorCode(), "Save device model data.").build();
115-
}
108+
msGwEntityService.saveDeviceModelData(deviceModelData);
116109

117110
// save to deviceModel enum attribute in add device of integration entities
118111
syncDeviceModelListToAdd(deviceModelData);
@@ -150,7 +143,7 @@ public Map<String, DeviceCodecData> batchGetDeviceCodecData(List<String> vendorD
150143
Set<String> deviceModelSet = new HashSet<>(vendorDeviceIdList);
151144
Map<String, DeviceModelData.VendorDeviceInfo> deviceModelMap = new HashMap<>();
152145
Map<String, String> vendorResourceMap = new HashMap<>();
153-
msGwEntityService.getModelData().iterateWhen((vendorInfo, deviceInfo) -> {
146+
msGwEntityService.getDeviceModelData().iterateWhen((vendorInfo, deviceInfo) -> {
154147
String deviceModelId = DeviceModelData.getDeviceModelId(vendorInfo, deviceInfo);
155148
if (deviceModelSet.remove(deviceModelId)) {
156149
deviceModelMap.put(deviceModelId, new DeviceModelData.VendorDeviceInfo(vendorInfo, deviceInfo));

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/service/DeviceService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import com.milesight.beaveriot.base.enums.ErrorCode;
88
import com.milesight.beaveriot.base.exception.ServiceException;
99
import com.milesight.beaveriot.context.api.DeviceServiceProvider;
10+
import com.milesight.beaveriot.context.api.EntityValueServiceProvider;
1011
import com.milesight.beaveriot.context.integration.enums.EntityValueType;
1112
import com.milesight.beaveriot.context.integration.model.Device;
1213
import com.milesight.beaveriot.context.integration.model.DeviceBuilder;
1314
import com.milesight.beaveriot.context.integration.model.Entity;
15+
import com.milesight.beaveriot.context.integration.model.ExchangePayload;
1416
import com.milesight.beaveriot.context.integration.model.event.ExchangeEvent;
1517
import com.milesight.beaveriot.context.integration.wrapper.EntityWrapper;
1618
import com.milesight.beaveriot.eventbus.annotations.EventSubscribe;
@@ -57,6 +59,9 @@ public class DeviceService {
5759
@Autowired
5860
GatewayRequester gatewayRequester;
5961

62+
@Autowired
63+
EntityValueServiceProvider entityValueServiceProvider;
64+
6065
private final ObjectMapper json = GatewayString.jsonInstance();
6166

6267
public List<Device> getDevices(List<String> euiList) {
@@ -143,8 +148,10 @@ public void onAddDevice(Event<MsGwIntegrationEntities.AddDevice> event) {
143148
}
144149

145150
// save script
146-
new EntityWrapper(updateResourceResult.getDecoderEntity()).saveValue(codecData.getDecoderStr());
147-
new EntityWrapper(updateResourceResult.getEncoderEntity()).saveValue(codecData.getEncoderStr());
151+
entityValueServiceProvider.saveLatestValues(ExchangePayload.create(Map.of(
152+
updateResourceResult.getDecoderEntity().getKey(), codecData.getDecoderStr(),
153+
updateResourceResult.getEncoderEntity().getKey(), codecData.getEncoderStr()
154+
)));
148155
}
149156

150157
public GatewayDeviceData getDeviceData(Device device) {

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/service/GatewayService.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ private Entity getAddDeviceGatewayEntity() {
177177
@DistributedLock(name = LockConstants.UPDATE_GATEWAY_DEVICE_ENUM_LOCK)
178178
private void putAddDeviceGatewayEui(List<Device> gateways) {
179179
Entity gatewayEuiEntity = getAddDeviceGatewayEntity();
180-
Map<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
180+
LinkedHashMap<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
181181
gateways.forEach(gateway -> attrEnum.put(getGatewayEui(gateway), gateway.getName()));
182-
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, getGatewayEui(gateways.get(0)));
182+
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, attrEnum.entrySet().iterator().next().getKey());
183183

184184
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, attrEnum);
185185
entityServiceProvider.save(gatewayEuiEntity);
@@ -188,11 +188,11 @@ private void putAddDeviceGatewayEui(List<Device> gateways) {
188188
@DistributedLock(name = LockConstants.UPDATE_GATEWAY_DEVICE_ENUM_LOCK)
189189
private void removeAddDeviceGatewayEui(List<String> gatewayEuiList) {
190190
Entity gatewayEuiEntity = getAddDeviceGatewayEntity();
191-
Map<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
191+
LinkedHashMap<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
192192
gatewayEuiList.forEach(eui -> attrEnum.remove(GatewayString.standardizeEUI(eui)));
193193
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, attrEnum);
194-
if (!gatewayEuiList.isEmpty()) {
195-
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, gatewayEuiList.get(0));
194+
if (!attrEnum.isEmpty()) {
195+
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, attrEnum.entrySet().iterator().next().getKey());
196196
} else {
197197
gatewayEuiEntity.getAttributes().remove(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE);
198198
}
@@ -352,13 +352,15 @@ public void onDeleteDevice(Event<MsGwIntegrationEntities.DeleteDevice> event) {
352352
public void syncGatewayListToAddDeviceGatewayEuiList() {
353353
List<Device> gatwayList = getAllGateways();
354354
Entity addDeviceGatewayEuiEntity = getAddDeviceGatewayEntity();
355-
Map<String, String> euiToNameMap = gatwayList.stream().collect(Collectors.toMap(
355+
LinkedHashMap<String, String> euiToNameMap = gatwayList.stream().collect(Collectors.toMap(
356356
this::getGatewayEui,
357-
Device::getName
357+
Device::getName,
358+
(existing, replacement) -> existing,
359+
LinkedHashMap::new
358360
));
359361
addDeviceGatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, euiToNameMap);
360362
if (!gatwayList.isEmpty()) {
361-
addDeviceGatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, getGatewayEui(gatwayList.get(0)));
363+
addDeviceGatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, euiToNameMap.entrySet().iterator().next().getKey());
362364
}
363365
entityServiceProvider.save(addDeviceGatewayEuiEntity);
364366
}

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/service/MsGwEntityService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.milesight.beaveriot.base.enums.ErrorCode;
66
import com.milesight.beaveriot.base.exception.ServiceException;
77
import com.milesight.beaveriot.context.api.EntityValueServiceProvider;
8+
import com.milesight.beaveriot.context.integration.model.ExchangePayload;
89
import com.milesight.beaveriot.context.integration.wrapper.AnnotatedEntityWrapper;
910
import com.milesight.beaveriot.integrations.milesightgateway.codec.ResourceConstant;
1011
import com.milesight.beaveriot.integrations.milesightgateway.entity.MsGwIntegrationEntities;
@@ -68,13 +69,26 @@ public String getDeviceModelRepoUrl() {
6869
public void saveGatewayRelation(Map<String, List<String>> gatewayRelation) {
6970
try {
7071
String relStr = json.writeValueAsString(gatewayRelation);
71-
new AnnotatedEntityWrapper<MsGwIntegrationEntities>().saveValue(MsGwIntegrationEntities::getGatewayDeviceRelation, relStr);
72+
entityValueServiceProvider.saveLatestValues(ExchangePayload.create(Map.of(
73+
MsGwIntegrationEntities.GATEWAY_DEVICE_RELATION_KEY, relStr
74+
)));
7275
} catch (Exception e) {
7376
throw ServiceException.with(ErrorCode.SERVER_ERROR.getErrorCode(), "Save relation error: " + e.getMessage()).build();
7477
}
7578
}
7679

77-
public DeviceModelData getModelData() {
80+
public void saveDeviceModelData(DeviceModelData deviceModelData) {
81+
try {
82+
entityValueServiceProvider.saveLatestValues(ExchangePayload.create(Map.of(
83+
MsGwIntegrationEntities.DEVICE_MODEL_DATA_KEY, ResourceString.jsonInstance().writeValueAsString(deviceModelData)
84+
)));
85+
} catch (Exception e) {
86+
throw ServiceException.with(ErrorCode.SERVER_ERROR.getErrorCode(), "Save device model error: " + e.getMessage()).build();
87+
}
88+
}
89+
90+
91+
public DeviceModelData getDeviceModelData() {
7892
AnnotatedEntityWrapper<MsGwIntegrationEntities> gatewayEntitiesWrapper = new AnnotatedEntityWrapper<>();
7993
String modelDataStr = (String) gatewayEntitiesWrapper.getValue(MsGwIntegrationEntities::getDeviceModelData).orElse("{}");
8094
try {

integrations/milesight-gateway/src/main/java/com/milesight/beaveriot/integrations/milesightgateway/service/SyncGatewayDeviceService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import java.util.*;
3333
import java.util.concurrent.CompletableFuture;
34-
import java.util.concurrent.Executor;
3534
import java.util.stream.Collectors;
3635

3736
/**
@@ -85,7 +84,7 @@ public List<SyncDeviceListItem> getGatewayDeviceSyncList(String gatewayEui) {
8584
existedDeviceEuiSet.addAll(existedDeviceEui);
8685
}
8786

88-
DeviceModelData deviceModelData = msGwEntityService.getModelData();
87+
DeviceModelData deviceModelData = msGwEntityService.getDeviceModelData();
8988
if (deviceModelData.getVendorInfoList() == null) {
9089
throw ServiceException.with(ErrorCode.PARAMETER_VALIDATION_FAILED.getErrorCode(), "Please synchronize your codec repo first!").build();
9190
}

0 commit comments

Comments
 (0)