Search in sources :

Example 1 with LatestPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class PacketHandlerInit method initHandlers.

public static <T extends PacketHandlingOption & CommandOption> Result initHandlers(T options, Supplier<? extends EnvironmentUpdater> environmentUpdaterSupplier, Collection<? extends PacketHandler> additionalPacketHandlers) throws IOException {
    List<DatabaseConfig> databaseConfigs = ConfigUtil.getDatabaseConfigs(options);
    PacketHandlerBundle packetHandlerBundle = PacketHandlerInit.getPacketHandlerBundle(databaseConfigs, SolarThingConstants.STATUS_DATABASE, SolarThingConstants.EVENT_DATABASE, options.getSourceId(), options.getFragmentId());
    List<PacketHandler> statusPacketHandlers = new ArrayList<>();
    final Runnable updateCommandActions;
    if (options.hasCommands()) {
        LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Command are enabled!");
        // this is used to determine the state of the system when a command is requested
        LatestPacketHandler latestPacketHandler = new LatestPacketHandler();
        statusPacketHandlers.add(latestPacketHandler);
        Map<String, ActionNode> actionNodeMap = ActionUtil.getActionNodeMap(CONFIG_MAPPER, options);
        ActionNodeDataReceiver commandReceiver = new ActionNodeDataReceiver(actionNodeMap, (source, injectEnvironmentBuilder) -> {
            injectEnvironmentBuilder.add(new NanoTimeProviderEnvironment(NanoTimeProvider.SYSTEM_NANO_TIME)).add(new TimeZoneEnvironment(options.getZoneId())).add(new LatestPacketGroupEnvironment(latestPacketHandler::getLatestPacketCollection)).add(new SourceEnvironment(source)).add(new EventReceiverEnvironment(PacketListReceiverHandlerBundle.createEventPacketListReceiverHandler(SolarMain.getSourceAndFragmentUpdater(options), options.getZoneId(), packetHandlerBundle)));
            EnvironmentUpdater environmentUpdater = environmentUpdaterSupplier.get();
            if (environmentUpdater == null) {
                throw new NullPointerException("The EnvironmentUpdater supplier gave a null value! (Fatal)");
            }
            environmentUpdater.updateInjectEnvironment(source, injectEnvironmentBuilder);
        });
        PacketGroupReceiver mainPacketGroupReceiver = new PacketGroupReceiverMultiplexer(Arrays.asList(commandReceiver, new RequestHeartbeatReceiver(PacketListReceiverHandlerBundle.createEventPacketListReceiverHandler(SolarMain.getSourceAndFragmentUpdater(options), options.getZoneId(), packetHandlerBundle))));
        statusPacketHandlers.add((packetCollection) -> commandReceiver.getActionUpdater().update());
        List<PacketHandler> commandPacketHandlers = CommandUtil.getCommandRequesterHandlerList(databaseConfigs, mainPacketGroupReceiver, options);
        statusPacketHandlers.add(new PacketHandlerMultiplexer(commandPacketHandlers));
        updateCommandActions = () -> commandReceiver.getActionUpdater().update();
    } else {
        LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Commands are disabled");
        updateCommandActions = () -> {
        };
    }
    statusPacketHandlers.addAll(additionalPacketHandlers);
    statusPacketHandlers.addAll(packetHandlerBundle.getStatusPacketHandlers());
    PacketListReceiverHandlerBundle bundle = PacketListReceiverHandlerBundle.createFrom(options, packetHandlerBundle, statusPacketHandlers);
    return new Result(bundle, updateCommandActions);
}
Also used : TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) EventReceiverEnvironment(me.retrodaredevil.solarthing.actions.environment.EventReceiverEnvironment) NanoTimeProviderEnvironment(me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) PacketGroupReceiver(me.retrodaredevil.solarthing.PacketGroupReceiver) ArrayList(java.util.ArrayList) ActionNode(me.retrodaredevil.action.node.ActionNode) RequestHeartbeatReceiver(me.retrodaredevil.solarthing.program.receiver.RequestHeartbeatReceiver) PacketGroupReceiverMultiplexer(me.retrodaredevil.solarthing.PacketGroupReceiverMultiplexer) EnvironmentUpdater(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater) SourceEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceEnvironment) FileWritePacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.FileWritePacketHandler) PostPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.PostPacketHandler) JacksonStringPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.JacksonStringPacketHandler) ActionNodeDataReceiver(me.retrodaredevil.solarthing.program.receiver.ActionNodeDataReceiver)

