Search in sources :

Example 1 with BatteryTemperature

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

the class SolarThingGraphQLFXService method queryFXCharging.

@GraphQLQuery(description = "Gives the timer values for the master FX of a single fragment over a time range")
public List<DataNode<FXChargingPacket>> queryFXCharging(@GraphQLArgument(name = "from") long from, @GraphQLArgument(name = "to") long to, @GraphQLArgument(name = "fragmentId") int fragmentId) {
    MetaDatabase metaDatabase = simpleQueryHandler.queryMeta();
    FXChargingSettingsPacket fxChargingSettingsPacket = null;
    FXChargingTemperatureAdjustPacket fxChargingTemperatureAdjustPacket = null;
    for (TargetedMetaPacket targetedMetaPacket : metaDatabase.getMeta(to, fragmentId)) {
        if (targetedMetaPacket.getPacketType() == TargetedMetaPacketType.FX_CHARGING_SETTINGS) {
            fxChargingSettingsPacket = (FXChargingSettingsPacket) targetedMetaPacket;
        } else if (targetedMetaPacket.getPacketType() == TargetedMetaPacketType.FX_CHARGING_TEMPERATURE_ADJUST) {
            fxChargingTemperatureAdjustPacket = (FXChargingTemperatureAdjustPacket) targetedMetaPacket;
        }
    }
    if (fxChargingSettingsPacket == null) {
        throw new UnexpectedResponseException("Could not find FX Charging settings in meta!");
    }
    // 3 hours back
    long startTime = from - 3 * 60 * 60 * 1000;
    List<? extends InstancePacketGroup> packets = simpleQueryHandler.queryStatus(startTime, to, null);
    // We make masterIdIgnoreDistance null because we will only be using fragmentId as the master fragment ID
    Map<String, List<FragmentedPacketGroup>> map = // separate based on source ID
    PacketGroups.sortPackets(packets, simpleQueryHandler.getDefaultInstanceOptions(), SolarThingConstants.STANDARD_MAX_TIME_DISTANCE.toMillis(), null, // make fragmentId be the master ID
    FragmentUtil.createPriorityComparator(fragmentId));
    List<FragmentedPacketGroup> sortedPackets = null;
    for (List<FragmentedPacketGroup> fragmentedPacketGroups : map.values()) {
        if (fragmentedPacketGroups.get(0).hasFragmentId(fragmentId)) {
            sortedPackets = fragmentedPacketGroups;
            break;
        }
    }
    if (sortedPackets == null) {
        throw new UnexpectedResponseException("Could not find fragment ID: " + fragmentId);
    }
    FXChargingSettings settings = fxChargingSettingsPacket.getFXChargingSettings();
    FXChargingStateHandler stateHandler = new FXChargingStateHandler(settings);
    Long lastUpdate = null;
    List<DataNode<FXChargingPacket>> r = new ArrayList<>();
    for (FragmentedPacketGroup packetGroup : sortedPackets) {
        List<FXStatusPacket> fxPackets = new ArrayList<>();
        Integer temperature = null;
        for (Packet packet : packetGroup.getPackets()) {
            if (packet instanceof FXStatusPacket && packetGroup.getFragmentId(packet) == fragmentId) {
                fxPackets.add((FXStatusPacket) packet);
            } else if (packet instanceof BatteryTemperature) {
                temperature = Math.round(((BatteryTemperature) packet).getBatteryTemperatureCelsius().floatValue());
            }
        }
        if (fxPackets.isEmpty()) {
            continue;
        }
        if (temperature == null) {
            // we need temperature data for accurate results // TODO add option for systems that don't use temperature compensation
            continue;
        }
        temperature += fxChargingTemperatureAdjustPacket == null ? 0 : fxChargingTemperatureAdjustPacket.getTemperatureAdjustCelsius();
        FXStatusPacket fx = OutbackUtil.getMasterFX(fxPackets);
        if (fx == null) {
            continue;
        }
        final long delta;
        if (lastUpdate == null) {
            delta = 1000;
        } else {
            delta = packetGroup.getDateMillis() - lastUpdate;
        }
        lastUpdate = packetGroup.getDateMillis();
        stateHandler.update(delta, fx, temperature);
        FXChargingPacket fxChargingPacket = new ImmutableFXChargingPacket(fx.getIdentifier(), stateHandler.getMode(), stateHandler.getRemainingAbsorbTimeMillis(), stateHandler.getRemainingFloatTimeMillis(), stateHandler.getRemainingEqualizeTimeMillis(), settings.getAbsorbTimeMillis(), settings.getFloatTimeMillis(), settings.getEqualizeTimeMillis());
        r.add(new DataNode<>(fxChargingPacket, fx, packetGroup.getDateMillis(), packetGroup.getSourceId(fx), fragmentId));
    }
    if (r.isEmpty() && !sortedPackets.isEmpty()) {
        throw new UnexpectedResponseException("There must have been no FX packets or no rover packets!");
    }
    return r;
}
Also used : ArrayList(java.util.ArrayList) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) FXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXChargingSettings(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingSettings) DataNode(me.retrodaredevil.solarthing.rest.graphql.packets.nodes.DataNode) ArrayList(java.util.ArrayList) List(java.util.List) FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) Packet(me.retrodaredevil.solarthing.packets.Packet) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXChargingTemperatureAdjustPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingTemperatureAdjustPacket) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) TargetedMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket) FXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingPacket) FXChargingSettingsPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingSettingsPacket) MetaDatabase(me.retrodaredevil.solarthing.type.closed.meta.MetaDatabase) BatteryTemperature(me.retrodaredevil.solarthing.solar.common.BatteryTemperature) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) TargetedMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket) FXChargingTemperatureAdjustPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingTemperatureAdjustPacket) UnexpectedResponseException(me.retrodaredevil.solarthing.rest.exceptions.UnexpectedResponseException) FXChargingSettingsPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingSettingsPacket) FXChargingStateHandler(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingStateHandler) GraphQLQuery(io.leangen.graphql.annotations.GraphQLQuery)

