use of org.eclipse.hono.util.RegistrationConstants.FIELD_DATA in project hono by eclipse.
the class FileBasedRegistrationService method saveToFile.
Future<Void> saveToFile() {
if (!getConfig().isSaveToFile()) {
return Future.succeededFuture();
} else if (dirty) {
return checkFileExists(true).compose(s -> {
final AtomicInteger idCount = new AtomicInteger();
final JsonArray tenants = new JsonArray();
for (Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
final JsonArray devices = new JsonArray();
for (Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
devices.add(new JsonObject().put(FIELD_PAYLOAD_DEVICE_ID, deviceEntry.getKey()).put(FIELD_DATA, deviceEntry.getValue()));
idCount.incrementAndGet();
}
tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
}
Future<Void> writeHandler = Future.future();
vertx.fileSystem().writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenants.encodePrettily()), writeHandler.completer());
return writeHandler.map(ok -> {
dirty = false;
log.trace("successfully wrote {} device identities to file {}", idCount.get(), getConfig().getFilename());
return (Void) null;
}).otherwise(t -> {
log.warn("could not write device identities to file {}", getConfig().getFilename(), t);
return (Void) null;
});
});
} else {
log.trace("registry does not need to be persisted");
return Future.succeededFuture();
}
}
Aggregations