Example 2 with LatestPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class ACModeActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestPacketGroupEnvironment.class);
    PacketGroupProvider packetGroupProvider = latestPacketGroupEnvironment.getPacketGroupProvider();
    return new SimpleAction(false) {

        @Override
        protected void onUpdate() {
            super.onUpdate();
            PacketGroup packetGroup = packetGroupProvider.getPacketGroup();
            if (packetGroup == null) {
                LOGGER.warn("packetGroup is null!");
                return;
            }
            FXStatusPacket fxStatusPacket = OutbackUtil.getMasterFX(packetGroup);
            if (fxStatusPacket == null) {
                LOGGER.warn("No master FX Status Packet!");
            } else {
                setDone((fxStatusPacket.getACMode() == acMode) == !not);
            }
        }
    };
}
Also used : PacketGroupProvider(me.retrodaredevil.solarthing.PacketGroupProvider) PacketGroup(me.retrodaredevil.solarthing.packets.collection.PacketGroup) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Example 3 with LatestPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class FXOperationalModeActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestPacketGroupEnvironment.class);
    PacketGroupProvider packetGroupProvider = latestPacketGroupEnvironment.getPacketGroupProvider();
    return new SimpleAction(false) {

        @Override
        protected void onUpdate() {
            super.onUpdate();
            PacketGroup packetGroup = packetGroupProvider.getPacketGroup();
            if (packetGroup == null) {
                // packetGroup will only be null in the automation program if a query fails or something. This should never happen in the mate program
                LOGGER.warn("packetGroup is null!");
                return;
            }
            FXStatusPacket fxStatusPacket = OutbackUtil.getMasterFX(packetGroup);
            if (fxStatusPacket == null) {
                LOGGER.warn("No master FX Status Packet!");
            } else {
                setDone((fxStatusPacket.getOperationalMode() == operationalMode) == !not);
            }
        }
    };
}
Also used : PacketGroupProvider(me.retrodaredevil.solarthing.PacketGroupProvider) PacketGroup(me.retrodaredevil.solarthing.packets.collection.PacketGroup) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Example 4 with LatestPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment 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 5 with LatestPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class AuxStateActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestPacketGroupEnvironment.class);
    PacketGroupProvider packetGroupProvider = latestPacketGroupEnvironment.getPacketGroupProvider();
    return new SimpleAction(false) {

        /**
         * A simple variable to prevent spamming of the log file
         */
        private boolean hadFX = true;

        @Override
        protected void onUpdate() {
            super.onUpdate();
            PacketGroup packetGroup = packetGroupProvider.getPacketGroup();
            if (packetGroup == null) {
                LOGGER.warn("packetGroup is null!");
                return;
            }
            FXStatusPacket fxStatusPacket = OutbackUtil.getMasterFX(packetGroup);
            if (fxStatusPacket == null) {
                if (hadFX) {
                    LOGGER.warn("No master FX Status Packet!");
                }
                hadFX = false;
            } else {
                setDone(fxStatusPacket.isAuxOn() == on);
                if (!hadFX) {
                    LOGGER.info("We now have a master FX Status Packet");
                }
                hadFX = true;
            }
        }
    };
}
Also used : PacketGroupProvider(me.retrodaredevil.solarthing.PacketGroupProvider) PacketGroup(me.retrodaredevil.solarthing.packets.collection.PacketGroup) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Aggregations

LatestPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment)5 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)4 SimpleAction (me.retrodaredevil.action.SimpleAction)3 PacketGroupProvider (me.retrodaredevil.solarthing.PacketGroupProvider)3 PacketGroup (me.retrodaredevil.solarthing.packets.collection.PacketGroup)3 ActionNode (me.retrodaredevil.action.node.ActionNode)2 NanoTimeProviderEnvironment (me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment)2 File (java.io.File)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Action (me.retrodaredevil.action.Action)1 ActionEnvironment (me.retrodaredevil.action.node.environment.ActionEnvironment)1 InjectEnvironment (me.retrodaredevil.action.node.environment.InjectEnvironment)1 VariableEnvironment (me.retrodaredevil.action.node.environment.VariableEnvironment)1 SerialConfigBuilder (me.retrodaredevil.io.serial.SerialConfigBuilder)1 FragmentedPacketGroupProvider (me.retrodaredevil.solarthing.FragmentedPacketGroupProvider)1 PacketGroupReceiver (me.retrodaredevil.solarthing.PacketGroupReceiver)1 PacketGroupReceiverMultiplexer (me.retrodaredevil.solarthing.PacketGroupReceiverMultiplexer)1 EnvironmentUpdater (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater)1 EventReceiverEnvironment (me.retrodaredevil.solarthing.actions.environment.EventReceiverEnvironment)1