Search in sources :

Example 1 with ImmutableRequestCommandPacket

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

the class FeedbackPacketTest method test.

@Test
void test() throws JsonProcessingException {
    PacketTestUtil.testJson(new ImmutableExecutionFeedbackPacket("hello there", "important.informational", new OpenSourceExecutionReason(new OpenSource("josh", 1632366551290L, new ImmutableRequestCommandPacket("GEN OFF"), "GEN OFF"))), FeedbackPacket.class);
    PacketTestUtil.testJson(new ImmutableHeartbeatPacket(new HeartbeatData("hello", "hello.category", Duration.ofHours(1), Duration.ofMinutes(5)), new OpenSourceExecutionReason(new OpenSource("josh", 1632366551290L, new ImmutableRequestCommandPacket("GEN OFF"), "GEN OFF"))), FeedbackPacket.class);
}
Also used : OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) Test(org.junit.jupiter.api.Test)

Example 2 with ImmutableRequestCommandPacket

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

the class AlterManagerAction method doSendCommand.

private void doSendCommand(VersionedPacket<StoredAlterPacket> versionedPacket, ScheduledCommandPacket scheduledCommandPacket) {
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Sending command from data: " + scheduledCommandPacket.getData());
    ScheduledCommandData data = scheduledCommandPacket.getData();
    RequestCommandPacket requestCommandPacket = new ImmutableRequestCommandPacket(data.getCommandName());
    // Having a document ID based off of the StoredAlterPacket's _id helps make sure we don't process it twice in case we are unable to delete it.
    // -- if there's an update conflict while uploading, we know we already processed it
    String documentId = "scheduled-request-" + versionedPacket.getPacket().getDbId();
    PacketCollectionCreator creator = commandManager.makeCreator(sourceId, zoneId, InstanceTargetPackets.create(data.getTargetFragmentIds()), requestCommandPacket, zonedDateTime -> documentId);
    executorService.execute(() -> {
        Instant uploadingNow = Instant.now();
        PacketCollection packetCollection = creator.create(uploadingNow);
        boolean shouldDeleteAlter = false;
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Successfully uploaded packet collection that schedules command from data: " + scheduledCommandPacket.getData() + " document ID: " + documentId);
            shouldDeleteAlter = true;
        } catch (UpdateConflictSolarThingDatabaseException e) {
            LOGGER.error("Got update conflict exception while uploading document ID: " + documentId + ". Will inspect existing document and overwrite if it's a malicious actor...", e);
            VersionedPacket<StoredPacketGroup> existingDocument = null;
            try {
                existingDocument = database.getOpenDatabase().getPacketCollection(documentId);
            } catch (SolarThingDatabaseException ex) {
                LOGGER.error("Could not retrieve document with document ID: " + documentId, ex);
            }
            if (existingDocument != null) {
                if (isDocumentMadeByUs(uploadingNow, data, existingDocument.getPacket())) {
                    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "False alarm everyone. The packet in the database was made by us and its timestamp is reasonable. document ID: " + documentId);
                    shouldDeleteAlter = true;
                } else {
                    LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The packet in the database with document ID: " + documentId + " was not made by us. Could be a malicious actor. We will overwrite that packet.");
                    try {
                        database.getOpenDatabase().uploadPacketCollection(packetCollection, existingDocument.getUpdateToken());
                        shouldDeleteAlter = true;
                    } catch (SolarThingDatabaseException ex) {
                        LOGGER.error("Could not overwrite malicious packet. Will likely try again. document ID: " + documentId, ex);
                    }
                }
            }
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Failed to upload our request command packet. documentId: " + documentId, e);
        }
        if (shouldDeleteAlter) {
            try {
                database.getAlterDatabase().delete(versionedPacket);
            } catch (SolarThingDatabaseException e) {
                LOGGER.error("Error while deleting an alter document. document ID: " + versionedPacket.getPacket().getDbId() + " update token: " + versionedPacket.getUpdateToken(), e);
            }
        }
    });
}
Also used : RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) Instant(java.time.Instant) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Example 3 with ImmutableRequestCommandPacket

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

the class CommandChatBotHandler method handleMessage.

@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
    AvailableCommand best = commandHelper.getBestCommand(message, message.getText());
    if (best == null) {
        return false;
    }
    CommandInfo info = best.getCommandInfo();
    messageSender.sendMessage("Sending command: " + info.getDisplayName());
    PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, InstanceTargetPackets.create(Collections.singleton(best.getFragmentId())), new ImmutableRequestCommandPacket(info.getName()), PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
    Instant now = Instant.now();
    PacketCollection packetCollection = creator.create(now);
    executorService.execute(() -> {
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            LOGGER.info("Uploaded command request document");
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Error while uploading document.", e);
            messageSender.sendMessage("Failed to upload command: " + info.getDisplayName());
        }
    });
    return true;
}
Also used : CommandInfo(me.retrodaredevil.solarthing.commands.CommandInfo) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) Instant(java.time.Instant) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Example 4 with ImmutableRequestCommandPacket

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

the class MateCommandFeedbackPacketTest method test.

@Test
void test() throws JsonProcessingException {
    PacketTestUtil.testJson(new ImmutableSuccessMateCommandPacket(null, MateCommand.AUX_OFF, "my_source", null), MateCommandFeedbackPacket.class);
    PacketTestUtil.testJson(new ImmutableSuccessMateCommandPacket(SuccessMateCommandPacket.VERSION_LATEST, MateCommand.AUX_OFF, "my_source", null), MateCommandFeedbackPacket.class);
    {
        OpenSource source = new OpenSource("josh", 1632198320733L, new ImmutableRequestCommandPacket("GEN OFF"), "GEN OFF");
        PacketTestUtil.testJson(new ImmutableSuccessMateCommandPacket(SuccessMateCommandPacket.VERSION_LATEST, MateCommand.AUX_OFF, source.toDataSource().toString(), new OpenSourceExecutionReason(source)), MateCommandFeedbackPacket.class);
    }
}
Also used : OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) Test(org.junit.jupiter.api.Test)

Aggregations

ImmutableRequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket)4 Instant (java.time.Instant)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)2 PacketCollectionCreator (me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)2 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)2 OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)2 Test (org.junit.jupiter.api.Test)2 CommandInfo (me.retrodaredevil.solarthing.commands.CommandInfo)1 RequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)1 ScheduledCommandData (me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)1