Search in sources :

Example 1 with BatteryVoltage

use of me.retrodaredevil.solarthing.solar.common.BatteryVoltage in project solarthing by wildmountainfarms.

the class BatteryRecordCacheNodeCreator method create.

@Override
public IdentificationCacheNode<BatteryRecordDataCache> create(IdentifierFragment identifierFragment, List<TimestampedPacket<BatteryVoltage>> timestampedPackets, Instant periodStart, Duration periodDuration) {
    long periodStartDateMillis = periodStart.toEpochMilli();
    long previousPeriodStartDateMillis = periodStartDateMillis - periodDuration.toMillis();
    long unknownCutOffDateMillis = periodStartDateMillis - CacheHandler.INFO_DURATION.toMillis();
    long periodEndDateMillis = periodStart.toEpochMilli() + periodDuration.toMillis();
    boolean firstInPeriod = true;
    // initializations to -1 are necessary. We will use firstInPeriod to know if they have been initialized
    float min = -1;
    long minDateMillis = -1;
    float max = -1;
    long maxDateMillis = -1;
    MutableIntegral mainVoltHourIntegral = new TrapezoidalRuleAccumulator();
    TimestampedPacket<BatteryVoltage> lastDataBeforePreviousPeriodStart = null;
    TimestampedPacket<BatteryVoltage> lastDataBeforePeriodStart = null;
    TimestampedPacket<BatteryVoltage> firstDataAfterPeriodStart = null;
    TimestampedPacket<BatteryVoltage> lastDataBeforePeriodEnd = null;
    for (TimestampedPacket<BatteryVoltage> packet : timestampedPackets) {
        final float voltage = packet.getPacket().getBatteryVoltage();
        final long dateMillis = packet.getDateMillis();
        if (dateMillis >= periodEndDateMillis) {
            break;
        }
        if (dateMillis < unknownCutOffDateMillis) {
            continue;
        }
        if (dateMillis < previousPeriodStartDateMillis) {
            lastDataBeforePreviousPeriodStart = packet;
        } else if (dateMillis < periodStartDateMillis) {
            lastDataBeforePeriodStart = packet;
        } else {
            if (firstDataAfterPeriodStart == null) {
                firstDataAfterPeriodStart = packet;
            }
            lastDataBeforePeriodEnd = packet;
            if (firstInPeriod || voltage < min) {
                min = voltage;
                minDateMillis = dateMillis;
            }
            if (firstInPeriod || voltage > max) {
                max = voltage;
                maxDateMillis = dateMillis;
            }
            if (firstInPeriod && lastDataBeforePeriodStart != null) {
                double hours = lastDataBeforePeriodStart.getDateMillis() / (1000.0 * 60 * 60);
                mainVoltHourIntegral.add(hours, lastDataBeforePeriodStart.getPacket().getBatteryVoltage());
            }
            double hours = dateMillis / (1000.0 * 60 * 60);
            mainVoltHourIntegral.add(hours, voltage);
            firstInPeriod = false;
        }
    }
    final BatteryRecordDataCache.Record record;
    final Long firstDateMillis;
    final Long lastDateMillis;
    final Long unknownStartDateMillis;
    if (firstInPeriod) {
        record = null;
        firstDateMillis = null;
        lastDateMillis = null;
        unknownStartDateMillis = null;
    } else {
        final double unknownBatteryVoltageHours;
        final long unknownDurationMillis;
        if (lastDataBeforePeriodStart == null) {
            // we have no data from the previous period
            firstDateMillis = firstDataAfterPeriodStart.getDateMillis();
            // we will only have "unknown" data if there is no data in the previous period and we were able to get data from before the previous period
            if (lastDataBeforePreviousPeriodStart == null) {
                unknownStartDateMillis = null;
                unknownDurationMillis = 0;
                unknownBatteryVoltageHours = 0.0;
            } else {
                unknownStartDateMillis = lastDataBeforePreviousPeriodStart.getDateMillis();
                unknownDurationMillis = firstDataAfterPeriodStart.getDateMillis() - unknownStartDateMillis;
                double unknownPeriodAverageVoltage = (lastDataBeforePreviousPeriodStart.getPacket().getBatteryVoltage() + firstDataAfterPeriodStart.getPacket().getBatteryVoltage()) / 2.0;
                double hours = unknownDurationMillis / (1000.0 * 60 * 60);
                unknownBatteryVoltageHours = unknownPeriodAverageVoltage * hours;
            }
        } else {
            firstDateMillis = lastDataBeforePeriodStart.getDateMillis();
            unknownStartDateMillis = null;
            unknownDurationMillis = 0;
            unknownBatteryVoltageHours = 0.0;
        }
        lastDateMillis = lastDataBeforePeriodEnd.getDateMillis();
        record = new BatteryRecordDataCache.Record(min, minDateMillis, max, maxDateMillis, unknownBatteryVoltageHours, unknownDurationMillis, mainVoltHourIntegral.getIntegral(), lastDataBeforePeriodEnd.getDateMillis() - firstDateMillis, 0.0, 0L);
    }
    return new IdentificationCacheNode<>(identifierFragment.getFragmentId(), new BatteryRecordDataCache(identifierFragment.getIdentifier(), firstDateMillis, lastDateMillis, unknownStartDateMillis, record));
}
Also used : MutableIntegral(me.retrodaredevil.solarthing.util.integration.MutableIntegral) TrapezoidalRuleAccumulator(me.retrodaredevil.solarthing.util.integration.TrapezoidalRuleAccumulator) BatteryRecordDataCache(me.retrodaredevil.solarthing.type.cache.packets.data.BatteryRecordDataCache) IdentificationCacheNode(me.retrodaredevil.solarthing.type.cache.packets.IdentificationCacheNode) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage)

