use of me.retrodaredevil.solarthing.packets.identification.IdentifierFragment 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.identification.IdentifierFragment 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.identification.IdentifierFragment in project solarthing by wildmountainfarms.
the class PacketFinder method updateWithRange.
private void updateWithRange(long queryStart, long queryEnd) {
List<? extends InstancePacketGroup> rawPackets = simpleQueryHandler.queryStatus(queryStart, queryEnd, null);
synchronized (cacheMap) {
for (InstancePacketGroup instancePacketGroup : rawPackets) {
int fragmentId = instancePacketGroup.getFragmentId();
for (Packet packet : instancePacketGroup.getPackets()) {
if (packet instanceof Identifiable) {
Identifiable identifiable = (Identifiable) packet;
IdentifierFragment packetIdentifierFragment = IdentifierFragment.create(fragmentId, identifiable.getIdentifier());
cacheMap.putIfAbsent(packetIdentifierFragment, identifiable);
}
}
}
}
}
use of me.retrodaredevil.solarthing.packets.identification.IdentifierFragment in project solarthing by wildmountainfarms.
the class PVOutputHandler method getStatus.
public AddStatusParameters getStatus(long dayStartTimeMillis, List<FragmentedPacketGroup> packetGroupList) {
FragmentedPacketGroup latestPacketGroup = packetGroupList.get(packetGroupList.size() - 1);
LOGGER.debug("Continuing with the latest packet group. Day start: " + dayStartTimeMillis);
AddStatusParametersBuilder addStatusParametersBuilder = createStatusBuilder(zoneId, latestPacketGroup.getDateMillis());
setStatusPowerValues(addStatusParametersBuilder, latestPacketGroup);
setStatusEnergyValues(addStatusParametersBuilder, packetGroupList, AccumulationConfig.createDefault(dayStartTimeMillis));
for (Packet packet : latestPacketGroup.getPackets()) {
if (packet instanceof PVCurrentAndVoltage) {
int fragmentId = latestPacketGroup.getFragmentId(packet);
PVCurrentAndVoltage pvCurrentAndVoltage = (PVCurrentAndVoltage) packet;
IdentifierFragment identifierFragment = IdentifierFragment.create(fragmentId, pvCurrentAndVoltage.getIdentifier());
if (voltageIdentifierFragmentMatcher.matches(identifierFragment)) {
float voltage = pvCurrentAndVoltage.getPVVoltage().floatValue();
addStatusParametersBuilder.setVoltage(voltage);
}
} else if (packet instanceof TemperaturePacket) {
int fragmentId = latestPacketGroup.getFragmentId(packet);
TemperaturePacket temperaturePacket = (TemperaturePacket) packet;
IdentifierFragment identifierFragment = IdentifierFragment.create(fragmentId, temperaturePacket.getIdentifier());
if (temperatureIdentifierFragmentMatcher.matches(identifierFragment)) {
float temperatureCelsius = temperaturePacket.getTemperatureCelsius();
if (!TemperaturePacket.POSSIBLE_BAD_VALUES.contains(temperatureCelsius)) {
addStatusParametersBuilder.setTemperatureCelsius(temperatureCelsius);
} else {
LOGGER.info("Not setting temperature: " + temperatureCelsius + " because it could be a bad reading");
}
}
}
}
return addStatusParametersBuilder.build();
}
use of me.retrodaredevil.solarthing.packets.identification.IdentifierFragment in project solarthing by wildmountainfarms.
the class DefaultIdentificationCacheDataPacket method combineNodes.
private static <T extends IdentificationCacheData> List<IdentificationCacheNode<T>> combineNodes(List<IdentificationCacheNode<T>> first, List<IdentificationCacheNode<T>> second) {
Map<IdentifierFragment, T> map = new HashMap<>();
putOnMap(map, first);
putOnMap(map, second);
return map.entrySet().stream().map((identifierFragmentTEntry -> {
IdentifierFragment identifierFragment = identifierFragmentTEntry.getKey();
T data = identifierFragmentTEntry.getValue();
return new IdentificationCacheNode<>(identifierFragment.getFragmentId(), data);
})).collect(Collectors.toList());
}
Aggregations