use of me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator in project solarthing by wildmountainfarms.
the class CommandManager method makeCreator.
/**
* @param instanceTargetPacket The {@link InstanceTargetPacket} to indicate which fragments to target or null. If null, it is not added to the packet collection
* @param commandOpenPacket The command packet
* @return A creator to make a packet collection. When supplied with an {@link Instant} representing now, a packet collection is created.
*/
public PacketCollectionCreator makeCreator(String sourceId, ZoneId zoneId, @Nullable InstanceTargetPacket instanceTargetPacket, CommandOpenPacket commandOpenPacket, PacketCollectionIdGenerator packetCollectionIdGenerator) {
// instanceTargetPacket may be null
KeyPair keyPair = getKeyPair();
InstanceSourcePacket instanceSourcePacket = InstanceSourcePackets.create(sourceId);
// ----
return now -> {
PacketCollection packetCollectionToNestAndEncrypt = PacketCollections.create(now, instanceTargetPacket == null ? Arrays.asList(commandOpenPacket, instanceSourcePacket) : Arrays.asList(commandOpenPacket, instanceSourcePacket, instanceTargetPacket), "unused document ID that does not get serialized");
// Note, on packetCollectionToNestAndEncrypt, _id is not serialized, so the generator and zoneId used above do NOT affect anything
final String payload;
try {
payload = MAPPER.writeValueAsString(packetCollectionToNestAndEncrypt);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
String hashString = Long.toHexString(now.toEpochMilli()) + "," + HashUtil.encodedHash(payload);
final String encrypted;
try {
synchronized (CIPHER) {
// It's possible we could be in a multi-threaded environment, and you cannot have multiple threads using a single cipher at once
encrypted = Encrypt.encrypt(CIPHER, keyPair.getPrivate(), hashString);
}
} catch (InvalidKeyException | EncryptException e) {
throw new RuntimeException(e);
}
List<Packet> packets = new ArrayList<>(Arrays.asList(new ImmutableLargeIntegrityPacket(sender, encrypted, payload), instanceSourcePacket));
if (instanceTargetPacket != null) {
packets.add(instanceTargetPacket);
}
return PacketCollections.createFromPackets(now, packets, packetCollectionIdGenerator, zoneId);
};
}
use of me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator 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.");
}
}
}
use of me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator 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.");
}
});
}
}
Aggregations