Example 2 with BatteryTemperature

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

the class StatusChatBotHandler method handleMessage.

@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
    if (ChatBotUtil.isSimilar("battery voltage", message.getText())) {
        FragmentedPacketGroup packetGroup = packetGroupProvider.getPacketGroup();
        Float batteryVoltageAverage = BatteryUtil.getBatteryVoltageAverage(packetGroup);
        if (batteryVoltageAverage == null) {
            messageSender.sendMessage("Battery voltage unavailable from latest data. Sorry!");
        } else {
            messageSender.sendMessage("Battery voltage: " + Formatting.HUNDREDTHS_FORMAT.format(batteryVoltageAverage));
        }
        return true;
    } else if (ChatBotUtil.isSimilar("battery temperature", message.getText()) || ChatBotUtil.isSimilar("battery temp", message.getText())) {
        FragmentedPacketGroup packetGroup = packetGroupProvider.getPacketGroup();
        List<String> lines = new ArrayList<>();
        for (Packet packet : packetGroup.getPackets()) {
            int fragmentId = packetGroup.getFragmentId(packet);
            if (packet instanceof BatteryTemperature && packet instanceof Identifiable) {
                float temperature = ((BatteryTemperature) packet).getBatteryTemperatureFahrenheit();
                IdentityInfo identityInfo = ((Identifiable) packet).getIdentityInfo();
                lines.add(identityInfo.getDisplayName() + " (" + fragmentId + "): " + temperature + "F");
            }
        }
        if (lines.isEmpty()) {
            messageSender.sendMessage("No devices to read temperature!");
        } else {
            messageSender.sendMessage(String.join("\n", lines));
        }
        return true;
    } else if (ChatBotUtil.isSimilar("alter", message.getText())) {
        List<VersionedPacket<StoredAlterPacket>> alterPackets = alterPacketsProvider.getPackets();
        if (alterPackets == null) {
            messageSender.sendMessage("Error - Must have failed to query alter database");
        } else {
            List<String> scheduledCommandLines = new ArrayList<>();
            for (VersionedPacket<StoredAlterPacket> versionedPacket : alterPackets) {
                StoredAlterPacket storedAlterPacket = versionedPacket.getPacket();
                AlterPacket alterPacket = storedAlterPacket.getPacket();
                if (alterPacket instanceof ScheduledCommandPacket) {
                    ScheduledCommandData data = ((ScheduledCommandPacket) alterPacket).getData();
                    // ExecutionReason executionReason = ((ScheduledCommandPacket) alterPacket).getExecutionReason();
                    String timeString = TimeUtil.instantToSlackDateSeconds(Instant.ofEpochMilli(data.getScheduledTimeMillis()));
                    scheduledCommandLines.add(data.getCommandName() + " - " + timeString);
                }
            }
            if (scheduledCommandLines.isEmpty()) {
                messageSender.sendMessage("No scheduled commands (from " + alterPackets.size() + " alter packets)");
            } else {
                messageSender.sendMessage("Scheduled commands:\n\t" + String.join("\n\t", scheduledCommandLines));
            }
        }
        return true;
    }
    return false;
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) Packet(me.retrodaredevil.solarthing.packets.Packet) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) BatteryTemperature(me.retrodaredevil.solarthing.solar.common.BatteryTemperature) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) ArrayList(java.util.ArrayList) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) Identifiable(me.retrodaredevil.solarthing.packets.identification.Identifiable) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) IdentityInfo(me.retrodaredevil.solarthing.packets.identification.IdentityInfo) ArrayList(java.util.ArrayList) List(java.util.List) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Packet (me.retrodaredevil.solarthing.packets.Packet)2 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)2 BatteryTemperature (me.retrodaredevil.solarthing.solar.common.BatteryTemperature)2 GraphQLQuery (io.leangen.graphql.annotations.GraphQLQuery)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 Identifiable (me.retrodaredevil.solarthing.packets.identification.Identifiable)1 IdentityInfo (me.retrodaredevil.solarthing.packets.identification.IdentityInfo)1 UnexpectedResponseException (me.retrodaredevil.solarthing.rest.exceptions.UnexpectedResponseException)1 DataNode (me.retrodaredevil.solarthing.rest.graphql.packets.nodes.DataNode)1 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)1 FXChargingPacket (me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingPacket)1 FXChargingSettings (me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingSettings)1 FXChargingStateHandler (me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingStateHandler)1 ImmutableFXChargingPacket (me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket)1 FXChargingSettingsPacket (me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingSettingsPacket)1 FXChargingTemperatureAdjustPacket (me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingTemperatureAdjustPacket)1 AlterPacket (me.retrodaredevil.solarthing.type.alter.AlterPacket)1 StoredAlterPacket (me.retrodaredevil.solarthing.type.alter.StoredAlterPacket)1