Search in sources :

Example 1 with MateCommand

use of me.retrodaredevil.solarthing.solar.outback.command.MateCommand in project solarthing by wildmountainfarms.

the class MateCommandActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    MateCommandEnvironment mateCommandEnvironment = actionEnvironment.getInjectEnvironment().get(MateCommandEnvironment.class);
    SourceEnvironment sourceEnvironment = actionEnvironment.getInjectEnvironment().get(SourceEnvironment.class);
    Queue<? super SourcedCommand<MateCommand>> queue = mateCommandEnvironment.getQueue();
    OpenSource source = sourceEnvironment.getSource();
    return Actions.createRunOnce(() -> queue.add(new SourcedCommand<>(source, mateCommand)));
}
Also used : SourceEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceEnvironment) MateCommandEnvironment(me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment) MateCommand(me.retrodaredevil.solarthing.solar.outback.command.MateCommand) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) SourcedCommand(me.retrodaredevil.solarthing.commands.command.SourcedCommand)

Example 2 with MateCommand

use of me.retrodaredevil.solarthing.solar.outback.command.MateCommand in project solarthing by wildmountainfarms.

the class MateCommandSender method onDataReceive.

public void onDataReceive(boolean firstData, boolean stale) {
    if (firstData && !stale) {
        SourcedCommand<MateCommand> sourcedCommand = commandProvider.pollCommand();
        if (sourcedCommand != null) {
            MateCommand command = sourcedCommand.getCommand();
            if (!allowedCommands.contains(command)) {
                LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "Command: " + command + " is not allowed!");
                return;
            }
            /*
				I can confirm that if you don't send the command in the correct time window, the Mate will not receive it

				This solution is only here because instantType is not always reliable.
				 */
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Sending command: " + command);
            try {
                Thread.sleep(10);
                for (int i = 0; i <= 20; i++) {
                    // send this over the period of 0.8 second so we can be sure it got received
                    if (i != 0) {
                        Thread.sleep(100);
                    }
                    command.send(outputStream);
                }
            } catch (IOException | InterruptedException e) {
                LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "Unable to send command: " + command, e);
                return;
            }
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Sent command: " + command + " at " + System.currentTimeMillis());
            onCommandExecute.onCommandExecute(sourcedCommand);
        }
    } else if (firstData) {
        LOGGER.info("Not going to (possibly) send commands now because stale=true!");
    }
}
Also used : MateCommand(me.retrodaredevil.solarthing.solar.outback.command.MateCommand) IOException(java.io.IOException)

Example 3 with MateCommand

use of me.retrodaredevil.solarthing.solar.outback.command.MateCommand 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)

Aggregations

MateCommand (me.retrodaredevil.solarthing.solar.outback.command.MateCommand)3 MateCommandEnvironment (me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment)2 SourcedCommand (me.retrodaredevil.solarthing.commands.command.SourcedCommand)2 IOException (java.io.IOException)1 EnvironmentUpdater (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater)1 EnvironmentUpdaterMultiplexer (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer)1 SourceEnvironment (me.retrodaredevil.solarthing.actions.environment.SourceEnvironment)1 AnalyticsManager (me.retrodaredevil.solarthing.analytics.AnalyticsManager)1 MateAnalyticsHandler (me.retrodaredevil.solarthing.analytics.MateAnalyticsHandler)1 CommandProvider (me.retrodaredevil.solarthing.commands.command.CommandProvider)1 AvailableCommandsListUpdater (me.retrodaredevil.solarthing.commands.packets.status.AvailableCommandsListUpdater)1 IOConfig (me.retrodaredevil.solarthing.config.io.IOConfig)1 DataRequester (me.retrodaredevil.solarthing.config.request.DataRequester)1 DataRequesterResult (me.retrodaredevil.solarthing.config.request.DataRequesterResult)1 RequestObject (me.retrodaredevil.solarthing.config.request.RequestObject)1 ReloadableIOBundle (me.retrodaredevil.solarthing.io.ReloadableIOBundle)1 DataIdentifiablePacketListChecker (me.retrodaredevil.solarthing.misc.common.DataIdentifiablePacketListChecker)1 PacketListReceiver (me.retrodaredevil.solarthing.packets.handling.PacketListReceiver)1 PacketListReceiverMultiplexer (me.retrodaredevil.solarthing.packets.handling.PacketListReceiverMultiplexer)1 TimedPacketReceiver (me.retrodaredevil.solarthing.packets.handling.implementations.TimedPacketReceiver)1