use of me.retrodaredevil.couchdbjava.CouchDbInstance in project solarthing by wildmountainfarms.
the class CouchDbSetupMain method createFrom.
public static CouchDbSetupMain createFrom(CouchDbDatabaseSettings settings) {
CouchDbInstance instance = CouchDbUtil.createInstance(settings.getCouchProperties(), settings.getOkHttpProperties());
Scanner scanner = new Scanner(System.in);
PrintStream out = System.out;
return new CouchDbSetupMain(instance, out, new ScannerPrompt(scanner));
}
use of me.retrodaredevil.couchdbjava.CouchDbInstance 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);
}
use of me.retrodaredevil.couchdbjava.CouchDbInstance in project solarthing by wildmountainfarms.
the class CacheHandlerProvider method init.
@PostConstruct
public void init() {
CouchDbInstance instance = CouchDbUtil.createInstance(couchDbDatabaseSettings.getCouchProperties(), couchDbDatabaseSettings.getOkHttpProperties());
cacheHandler = new CacheHandler(JacksonUtil.defaultMapper(), defaultInstanceOptions, instance);
}
use of me.retrodaredevil.couchdbjava.CouchDbInstance 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;
}
Aggregations