Search in sources :

Example 1 with IndividualSettings

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

the class PacketHandlerInit method getPacketHandlerBundle.

public static PacketHandlerBundle getPacketHandlerBundle(List<DatabaseConfig> configs, String uniqueStatusName, String uniqueEventName, String sourceId, int fragmentId) {
    List<PacketHandler> statusPacketHandlers = new ArrayList<>();
    List<PacketHandler> eventPacketHandlers = new ArrayList<>();
    for (DatabaseConfig config : configs) {
        IndividualSettings statusIndividualSettings = config.getIndividualSettingsOrDefault(Constants.DATABASE_UPLOAD_ID, null);
        FrequencySettings statusFrequencySettings = statusIndividualSettings != null ? statusIndividualSettings.getFrequencySettings() : FrequencySettings.NORMAL_SETTINGS;
        if (CouchDbDatabaseSettings.TYPE.equals(config.getType())) {
            CouchDbDatabaseSettings settings = (CouchDbDatabaseSettings) config.getSettings();
            CouchDbInstance instance = CouchDbUtil.createInstance(settings.getCouchProperties(), settings.getOkHttpProperties());
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new PrintPacketHandleExceptionWrapper(new CouchDbPacketSaver(instance.getDatabase(uniqueStatusName), false))), statusFrequencySettings));
            eventPacketHandlers.add(new AsyncRetryingPacketHandler(new CouchDbPacketSaver(instance.getDatabase(uniqueEventName), true)));
        } else if (InfluxDbDatabaseSettings.TYPE.equals(config.getType())) {
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "You are using InfluxDB 1.X! It is recommended that you switch to 2.0, but is not required.");
            InfluxDbDatabaseSettings settings = (InfluxDbDatabaseSettings) config.getSettings();
            String databaseName = settings.getDatabaseName();
            String measurementName = settings.getMeasurementName();
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new PrintPacketHandleExceptionWrapper(new InfluxDbPacketSaver(settings.getInfluxProperties(), settings.getOkHttpProperties(), new ConstantNameGetter(databaseName != null ? databaseName : uniqueStatusName), measurementName != null ? new ConstantMeasurementPacketPointCreator(measurementName) : (databaseName != null ? new ConstantMeasurementPacketPointCreator(uniqueStatusName) : DocumentedMeasurementPacketPointCreator.INSTANCE), new FrequentRetentionPolicyGetter(new FrequentHandler<>(settings.getFrequentStatusRetentionPolicyList()))))), statusFrequencySettings));
            eventPacketHandlers.add(new AsyncRetryingPacketHandler(new InfluxDbPacketSaver(settings.getInfluxProperties(), settings.getOkHttpProperties(), new ConstantNameGetter(databaseName != null ? databaseName : uniqueEventName), measurementName != null ? new ConstantMeasurementPacketPointCreator(measurementName) : (databaseName != null ? new ConstantMeasurementPacketPointCreator(uniqueEventName) : DocumentedMeasurementPacketPointCreator.INSTANCE), new ConstantRetentionPolicyGetter(settings.getEventRetentionPolicy()))));
        } else if (InfluxDb2DatabaseSettings.TYPE.equals(config.getType())) {
            InfluxDb2DatabaseSettings settings = (InfluxDb2DatabaseSettings) config.getSettings();
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new PrintPacketHandleExceptionWrapper(new InfluxDb2PacketSaver(settings.getInfluxDbProperties(), settings.getOkHttpProperties(), new ConstantNameGetter(uniqueStatusName), DocumentedMeasurementPacketPoint2Creator.INSTANCE))), statusFrequencySettings));
            eventPacketHandlers.add(new AsyncRetryingPacketHandler(new InfluxDb2PacketSaver(settings.getInfluxDbProperties(), settings.getOkHttpProperties(), new ConstantNameGetter(uniqueEventName), DocumentedMeasurementPacketPoint2Creator.INSTANCE)));
        } else if (LatestFileDatabaseSettings.TYPE.equals(config.getType())) {
            LatestFileDatabaseSettings settings = (LatestFileDatabaseSettings) config.getSettings();
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Adding latest file 'database'. This currently only saves 'status' packets");
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new FileWritePacketHandler(settings.getFile(), new JacksonStringPacketHandler(MAPPER), false), statusFrequencySettings));
        } else if (PostDatabaseSettings.TYPE.equals(config.getType())) {
            PostDatabaseSettings settings = (PostDatabaseSettings) config.getSettings();
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new PostPacketHandler(settings.getUrl(), new JacksonStringPacketHandler(MAPPER), MediaType.get("application/json"))), statusFrequencySettings));
        } else if (MqttDatabaseSettings.TYPE.equals(config.getType())) {
            MqttDatabaseSettings settings = (MqttDatabaseSettings) config.getSettings();
            String client = settings.getClientId();
            if (client == null) {
                client = "solarthing-" + sourceId + "-" + fragmentId;
            }
            statusPacketHandlers.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new MqttPacketSaver(settings.getBroker(), client, settings.getUsername(), settings.getPassword(), settings.getTopicFormat(), settings.isRetain(), sourceId, fragmentId)), statusFrequencySettings));
        }
    }
    return new PacketHandlerBundle(statusPacketHandlers, eventPacketHandlers);
}
Also used : ArrayList(java.util.ArrayList) FileWritePacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.FileWritePacketHandler) CouchDbPacketSaver(me.retrodaredevil.solarthing.couchdb.CouchDbPacketSaver) PostPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.PostPacketHandler) CouchDbInstance(me.retrodaredevil.couchdbjava.CouchDbInstance) MqttPacketSaver(me.retrodaredevil.solarthing.mqtt.MqttPacketSaver) FrequentRetentionPolicyGetter(me.retrodaredevil.solarthing.influxdb.retention.FrequentRetentionPolicyGetter) ConstantMeasurementPacketPointCreator(me.retrodaredevil.solarthing.influxdb.influxdb1.ConstantMeasurementPacketPointCreator) ConstantRetentionPolicyGetter(me.retrodaredevil.solarthing.influxdb.retention.ConstantRetentionPolicyGetter) InfluxDbPacketSaver(me.retrodaredevil.solarthing.influxdb.influxdb1.InfluxDbPacketSaver) InfluxDb2PacketSaver(me.retrodaredevil.solarthing.influxdb.infuxdb2.InfluxDb2PacketSaver) FileWritePacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.FileWritePacketHandler) PostPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.PostPacketHandler) JacksonStringPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.JacksonStringPacketHandler) JacksonStringPacketHandler(me.retrodaredevil.solarthing.packets.handling.implementations.JacksonStringPacketHandler) ConstantNameGetter(me.retrodaredevil.solarthing.influxdb.ConstantNameGetter) IndividualSettings(me.retrodaredevil.solarthing.config.databases.IndividualSettings)

