Search in sources :

Example 1 with Action

use of me.retrodaredevil.action.Action in project solarthing by wildmountainfarms.

the class CommandController method runCommand.

/**
 * Runs the given action. A response is not returned until the action is done running
 *
 * @param apiKey The api key
 * @param commandName The name of the command, which corresponds to an action
 * @return
 */
@GetMapping(path = "/run", produces = "application/json")
public CommandRequestResponse runCommand(String apiKey, String commandName) {
    // Also consider using this way instead of exceptions: https://stackoverflow.com/a/60079942
    if (apiKey == null) {
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "apiKey is required!");
    }
    if (commandName == null) {
        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "commandName is required!");
    }
    if (!commandHandler.isAuthorized(apiKey)) {
        throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "You are not authorized with the given api key!");
    }
    ActionNode actionNode = commandHandler.getActionNode(commandName);
    if (actionNode == null) {
        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No corresponding command with commandName: " + commandName);
    }
    InjectEnvironment injectEnvironment = commandHandler.createInjectEnvironment(commandName);
    // We don't know or care what thread this is running on, so we won't have a shared global variable environment.
    // We could make a shared global environment a feature of this down the line, but for now let's keep this simple
    ActionEnvironment actionEnvironment = new ActionEnvironment(new VariableEnvironment(), new VariableEnvironment(), injectEnvironment);
    Action action = actionNode.createAction(actionEnvironment);
    while (!action.isDone()) {
        action.update();
        try {
            // This is here to make sure our CPU doesn't go to 100% for no reason
            Thread.sleep(5);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            LOGGER.error("Interrupted while action was being performed.", e);
            action.end();
            return new CommandRequestResponse(false);
        }
    }
    action.end();
    return new CommandRequestResponse(true);
}
Also used : ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) Action(me.retrodaredevil.action.Action) ActionNode(me.retrodaredevil.action.node.ActionNode) VariableEnvironment(me.retrodaredevil.action.node.environment.VariableEnvironment) ResponseStatusException(org.springframework.web.server.ResponseStatusException) InjectEnvironment(me.retrodaredevil.action.node.environment.InjectEnvironment) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with Action

use of me.retrodaredevil.action.Action in project solarthing by wildmountainfarms.

the class ActionNodeTest method testDeclaration.

@Test
void testDeclaration() throws JsonProcessingException {
    String json = "{\n" + "  \"type\": \"race\",\n" + "  \"racers\": [\n" + "    [{ \"type\": \"waitms\", \"wait\": 500}, { \"type\": \"log\", \"message\": \"500ms finished first!\"}],\n" + "    [{ \"type\": \"waitms\", \"wait\": 200}, { \"type\": \"log\", \"message\": \"200ms finished first!\"}],\n" + "    [{ \"type\": \"waitms\", \"wait\": 1000}, { \"type\": \"log\", \"message\": \"1000ms finished first!\"}]\n" + "  ]\n" + "}";
    ActionNode actionNode = MAPPER.readValue(json, ActionNode.class);
    Action action = actionNode.createAction(createEnvironment());
    do {
        action.update();
    } while (!action.isDone());
    action.end();
}
Also used : Action(me.retrodaredevil.action.Action) ActionNode(me.retrodaredevil.action.node.ActionNode) CallActionNode(me.retrodaredevil.action.node.CallActionNode) Test(org.junit.jupiter.api.Test)

Example 3 with Action

use of me.retrodaredevil.action.Action in project solarthing by wildmountainfarms.

the class ActionNodeDataReceiver method receiveData.

