Search in sources :

Example 1 with OpenSource

use of me.retrodaredevil.solarthing.type.open.OpenSource in project solarthing by wildmountainfarms.

the class ExecutingCommandFeedbackActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    SourceEnvironment sourceEnvironment = actionEnvironment.getInjectEnvironment().get(SourceEnvironment.class);
    EventReceiverEnvironment eventReceiverEnvironment = actionEnvironment.getInjectEnvironment().get(EventReceiverEnvironment.class);
    OpenSource source = sourceEnvironment.getSource();
    PacketListReceiverHandler packetListReceiverHandler = eventReceiverEnvironment.getEventPacketListReceiverHandler();
    List<Packet> packets = Arrays.asList(new ImmutableExecutionFeedbackPacket(message, category, new OpenSourceExecutionReason(source)));
    return Actions.createRunOnce(() -> {
        LOGGER.debug("Going to upload an execution feedback packet.");
        Instant now = Instant.now();
        packetListReceiverHandler.uploadSimple(now, packets);
    });
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) ImmutableExecutionFeedbackPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableExecutionFeedbackPacket) SourceEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceEnvironment) EventReceiverEnvironment(me.retrodaredevil.solarthing.actions.environment.EventReceiverEnvironment) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) ImmutableExecutionFeedbackPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableExecutionFeedbackPacket) Instant(java.time.Instant) PacketListReceiverHandler(me.retrodaredevil.solarthing.program.PacketListReceiverHandler) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)

Example 2 with OpenSource

use of me.retrodaredevil.solarthing.type.open.OpenSource in project solarthing by wildmountainfarms.

the class MateCommandActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    MateCommandEnvironment mateCommandEnvironment = actionEnvironment.getInjectEnvironment().get(MateCommandEnvironment.class);
    SourceEnvironment sourceEnvironment = actionEnvironment.getInjectEnvironment().get(SourceEnvironment.class);
    Queue<? super SourcedCommand<MateCommand>> queue = mateCommandEnvironment.getQueue();
    OpenSource source = sourceEnvironment.getSource();
    return Actions.createRunOnce(() -> queue.add(new SourcedCommand<>(source, mateCommand)));
}
Also used : SourceEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceEnvironment) MateCommandEnvironment(me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment) MateCommand(me.retrodaredevil.solarthing.solar.outback.command.MateCommand) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) SourcedCommand(me.retrodaredevil.solarthing.commands.command.SourcedCommand)

Example 3 with OpenSource

use of me.retrodaredevil.solarthing.type.open.OpenSource 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 4 with OpenSource

use of me.retrodaredevil.solarthing.type.open.OpenSource in project solarthing by wildmountainfarms.

the class AlterManagerAction method receivePacketWithIntegrity.

private void receivePacketWithIntegrity(String sender, TargetPacketGroup packetGroup) {
    LOGGER.debug("Sender: " + sender + " is authorized and sent a packet targeting no fragments, so we will see if we want to handle anything from it.");
    long now = System.currentTimeMillis();
    List<StoredAlterPacket> storedAlterPacketsToUpload = new ArrayList<>();
    List<DeleteAlterPacket> deleteAlterPackets = new ArrayList<>();
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof ScheduleCommandPacket) {
            ScheduleCommandPacket scheduleCommandPacket = (ScheduleCommandPacket) packet;
            ScheduledCommandData data = scheduleCommandPacket.getData();
            ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), scheduleCommandPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
            scheduleCommandPacket.getUniqueString()));
            ScheduledCommandPacket scheduledCommandPacket = new ScheduledCommandPacket(data, executionReason);
            // This databaseId is basically an arbitrary way to generate a unique ID. It contains some stuff such as the command name to debug more easily
            String databaseId = "alter-scheduled-command-" + data.getCommandName() + "-" + Long.toHexString(data.getScheduledTimeMillis()) + "-" + sender + "-" + Math.random();
            StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, scheduledCommandPacket, this.sourceId);
            storedAlterPacketsToUpload.add(storedAlterPacket);
        } else if (packet instanceof DeleteAlterPacket) {
            DeleteAlterPacket deleteAlterPacket = (DeleteAlterPacket) packet;
            try {
                database.validateUpdateToken(deleteAlterPacket.getUpdateToken());
                deleteAlterPackets.add(deleteAlterPacket);
            } catch (IncompatibleUpdateTokenException ex) {
                LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "For some reason we have an incompatible update token!", ex);
            }
        } else if (packet instanceof RequestFlagPacket) {
            RequestFlagPacket requestFlagPacket = (RequestFlagPacket) packet;
            // Originally I was going to not upload a FlagPacket if an existing FlagPacket's
            // time range fully encapsulated the newly requested one, but that adds some complexity
            // that is not needed at the moment
            FlagData flagData = requestFlagPacket.getFlagData();
            ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), requestFlagPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
            requestFlagPacket.getUniqueString()));
            FlagPacket flagPacket = new FlagPacket(flagData, executionReason);
            String databaseId = "alter-flag-" + flagData.getFlagName() + "-" + sender + "-" + Math.random();
            StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, flagPacket, sourceId);
            storedAlterPacketsToUpload.add(storedAlterPacket);
        }
    }
    if (storedAlterPacketsToUpload.isEmpty() && deleteAlterPackets.isEmpty()) {
        // Nothing for us to do, so no need to schedule a runnable
        return;
    }
    executorService.execute(() -> {
        int uploadCount = 0;
        try {
            for (StoredAlterPacket storedAlterPacket : storedAlterPacketsToUpload) {
                this.database.getAlterDatabase().upload(storedAlterPacket);
                uploadCount++;
            }
        } catch (SolarThingDatabaseException e) {
            // TODO in future we should try multiple times to upload
            LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not upload a stored alter packet! uploaded: " + uploadCount + " / " + storedAlterPacketsToUpload.size(), e);
        }
        int deleteCount = 0;
        try {
            for (DeleteAlterPacket deleteAlterPacket : deleteAlterPackets) {
                this.database.getAlterDatabase().delete(deleteAlterPacket.getDocumentIdToDelete(), deleteAlterPacket.getUpdateToken());
                deleteCount++;
            }
        } catch (SolarThingDatabaseException e) {
            LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not delete alter packets! deleted: " + deleteCount + " / " + deleteAlterPackets.size(), e);
        }
    });
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) LargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.LargeIntegrityPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ArrayList(java.util.ArrayList) ExecutionReason(me.retrodaredevil.solarthing.reason.ExecutionReason) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) IncompatibleUpdateTokenException(me.retrodaredevil.solarthing.database.exception.IncompatibleUpdateTokenException) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) ScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)

Example 5 with OpenSource

use of me.retrodaredevil.solarthing.type.open.OpenSource 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

OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)8 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)6 Packet (me.retrodaredevil.solarthing.packets.Packet)5 ImmutableRequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket)3 ExecutionReason (me.retrodaredevil.solarthing.reason.ExecutionReason)3 Instant (java.time.Instant)2 SourceEnvironment (me.retrodaredevil.solarthing.actions.environment.SourceEnvironment)2 CommandOpenPacket (me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)2 RequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 EventReceiverEnvironment (me.retrodaredevil.solarthing.actions.environment.EventReceiverEnvironment)1 MateCommandEnvironment (me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment)1 SourcedCommand (me.retrodaredevil.solarthing.commands.command.SourcedCommand)1 DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)1 RequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket)1 RequestHeartbeatPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestHeartbeatPacket)1 ScheduleCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 IncompatibleUpdateTokenException (me.retrodaredevil.solarthing.database.exception.IncompatibleUpdateTokenException)1