Example 2 with IndividualSettings

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

the class CommandUtil method getCommandRequesterHandlerList.

/**
 * Gets packet handlers that will download requested commands
 * @param databaseConfigs The list of database configs
 * @param packetGroupReceiver Receives data that has been downloaded. Note that this may be called in a separate thread, so make sure it is thread safe
 * @param options The options object
 * @return A list of packet handlers that, when called, will possibly download commands and then forward those commands to {@code packetGroupReceiver}
 */
public static List<PacketHandler> getCommandRequesterHandlerList(List<DatabaseConfig> databaseConfigs, PacketGroupReceiver packetGroupReceiver, PacketHandlingOption options) {
    // Handlers to request and get new commands to send (This may block the current thread). (This doesn't actually handle packets)
    final List<PacketHandler> commandRequesterHandlerList = new ArrayList<>();
    for (DatabaseConfig config : databaseConfigs) {
        if (CouchDbDatabaseSettings.TYPE.equals(config.getType())) {
            CouchDbDatabaseSettings settings = (CouchDbDatabaseSettings) config.getSettings();
            CouchDbInstance instance = CouchDbUtil.createInstance(settings.getCouchProperties(), settings.getOkHttpProperties());
            SolarThingDatabase database = CouchDbSolarThingDatabase.create(instance);
            IndividualSettings individualSettings = config.getIndividualSettingsOrDefault(Constants.DATABASE_COMMAND_DOWNLOAD_ID, null);
            FrequencySettings frequencySettings = individualSettings != null ? individualSettings.getFrequencySettings() : FrequencySettings.NORMAL_SETTINGS;
            PacketHandler packetHandler = new PacketHandler() {

                private final SecurityPacketReceiver securityPacketReceiver = new SecurityPacketReceiver(DatabaseDocumentKeyMap.createFromDatabase(database), packetGroupReceiver, new SecurityPacketReceiver.InstanceTargetPredicate(options.getSourceId(), options.getFragmentId()), Collections.singleton(CommandOpenPacket.class), System.currentTimeMillis(), options.getFragmentId(), options.getSourceId(), database.getEventDatabase());

                @Override
                public void handle(PacketCollection packetCollection) throws PacketHandleException {
                    final List<StoredPacketGroup> packetGroups;
                    try {
                        packetGroups = database.getOpenDatabase().query(new MillisQueryBuilder().startKey(System.currentTimeMillis() - 5 * 60 * 1000).build());
                    } catch (SolarThingDatabaseException e) {
                        throw new PacketHandleException(e);
                    }
                    securityPacketReceiver.receivePacketGroups(packetGroups);
                }
            };
            commandRequesterHandlerList.add(new ThrottleFactorPacketHandler(new AsyncPacketHandlerWrapper(new PrintPacketHandleExceptionWrapper(packetHandler)), frequencySettings));
        }
    }
    return commandRequesterHandlerList;
}
Also used : PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) ArrayList(java.util.ArrayList) MillisQueryBuilder(me.retrodaredevil.solarthing.database.MillisQueryBuilder) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) StoredPacketGroup(me.retrodaredevil.solarthing.packets.collection.StoredPacketGroup) CouchDbInstance(me.retrodaredevil.couchdbjava.CouchDbInstance) CouchDbSolarThingDatabase(me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) IndividualSettings(me.retrodaredevil.solarthing.config.databases.IndividualSettings)

