Search in sources :

Example 1 with DataRequesterResult

use of me.retrodaredevil.solarthing.config.request.DataRequesterResult in project solarthing by wildmountainfarms.

the class TracerModbusRequester method create.

@Override
public DataRequesterResult create(RequestObject requestObject, SuccessReporter successReporter, ModbusSlave modbus) {
    final TracerReadTable read;
    final Runnable reloadCache;
    if (bulkRequest) {
        ModbusCacheSlave modbusCacheSlave = new ModbusCacheSlave(modbus);
        read = new TracerModbusSlaveRead(modbusCacheSlave);
        reloadCache = () -> cacheRegisters(modbusCacheSlave);
    } else {
        read = new TracerModbusSlaveRead(modbus);
        reloadCache = () -> {
        };
    }
    TracerWriteTable write = new TracerModbusSlaveWrite(modbus);
    TracerModbusEnvironment tracerModbusEnvironment = new TracerModbusEnvironment(read, write);
    final NetCatServerHandler netCatServerHandler;
    if (configurationServerConfig == null) {
        netCatServerHandler = null;
    } else {
        try {
            netCatServerHandler = new NetCatServerHandler(configurationServerConfig.getBindAddress(), configurationServerConfig.getPort());
        } catch (IOException e) {
            throw new RuntimeException("Could not create NetCatServerHandler", e);
        }
    }
    return new DataRequesterResult(new ModbusListUpdaterWrapper(new TracerPacketListUpdater(number, read, write, tracerClockOptions, netCatServerHandler == null ? null : new ConnectionHandler(netCatServerHandler), connectionHandlerHasFlushLogic), reloadCache, successReporter, sendErrorPackets, "tracer.error." + number), new AttachToCommandEnvironmentUpdater(Collections.singletonList(tracerModbusEnvironment), attachToCommands::contains));
}
Also used : NetCatServerHandler(me.retrodaredevil.solarthing.netcat.NetCatServerHandler) TracerWriteTable(me.retrodaredevil.solarthing.solar.tracer.TracerWriteTable) TracerPacketListUpdater(me.retrodaredevil.solarthing.program.receiver.TracerPacketListUpdater) ModbusCacheSlave(me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave) TracerModbusSlaveWrite(me.retrodaredevil.solarthing.solar.tracer.modbus.TracerModbusSlaveWrite) IOException(java.io.IOException) TracerModbusSlaveRead(me.retrodaredevil.solarthing.solar.tracer.modbus.TracerModbusSlaveRead) DataRequesterResult(me.retrodaredevil.solarthing.config.request.DataRequesterResult) TracerReadTable(me.retrodaredevil.solarthing.solar.tracer.TracerReadTable) ConnectionHandler(me.retrodaredevil.solarthing.netcat.ConnectionHandler) TracerModbusEnvironment(me.retrodaredevil.solarthing.actions.environment.TracerModbusEnvironment) ModbusListUpdaterWrapper(me.retrodaredevil.solarthing.program.receiver.ModbusListUpdaterWrapper)

Example 2 with DataRequesterResult

use of me.retrodaredevil.solarthing.config.request.DataRequesterResult 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));
}
Also used : EnvironmentUpdaterMultiplexer(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer) AvailableCommandsListUpdater(me.retrodaredevil.solarthing.commands.packets.status.AvailableCommandsListUpdater) ArrayList(java.util.ArrayList) EnvironmentUpdater(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater) RoverAnalyticsHandler(me.retrodaredevil.solarthing.analytics.RoverAnalyticsHandler) RoverEventUpdaterListReceiver(me.retrodaredevil.solarthing.program.receiver.RoverEventUpdaterListReceiver) DataIdentifiablePacketListChecker(me.retrodaredevil.solarthing.misc.common.DataIdentifiablePacketListChecker) DaySummaryLogListReceiver(me.retrodaredevil.solarthing.solar.DaySummaryLogListReceiver) DataRequesterResult(me.retrodaredevil.solarthing.config.request.DataRequesterResult) PacketListReceiverMultiplexer(me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer) DataRequester(me.retrodaredevil.solarthing.config.request.DataRequester) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) TracerEventUpdaterListReceiver(me.retrodaredevil.solarthing.program.receiver.TracerEventUpdaterListReceiver) RequestObject(me.retrodaredevil.solarthing.config.request.RequestObject)

Example 3 with DataRequesterResult

use of me.retrodaredevil.solarthing.config.request.DataRequesterResult 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())))));
    }
}
Also used : FXStatusListUpdater(me.retrodaredevil.solarthing.solar.outback.FXStatusListUpdater) CommandProvider(me.retrodaredevil.solarthing.commands.command.CommandProvider) DataIdentifiablePacketListChecker(me.retrodaredevil.solarthing.misc.common.DataIdentifiablePacketListChecker) DaySummaryLogListReceiver(me.retrodaredevil.solarthing.solar.DaySummaryLogListReceiver) DataRequesterResult(me.retrodaredevil.solarthing.config.request.DataRequesterResult) MatePacketCreator49(me.retrodaredevil.solarthing.solar.outback.MatePacketCreator49) FXEventUpdaterListReceiver(me.retrodaredevil.solarthing.solar.outback.fx.FXEventUpdaterListReceiver) MateCommandEnvironment(me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) ReloadableIOBundle(me.retrodaredevil.solarthing.io.ReloadableIOBundle) AnalyticsManager(me.retrodaredevil.solarthing.analytics.AnalyticsManager) EnvironmentUpdaterMultiplexer(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer) AvailableCommandsListUpdater(me.retrodaredevil.solarthing.commands.packets.status.AvailableCommandsListUpdater) EnvironmentUpdater(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater) MXEventUpdaterListReceiver(me.retrodaredevil.solarthing.solar.outback.mx.MXEventUpdaterListReceiver) IOConfig(me.retrodaredevil.solarthing.config.io.IOConfig) PacketListReceiverMultiplexer(me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer) DataRequester(me.retrodaredevil.solarthing.config.request.DataRequester) MateCommand(me.retrodaredevil.solarthing.solar.outback.command.MateCommand) DailyIdentifier(me.retrodaredevil.solarthing.util.time.DailyIdentifier) SourcedCommand(me.retrodaredevil.solarthing.commands.command.SourcedCommand) TimedPacketReceiver(me.retrodaredevil.solarthing.packets.handling.implementations.TimedPacketReceiver) MateAnalyticsHandler(me.retrodaredevil.solarthing.analytics.MateAnalyticsHandler) RequestObject(me.retrodaredevil.solarthing.config.request.RequestObject)

