Search in sources :

Example 1 with DatabaseConfig

use of me.retrodaredevil.solarthing.program.DatabaseConfig in project solarthing by wildmountainfarms.

the class DeserializeTest method testInfluxDb1.

@Test
void testInfluxDb1() throws JsonProcessingException {
    ObjectMapper mapper = JacksonUtil.defaultMapper();
    String json = "{\n" + "  \"type\": \"influxdb\",\n" + "  \"config\": {\n" + "    \"url\": \"http://localhost:8086\",\n" + "    \"username\": \"root\",\n" + "    \"password\": \"root\",\n" + "    \"database\": \"default_database\",\n" + "    \"measurement\": null,\n" + "\n" + "    \"status_retention_policies\": [\n" + "      {\n" + "        \"frequency\": 120,\n" + "        \"name\": \"autogen\"\n" + "      }\n" + "    ],\n" + "\n" + "    \"event_retention_policy\": {\n" + "      \"name\": \"autogen\"\n" + "    }\n" + "  }\n" + "}";
    mapper.registerSubtypes(DatabaseSettings.class, InfluxDbDatabaseSettings.class);
    DatabaseConfig config = mapper.readValue(json, DatabaseConfig.class);
    assertTrue(config.getSettings() instanceof InfluxDbDatabaseSettings);
}
Also used : InfluxDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.InfluxDbDatabaseSettings) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DatabaseConfig(me.retrodaredevil.solarthing.program.DatabaseConfig) Test(org.junit.jupiter.api.Test)

Example 2 with DatabaseConfig

use of me.retrodaredevil.solarthing.program.DatabaseConfig 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 3 with DatabaseConfig

use of me.retrodaredevil.solarthing.program.DatabaseConfig in project solarthing by wildmountainfarms.

the class CommonProvider method init.

@PostConstruct
public void init() {
    defaultInstanceOptions = DefaultInstanceOptions.create(getDefaultSourceId(), getDefaultFragmentId());
    LOGGER.debug("Using defaultInstanceOptions=" + defaultInstanceOptions);
    LOGGER.debug("Database file: " + databaseFile.getAbsolutePath());
    LOGGER.debug("Working directory: " + new File(".").getAbsolutePath());
    ObjectMapper objectMapper = JacksonUtil.defaultMapper();
    objectMapper.getSubtypeResolver().registerSubtypes(DatabaseSettings.class, CouchDbDatabaseSettings.class);
    final FileInputStream reader;
    try {
        reader = new FileInputStream(databaseFile);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    final DatabaseConfig databaseConfig;
    try {
        databaseConfig = objectMapper.readValue(reader, DatabaseConfig.class);
    } catch (IOException e) {
        throw new RuntimeException("Couldn't parse data!", e);
    }
    DatabaseSettings databaseSettings = databaseConfig.getSettings();
    if (!(databaseSettings instanceof CouchDbDatabaseSettings)) {
        throw new UnsupportedOperationException("Only CouchDB is supported right now!");
    }
    couchDbDatabaseSettings = (CouchDbDatabaseSettings) databaseSettings;
}
Also used : CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) DatabaseConfig(me.retrodaredevil.solarthing.program.DatabaseConfig) PostConstruct(javax.annotation.PostConstruct)

Aggregations

DatabaseConfig (me.retrodaredevil.solarthing.program.DatabaseConfig)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ZoneId (java.time.ZoneId)1 PostConstruct (javax.annotation.PostConstruct)1 AnalyticsManager (me.retrodaredevil.solarthing.analytics.AnalyticsManager)1 DatabaseSettings (me.retrodaredevil.solarthing.config.databases.DatabaseSettings)1 DatabaseType (me.retrodaredevil.solarthing.config.databases.DatabaseType)1 InfluxDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.InfluxDbDatabaseSettings)1 SolarThingDatabase (me.retrodaredevil.solarthing.database.SolarThingDatabase)1 CouchDbSolarThingDatabase (me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase)1 SimpleDate (me.retrodaredevil.solarthing.pvoutput.SimpleDate)1 PVOutputService (me.retrodaredevil.solarthing.pvoutput.service.PVOutputService)1 OkHttpClient (okhttp3.OkHttpClient)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1