Search in sources :

Example 1 with CommandOpenPacket

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

the class SendEncryptedActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    SolarThingDatabase database = actionEnvironment.getInjectEnvironment().get(SolarThingDatabaseEnvironment.class).getSolarThingDatabase();
    String sourceId = actionEnvironment.getInjectEnvironment().get(SourceIdEnvironment.class).getSourceId();
    ZoneId zoneId = actionEnvironment.getInjectEnvironment().get(TimeZoneEnvironment.class).getZoneId();
    CommandOpenPacket packet = packetProvider.get();
    requireNonNull(packet, "The supplier should have given us a packet!");
    PacketCollectionCreator creator = commandManager.makeCreator(sourceId, zoneId, InstanceTargetPackets.create(fragmentIdTargets), packet, PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
    return Actions.createLinkedActionRunner(new SendPacketAction(threadFactory, database::getOpenDatabase, creator, 100, 10, // TODO allow actions to be passed in for these
    PassActionNode.getInstance().createAction(actionEnvironment), PassActionNode.getInstance().createAction(actionEnvironment)), WhenDone.BE_DONE, true);
}
Also used : SolarThingDatabaseEnvironment(me.retrodaredevil.solarthing.actions.environment.SolarThingDatabaseEnvironment) SourceIdEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceIdEnvironment) TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) ZoneId(java.time.ZoneId) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)

Example 2 with CommandOpenPacket

use of me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket 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);
    };
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) KeyPair(java.security.KeyPair) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) me.retrodaredevil.solarthing.packets.security.crypto(me.retrodaredevil.solarthing.packets.security.crypto) LoggerFactory(org.slf4j.LoggerFactory) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) Cipher(javax.crypto.Cipher) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) Objects.requireNonNull(java.util.Objects.requireNonNull) JacksonUtil(me.retrodaredevil.solarthing.util.JacksonUtil) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StandardOpenOption(java.nio.file.StandardOpenOption) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) PublicKey(java.security.PublicKey) Instant(java.time.Instant) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) File(java.io.File) ZoneId(java.time.ZoneId) PacketCollections(me.retrodaredevil.solarthing.packets.collection.PacketCollections) InstanceSourcePackets(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets) List(java.util.List) PrivateKey(java.security.PrivateKey) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceTargetPacket(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) KeyPair(java.security.KeyPair) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with CommandOpenPacket

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

the class ActionNodeDataReceiver method receivePacketGroup.

@Override
public void receivePacketGroup(String sender, TargetPacketGroup packetGroup) {
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof CommandOpenPacket) {
            CommandOpenPacket commandOpenPacket = (CommandOpenPacket) packet;
            if (commandOpenPacket.getPacketType() == CommandOpenPacketType.REQUEST_COMMAND) {
                RequestCommandPacket requestCommand = (RequestCommandPacket) commandOpenPacket;
                OpenSource source = new OpenSource(sender, packetGroup.getDateMillis(), requestCommand, requestCommand.getCommandName());
                receiveData(source, requestCommand.getCommandName());
            }
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)

Example 4 with CommandOpenPacket

use of me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket 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

CommandOpenPacket (me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)4 PacketCollectionCreator (me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)3 Instant (java.time.Instant)2 ZoneId (java.time.ZoneId)2 Packet (me.retrodaredevil.solarthing.packets.Packet)2 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1