use of me.retrodaredevil.solarthing.packets.TimestampedPacket in project solarthing by wildmountainfarms.
the class DefaultIdentificationCacheCreator method createFrom.
@Override
public IdentificationCacheDataPacket<T> createFrom(String sourceId, List<InstancePacketGroup> packetGroups, Instant periodStart, Duration periodDuration) {
Class<U> acceptedType = identificationCacheNodeCreator.getAcceptedType();
Map<String, Map<IdentifierFragment, List<TimestampedPacket<U>>>> mappedPackets = convertPackets(packetGroups, acceptedType);
Map<IdentifierFragment, List<TimestampedPacket<U>>> identifierFragmentMap = mappedPackets.getOrDefault(sourceId, Collections.emptyMap());
List<IdentificationCacheNode<T>> nodes = new ArrayList<>(identifierFragmentMap.size());
for (Map.Entry<IdentifierFragment, List<TimestampedPacket<U>>> entry : identifierFragmentMap.entrySet()) {
IdentifierFragment identifierFragment = entry.getKey();
List<TimestampedPacket<U>> packets = entry.getValue();
IdentificationCacheNode<T> node = identificationCacheNodeCreator.create(identifierFragment, packets, periodStart, periodDuration);
nodes.add(node);
}
return new DefaultIdentificationCacheDataPacket<>(periodStart.toEpochMilli(), periodDuration.toMillis(), sourceId, identificationCacheNodeCreator.getCacheName(), nodes);
}
use of me.retrodaredevil.solarthing.packets.TimestampedPacket in project solarthing by wildmountainfarms.
the class DefaultIdentificationCacheCreator method convertPackets.
@SuppressWarnings("unchecked")
private static <T extends Identifiable> Map<String, Map<IdentifierFragment, List<TimestampedPacket<T>>>> convertPackets(List<? extends FragmentedPacketGroup> packetGroups, Class<T> acceptedType) {
Map<String, Map<IdentifierFragment, List<TimestampedPacket<T>>>> r = new HashMap<>();
for (FragmentedPacketGroup packetGroup : packetGroups) {
for (Packet packet : packetGroup.getPackets()) {
if (!acceptedType.isInstance(packet)) {
continue;
}
T identifiablePacket = (T) packet;
String sourceId = packetGroup.getSourceId(packet);
int fragmentId = packetGroup.getFragmentId(packet);
Identifier identifier = identifiablePacket.getIdentifier();
IdentifierFragment identifierFragment = IdentifierFragment.create(fragmentId, identifier);
List<TimestampedPacket<T>> list = r.computeIfAbsent(sourceId, k -> new HashMap<>()).computeIfAbsent(identifierFragment, k -> new ArrayList<>());
long dateMillis = packetGroup.getDateMillisOrKnown(packet);
list.add(new TimestampedPacket<>(identifiablePacket, dateMillis));
}
}
return r;
}
use of me.retrodaredevil.solarthing.packets.TimestampedPacket in project solarthing by wildmountainfarms.
the class AccumulationUtil method mapPackets.
@SuppressWarnings("unchecked")
@Contract(pure = true)
public static <T extends Identifiable> Map<IdentifierFragment, List<TimestampedPacket<T>>> mapPackets(Class<T> clazz, List<? extends FragmentedPacketGroup> packetGroups) {
Map<IdentifierFragment, List<TimestampedPacket<T>>> packetMap = new HashMap<>();
for (FragmentedPacketGroup packetGroup : packetGroups) {
for (Packet packet : packetGroup.getPackets()) {
if (clazz.isInstance(packet)) {
int fragmentId = packetGroup.getFragmentId(packet);
T t = (T) packet;
IdentifierFragment identifierFragment = IdentifierFragment.create(fragmentId, t.getIdentifier());
List<TimestampedPacket<T>> packetList = packetMap.computeIfAbsent(identifierFragment, k -> new ArrayList<>());
long dateMillis = packetGroup.getDateMillisOrKnown(packet);
packetList.add(new TimestampedPacket<>(t, dateMillis));
}
}
}
return packetMap;
}
Aggregations