private void receiveData(OpenSource source, String commandName) {
    ActionNode requested = actionNodeMap.get(commandName);
    if (requested != null) {
        InjectEnvironment.Builder injectEnvironmentBuilder = new InjectEnvironment.Builder();
        environmentUpdater.updateInjectEnvironment(source, injectEnvironmentBuilder);
        Action action = requested.createAction(new ActionEnvironment(variableEnvironment, new VariableEnvironment(), injectEnvironmentBuilder.build()));
        // Now that action has been created, add it to the action multiplexer. (Adding is thread safe).
        // The action has not been used by this thread, so when a different thread starts executing it, there will be no problems.
        actionMultiplexer.add(action);
        LOGGER.info(SolarThingConstants.SUMMARY_MARKER, source.getSender() + " has requested command sequence: " + commandName);
    } else {
        LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Sender: " + source.getSender() + " has requested unknown command: " + commandName);
    }
}
Also used : ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) Action(me.retrodaredevil.action.Action) ActionNode(me.retrodaredevil.action.node.ActionNode) VariableEnvironment(me.retrodaredevil.action.node.environment.VariableEnvironment) InjectEnvironment(me.retrodaredevil.action.node.environment.InjectEnvironment)

Example 4 with Action

use of me.retrodaredevil.action.Action in project solarthing by wildmountainfarms.

the class SlackChatBotActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    AlterPacketsEnvironment alterPacketsEnvironment = actionEnvironment.getInjectEnvironment().get(AlterPacketsEnvironment.class);
    SolarThingDatabaseEnvironment solarThingDatabaseEnvironment = actionEnvironment.getInjectEnvironment().get(SolarThingDatabaseEnvironment.class);
    SourceIdEnvironment sourceIdEnvironment = actionEnvironment.getInjectEnvironment().get(SourceIdEnvironment.class);
    TimeZoneEnvironment timeZoneEnvironment = actionEnvironment.getInjectEnvironment().get(TimeZoneEnvironment.class);
    EventDatabaseCacheEnvironment eventDatabaseCacheEnvironment = actionEnvironment.getInjectEnvironment().get(EventDatabaseCacheEnvironment.class);
    // Note that all objects listed here must be thread safe, as data will be accessed from them on a separate thread
    FragmentedPacketGroupProvider packetGroupProvider = latestPacketGroupEnvironment.getFragmentedPacketGroupProvider();
    AlterPacketsProvider alterPacketsProvider = alterPacketsEnvironment.getAlterPacketsProvider();
    SolarThingDatabase database = solarThingDatabaseEnvironment.getSolarThingDatabase();
    String sourceId = sourceIdEnvironment.getSourceId();
    ZoneId zoneId = timeZoneEnvironment.getZoneId();
    ResourceManager<? extends DatabaseCache> eventDatabaseCacheManager = eventDatabaseCacheEnvironment.getEventDatabaseCacheManager();
    Slack slack = Slack.getInstance(new SlackConfig(), new SlackHttpClient(new OkHttpClient.Builder().callTimeout(Duration.ofSeconds(10)).connectTimeout(Duration.ofSeconds(4)).build()));
    ChatBotCommandHelper commandHelper = new ChatBotCommandHelper(permissionMap, packetGroupProvider, new CommandManager(keyDirectory, sender));
    return new SlackChatBotAction(appToken, new SlackMessageSender(authToken, channelId, slack), slack, new HelpChatBotHandler(new ChatBotHandlerMultiplexer(Arrays.asList(// note: this isn't applied to "help" commands
    new StaleMessageHandler(), new ScheduleCommandChatBotHandler(commandHelper, database, sourceId, zoneId), new CancelCommandChatBotHandler(commandHelper, database, sourceId, zoneId, alterPacketsProvider), new FlagCommandChatBotHandler(commandHelper, database, sourceId, zoneId, alterPacketsProvider), new CommandChatBotHandler(commandHelper, database, sourceId, zoneId), new StatusChatBotHandler(packetGroupProvider, alterPacketsProvider), new HeartbeatCommandChatBotHandler(eventDatabaseCacheManager), (message, messageSender) -> {
        messageSender.sendMessage("Unknown command!");
        return true;
    }))));
}
Also used : EventDatabaseCacheEnvironment(me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment) SlackHttpClient(com.slack.api.util.http.SlackHttpClient) CommandManager(me.retrodaredevil.solarthing.commands.util.CommandManager) Arrays(java.util.Arrays) Slack(com.slack.api.Slack) me.retrodaredevil.solarthing.chatbot(me.retrodaredevil.solarthing.chatbot) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider) SourceIdEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceIdEnvironment) TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) SlackMessageSender(me.retrodaredevil.solarthing.message.implementations.SlackMessageSender) ResourceManager(me.retrodaredevil.solarthing.util.sync.ResourceManager) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) Action(me.retrodaredevil.action.Action) Duration(java.time.Duration) Map(java.util.Map) SolarThingDatabaseEnvironment(me.retrodaredevil.solarthing.actions.environment.SolarThingDatabaseEnvironment) EventDatabaseCacheEnvironment(me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) ActionNode(me.retrodaredevil.action.node.ActionNode) DatabaseCache(me.retrodaredevil.solarthing.database.cache.DatabaseCache) File(java.io.File) ZoneId(java.time.ZoneId) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) SlackConfig(com.slack.api.SlackConfig) AlterPacketsEnvironment(me.retrodaredevil.solarthing.actions.environment.AlterPacketsEnvironment) SourceIdEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceIdEnvironment) TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) OkHttpClient(okhttp3.OkHttpClient) SlackHttpClient(com.slack.api.util.http.SlackHttpClient) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) SlackMessageSender(me.retrodaredevil.solarthing.message.implementations.SlackMessageSender) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) SlackConfig(com.slack.api.SlackConfig) CommandManager(me.retrodaredevil.solarthing.commands.util.CommandManager) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) ZoneId(java.time.ZoneId) AlterPacketsEnvironment(me.retrodaredevil.solarthing.actions.environment.AlterPacketsEnvironment) SolarThingDatabaseEnvironment(me.retrodaredevil.solarthing.actions.environment.SolarThingDatabaseEnvironment) Slack(com.slack.api.Slack) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider)

