use of me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater 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);
}
use of me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater in project solarthing by wildmountainfarms.
the class RequestMain method startRequestProgram.
public static <T extends PacketHandlingOption & CommandOption> int startRequestProgram(T options, AnalyticsManager analyticsManager, List<DataRequester> dataRequesterList, long period, long minimumWait) throws Exception {
EnvironmentUpdater[] environmentUpdaterReference = new EnvironmentUpdater[1];
PacketHandlerInit.Result handlersResult = PacketHandlerInit.initHandlers(options, () -> environmentUpdaterReference[0], Collections.singleton(new RoverAnalyticsHandler(analyticsManager)));
PacketListReceiverHandlerBundle bundle = handlersResult.getBundle();
List<PacketListReceiver> packetListReceiverList = new ArrayList<>();
List<EnvironmentUpdater> environmentUpdaters = new ArrayList<>();
for (DataRequester dataRequester : dataRequesterList) {
DataRequesterResult result = dataRequester.create(new RequestObject(bundle.getEventHandler().getPacketListReceiverAccepter()));
packetListReceiverList.add(result.getStatusPacketListReceiver());
environmentUpdaters.add(result.getEnvironmentUpdater());
}
// will add events for each rover packet if there are any to add
packetListReceiverList.add(new RoverEventUpdaterListReceiver(bundle.getEventHandler().getPacketListReceiverAccepter()));
// will add events for each tracer packet if there are any to add
packetListReceiverList.add(new TracerEventUpdaterListReceiver(bundle.getEventHandler().getPacketListReceiverAccepter()));
if (options.hasCommands()) {
packetListReceiverList.add(new AvailableCommandsListUpdater(options.getCommandInfoList(), false));
}
environmentUpdaterReference[0] = new EnvironmentUpdaterMultiplexer(environmentUpdaters);
packetListReceiverList.add(new DataIdentifiablePacketListChecker());
packetListReceiverList.add(new DaySummaryLogListReceiver());
packetListReceiverList.addAll(bundle.createDefaultPacketListReceivers());
return doRequest(new PacketListReceiverMultiplexer(packetListReceiverList), Duration.ofMillis(period), Duration.ofMillis(minimumWait));
}
use of me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater in project solarthing by wildmountainfarms.
the class OutbackMateMain method connectMate.
@SuppressWarnings("SameReturnValue")
public static int connectMate(MateProgramOptions options, File dataDirectory) throws Exception {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning mate program");
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
analyticsManager.sendStartUp(ProgramType.MATE);
LOGGER.debug("IO Bundle File: " + options.getIOBundleFile());
IOConfig ioConfig = ConfigUtil.parseIOConfig(options.getIOBundleFile(), OutbackConstants.MATE_CONFIG);
try (ReloadableIOBundle ioBundle = new ReloadableIOBundle(ioConfig::createIOBundle)) {
EnvironmentUpdater[] environmentUpdaterReference = new EnvironmentUpdater[1];
PacketHandlerInit.Result handlersResult = PacketHandlerInit.initHandlers(options, () -> environmentUpdaterReference[0], Collections.singleton(new MateAnalyticsHandler(analyticsManager)));
PacketListReceiverHandlerBundle bundle = handlersResult.getBundle();
List<PacketListReceiver> packetListReceiverList = new ArrayList<>(Arrays.asList(OutbackDuplicatePacketRemover.INSTANCE, new FXEventUpdaterListReceiver(bundle.getEventHandler().getPacketListReceiverAccepter(), options.getFXWarningIgnoreMap()), new MXEventUpdaterListReceiver(bundle.getEventHandler().getPacketListReceiverAccepter()), new FXStatusListUpdater(new DailyIdentifier(options.getZoneId()))));
List<EnvironmentUpdater> environmentUpdaters = new ArrayList<>();
for (DataRequester dataRequester : options.getDataRequesterList()) {
DataRequesterResult result = dataRequester.create(new RequestObject(bundle.getEventHandler().getPacketListReceiverAccepter()));
packetListReceiverList.add(result.getStatusPacketListReceiver());
environmentUpdaters.add(result.getEnvironmentUpdater());
}
final List<CommandProvider<MateCommand>> commandProviders;
if (options.hasCommands()) {
packetListReceiverList.add(new AvailableCommandsListUpdater(options.getCommandInfoList(), false));
Queue<SourcedCommand<MateCommand>> queue = new LinkedList<>();
// if there are no commands, this should remain empty
commandProviders = new ArrayList<>();
final CommandProvider<MateCommand> commandProvider = () -> {
handlersResult.getUpdateCommandActions().run();
return queue.poll();
};
commandProviders.add(commandProvider);
environmentUpdaters.add((source, injectEnvironmentBuilder) -> {
injectEnvironmentBuilder.add(new MateCommandEnvironment(queue));
});
} else {
commandProviders = Collections.emptyList();
}
environmentUpdaterReference[0] = new EnvironmentUpdaterMultiplexer(environmentUpdaters);
packetListReceiverList.add(new DataIdentifiablePacketListChecker());
packetListReceiverList.add(new DaySummaryLogListReceiver());
packetListReceiverList.addAll(bundle.createDefaultPacketListReceivers());
return SolarMain.initReader(requireNonNull(ioBundle.getInputStream()), ioBundle::reload, new MatePacketCreator49(MateProgramOptions.getIgnoreCheckSum(options)), new TimedPacketReceiver(Duration.ofMillis(250), new PacketListReceiverMultiplexer(packetListReceiverList), new MateCommandSender(// if commands aren't allowed, commandProviders will be empty, so this will do nothing
new CommandProviderMultiplexer<>(commandProviders), ioBundle.getOutputStream(), ALLOWED_COMMANDS, new OnMateCommandSent(new PacketListReceiverMultiplexer(bundle.getEventHandler().getPacketListReceiverAccepter(), bundle.getEventHandler().getPacketListReceiverPacker(), bundle.getEventHandler().getPacketListReceiverHandler())))));
}
}
use of me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater in project solarthing by wildmountainfarms.
the class ModbusDataRequester method create.
@Override
public DataRequesterResult create(RequestObject requestObject) {
ModbusRequester first = addressToModbusRequesterMap.values().stream().findFirst().orElseThrow(NoSuchElementException::new);
IOConfig ioConfig = ConfigUtil.parseIOConfig(ioBundleFile, first.getDefaultSerialConfig());
ReloadableIOBundle ioBundle = new ReloadableIOBundle(ioConfig::createIOBundle);
ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder(rtuInitialTimeoutMillis, rtuEndTimeoutMillis, 4));
ReloadIOSuccessReporterHandler reloadIOSuccessReporterHandler = new ReloadIOSuccessReporterHandler(ioBundle::reload);
List<PacketListReceiver> packetListReceiverList = new ArrayList<>();
List<EnvironmentUpdater> environmentUpdaterList = new ArrayList<>();
for (Map.Entry<Integer, ModbusRequester> entry : addressToModbusRequesterMap.entrySet()) {
int address = entry.getKey();
ModbusRequester modbusRequester = entry.getValue();
ModbusSlave slave = new ImmutableAddressModbusSlave(address, modbus);
DataRequesterResult result = modbusRequester.create(requestObject, reloadIOSuccessReporterHandler.createReporter(), slave);
packetListReceiverList.add(result.getStatusPacketListReceiver());
environmentUpdaterList.add(result.getEnvironmentUpdater());
}
return new DataRequesterResult(new PacketListReceiverMultiplexer(packetListReceiverList), new EnvironmentUpdaterMultiplexer(environmentUpdaterList));
}
Aggregations