|
|
@ -43,7 +43,6 @@ import java.util.HashMap; |
|
|
import java.util.HashSet; |
|
|
import java.util.HashSet; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.Optional; |
|
|
|
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
@ -94,16 +93,12 @@ public class TbEntityDataSubCtx extends TbAbstractDataSubCtx<EntityDataQuery> { |
|
|
|
|
|
|
|
|
private void sendLatestWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) { |
|
|
private void sendLatestWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) { |
|
|
Map<String, TsValue> latestUpdate = new HashMap<>(); |
|
|
Map<String, TsValue> latestUpdate = new HashMap<>(); |
|
|
subscriptionUpdate.getData().forEach((key, data) -> data.stream() |
|
|
subscriptionUpdate.getData().forEach((key, values) -> { |
|
|
.filter(o -> o instanceof Object[] && ((Object[]) o).length > 0 && ((Object[]) o)[0] instanceof Long) |
|
|
latestUpdate.put(key, getLatest(values)); |
|
|
.max(Comparator.comparingLong(o -> (Long) ((Object[]) o)[0])) |
|
|
}); |
|
|
.ifPresent(max -> { |
|
|
|
|
|
Object[] arr = (Object[]) max; |
|
|
|
|
|
latestUpdate.put(key, new TsValue((Long) arr[0], (String) arr[1])); |
|
|
|
|
|
})); |
|
|
|
|
|
EntityData entityData = getDataForEntity(entityId); |
|
|
EntityData entityData = getDataForEntity(entityId); |
|
|
if (entityData != null && entityData.getLatest() != null) { |
|
|
if (entityData != null && entityData.getLatest() != null) { |
|
|
Map<String, TsValue> latestCtxValues = entityData.getLatest().computeIfAbsent(keyType, key -> new HashMap<>()); |
|
|
Map<String, TsValue> latestCtxValues = entityData.getLatest().computeIfAbsent(keyType, __ -> new HashMap<>()); |
|
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues); |
|
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues); |
|
|
latestCtxValues.forEach((k, v) -> { |
|
|
latestCtxValues.forEach((k, v) -> { |
|
|
TsValue update = latestUpdate.get(k); |
|
|
TsValue update = latestUpdate.get(k); |
|
|
@ -134,40 +129,39 @@ public class TbEntityDataSubCtx extends TbAbstractDataSubCtx<EntityDataQuery> { |
|
|
|
|
|
|
|
|
private void sendTsWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) { |
|
|
private void sendTsWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) { |
|
|
Map<String, List<TsValue>> tsUpdate = new HashMap<>(); |
|
|
Map<String, List<TsValue>> tsUpdate = new HashMap<>(); |
|
|
subscriptionUpdate.getData().forEach((k, v) -> { |
|
|
subscriptionUpdate.getData().forEach((key, values) -> { |
|
|
Object[] data = (Object[]) v.get(0); |
|
|
tsUpdate.put(key, new ArrayList<>(values)); |
|
|
tsUpdate.computeIfAbsent(k, key -> new ArrayList<>()).add(new TsValue((Long) data[0], (String) data[1])); |
|
|
|
|
|
}); |
|
|
}); |
|
|
Map<String, TsValue> latestCtxValues = getLatestTsValuesForEntity(entityId); |
|
|
Map<String, TsValue> latestCtxValues = getLatestTsValuesForEntity(entityId); |
|
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues); |
|
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues); |
|
|
if (latestCtxValues != null) { |
|
|
if (latestCtxValues != null) { |
|
|
latestCtxValues.forEach((k, v) -> { |
|
|
latestCtxValues.forEach((key, latest) -> { |
|
|
List<TsValue> updateList = tsUpdate.get(k); |
|
|
List<TsValue> updateList = tsUpdate.get(key); |
|
|
if (updateList != null) { |
|
|
if (updateList != null) { |
|
|
for (TsValue update : new ArrayList<>(updateList)) { |
|
|
for (TsValue update : new ArrayList<>(updateList)) { |
|
|
if (update.getTs() < v.getTs()) { |
|
|
if (update.getTs() < latest.getTs()) { |
|
|
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs()); |
|
|
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), key, update.getTs()); |
|
|
// Looks like this is redundant feature and our UI is ready to merge the updates.
|
|
|
// Looks like this is redundant feature and our UI is ready to merge the updates.
|
|
|
//updateList.remove(update);
|
|
|
//updateList.remove(update);
|
|
|
} else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) { |
|
|
} else if ((update.getTs() == latest.getTs() && update.getValue().equals(latest.getValue()))) { |
|
|
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs()); |
|
|
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), key, update.getTs()); |
|
|
updateList.remove(update); |
|
|
updateList.remove(update); |
|
|
} |
|
|
} |
|
|
if (updateList.isEmpty()) { |
|
|
if (updateList.isEmpty()) { |
|
|
tsUpdate.remove(k); |
|
|
tsUpdate.remove(key); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
//Setting new values
|
|
|
//Setting new values
|
|
|
tsUpdate.forEach((k, v) -> { |
|
|
tsUpdate.forEach((key, values) -> { |
|
|
Optional<TsValue> maxValue = v.stream().max(Comparator.comparingLong(TsValue::getTs)); |
|
|
values.stream().max(Comparator.comparingLong(TsValue::getTs)) |
|
|
maxValue.ifPresent(max -> latestCtxValues.put(k, max)); |
|
|
.ifPresent(latest -> latestCtxValues.put(key, latest)); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
if (!tsUpdate.isEmpty()) { |
|
|
if (!tsUpdate.isEmpty()) { |
|
|
Map<String, TsValue[]> tsMap = new HashMap<>(); |
|
|
Map<String, TsValue[]> tsMap = new HashMap<>(); |
|
|
tsUpdate.forEach((key, tsValue) -> tsMap.put(key, tsValue.toArray(new TsValue[tsValue.size()]))); |
|
|
tsUpdate.forEach((key, values) -> tsMap.put(key, values.toArray(new TsValue[0]))); |
|
|
EntityData entityData = new EntityData(entityId, null, tsMap); |
|
|
EntityData entityData = new EntityData(entityId, null, tsMap); |
|
|
sendWsMsg(new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription)); |
|
|
sendWsMsg(new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription)); |
|
|
} |
|
|
} |
|
|
|