Search in sources :

Example 1 with CouchDbDatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings in project solarthing by wildmountainfarms.

the class GraphQLProviderTest method testGeneration.

@Test
void testGeneration() {
    CouchDbDatabaseSettings couchDbDatabaseSettings = new CouchDbDatabaseSettings(new CouchPropertiesBuilder("http", "localhost", 5984, null, null).build(), new OkHttpPropertiesBuilder().build());
    ObjectMapper mapper = JacksonUtil.defaultMapper();
    // Use DefaultInstanceOptions.REQUIRE_NO_DEFAULTS while testing because first, we don't actually query any data,
    // and second, we're trying to get rid of usages of DefaultInstanceOptions.DEFAULT_DEFAULT_INSTANCE_OPTIONS
    CacheController cacheController = new CacheController(new CacheHandler(mapper, DefaultInstanceOptions.REQUIRE_NO_DEFAULTS, CouchDbUtil.createInstance(couchDbDatabaseSettings.getCouchProperties(), couchDbDatabaseSettings.getOkHttpProperties())));
    GraphQLSchema schema = GraphQLProvider.createGraphQLSchemaGenerator(JacksonUtil.defaultMapper(), couchDbDatabaseSettings, DefaultInstanceOptions.REQUIRE_NO_DEFAULTS, new SolcastConfig(Collections.emptyMap()), cacheController).generate();
    GraphQL.newGraphQL(schema).build();
}
Also used : SolcastConfig(me.retrodaredevil.solarthing.rest.graphql.solcast.SolcastConfig) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) CacheController(me.retrodaredevil.solarthing.rest.cache.CacheController) CacheHandler(me.retrodaredevil.solarthing.rest.cache.CacheHandler) CouchPropertiesBuilder(me.retrodaredevil.couchdb.CouchPropertiesBuilder) GraphQLSchema(graphql.schema.GraphQLSchema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) OkHttpPropertiesBuilder(me.retrodaredevil.okhttp3.OkHttpPropertiesBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with CouchDbDatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings 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 CouchDbDatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings in project solarthing by wildmountainfarms.

the class SolarMain method doMain.

private static int doMain(String[] args) {
    String logMessage = "Beginning main. Jar: " + getJarInfo();
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "[LOG] " + logMessage);
    System.out.println("[stdout] " + logMessage);
    System.err.println("[stderr] " + logMessage);
    Cli<CommandOptions> cli = CliFactory.createCli(CommandOptions.class);
    final CommandOptions commandOptions;
    try {
        commandOptions = cli.parseArguments(args);
    } catch (ArgumentValidationException ex) {
        System.out.println(cli.getHelpMessage());
        if (ex instanceof HelpRequestedException) {
            return 0;
        }
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, ex.getMessage());
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Incorrect args");
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    if (commandOptions.getBaseConfigFile() != null) {
        return doMainCommand(commandOptions, commandOptions.getBaseConfigFile());
    }
    if (commandOptions.getCouchDbSetupFile() != null) {
        final DatabaseConfig config;
        try {
            config = ConfigUtil.MAPPER.readValue(commandOptions.getCouchDbSetupFile(), DatabaseConfig.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Problem reading CouchDB database settings file.");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        DatabaseSettings settings = config.getSettings();
        if (!(settings instanceof CouchDbDatabaseSettings)) {
            System.err.println("Must be CouchDB database settings!");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        try {
            return CouchDbSetupMain.createFrom((CouchDbDatabaseSettings) settings).doCouchDbSetupMain();
        } catch (CouchDbException e) {
            if (e instanceof CouchDbCodeException) {
                ErrorResponse error = ((CouchDbCodeException) e).getErrorResponse();
                if (error != null) {
                    System.err.println(error.getError());
                    System.err.println(error.getReason());
                }
            }
            throw new RuntimeException(e);
        }
    }
    List<String> legacyArguments = commandOptions.getLegacyOptionsRaw();
    if (legacyArguments == null || legacyArguments.isEmpty()) {
        System.err.println(cli.getHelpMessage());
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    System.err.println("Invalid sub command: " + legacyArguments.get(0));
    return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
}
Also used : CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) ErrorResponse(me.retrodaredevil.couchdbjava.response.ErrorResponse) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) HelpRequestedException(com.lexicalscope.jewel.cli.HelpRequestedException) CouchDbCodeException(me.retrodaredevil.couchdbjava.exception.CouchDbCodeException) ArgumentValidationException(com.lexicalscope.jewel.cli.ArgumentValidationException)

Example 4 with CouchDbDatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings in project solarthing by wildmountainfarms.

the class AutomationMain method startAutomation.

public static int startAutomation(List<ActionNode> actionNodes, DatabaseTimeZoneOptionBase options, long periodMillis) {
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Starting automation program.");
    final CouchDbDatabaseSettings couchSettings;
    try {
        couchSettings = ConfigUtil.expectCouchDbDatabaseSettings(options);
    } catch (IllegalArgumentException ex) {
        LOGGER.error("(Fatal)", ex);
        return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
    }
    SolarThingDatabase database = CouchDbSolarThingDatabase.create(CouchDbUtil.createInstance(couchSettings.getCouchProperties(), couchSettings.getOkHttpProperties()));
    VariableEnvironment variableEnvironment = new VariableEnvironment();
    // Use atomic reference so that access is thread safe
    AtomicReference<FragmentedPacketGroup> latestPacketGroupReference = new AtomicReference<>(null);
    // Use atomic reference so that access is thread safe
    AtomicReference<List<VersionedPacket<StoredAlterPacket>>> alterPacketsReference = new AtomicReference<>(null);
    // note this may return null, and that's OK // This is thread safe if needed
    FragmentedPacketGroupProvider fragmentedPacketGroupProvider = latestPacketGroupReference::get;
    Clock clock = Clock.systemUTC();
    SimpleDatabaseCache statusDatabaseCache = SimpleDatabaseCache.createDefault(clock);
    // not thread safe
    ResourceManager<SimpleDatabaseCache> statusDatabaseCacheManager = new BasicResourceManager<>(statusDatabaseCache);
    SimpleDatabaseCache eventDatabaseCache = SimpleDatabaseCache.createDefault(clock);
    ResourceManager<SimpleDatabaseCache> eventDatabaseCacheManager = new ReadWriteResourceManager<>(eventDatabaseCache);
    SimpleDatabaseCache openDatabaseCache = new SimpleDatabaseCache(Duration.ofMinutes(60), Duration.ofMinutes(40), Duration.ofMinutes(20), Duration.ofMinutes(15), clock);
    // not thread safe
    ResourceManager<SimpleDatabaseCache> openDatabaseCacheManager = new BasicResourceManager<>(openDatabaseCache);
    SimplePacketCache<AuthorizationPacket> authorizationPacketCache = new SimplePacketCache<>(Duration.ofSeconds(20), DatabaseDocumentKeyMap.createPacketSourceFromDatabase(database), false);
    String sourceId = options.getSourceId();
    InjectEnvironment injectEnvironment = new InjectEnvironment.Builder().add(new NanoTimeProviderEnvironment(NanoTimeProvider.SYSTEM_NANO_TIME)).add(new SourceIdEnvironment(sourceId)).add(// most of the time, it's better to use SolarThingDatabaseEnvironment instead, but this option is here in case it's needed
    new CouchDbEnvironment(couchSettings)).add(new SolarThingDatabaseEnvironment(CouchDbSolarThingDatabase.create(CouchDbUtil.createInstance(couchSettings.getCouchProperties(), couchSettings.getOkHttpProperties())))).add(new TimeZoneEnvironment(options.getZoneId())).add(// access is thread safe if needed
    new LatestPacketGroupEnvironment(fragmentedPacketGroupProvider)).add(// access is thread safe if needed
    new LatestFragmentedPacketGroupEnvironment(fragmentedPacketGroupProvider)).add(new EventDatabaseCacheEnvironment(eventDatabaseCacheManager)).add(new OpenDatabaseCacheEnvironment(openDatabaseCache)).add(// access is thread safe if needed
    new AlterPacketsEnvironment(alterPacketsReference::get)).add(new AuthorizationEnvironment(new DatabaseDocumentKeyMap(authorizationPacketCache))).build();
    ActionMultiplexer multiplexer = new Actions.ActionMultiplexerBuilder().build();
    while (!Thread.currentThread().isInterrupted()) {
        queryAndFeed(database.getStatusDatabase(), statusDatabaseCacheManager, true);
        queryAndFeed(database.getEventDatabase(), eventDatabaseCacheManager, true);
        queryAndFeed(database.getOpenDatabase(), openDatabaseCacheManager, false);
        {
            // Never cache alter packets, because it's always important that we have up-to-date data, or no data at all.
            List<VersionedPacket<StoredAlterPacket>> alterPackets = null;
            try {
                alterPackets = database.getAlterDatabase().queryAll(sourceId);
                LOGGER.debug("Got " + alterPackets.size() + " alter packets");
            } catch (SolarThingDatabaseException e) {
                LOGGER.error("Could not get alter packets", e);
            }
            alterPacketsReference.set(alterPackets);
        }
        // we have auto update turned off, so we have to call this
        authorizationPacketCache.updateIfNeeded();
        List<FragmentedPacketGroup> statusPacketGroups = PacketUtil.getPacketGroups(options.getSourceId(), options.getDefaultInstanceOptions(), statusDatabaseCache.getAllCachedPackets());
        if (statusPacketGroups != null) {
            FragmentedPacketGroup statusPacketGroup = statusPacketGroups.get(statusPacketGroups.size() - 1);
            latestPacketGroupReference.set(statusPacketGroup);
        }
        for (ActionNode actionNode : actionNodes) {
            multiplexer.add(actionNode.createAction(new ActionEnvironment(variableEnvironment, new VariableEnvironment(), injectEnvironment)));
        }
        multiplexer.update();
        LOGGER.debug("There are " + multiplexer.getActiveActions().size() + " active actions");
        try {
            Thread.sleep(periodMillis);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(ex);
        }
    }
    return 0;
}
Also used : ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) NanoTimeProviderEnvironment(me.retrodaredevil.action.node.environment.NanoTimeProviderEnvironment) ActionNode(me.retrodaredevil.action.node.ActionNode) BasicResourceManager(me.retrodaredevil.solarthing.util.sync.BasicResourceManager) Clock(java.time.Clock) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) InjectEnvironment(me.retrodaredevil.action.node.environment.InjectEnvironment) AuthorizationPacket(me.retrodaredevil.solarthing.type.closed.authorization.AuthorizationPacket) SimpleDatabaseCache(me.retrodaredevil.solarthing.database.cache.SimpleDatabaseCache) SimplePacketCache(me.retrodaredevil.solarthing.database.cache.SimplePacketCache) ArrayList(java.util.ArrayList) List(java.util.List) ActionMultiplexer(me.retrodaredevil.action.ActionMultiplexer) CouchDbSolarThingDatabase(me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase) ReadWriteResourceManager(me.retrodaredevil.solarthing.util.sync.ReadWriteResourceManager) FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) Actions(me.retrodaredevil.action.Actions) AtomicReference(java.util.concurrent.atomic.AtomicReference) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) VariableEnvironment(me.retrodaredevil.action.node.environment.VariableEnvironment)

Example 5 with CouchDbDatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings 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

CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)7 CouchDbSolarThingDatabase (me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2 DatabaseSettings (me.retrodaredevil.solarthing.config.databases.DatabaseSettings)2 DatabaseType (me.retrodaredevil.solarthing.config.databases.DatabaseType)2 SolarThingDatabase (me.retrodaredevil.solarthing.database.SolarThingDatabase)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 DatabaseConfig (me.retrodaredevil.solarthing.program.DatabaseConfig)2 ArgumentValidationException (com.lexicalscope.jewel.cli.ArgumentValidationException)1 HelpRequestedException (com.lexicalscope.jewel.cli.HelpRequestedException)1 GraphQLSchema (graphql.schema.GraphQLSchema)1 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 Clock (java.time.Clock)1 ZoneId (java.time.ZoneId)1 List (java.util.List)1