Search in sources :

Example 1 with DeleteAlterPacket

use of me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket in project solarthing by wildmountainfarms.

the class AlterManagerAction method receivePacketWithIntegrity.

private void receivePacketWithIntegrity(String sender, TargetPacketGroup packetGroup) {
    LOGGER.debug("Sender: " + sender + " is authorized and sent a packet targeting no fragments, so we will see if we want to handle anything from it.");
    long now = System.currentTimeMillis();
    List<StoredAlterPacket> storedAlterPacketsToUpload = new ArrayList<>();
    List<DeleteAlterPacket> deleteAlterPackets = new ArrayList<>();
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof ScheduleCommandPacket) {
            ScheduleCommandPacket scheduleCommandPacket = (ScheduleCommandPacket) packet;
            ScheduledCommandData data = scheduleCommandPacket.getData();
            ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), scheduleCommandPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
            scheduleCommandPacket.getUniqueString()));
            ScheduledCommandPacket scheduledCommandPacket = new ScheduledCommandPacket(data, executionReason);
            // This databaseId is basically an arbitrary way to generate a unique ID. It contains some stuff such as the command name to debug more easily
            String databaseId = "alter-scheduled-command-" + data.getCommandName() + "-" + Long.toHexString(data.getScheduledTimeMillis()) + "-" + sender + "-" + Math.random();
            StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, scheduledCommandPacket, this.sourceId);
            storedAlterPacketsToUpload.add(storedAlterPacket);
        } else if (packet instanceof DeleteAlterPacket) {
            DeleteAlterPacket deleteAlterPacket = (DeleteAlterPacket) packet;
            try {
                database.validateUpdateToken(deleteAlterPacket.getUpdateToken());
                deleteAlterPackets.add(deleteAlterPacket);
            } catch (IncompatibleUpdateTokenException ex) {
                LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "For some reason we have an incompatible update token!", ex);
            }
        } else if (packet instanceof RequestFlagPacket) {
            RequestFlagPacket requestFlagPacket = (RequestFlagPacket) packet;
            // Originally I was going to not upload a FlagPacket if an existing FlagPacket's
            // time range fully encapsulated the newly requested one, but that adds some complexity
            // that is not needed at the moment
            FlagData flagData = requestFlagPacket.getFlagData();
            ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), requestFlagPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
            requestFlagPacket.getUniqueString()));
            FlagPacket flagPacket = new FlagPacket(flagData, executionReason);
            String databaseId = "alter-flag-" + flagData.getFlagName() + "-" + sender + "-" + Math.random();
            StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, flagPacket, sourceId);
            storedAlterPacketsToUpload.add(storedAlterPacket);
        }
    }
    if (storedAlterPacketsToUpload.isEmpty() && deleteAlterPackets.isEmpty()) {
        // Nothing for us to do, so no need to schedule a runnable
        return;
    }
    executorService.execute(() -> {
        int uploadCount = 0;
        try {
            for (StoredAlterPacket storedAlterPacket : storedAlterPacketsToUpload) {
                this.database.getAlterDatabase().upload(storedAlterPacket);
                uploadCount++;
            }
        } catch (SolarThingDatabaseException e) {
            // TODO in future we should try multiple times to upload
            LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not upload a stored alter packet! uploaded: " + uploadCount + " / " + storedAlterPacketsToUpload.size(), e);
        }
        int deleteCount = 0;
        try {
            for (DeleteAlterPacket deleteAlterPacket : deleteAlterPackets) {
                this.database.getAlterDatabase().delete(deleteAlterPacket.getDocumentIdToDelete(), deleteAlterPacket.getUpdateToken());
                deleteCount++;
            }
        } catch (SolarThingDatabaseException e) {
            LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not delete alter packets! deleted: " + deleteCount + " / " + deleteAlterPackets.size(), e);
        }
    });
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) LargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.LargeIntegrityPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ArrayList(java.util.ArrayList) ExecutionReason(me.retrodaredevil.solarthing.reason.ExecutionReason) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) IncompatibleUpdateTokenException(me.retrodaredevil.solarthing.database.exception.IncompatibleUpdateTokenException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) ScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)

Example 2 with DeleteAlterPacket

use of me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket in project solarthing by wildmountainfarms.

the class FlagCommandChatBotHandler method clearFlag.

private void clearFlag(MessageSender messageSender, String flagName) {
    List<VersionedPacket<StoredAlterPacket>> packets = getPacketsWithFlagName(flagName);
    if (packets == null) {
        messageSender.sendMessage("Could not get flags. Please try again.");
        return;
    }
    Instant now = Instant.now();
    if (packets.isEmpty()) {
        messageSender.sendMessage("Flag: '" + flagName + "' is not set!");
        return;
    }
    List<VersionedPacket<StoredAlterPacket>> activePackets = packets.stream().filter(versionedPacket -> {
        FlagPacket flagPacket = (FlagPacket) versionedPacket.getPacket().getPacket();
        return flagPacket.getFlagData().getActivePeriod().isActive(now);
    }).collect(Collectors.toList());
    if (activePackets.isEmpty()) {
        messageSender.sendMessage("Flag: '" + flagName + "' is not currently active. In a future update we may allow you to clear inactive flags.");
        return;
    }
    // We know that the user wants this flag cleared, so let's delete all the packets that are active.
    for (VersionedPacket<StoredAlterPacket> packetToRequestDeleteFor : activePackets) {
        DeleteAlterPacket deleteAlterPacket = new ImmutableDeleteAlterPacket(packetToRequestDeleteFor.getPacket().getDbId(), packetToRequestDeleteFor.getUpdateToken());
        PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, null, deleteAlterPacket, PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
        PacketCollection packetCollection = creator.create(now);
        boolean success = false;
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            success = true;
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Could not upload request to delete alter packet for ID: " + packetToRequestDeleteFor.getPacket().getDbId(), e);
        }
        if (success) {
            messageSender.sendMessage("Requested delete for flag: '" + flagName + "' under ID: " + packetToRequestDeleteFor.getPacket().getDbId());
        } else {
            messageSender.sendMessage("Could not request delete for flag: '" + flagName + "' under ID: " + packetToRequestDeleteFor.getPacket().getDbId() + ". See logs for details.");
        }
    }
}
Also used : Arrays(java.util.Arrays) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider) LoggerFactory(org.slf4j.LoggerFactory) NotNull(me.retrodaredevil.solarthing.annotations.NotNull) MessageSender(me.retrodaredevil.solarthing.message.MessageSender) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) Instant(java.time.Instant) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) Collectors(java.util.stream.Collectors) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) List(java.util.List) TimeRange(me.retrodaredevil.solarthing.util.TimeRange) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) Collections(java.util.Collections) ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) 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) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)

Aggregations

DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)2 RequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket)2 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 AlterPacket (me.retrodaredevil.solarthing.type.alter.AlterPacket)2 StoredAlterPacket (me.retrodaredevil.solarthing.type.alter.StoredAlterPacket)2 FlagData (me.retrodaredevil.solarthing.type.alter.flag.FlagData)2 FlagPacket (me.retrodaredevil.solarthing.type.alter.packets.FlagPacket)2 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Collectors (java.util.stream.Collectors)1 AlterPacketsProvider (me.retrodaredevil.solarthing.AlterPacketsProvider)1 NotNull (me.retrodaredevil.solarthing.annotations.NotNull)1 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)1