Search in sources :

Example 1 with RequestHeartbeatReceiver

use of me.retrodaredevil.solarthing.program.receiver.RequestHeartbeatReceiver 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)

Aggregations

ArrayList (java.util.ArrayList)1 ActionNode (me.retrodaredevil.action.node.ActionNode)1 NanoTimeProviderEnvironment (me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment)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 LatestPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment)1 SourceEnvironment (me.retrodaredevil.solarthing.actions.environment.SourceEnvironment)1 TimeZoneEnvironment (me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment)1 FileWritePacketHandler (me.retrodaredevil.solarthing.packets.handling.implementations.FileWritePacketHandler)1 JacksonStringPacketHandler (me.retrodaredevil.solarthing.packets.handling.implementations.JacksonStringPacketHandler)1 PostPacketHandler (me.retrodaredevil.solarthing.packets.handling.implementations.PostPacketHandler)1 ActionNodeDataReceiver (me.retrodaredevil.solarthing.program.receiver.ActionNodeDataReceiver)1 RequestHeartbeatReceiver (me.retrodaredevil.solarthing.program.receiver.RequestHeartbeatReceiver)1