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);
}
Aggregations