use of me.retrodaredevil.solarthing.mqtt.MqttPacketSaver 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);
}
Aggregations