Example 4 with DataRequesterResult

use of me.retrodaredevil.solarthing.config.request.DataRequesterResult in project solarthing by wildmountainfarms.

the class RoverModbusRequester method create.

@Override
public DataRequesterResult create(RequestObject requestObject, SuccessReporter successReporter, ModbusSlave modbus) {
    final RoverReadTable read;
    final Runnable reloadCache;
    if (bulkRequest) {
        ModbusCacheSlave modbusCacheSlave = new ModbusCacheSlave(modbus);
        read = new RoverModbusSlaveRead(modbusCacheSlave);
        reloadCache = () -> cacheRegisters(modbusCacheSlave);
    } else {
        read = new RoverModbusSlaveRead(modbus);
        reloadCache = () -> {
        };
    }
    RoverWriteTable write = new RoverModbusSlaveWrite(modbus);
    RoverModbusEnvironment roverModbusEnvironment = new RoverModbusEnvironment(read, write);
    final NetCatServerHandler netCatServerHandler;
    if (configurationServerConfig == null) {
        netCatServerHandler = null;
    } else {
        try {
            netCatServerHandler = new NetCatServerHandler(configurationServerConfig.getBindAddress(), configurationServerConfig.getPort());
        } catch (IOException e) {
            throw new RuntimeException("Could not create NetCatServerHandler", e);
        }
    }
    return new DataRequesterResult(new ModbusListUpdaterWrapper(new RoverPacketListUpdater(number, read, write, netCatServerHandler == null ? null : new ConnectionHandler(netCatServerHandler)), reloadCache, successReporter, sendErrorPackets, "rover.error." + number), new AttachToCommandEnvironmentUpdater(Collections.singletonList(roverModbusEnvironment), attachToCommands::contains));
}
Also used : RoverModbusSlaveRead(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveRead) NetCatServerHandler(me.retrodaredevil.solarthing.netcat.NetCatServerHandler) ModbusCacheSlave(me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave) IOException(java.io.IOException) RoverReadTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverReadTable) RoverPacketListUpdater(me.retrodaredevil.solarthing.program.receiver.RoverPacketListUpdater) DataRequesterResult(me.retrodaredevil.solarthing.config.request.DataRequesterResult) RoverModbusEnvironment(me.retrodaredevil.solarthing.actions.environment.RoverModbusEnvironment) ConnectionHandler(me.retrodaredevil.solarthing.netcat.ConnectionHandler) ModbusListUpdaterWrapper(me.retrodaredevil.solarthing.program.receiver.ModbusListUpdaterWrapper) RoverModbusSlaveWrite(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveWrite) RoverWriteTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverWriteTable)

Example 5 with DataRequesterResult

use of me.retrodaredevil.solarthing.config.request.DataRequesterResult 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));
}
Also used : EnvironmentUpdaterMultiplexer(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer) ArrayList(java.util.ArrayList) EnvironmentUpdater(me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater) IOConfig(me.retrodaredevil.solarthing.config.io.IOConfig) DataRequesterResult(me.retrodaredevil.solarthing.config.request.DataRequesterResult) PacketListReceiverMultiplexer(me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) ReloadableIOBundle(me.retrodaredevil.solarthing.io.ReloadableIOBundle) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

DataRequesterResult (me.retrodaredevil.solarthing.config.request.DataRequesterResult)5 EnvironmentUpdater (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater)3 EnvironmentUpdaterMultiplexer (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer)3 PacketListReceiver (me.retrodaredevil.solarthing.packets.handling.PacketListReceiver)3 PacketListReceiverMultiplexer (me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AvailableCommandsListUpdater (me.retrodaredevil.solarthing.commands.packets.status.AvailableCommandsListUpdater)2 IOConfig (me.retrodaredevil.solarthing.config.io.IOConfig)2 DataRequester (me.retrodaredevil.solarthing.config.request.DataRequester)2 RequestObject (me.retrodaredevil.solarthing.config.request.RequestObject)2 ReloadableIOBundle (me.retrodaredevil.solarthing.io.ReloadableIOBundle)2 DataIdentifiablePacketListChecker (me.retrodaredevil.solarthing.misc.common.DataIdentifiablePacketListChecker)2 ConnectionHandler (me.retrodaredevil.solarthing.netcat.ConnectionHandler)2 NetCatServerHandler (me.retrodaredevil.solarthing.netcat.NetCatServerHandler)2 ModbusCacheSlave (me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave)2 ModbusListUpdaterWrapper (me.retrodaredevil.solarthing.program.receiver.ModbusListUpdaterWrapper)2 DaySummaryLogListReceiver (me.retrodaredevil.solarthing.solar.DaySummaryLogListReceiver)2 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1