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.");
}
});
}
Aggregations