Search in sources :

Example 21 with SolarThingDatabaseException

use of me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException in project solarthing by wildmountainfarms.

the class CancelCommandChatBotHandler method cancelCommand.

private void cancelCommand(MessageSender messageSender, UUID schedulingId) {
    List<VersionedPacket<StoredAlterPacket>> packets = alterPacketsProvider.getPackets();
    if (packets == null) {
        messageSender.sendMessage("Unable to cancel commands, as we are unable to reach the alter database.");
        return;
    }
    List<VersionedPacket<StoredAlterPacket>> targets = findStoredPacketsWithSchedulingIdOrNull(packets.stream(), schedulingId);
    if (targets.isEmpty()) {
        messageSender.sendMessage("Unable to find a scheduled command that was scheduled with the ID of " + schedulingId);
    } else if (targets.size() > 1) {
        messageSender.sendMessage("Multiple packets corresponded to ID: " + schedulingId + ". Please report this error.");
    } else {
        VersionedPacket<StoredAlterPacket> target = targets.get(0);
        messageSender.sendMessage("Going request cancel of " + target.getPacket().getDbId());
        CommandOpenPacket packet = new ImmutableDeleteAlterPacket(target.getPacket().getDbId(), target.getUpdateToken());
        PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, null, packet, PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
        executorService.execute(() -> {
            Instant now = Instant.now();
            PacketCollection packetCollection = creator.create(now);
            boolean success = true;
            try {
                database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            } catch (SolarThingDatabaseException e) {
                LOGGER.error("Could not upload alter delete request", e);
                success = false;
            }
            if (success) {
                messageSender.sendMessage("Sent request to delete the scheduled command");
            } else {
                messageSender.sendMessage("Could not upload request to delete scheduled command. You can try again.");
            }
        });
    }
}
Also used : ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) Instant(java.time.Instant) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Aggregations

SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)21 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)9 Instant (java.time.Instant)7 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)6 PacketCollectionCreator (me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)6 MillisQueryBuilder (me.retrodaredevil.solarthing.database.MillisQueryBuilder)5 StoredAlterPacket (me.retrodaredevil.solarthing.type.alter.StoredAlterPacket)5 ArrayList (java.util.ArrayList)4 RequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket)4 NotFoundSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException)4 FlagData (me.retrodaredevil.solarthing.type.alter.flag.FlagData)4 ScheduledCommandData (me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)4 List (java.util.List)3 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)3 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)3 DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)3 ImmutableRequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket)3 UnauthorizedSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UnauthorizedSolarThingDatabaseException)3