Search in sources :

Example 1 with ImmutableScheduleCommandPacket

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

the class ScheduleCommandChatBotHandler method schedule.

private void schedule(MessageSender messageSender, Instant now, Instant targetTime, AvailableCommand availableCommand) {
    if (now.plus(Duration.ofMinutes(1)).isAfter(targetTime)) {
        messageSender.sendMessage("Cannot schedule a command less than one minute from now.");
        return;
    }
    if (now.plus(Duration.ofHours(72)).isBefore(targetTime)) {
        messageSender.sendMessage("Cannot schedule a command more than 72 hours from now.");
        return;
    }
    UUID uniqueId = UUID.randomUUID();
    PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, // We don't have an InstanceTargetPacket because scheduling commands is not handled by a program with a fragment ID // also look at PacketGroups.parseToTargetPacketGroup() for interpretation without a TargetInstancePacket
    null, new ImmutableScheduleCommandPacket(new ScheduledCommandData(targetTime.toEpochMilli(), availableCommand.getCommandInfo().getName(), Collections.singleton(availableCommand.getFragmentId())), uniqueId), PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
    PacketCollection packetCollection = creator.create(now);
    messageSender.sendMessage("Scheduling " + availableCommand.getCommandInfo().getDisplayName() + " at " + TimeUtil.instantToSlackDateSeconds(targetTime));
    executorService.execute(() -> {
        boolean success = false;
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            success = true;
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Could not upload schedule command packet collection", e);
        }
        if (success) {
            messageSender.sendMessage("Successfully requested schedule. ID: " + uniqueId);
        } else {
            messageSender.sendMessage("Could not upload schedule command request.");
        }
    });
}
Also used : PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) UUID(java.util.UUID) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) ImmutableScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableScheduleCommandPacket) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Aggregations

UUID (java.util.UUID)1 ImmutableScheduleCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableScheduleCommandPacket)1 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)1 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)1 PacketCollectionCreator (me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)1 ScheduledCommandData (me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)1