Aggregations

ArrayList (java.util.ArrayList)2 CouchDbInstance (me.retrodaredevil.couchdbjava.CouchDbInstance)2 IndividualSettings (me.retrodaredevil.solarthing.config.databases.IndividualSettings)2 CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)1 CouchDbPacketSaver (me.retrodaredevil.solarthing.couchdb.CouchDbPacketSaver)1 MillisQueryBuilder (me.retrodaredevil.solarthing.database.MillisQueryBuilder)1 SolarThingDatabase (me.retrodaredevil.solarthing.database.SolarThingDatabase)1 CouchDbSolarThingDatabase (me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase)1 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)1 ConstantNameGetter (me.retrodaredevil.solarthing.influxdb.ConstantNameGetter)1 ConstantMeasurementPacketPointCreator (me.retrodaredevil.solarthing.influxdb.influxdb1.ConstantMeasurementPacketPointCreator)1 InfluxDbPacketSaver (me.retrodaredevil.solarthing.influxdb.influxdb1.InfluxDbPacketSaver)1 InfluxDb2PacketSaver (me.retrodaredevil.solarthing.influxdb.infuxdb2.InfluxDb2PacketSaver)1 ConstantRetentionPolicyGetter (me.retrodaredevil.solarthing.influxdb.retention.ConstantRetentionPolicyGetter)1 FrequentRetentionPolicyGetter (me.retrodaredevil.solarthing.influxdb.retention.FrequentRetentionPolicyGetter)1 MqttPacketSaver (me.retrodaredevil.solarthing.mqtt.MqttPacketSaver)1 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)1 StoredPacketGroup (me.retrodaredevil.solarthing.packets.collection.StoredPacketGroup)1 FileWritePacketHandler (me.retrodaredevil.solarthing.packets.handling.implementations.FileWritePacketHandler)1 JacksonStringPacketHandler (me.retrodaredevil.solarthing.packets.handling.implementations.JacksonStringPacketHandler)1