Search in sources :

Example 1 with IOConfig

use of me.retrodaredevil.solarthing.config.io.IOConfig in project solarthing by wildmountainfarms.

the class ConfigUtil method parseIOConfig.

public static IOConfig parseIOConfig(File configFile, SerialConfig defaultSerialConfig) {
    final FileInputStream inputStream;
    try {
        inputStream = new FileInputStream(configFile);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    InjectableValues.Std iv = new InjectableValues.Std();
    iv.addValue(SerialIOConfig.DEFAULT_SERIAL_CONFIG_KEY, defaultSerialConfig);
    ObjectMapper mapper = JacksonUtil.configurationMapper(JacksonUtil.defaultMapper());
    mapper.setInjectableValues(iv);
    final IOConfig config;
    try {
        config = mapper.readValue(inputStream, IOConfig.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return config;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) InjectableValues(com.fasterxml.jackson.databind.InjectableValues) FileInputStream(java.io.FileInputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOConfig(me.retrodaredevil.solarthing.config.io.IOConfig) SerialIOConfig(me.retrodaredevil.solarthing.config.io.SerialIOConfig)

Example 2 with IOConfig

use of me.retrodaredevil.solarthing.config.io.IOConfig 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 3 with IOConfig

use of me.retrodaredevil.solarthing.config.io.IOConfig in project solarthing by wildmountainfarms.

the class RoverMain method doRoverProgram.

private static int doRoverProgram(RoverOption options, RoverProgramRunner runner, @Nullable RegisterCacheHandler registerCacheHandler) {
    IOConfig ioConfig = ConfigUtil.parseIOConfig(options.getIOBundleFile(), RoverReadTable.SERIAL_CONFIG);
    try (ReloadableIOBundle ioBundle = new ReloadableIOBundle(ioConfig::createIOBundle)) {
        ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder(2000, 20, 4));
        MutableAddressModbusSlave slave = new MutableAddressModbusSlave(options.getModbusAddress(), modbus);
        final RoverReadTable read;
        final Runnable reloadCache;
        if (registerCacheHandler != null) {
            ModbusCacheSlave modbusCacheSlave = new ModbusCacheSlave(slave);
            read = new RoverModbusSlaveRead(modbusCacheSlave);
            reloadCache = () -> registerCacheHandler.cacheRegisters(modbusCacheSlave);
        } else {
            read = new RoverModbusSlaveRead(slave);
            reloadCache = () -> {
            };
        }
        RoverWriteTable write = new RoverModbusSlaveWrite(slave);
        return runner.doProgram(slave, read, write, reloadCache, ioBundle::reload);
    } catch (Exception e) {
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Got exception!", e);
        return SolarThingConstants.EXIT_CODE_CRASH;
    }
}
Also used : RoverModbusSlaveRead(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveRead) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusCacheSlave(me.retrodaredevil.solarthing.program.modbus.ModbusCacheSlave) IOConfig(me.retrodaredevil.solarthing.config.io.IOConfig) RoverReadTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverReadTable) MutableAddressModbusSlave(me.retrodaredevil.solarthing.program.modbus.MutableAddressModbusSlave) RtuDataEncoder(me.retrodaredevil.io.modbus.RtuDataEncoder) RoverModbusSlaveWrite(me.retrodaredevil.solarthing.solar.renogy.rover.modbus.RoverModbusSlaveWrite) ReloadableIOBundle(me.retrodaredevil.solarthing.io.ReloadableIOBundle) IOModbusSlaveBus(me.retrodaredevil.io.modbus.IOModbusSlaveBus) ModbusSlaveBus(me.retrodaredevil.io.modbus.ModbusSlaveBus) RoverWriteTable(me.retrodaredevil.solarthing.solar.renogy.rover.RoverWriteTable)

Example 4 with IOConfig

use of me.retrodaredevil.solarthing.config.io.IOConfig 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

IOConfig (me.retrodaredevil.solarthing.config.io.IOConfig)4 ReloadableIOBundle (me.retrodaredevil.solarthing.io.ReloadableIOBundle)3 EnvironmentUpdater (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater)2 EnvironmentUpdaterMultiplexer (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer)2 DataRequesterResult (me.retrodaredevil.solarthing.config.request.DataRequesterResult)2 PacketListReceiver (me.retrodaredevil.solarthing.packets.handling.PacketListReceiver)2 PacketListReceiverMultiplexer (me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer)2 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 IOModbusSlaveBus (me.retrodaredevil.io.modbus.IOModbusSlaveBus)1 ModbusSlaveBus (me.retrodaredevil.io.modbus.ModbusSlaveBus)1 RtuDataEncoder (me.retrodaredevil.io.modbus.RtuDataEncoder)1 MateCommandEnvironment (me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment)1 AnalyticsManager (me.retrodaredevil.solarthing.analytics.AnalyticsManager)1