Search in sources :

Example 1 with CacheCreator

use of me.retrodaredevil.solarthing.rest.cache.creators.CacheCreator in project solarthing by wildmountainfarms.

the class CacheHandler method calculatePeriod.

private List<CacheDataPacket> calculatePeriod(long startPeriodNumber, long endPeriodNumber) {
    Instant firstPeriodStart = getPeriodStartFromNumber(startPeriodNumber);
    Instant lastPeriodEnd = getPeriodStartFromNumber(endPeriodNumber).plus(duration);
    Instant queryStart = firstPeriodStart.minus(INFO_DURATION);
    MillisQuery millisQuery = new MillisQueryBuilder().startKey(queryStart.toEpochMilli()).endKey(lastPeriodEnd.toEpochMilli()).inclusiveEnd(false).build();
    final List<? extends PacketGroup> packetGroups;
    try {
        packetGroups = database.getStatusDatabase().query(millisQuery);
    } catch (SolarThingDatabaseException e) {
        // The consumers of this API may be ok if there are holes in the data rather than getting no data at all, so maybe change this later?
        throw new DatabaseException("Couldn't query status packets for period. startPeriodNumber: " + startPeriodNumber + " endPeriodNumber: " + endPeriodNumber + " firstPeriodStart: " + firstPeriodStart, e);
    }
    List<CacheDataPacket> r = new ArrayList<>();
    Map<String, List<InstancePacketGroup>> sourceMap = PacketGroups.parsePackets(packetGroups, defaultInstanceOptions);
    for (Map.Entry<String, List<InstancePacketGroup>> entry : sourceMap.entrySet()) {
        String sourceId = entry.getKey();
        List<InstancePacketGroup> packets = entry.getValue();
        for (long periodNumber = startPeriodNumber; periodNumber <= endPeriodNumber; periodNumber++) {
            Instant periodStart = getPeriodStartFromNumber(periodNumber);
            for (CacheCreator creator : CACHE_CREATORS) {
                r.add(creator.createFrom(sourceId, packets, periodStart, duration));
            }
        }
    }
    return r;
}
Also used : InstancePacketGroup(me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup) Instant(java.time.Instant) MillisQueryBuilder(me.retrodaredevil.solarthing.database.MillisQueryBuilder) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) CacheDataPacket(me.retrodaredevil.solarthing.type.cache.packets.CacheDataPacket) MillisQuery(me.retrodaredevil.solarthing.database.MillisQuery) DatabaseException(me.retrodaredevil.solarthing.rest.exceptions.DatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) CacheCreator(me.retrodaredevil.solarthing.rest.cache.creators.CacheCreator) DefaultIdentificationCacheCreator(me.retrodaredevil.solarthing.rest.cache.creators.DefaultIdentificationCacheCreator)

Aggregations

Instant (java.time.Instant)1 MillisQuery (me.retrodaredevil.solarthing.database.MillisQuery)1 MillisQueryBuilder (me.retrodaredevil.solarthing.database.MillisQueryBuilder)1 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)1 InstancePacketGroup (me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup)1 CacheCreator (me.retrodaredevil.solarthing.rest.cache.creators.CacheCreator)1 DefaultIdentificationCacheCreator (me.retrodaredevil.solarthing.rest.cache.creators.DefaultIdentificationCacheCreator)1 DatabaseException (me.retrodaredevil.solarthing.rest.exceptions.DatabaseException)1 CacheDataPacket (me.retrodaredevil.solarthing.type.cache.packets.CacheDataPacket)1