Search in sources :

Example 6 with ActionNode

use of me.retrodaredevil.action.node.ActionNode 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)

Example 7 with ActionNode

use of me.retrodaredevil.action.node.ActionNode in project solarthing by wildmountainfarms.

the class ActionUtil method getActionNodeMap.

public static Map<String, ActionNode> getActionNodeMap(ObjectMapper objectMapper, CommandOption options) throws IOException {
    Map<String, ActionNode> actionNodeMap = new HashMap<>();
    for (Map.Entry<String, File> entry : options.getCommandFileMap().entrySet()) {
        String name = entry.getKey();
        File file = entry.getValue();
        final ActionNode actionNode = objectMapper.readValue(file, ActionNode.class);
        actionNodeMap.put(name, actionNode);
    }
    return actionNodeMap;
}
Also used : HashMap(java.util.HashMap) WrappedSlackChatBotActionNode(me.retrodaredevil.solarthing.actions.chatbot.WrappedSlackChatBotActionNode) RoverBoostSetActionNode(me.retrodaredevil.solarthing.actions.rover.RoverBoostSetActionNode) MessageSenderActionNode(me.retrodaredevil.solarthing.actions.message.MessageSenderActionNode) FlagActionNode(me.retrodaredevil.solarthing.actions.command.FlagActionNode) MateCommandWaitActionNode(me.retrodaredevil.solarthing.actions.mate.MateCommandWaitActionNode) SolcastActionNode(me.retrodaredevil.solarthing.actions.solcast.SolcastActionNode) MateCommandActionNode(me.retrodaredevil.solarthing.actions.mate.MateCommandActionNode) ActionNode(me.retrodaredevil.action.node.ActionNode) WrappedAlterManagerActionNode(me.retrodaredevil.solarthing.actions.command.WrappedAlterManagerActionNode) ExecutingCommandFeedbackActionNode(me.retrodaredevil.solarthing.actions.command.ExecutingCommandFeedbackActionNode) RoverLoadActionNode(me.retrodaredevil.solarthing.actions.rover.RoverLoadActionNode) HomeAssistantActionNode(me.retrodaredevil.solarthing.actions.homeassistant.HomeAssistantActionNode) SendMessageActionNode(me.retrodaredevil.solarthing.actions.message.SendMessageActionNode) RoverBoostVoltageActionNode(me.retrodaredevil.solarthing.actions.rover.RoverBoostVoltageActionNode) TracerLoadActionNode(me.retrodaredevil.solarthing.actions.tracer.TracerLoadActionNode) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

ActionNode (me.retrodaredevil.action.node.ActionNode)7 Action (me.retrodaredevil.action.Action)4 ActionEnvironment (me.retrodaredevil.action.node.environment.ActionEnvironment)4 InjectEnvironment (me.retrodaredevil.action.node.environment.InjectEnvironment)4 VariableEnvironment (me.retrodaredevil.action.node.environment.VariableEnvironment)4 NanoTimeProviderEnvironment (me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 FragmentedPacketGroupProvider (me.retrodaredevil.solarthing.FragmentedPacketGroupProvider)2 LatestPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment)2 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)2 Test (org.junit.jupiter.api.Test)2 Clock (java.time.Clock)1 Duration (java.time.Duration)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ActionMultiplexer (me.retrodaredevil.action.ActionMultiplexer)1 Actions (me.retrodaredevil.action.Actions)1