From a61028c5c0012733652eca6e35f2008ca088aba6 Mon Sep 17 00:00:00 2001 From: dernasherbrezon Date: Sat, 24 Aug 2024 17:52:41 +0100 Subject: [PATCH] save and load altitude via API --- .../r2cloud/model/GeneralConfiguration.java | 128 ++++++++++++++++++ .../java/ru/r2cloud/util/Configuration.java | 31 +++++ .../web/api/configuration/General.java | 39 ++---- .../r2cloud/it/GeneralConfigurationTest.java | 22 +-- .../java/ru/r2cloud/it/util/RestClient.java | 22 +-- .../r2cloud/model/GeneralConfiguration.java | 60 -------- 6 files changed, 187 insertions(+), 115 deletions(-) create mode 100644 src/main/java/ru/r2cloud/model/GeneralConfiguration.java delete mode 100644 src/test/java/ru/r2cloud/model/GeneralConfiguration.java diff --git a/src/main/java/ru/r2cloud/model/GeneralConfiguration.java b/src/main/java/ru/r2cloud/model/GeneralConfiguration.java new file mode 100644 index 00000000..e1fd52bd --- /dev/null +++ b/src/main/java/ru/r2cloud/model/GeneralConfiguration.java @@ -0,0 +1,128 @@ +package ru.r2cloud.model; + +import com.eclipsesource.json.JsonObject; +import com.eclipsesource.json.JsonValue; + +public class GeneralConfiguration { + + private boolean locationAuto; + private Double lat; + private Double lng; + private Double alt; + private boolean autoUpdate; + private boolean presentationMode; + private Integer retentionRawCount; + private Long retentionMaxSizeBytes; + + public boolean isLocationAuto() { + return locationAuto; + } + + public void setLocationAuto(boolean locationAuto) { + this.locationAuto = locationAuto; + } + + public Long getRetentionMaxSizeBytes() { + return retentionMaxSizeBytes; + } + + public void setRetentionMaxSizeBytes(Long retentionMaxSizeBytes) { + this.retentionMaxSizeBytes = retentionMaxSizeBytes; + } + + public Integer getRetentionRawCount() { + return retentionRawCount; + } + + public void setRetentionRawCount(Integer retentionRawCount) { + this.retentionRawCount = retentionRawCount; + } + + public Double getLat() { + return lat; + } + + public void setLat(Double lat) { + this.lat = lat; + } + + public Double getLng() { + return lng; + } + + public void setLng(Double lng) { + this.lng = lng; + } + + public Double getAlt() { + return alt; + } + + public void setAlt(Double alt) { + this.alt = alt; + } + + public boolean isAutoUpdate() { + return autoUpdate; + } + + public void setAutoUpdate(boolean autoUpdate) { + this.autoUpdate = autoUpdate; + } + + public boolean isPresentationMode() { + return presentationMode; + } + + public void setPresentationMode(boolean presentationMode) { + this.presentationMode = presentationMode; + } + + public JsonObject toJson() { + JsonObject result = new JsonObject(); + result.add("locationAuto", locationAuto); + if (lat != null) { + result.add("lat", lat); + } + if (lng != null) { + result.add("lng", lng); + } + if (alt != null) { + result.add("alt", alt); + } + result.add("autoUpdate", autoUpdate); + result.add("presentationMode", presentationMode); + result.add("retentionRawCount", retentionRawCount); + result.add("retentionMaxSizeBytes", retentionMaxSizeBytes); + return result; + } + + public static GeneralConfiguration fromJson(JsonObject meta) { + GeneralConfiguration result = new GeneralConfiguration(); + result.setLocationAuto(meta.getBoolean("locationAuto", false)); + JsonValue lat = meta.get("lat"); + if (lat != null) { + result.setLat(lat.asDouble()); + } + JsonValue lng = meta.get("lng"); + if (lng != null) { + result.setLng(lng.asDouble()); + } + JsonValue alt = meta.get("alt"); + if (alt != null) { + result.setAlt(alt.asDouble()); + } + result.setAutoUpdate(meta.getBoolean("autoUpdate", false)); + result.setPresentationMode(meta.getBoolean("presentationMode", false)); + JsonValue retentionRawCount = meta.get("retentionRawCount"); + if (retentionRawCount != null) { + result.setRetentionRawCount(retentionRawCount.asInt()); + } + JsonValue retentionMaxSizeBytes = meta.get("retentionMaxSizeBytes"); + if (retentionMaxSizeBytes != null) { + result.setRetentionMaxSizeBytes(retentionMaxSizeBytes.asLong()); + } + return result; + } + +} diff --git a/src/main/java/ru/r2cloud/util/Configuration.java b/src/main/java/ru/r2cloud/util/Configuration.java index d55268cc..ad9a958e 100644 --- a/src/main/java/ru/r2cloud/util/Configuration.java +++ b/src/main/java/ru/r2cloud/util/Configuration.java @@ -30,6 +30,7 @@ import ru.r2cloud.model.DemodulatorType; import ru.r2cloud.model.DeviceConfiguration; import ru.r2cloud.model.DeviceType; +import ru.r2cloud.model.GeneralConfiguration; import ru.r2cloud.model.IntegrationConfiguration; import ru.r2cloud.model.Modulation; import ru.r2cloud.model.RotatorConfiguration; @@ -291,6 +292,36 @@ public void saveIntegrationConfiguration(IntegrationConfiguration config) { setProperty("influxdb.database", config.getInfluxdbDatabase()); } + public GeneralConfiguration getGeneralConfiguration() { + GeneralConfiguration result = new GeneralConfiguration(); + result.setAlt(getDouble("locaiton.alt")); + result.setLat(getDouble("locaiton.lat")); + result.setLng(getDouble("locaiton.lon")); + result.setLocationAuto(getBoolean("location.auto")); + result.setPresentationMode(getBoolean("presentationMode")); + result.setRetentionMaxSizeBytes(getLong("scheduler.data.retention.maxSizeBytes")); + result.setRetentionRawCount(getInteger("scheduler.data.retention.raw.count")); + return result; + } + + public void saveGeneralConfiguration(GeneralConfiguration config) { + setProperty("location.auto", config.isLocationAuto()); + if (!config.isLocationAuto()) { + setProperty("locaiton.lat", config.getLat()); + setProperty("locaiton.lon", config.getLng()); + if (config.getAlt() != null) { + setProperty("locaiton.alt", config.getAlt()); + } + } + setProperty("presentationMode", config.isPresentationMode()); + if (config.getRetentionRawCount() != null) { + setProperty("scheduler.data.retention.raw.count", config.getRetentionRawCount()); + } + if (config.getRetentionMaxSizeBytes() != null) { + setProperty("scheduler.data.retention.maxSizeBytes", config.getRetentionMaxSizeBytes()); + } + } + public List getPlutoSdrConfigurations() { DeviceType deviceType = DeviceType.PLUTOSDR; List loraDevices = getProperties(deviceType.name().toLowerCase(Locale.UK) + ".devices"); diff --git a/src/main/java/ru/r2cloud/web/api/configuration/General.java b/src/main/java/ru/r2cloud/web/api/configuration/General.java index 57f0e54f..a20830a7 100644 --- a/src/main/java/ru/r2cloud/web/api/configuration/General.java +++ b/src/main/java/ru/r2cloud/web/api/configuration/General.java @@ -7,13 +7,13 @@ import fi.iki.elonen.NanoHTTPD.IHTTPSession; import ru.r2cloud.AutoUpdate; +import ru.r2cloud.model.GeneralConfiguration; import ru.r2cloud.util.Configuration; import ru.r2cloud.web.AbstractHttpController; import ru.r2cloud.web.BadRequest; import ru.r2cloud.web.ModelAndView; import ru.r2cloud.web.Success; import ru.r2cloud.web.ValidationResult; -import ru.r2cloud.web.WebServer; import ru.r2cloud.web.api.Messages; public class General extends AbstractHttpController { @@ -31,13 +31,8 @@ public General(Configuration config, AutoUpdate autoUpdate) { @Override public ModelAndView doGet(IHTTPSession session) { ModelAndView result = new ModelAndView(); - JsonObject entity = new JsonObject(); - entity.add("lat", config.getDouble("locaiton.lat")); - entity.add("lng", config.getDouble("locaiton.lon")); + JsonObject entity = config.getGeneralConfiguration().toJson(); entity.add("autoUpdate", autoUpdate.isEnabled()); - entity.add("presentationMode", config.getBoolean("presentationMode")); - entity.add("retentionRawCount", config.getInteger("scheduler.data.retention.raw.count")); - entity.add("retentionMaxSizeBytes", config.getLong("scheduler.data.retention.maxSizeBytes")); result.setData(entity.toString()); return result; } @@ -45,31 +40,21 @@ public ModelAndView doGet(IHTTPSession session) { @Override public ModelAndView doPost(JsonObject request) { ValidationResult errors = new ValidationResult(); - Double lat = WebServer.getDouble(request, "lat"); - Double lon = WebServer.getDouble(request, "lng"); - Integer retentionRawCount = WebServer.getInteger(request, "retentionRawCount"); - Long retentionMaxSizeBytes = WebServer.getLong(request, "retentionMaxSizeBytes"); - boolean presentationMode = WebServer.getBoolean(request, "presentationMode"); - if (lat == null) { - errors.put("lat", Messages.CANNOT_BE_EMPTY); - } - if (lon == null) { - errors.put("lng", Messages.CANNOT_BE_EMPTY); + GeneralConfiguration generalConfig = GeneralConfiguration.fromJson(request); + if (!generalConfig.isLocationAuto()) { + if (generalConfig.getLat() == null) { + errors.put("lat", Messages.CANNOT_BE_EMPTY); + } + if (generalConfig.getLng() == null) { + errors.put("lng", Messages.CANNOT_BE_EMPTY); + } } if (!errors.isEmpty()) { LOG.info("unable to save: {}", errors); return new BadRequest(errors); } - autoUpdate.setEnabled(WebServer.getBoolean(request, "autoUpdate")); - config.setProperty("locaiton.lat", lat); - config.setProperty("locaiton.lon", lon); - config.setProperty("presentationMode", presentationMode); - if (retentionRawCount != null) { - config.setProperty("scheduler.data.retention.raw.count", retentionRawCount); - } - if (retentionMaxSizeBytes != null) { - config.setProperty("scheduler.data.retention.maxSizeBytes", retentionMaxSizeBytes); - } + autoUpdate.setEnabled(generalConfig.isAutoUpdate()); + config.saveGeneralConfiguration(generalConfig); config.update(); return new Success(); } diff --git a/src/test/java/ru/r2cloud/it/GeneralConfigurationTest.java b/src/test/java/ru/r2cloud/it/GeneralConfigurationTest.java index 9e56b4f1..e611344d 100644 --- a/src/test/java/ru/r2cloud/it/GeneralConfigurationTest.java +++ b/src/test/java/ru/r2cloud/it/GeneralConfigurationTest.java @@ -16,14 +16,15 @@ public class GeneralConfigurationTest extends RegisteredTest { @Test public void testConfiguration() { GeneralConfiguration config = createConfig(); - client.setGeneralConfiguration(config); - JsonObject configuration = client.getGeneralConfiguration(); - assertEquals(config.getLat(), configuration.getDouble("lat", 0.0), 0.0); - assertEquals(config.getLng(), configuration.getDouble("lng", 0.0), 0.0); - assertEquals(config.isAutoUpdate(), configuration.getBoolean("autoUpdate", !config.isAutoUpdate())); - assertEquals(config.isPresentationMode(), configuration.getBoolean("presentationMode", false)); - assertEquals(config.getRetentionRawCount().intValue(), configuration.getInt("retentionRawCount", 0)); - assertEquals(config.getRetentionMaxSizeBytes().longValue(), configuration.getLong("retentionMaxSizeBytes", 0)); + client.saveGeneralConfiguration(config); + GeneralConfiguration actual = GeneralConfiguration.fromJson(client.getGeneralConfiguration()); + assertEquals(config.getLat(), actual.getLat(), 0.0); + assertEquals(config.getLng(), actual.getLng(), 0.0); + assertEquals(config.getAlt(), actual.getAlt(), 0.0); + assertEquals(config.isAutoUpdate(), actual.isAutoUpdate()); + assertEquals(config.isPresentationMode(), actual.isPresentationMode()); + assertEquals(config.getRetentionRawCount().intValue(), actual.getRetentionRawCount().intValue()); + assertEquals(config.getRetentionMaxSizeBytes().longValue(), actual.getRetentionMaxSizeBytes().longValue()); JsonObject configured = client.getConfigured(); assertEquals(config.isPresentationMode(), configured.getBoolean("presentationMode", false)); @@ -33,7 +34,7 @@ public void testConfiguration() { public void testInvalidLat() { GeneralConfiguration config = createConfig(); config.setLat(null); - HttpResponse response = client.setGeneralConfigurationWithResponse(config); + HttpResponse response = client.saveGeneralConfigurationWithResponse(config); assertEquals(400, response.statusCode()); assertErrorInField("lat", response); } @@ -42,7 +43,7 @@ public void testInvalidLat() { public void testInvalidLng() { GeneralConfiguration config = createConfig(); config.setLng(null); - HttpResponse response = client.setGeneralConfigurationWithResponse(config); + HttpResponse response = client.saveGeneralConfigurationWithResponse(config); assertEquals(400, response.statusCode()); assertErrorInField("lng", response); } @@ -51,6 +52,7 @@ private static GeneralConfiguration createConfig() { GeneralConfiguration config = new GeneralConfiguration(); config.setLat(10.1); config.setLng(23.4); + config.setAlt(45.4); config.setAutoUpdate(true); config.setPresentationMode(true); config.setRetentionRawCount(10); diff --git a/src/test/java/ru/r2cloud/it/util/RestClient.java b/src/test/java/ru/r2cloud/it/util/RestClient.java index 2d2d9c85..9223c2ca 100644 --- a/src/test/java/ru/r2cloud/it/util/RestClient.java +++ b/src/test/java/ru/r2cloud/it/util/RestClient.java @@ -349,29 +349,15 @@ public HttpResponse saveDeviceConfig(JsonObject device) { } } - public void setGeneralConfiguration(GeneralConfiguration config) { - HttpResponse response = setGeneralConfigurationWithResponse(config); + public void saveGeneralConfiguration(GeneralConfiguration config) { + HttpResponse response = saveGeneralConfigurationWithResponse(config); if (response.statusCode() != 200) { throw new RuntimeException("invalid status code: " + response.statusCode()); } } - public HttpResponse setGeneralConfigurationWithResponse(GeneralConfiguration config) { - JsonObject json = Json.object(); - if (config.getLat() != null) { - json.add("lat", config.getLat()); - } - if (config.getLng() != null) { - json.add("lng", config.getLng()); - } - json.add("autoUpdate", config.isAutoUpdate()); - json.add("presentationMode", config.isPresentationMode()); - if (config.getRetentionRawCount() != null) { - json.add("retentionRawCount", config.getRetentionRawCount()); - } - if (config.getRetentionMaxSizeBytes() != null) { - json.add("retentionMaxSizeBytes", config.getRetentionMaxSizeBytes()); - } + public HttpResponse saveGeneralConfigurationWithResponse(GeneralConfiguration config) { + JsonObject json = config.toJson(); HttpRequest request = createJsonPost("/api/v1/admin/config/general", json).build(); try { return httpclient.send(request, BodyHandlers.ofString()); diff --git a/src/test/java/ru/r2cloud/model/GeneralConfiguration.java b/src/test/java/ru/r2cloud/model/GeneralConfiguration.java deleted file mode 100644 index e08953bb..00000000 --- a/src/test/java/ru/r2cloud/model/GeneralConfiguration.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.r2cloud.model; - -public class GeneralConfiguration { - - private Double lat; - private Double lng; - private boolean autoUpdate; - private boolean presentationMode; - private Integer retentionRawCount; - private Long retentionMaxSizeBytes; - - public Long getRetentionMaxSizeBytes() { - return retentionMaxSizeBytes; - } - - public void setRetentionMaxSizeBytes(Long retentionMaxSizeBytes) { - this.retentionMaxSizeBytes = retentionMaxSizeBytes; - } - - public Integer getRetentionRawCount() { - return retentionRawCount; - } - - public void setRetentionRawCount(Integer retentionRawCount) { - this.retentionRawCount = retentionRawCount; - } - - public Double getLat() { - return lat; - } - - public void setLat(Double lat) { - this.lat = lat; - } - - public Double getLng() { - return lng; - } - - public void setLng(Double lng) { - this.lng = lng; - } - - public boolean isAutoUpdate() { - return autoUpdate; - } - - public void setAutoUpdate(boolean autoUpdate) { - this.autoUpdate = autoUpdate; - } - - public boolean isPresentationMode() { - return presentationMode; - } - - public void setPresentationMode(boolean presentationMode) { - this.presentationMode = presentationMode; - } - -}