Example 2 with BatteryVoltage

use of me.retrodaredevil.solarthing.solar.common.BatteryVoltage in project solarthing by wildmountainfarms.

the class BatteryUtil method getBatteryVoltageAverage.

@Nullable
public static Float getBatteryVoltageAverage(PacketGroup packetGroup) {
    float sum = 0;
    int count = 0;
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof BatteryVoltage) {
            sum += ((BatteryVoltage) packet).getBatteryVoltage();
            count++;
        }
    }
    if (count == 0) {
        return null;
    }
    return sum / count;
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 3 with BatteryVoltage

use of me.retrodaredevil.solarthing.solar.common.BatteryVoltage in project solarthing by wildmountainfarms.

the class CheckMain method doModbus.

private static boolean doModbus(@NotNull String port, int startingAddress, boolean scan, SerialConfig serialConfig, Function<ModbusSlave, BatteryVoltage> slaveToReadTable) throws SerialPortException {
    System.out.println("Going to open serial port using default serial configuration...");
    try (JSerialIOBundle ioBundle = JSerialIOBundle.createPort(port, serialConfig)) {
        System.out.println("Successfully opened serial port...");
        ModbusSlaveBus bus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder());
        MutableAddressModbusSlave modbusSlave = new MutableAddressModbusSlave(startingAddress, bus);
        BatteryVoltage readTable = slaveToReadTable.apply(modbusSlave);
        int maxAddress = scan ? 247 : startingAddress;
        for (int currentAddress = startingAddress; currentAddress <= maxAddress; currentAddress++) {
            modbusSlave.setAddress(currentAddress);
            System.out.println("Checking on address: " + currentAddress);
            try {
                float batteryVoltage = readTable.getBatteryVoltage();
                System.out.println("Success! Battery Voltage: " + batteryVoltage);
                return true;
            } catch (ModbusTimeoutException e) {
                System.err.println("Got timeout. This means that the modbus address is incorrect or that the cable is not functioning properly.");
            } catch (ModbusRuntimeException e) {
                e.printStackTrace();
                System.err.println("Got some sort of modbus error. Info logged above");
            }
        }
        System.err.println("Did not find a device");
        return false;
    }
}
Also used : MutableAddressModbusSlave(me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) RtuDataEncoder(me.retrodaredevil.io.modbus.RtuDataEncoder) ModbusTimeoutException(me.retrodaredevil.io.modbus.ModbusTimeoutException) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusSlaveBus(me.retrodaredevil.io.modbus.ModbusSlaveBus) JSerialIOBundle(me.retrodaredevil.io.serial.JSerialIOBundle) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage)

Aggregations

BatteryVoltage (me.retrodaredevil.solarthing.solar.common.BatteryVoltage)3 IOModbusSlaveBus (me.retrodaredevil.io.modbus.IOModbusSlaveBus)1 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)1 ModbusSlaveBus (me.retrodaredevil.io.modbus.ModbusSlaveBus)1 ModbusTimeoutException (me.retrodaredevil.io.modbus.ModbusTimeoutException)1 RtuDataEncoder (me.retrodaredevil.io.modbus.RtuDataEncoder)1 JSerialIOBundle (me.retrodaredevil.io.serial.JSerialIOBundle)1 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)1 Packet (me.retrodaredevil.solarthing.packets.Packet)1 MutableAddressModbusSlave (me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave)1 IdentificationCacheNode (me.retrodaredevil.solarthing.type.cache.packets.IdentificationCacheNode)1 BatteryRecordDataCache (me.retrodaredevil.solarthing.type.cache.packets.data.BatteryRecordDataCache)1 MutableIntegral (me.retrodaredevil.solarthing.util.integration.MutableIntegral)1 TrapezoidalRuleAccumulator (me.retrodaredevil.solarthing.util.integration.TrapezoidalRuleAccumulator)1