Search in sources :

Example 1 with AnalyticsManager

use of me.retrodaredevil.solarthing.analytics.AnalyticsManager in project solarthing by wildmountainfarms.

the class PVOutputUploadMain method startPVOutputUpload.

// TODO Make this an action for the automation program
@SuppressWarnings({ "SameReturnValue", "deprecation" })
public static int startPVOutputUpload(PVOutputUploadProgramOptions options, CommandOptions commandOptions, File dataDirectory) {
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Starting PV Output upload program");
    ZoneId zoneId = options.getZoneId();
    // Use US local since I (retrodaredevil) am the one debugging
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Using time zone: {}", zoneId.getDisplayName(TextStyle.FULL, Locale.US));
    LOGGER.info("Using default instance options: " + options.getDefaultInstanceOptions());
    DatabaseConfig databaseConfig = ConfigUtil.getDatabaseConfig(options.getDatabase());
    DatabaseType databaseType = databaseConfig.getType();
    if (databaseType != CouchDbDatabaseSettings.TYPE) {
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Only CouchDb can be used for this program type right now!");
        return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
    }
    CouchDbDatabaseSettings couchDbDatabaseSettings = (CouchDbDatabaseSettings) databaseConfig.getSettings();
    SolarThingDatabase database = CouchDbSolarThingDatabase.create(CouchDbUtil.createInstance(couchDbDatabaseSettings.getCouchProperties(), couchDbDatabaseSettings.getOkHttpProperties()));
    OkHttpClient client = PVOutputOkHttpUtil.configure(new OkHttpClient.Builder(), options.getApiKey(), options.getSystemId()).addInterceptor(new HttpLoggingInterceptor(LOGGER::debug).setLevel(HttpLoggingInterceptor.Level.BASIC)).build();
    Retrofit retrofit = PVOutputRetrofitUtil.defaultBuilder().client(client).build();
    PVOutputService service = retrofit.create(PVOutputService.class);
    PVOutputHandler handler = new PVOutputHandler(zoneId, options.getRequiredIdentifierMap(), options.getVoltageIdentifierFragmentMatcher(), options.getTemperatureIdentifierFragmentMatcher());
    String fromDateString = commandOptions.getPVOutputFromDate();
    String toDateString = commandOptions.getPVOutputToDate();
    if (fromDateString != null && toDateString != null) {
        System.out.println("Starting range upload");
        final SimpleDate fromDate;
        final SimpleDate toDate;
        try {
            // TODO Don't use SimpleDateFormat anymore and remove supress warnings for deprecation
            fromDate = SimpleDate.fromDate(DATE_FORMAT.parse(fromDateString));
            toDate = SimpleDate.fromDate(DATE_FORMAT.parse(toDateString));
        } catch (ParseException e) {
            e.printStackTrace();
            System.err.println("Unable to parser either from date or to date. Use the yyyy-MM-dd format");
            return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
        }
        return startRangeUpload(fromDate, toDate, options, database, handler, service, options.getZoneId());
    } else if ((fromDateString == null) != (toDateString == null)) {
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)You need to define both from and to, or define neither to do the normal PVOutput program!");
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
    analyticsManager.sendStartUp(ProgramType.PVOUTPUT_UPLOAD);
    return startRealTimeProgram(options, database, handler, service, options.getZoneId());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ZoneId(java.time.ZoneId) DatabaseType(me.retrodaredevil.solarthing.config.databases.DatabaseType) PVOutputService(me.retrodaredevil.solarthing.pvoutput.service.PVOutputService) Retrofit(retrofit2.Retrofit) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) SimpleDate(me.retrodaredevil.solarthing.pvoutput.SimpleDate) ParseException(java.text.ParseException) CouchDbSolarThingDatabase(me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) DatabaseConfig(me.retrodaredevil.solarthing.program.DatabaseConfig) AnalyticsManager(me.retrodaredevil.solarthing.analytics.AnalyticsManager)

Example 2 with AnalyticsManager

use of me.retrodaredevil.solarthing.analytics.AnalyticsManager in project solarthing by wildmountainfarms.

the class RequestMain method startRequestProgram.

public static int startRequestProgram(RequestProgramOptions options, File dataDirectory) throws Exception {
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning request program");
    AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
    analyticsManager.sendStartUp(ProgramType.REQUEST);
    return startRequestProgram(options, analyticsManager, options.getDataRequesterList(), options.getPeriod(), options.getMinimumWait());
}
Also used : AnalyticsManager(me.retrodaredevil.solarthing.analytics.AnalyticsManager)

Example 3 with AnalyticsManager

use of me.retrodaredevil.solarthing.analytics.AnalyticsManager 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 AnalyticsManager

use of me.retrodaredevil.solarthing.analytics.AnalyticsManager in project solarthing by wildmountainfarms.

the class RoverMain method connectRover.

public static int connectRover(RoverProgramOptions options, File dataDirectory) throws Exception {
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning rover program");
    AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
    analyticsManager.sendStartUp(ProgramType.ROVER);
    List<DataRequester> dataRequesterList = new ArrayList<>(options.getDataRequesterList());
    return doRover(options, analyticsManager, dataRequesterList);
}
Also used : ModbusDataRequester(me.retrodaredevil.solarthing.config.request.modbus.ModbusDataRequester) DataRequester(me.retrodaredevil.solarthing.config.request.DataRequester) ArrayList(java.util.ArrayList) AnalyticsManager(me.retrodaredevil.solarthing.analytics.AnalyticsManager)

Aggregations

AnalyticsManager (me.retrodaredevil.solarthing.analytics.AnalyticsManager)4 DataRequester (me.retrodaredevil.solarthing.config.request.DataRequester)2 ParseException (java.text.ParseException)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 EnvironmentUpdater (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdater)1 EnvironmentUpdaterMultiplexer (me.retrodaredevil.solarthing.actions.command.EnvironmentUpdaterMultiplexer)1 MateCommandEnvironment (me.retrodaredevil.solarthing.actions.environment.MateCommandEnvironment)1 MateAnalyticsHandler (me.retrodaredevil.solarthing.analytics.MateAnalyticsHandler)1 CommandProvider (me.retrodaredevil.solarthing.commands.command.CommandProvider)1 SourcedCommand (me.retrodaredevil.solarthing.commands.command.SourcedCommand)1 AvailableCommandsListUpdater (me.retrodaredevil.solarthing.commands.packets.status.AvailableCommandsListUpdater)1 DatabaseType (me.retrodaredevil.solarthing.config.databases.DatabaseType)1 CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)1 IOConfig (me.retrodaredevil.solarthing.config.io.IOConfig)1 DataRequesterResult (me.retrodaredevil.solarthing.config.request.DataRequesterResult)1 RequestObject (me.retrodaredevil.solarthing.config.request.RequestObject)1 ModbusDataRequester (me.retrodaredevil.solarthing.config.request.modbus.ModbusDataRequester)1 SolarThingDatabase (me.retrodaredevil.solarthing.database.SolarThingDatabase)1 CouchDbSolarThingDatabase (me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase)1