Example 5 with Action

use of me.retrodaredevil.action.Action in project solarthing by wildmountainfarms.

the class DeserializeTest method testRunAlertGeneratorOffWhileAuxOn.

@Test
void testRunAlertGeneratorOffWhileAuxOn() throws IOException, ParsePacketAsciiDecimalDigitException, CheckSumException {
    File file = new File(ACTION_CONFIG_DIRECTORY, "alert_generator_off_while_aux_on.json");
    ActionNode actionNode = MAPPER.readValue(file, ActionNode.class);
    // We need to simulate an automation program environment to run this action
    Duration[] timeReference = new Duration[] { Duration.ZERO };
    FragmentedPacketGroup[] packetGroupReference = new FragmentedPacketGroup[] { null };
    FragmentedPacketGroupProvider fragmentedPacketGroupProvider = () -> requireNonNull(packetGroupReference[0]);
    InjectEnvironment injectEnvironment = new InjectEnvironment.Builder().add(new NanoTimeProviderEnvironment(() -> timeReference[0].toNanos())).add(new LatestPacketGroupEnvironment(fragmentedPacketGroupProvider)).add(new LatestFragmentedPacketGroupEnvironment(fragmentedPacketGroupProvider)).build();
    FXStatusPacket auxOnNoAC = FXStatusPackets.createFromChars("\n1,00,00,02,123,123,00,10,000,00,252,136,000,999\r".toCharArray(), IgnoreCheckSum.IGNORE);
    FXStatusPacket auxOffNoAC = FXStatusPackets.createFromChars("\n1,00,00,02,123,123,00,10,000,00,252,008,000,999\r".toCharArray(), IgnoreCheckSum.IGNORE);
    FXStatusPacket auxOnACUse = FXStatusPackets.createFromChars("\n1,00,00,02,123,123,00,10,000,02,252,136,000,999\r".toCharArray(), IgnoreCheckSum.IGNORE);
    FXStatusPacket auxOffACUse = FXStatusPackets.createFromChars("\n1,00,00,02,123,123,00,10,000,02,252,008,000,999\r".toCharArray(), IgnoreCheckSum.IGNORE);
    for (FXStatusPacket packet : new FXStatusPacket[] { auxOffNoAC, auxOnACUse, auxOffACUse }) {
        // for these three cases, the action should end immediately
        packetGroupReference[0] = PacketGroups.createInstancePacketGroup(Collections.singleton(packet), 0L, "my_source_id", 999);
        Action action = actionNode.createAction(new ActionEnvironment(new VariableEnvironment(), new VariableEnvironment(), injectEnvironment));
        action.update();
        assertTrue(action.isDone());
    }
    {
        // Test that no alert is sent unless the aux is on, and it's in No AC for 30 seconds
        packetGroupReference[0] = PacketGroups.createInstancePacketGroup(Collections.singleton(auxOnNoAC), 0L, "my_source_id", 999);
        Action action = actionNode.createAction(new ActionEnvironment(new VariableEnvironment(), new VariableEnvironment(), injectEnvironment));
        action.update();
        assertFalse(action.isDone());
        timeReference[0] = timeReference[0].plus(Duration.ofSeconds(29));
        action.update();
        assertFalse(action.isDone());
        packetGroupReference[0] = PacketGroups.createInstancePacketGroup(Collections.singleton(auxOnACUse), 0L, "my_source_id", 999);
        action.update();
        // No alert has been sent, since it started to AC Use before the 30 second period completed.
        assertTrue(action.isDone());
    }
    {
        // Test that the alert gets sent and the action doesn't end until the 300-second timeout completes
        packetGroupReference[0] = PacketGroups.createInstancePacketGroup(Collections.singleton(auxOnNoAC), 0L, "my_source_id", 999);
        Action action = actionNode.createAction(new ActionEnvironment(new VariableEnvironment(), new VariableEnvironment(), injectEnvironment));
        action.update();
        assertFalse(action.isDone());
        timeReference[0] = timeReference[0].plus(Duration.ofSeconds(30));
        action.update();
        assertFalse(action.isDone());
        packetGroupReference[0] = PacketGroups.createInstancePacketGroup(Collections.singleton(auxOnACUse), 0L, "my_source_id", 999);
        action.update();
        // Alert has been sent, so the action isn't going to end
        assertFalse(action.isDone());
        timeReference[0] = timeReference[0].plus(Duration.ofSeconds(299));
        action.update();
        assertFalse(action.isDone());
        timeReference[0] = timeReference[0].plus(Duration.ofSeconds(1));
        action.update();
        // the 300-second timeout has completed, so the action will end
        assertTrue(action.isDone());
    }
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) Action(me.retrodaredevil.action.Action) NanoTimeProviderEnvironment(me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) SerialConfigBuilder(me.retrodaredevil.io.serial.SerialConfigBuilder) ActionNode(me.retrodaredevil.action.node.ActionNode) Duration(java.time.Duration) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) InjectEnvironment(me.retrodaredevil.action.node.environment.InjectEnvironment) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) VariableEnvironment(me.retrodaredevil.action.node.environment.VariableEnvironment) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Action (me.retrodaredevil.action.Action)5 ActionNode (me.retrodaredevil.action.node.ActionNode)5 ActionEnvironment (me.retrodaredevil.action.node.environment.ActionEnvironment)4 InjectEnvironment (me.retrodaredevil.action.node.environment.InjectEnvironment)3 VariableEnvironment (me.retrodaredevil.action.node.environment.VariableEnvironment)3 File (java.io.File)2 Duration (java.time.Duration)2 FragmentedPacketGroupProvider (me.retrodaredevil.solarthing.FragmentedPacketGroupProvider)2 LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)2 Test (org.junit.jupiter.api.Test)2 Slack (com.slack.api.Slack)1 SlackConfig (com.slack.api.SlackConfig)1 SlackHttpClient (com.slack.api.util.http.SlackHttpClient)1 ZoneId (java.time.ZoneId)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 CallActionNode (me.retrodaredevil.action.node.CallActionNode)1 NanoTimeProviderEnvironment (me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment)1 SerialConfigBuilder (me.retrodaredevil.io.serial.SerialConfigBuilder)1