Skip to content

Commit

Permalink
save and load altitude via API
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Aug 24, 2024
1 parent f0539d4 commit a61028c
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 115 deletions.
128 changes: 128 additions & 0 deletions src/main/java/ru/r2cloud/model/GeneralConfiguration.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
31 changes: 31 additions & 0 deletions src/main/java/ru/r2cloud/util/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DeviceConfiguration> getPlutoSdrConfigurations() {
DeviceType deviceType = DeviceType.PLUTOSDR;
List<String> loraDevices = getProperties(deviceType.name().toLowerCase(Locale.UK) + ".devices");
Expand Down
39 changes: 12 additions & 27 deletions src/main/java/ru/r2cloud/web/api/configuration/General.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,45 +31,30 @@ 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;
}

@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();
}
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/ru/r2cloud/it/GeneralConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -33,7 +34,7 @@ public void testConfiguration() {
public void testInvalidLat() {
GeneralConfiguration config = createConfig();
config.setLat(null);
HttpResponse<String> response = client.setGeneralConfigurationWithResponse(config);
HttpResponse<String> response = client.saveGeneralConfigurationWithResponse(config);
assertEquals(400, response.statusCode());
assertErrorInField("lat", response);
}
Expand All @@ -42,7 +43,7 @@ public void testInvalidLat() {
public void testInvalidLng() {
GeneralConfiguration config = createConfig();
config.setLng(null);
HttpResponse<String> response = client.setGeneralConfigurationWithResponse(config);
HttpResponse<String> response = client.saveGeneralConfigurationWithResponse(config);
assertEquals(400, response.statusCode());
assertErrorInField("lng", response);
}
Expand All @@ -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);
Expand Down
22 changes: 4 additions & 18 deletions src/test/java/ru/r2cloud/it/util/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,29 +349,15 @@ public HttpResponse<String> saveDeviceConfig(JsonObject device) {
}
}

public void setGeneralConfiguration(GeneralConfiguration config) {
HttpResponse<String> response = setGeneralConfigurationWithResponse(config);
public void saveGeneralConfiguration(GeneralConfiguration config) {
HttpResponse<String> response = saveGeneralConfigurationWithResponse(config);
if (response.statusCode() != 200) {
throw new RuntimeException("invalid status code: " + response.statusCode());
}
}

public HttpResponse<String> 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<String> saveGeneralConfigurationWithResponse(GeneralConfiguration config) {
JsonObject json = config.toJson();
HttpRequest request = createJsonPost("/api/v1/admin/config/general", json).build();
try {
return httpclient.send(request, BodyHandlers.ofString());
Expand Down
60 changes: 0 additions & 60 deletions src/test/java/ru/r2cloud/model/GeneralConfiguration.java

This file was deleted.

0 comments on commit a61028c

Please sign in to comment.