diff --git a/build.gradle b/build.gradle index 2eadc06..d85011d 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ def ossrhPassword = findProperty('ossrhPassword') apply plugin: 'biz.aQute.bnd.builder' group 'io.github.floriansw' -version '2.0.0' +version '3.0.0' archivesBaseName = 'ondus-api' compileJava { diff --git a/fake-api/index.js b/fake-api/index.js index bebdce0..30dd194 100644 --- a/fake-api/index.js +++ b/fake-api/index.js @@ -5,7 +5,7 @@ const dashboard = require('./responses/dashboard.json'); const location = require('./responses/locations/14521.json'); const room = require('./responses/locations/rooms/23547.json'); const guard = require('./responses/locations/rooms/appliances/550e8400-e29b-11d4-a716-446655440000.json'); -const guardData = require('./responses/locations/rooms/appliances/data/550e8400-e29b-11d4-a716-446655440000.json'); +const guardAggregatedData = require('./responses/locations/rooms/appliances/data/aggregated/550e8400-e29b-11d4-a716-446655440000.json'); const guardNotifications = require('./responses/locations/rooms/appliances/notifications/550e8400-e29b-11d4-a716-446655440000.json'); const guardCommand = require('./responses/locations/rooms/appliances/command/550e8400-e29b-11d4-a716-446655440000.json'); const blue = require('./responses/locations/rooms/appliances/550e8400-e29b-11d4-a716-446655440001.json'); @@ -76,9 +76,9 @@ const requestHandler = (request, response) => { ifAuthenticated(request, response, function (resp) { resp.write(JSON.stringify([guard])); }); - } else if (parsed.pathname === '/v3/iot/locations/14521/rooms/23547/appliances/550e8400-e29b-11d4-a716-446655440000/data' && request.method === 'GET') { + } else if (parsed.pathname === '/v3/iot/locations/14521/rooms/23547/appliances/550e8400-e29b-11d4-a716-446655440000/data/aggregated' && request.method === 'GET') { ifAuthenticated(request, response, function (resp) { - resp.write(JSON.stringify(guardData)); + resp.write(JSON.stringify(guardAggregatedData)); }); } else if (parsed.pathname === '/v3/iot/locations/14521/rooms/23547/appliances/550e8400-e29b-11d4-a716-446655440000/notifications' && request.method === 'GET') { ifAuthenticated(request, response, function (resp) { diff --git a/fake-api/responses/locations/rooms/appliances/data/550e8400-e29b-11d4-a716-446655440000.json b/fake-api/responses/locations/rooms/appliances/data/aggregated/550e8400-e29b-11d4-a716-446655440000.json similarity index 59% rename from fake-api/responses/locations/rooms/appliances/data/550e8400-e29b-11d4-a716-446655440000.json rename to fake-api/responses/locations/rooms/appliances/data/aggregated/550e8400-e29b-11d4-a716-446655440000.json index 66570f5..1e03737 100644 --- a/fake-api/responses/locations/rooms/appliances/data/550e8400-e29b-11d4-a716-446655440000.json +++ b/fake-api/responses/locations/rooms/appliances/data/aggregated/550e8400-e29b-11d4-a716-446655440000.json @@ -4,25 +4,25 @@ "data": { "measurement": [ { - "timestamp": "2019-02-25T11:02:37.000+01:00", + "date": "2019-02-25", "flowrate": 0, "pressure": 4.1, "temperature_guard": 14.5 }, { - "timestamp": "2019-02-25T11:17:37.000+01:00", + "date": "2019-02-25", "flowrate": 10.5, "pressure": 3.1, "temperature_guard": 14.5 }, { - "timestamp": "2019-02-25T11:32:37.000+01:00", + "date": "2019-02-25", "flowrate": 0, "pressure": 4.1, "temperature_guard": 14 }, { - "timestamp": "2019-02-25T11:47:37.000+01:00", + "date": "2019-02-25", "flowrate": 0, "pressure": 4, "temperature_guard": 14.5 @@ -30,37 +30,29 @@ ], "withdrawals": [ { - "starttime": "2019-02-25T10:35:34.000+01:00", - "stoptime": "2019-02-25T10:35:41.000+01:00", + "date": "2019-02-25", "waterconsumption": 0.8, - "maxflowrate": 7.7, "hotwater_share": 0.2, "water_cost": 0.003405, "energy_cost": 0.000954 }, { - "starttime": "2019-02-25T10:35:30.000+01:00", - "stoptime": "2019-02-25T10:35:30.000+01:00", + "date": "2019-02-25", "waterconsumption": 0, - "maxflowrate": 3.4, "hotwater_share": 0, "water_cost": 0, "energy_cost": 0 }, { - "starttime": "2019-02-25T11:01:34.000+01:00", - "stoptime": "2019-02-25T11:01:42.000+01:00", + "date": "2019-02-25", "waterconsumption": 0.9, - "maxflowrate": 7.7, "hotwater_share": 0.3, "water_cost": 0.00383, "energy_cost": 0.001074 }, { - "starttime": "2019-02-25T11:17:09.000+01:00", - "stoptime": "2019-02-25T11:18:13.000+01:00", + "date": "2019-02-25", "waterconsumption": 9, - "maxflowrate": 11, "hotwater_share": 0, "water_cost": 0.038304, "energy_cost": 0 diff --git a/src/main/java/io/github/floriansw/ondus/api/actions/ApplianceAction.java b/src/main/java/io/github/floriansw/ondus/api/actions/ApplianceAction.java index aeb5889..60b19ec 100644 --- a/src/main/java/io/github/floriansw/ondus/api/actions/ApplianceAction.java +++ b/src/main/java/io/github/floriansw/ondus/api/actions/ApplianceAction.java @@ -19,8 +19,8 @@ @NoArgsConstructor public class ApplianceAction extends AbstractAction { private static final String APPLIANCE_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s"; - private static final String APPLIANCE_DATA_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/data"; - private static final String APPLIANCE_DATA_WITH_RANGE_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/data?from=%s&to=%s"; + private static final String APPLIANCE_DATA_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/data/aggregated"; + private static final String APPLIANCE_DATA_WITH_RANGE_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/data/aggregated?from=%s&to=%s"; private static final String APPLIANCE_COMMAND_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/command"; private static final String APPLIANCE_STATUS_URL_TEMPLATE = "iot/locations/%d/rooms/%d/appliances/%s/status"; diff --git a/src/main/java/io/github/floriansw/ondus/api/model/DateDeserializer.java b/src/main/java/io/github/floriansw/ondus/api/model/DateDeserializer.java new file mode 100644 index 0000000..b1c3e15 --- /dev/null +++ b/src/main/java/io/github/floriansw/ondus/api/model/DateDeserializer.java @@ -0,0 +1,26 @@ +package io.github.floriansw.ondus.api.model; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; + +public class DateDeserializer extends JsonDeserializer { + private static final DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd H:m:s"); + private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + @Override + public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + String node = jp.readValueAs(String.class); + if (node.contains(" ")) { + return LocalDateTime.parse(node, dateTimeFormat); + } + return LocalDate.parse(node, dateFormat).atTime(0, 0); + } +} diff --git a/src/main/java/io/github/floriansw/ondus/api/model/guard/ApplianceData.java b/src/main/java/io/github/floriansw/ondus/api/model/guard/ApplianceData.java index 001ebfe..f690f37 100644 --- a/src/main/java/io/github/floriansw/ondus/api/model/guard/ApplianceData.java +++ b/src/main/java/io/github/floriansw/ondus/api/model/guard/ApplianceData.java @@ -2,14 +2,16 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import io.github.floriansw.ondus.api.model.BaseAppliance; import io.github.floriansw.ondus.api.model.BaseApplianceData; +import io.github.floriansw.ondus.api.model.DateDeserializer; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; @Getter @@ -41,8 +43,9 @@ public static final class Data { @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public static final class Measurement { - @JsonProperty("timestamp") - public String timestamp; + @JsonProperty("date") + @JsonDeserialize(using = DateDeserializer.class) + public LocalDateTime date; @JsonProperty("flowrate") public Float flowrate; @JsonProperty("pressure") @@ -56,13 +59,15 @@ public static final class Measurement { @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public static final class Withdrawals { - @JsonProperty("starttime") - public Date starttime; - @JsonProperty("stoptime") - public Date stoptime; + @JsonProperty("date") + public String date; @JsonProperty("waterconsumption") public Float waterconsumption; - @JsonProperty("maxflowrate") - public Float maxflowrate; + @JsonProperty("hotwater_share") + public Float hotWaterShare; + @JsonProperty("water_cost") + public Float waterCost; + @JsonProperty("energy_cost") + public Float energyCost; } } diff --git a/src/main/java/io/github/floriansw/ondus/api/model/sense/ApplianceData.java b/src/main/java/io/github/floriansw/ondus/api/model/sense/ApplianceData.java index c60ec68..c033906 100644 --- a/src/main/java/io/github/floriansw/ondus/api/model/sense/ApplianceData.java +++ b/src/main/java/io/github/floriansw/ondus/api/model/sense/ApplianceData.java @@ -2,13 +2,16 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import io.github.floriansw.ondus.api.model.BaseAppliance; import io.github.floriansw.ondus.api.model.BaseApplianceData; +import io.github.floriansw.ondus.api.model.DateDeserializer; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import java.time.LocalDateTime; import java.util.List; @Getter @@ -38,8 +41,9 @@ public static final class Data { @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public static final class Measurement { - @JsonProperty("timestamp") - public String timestamp; + @JsonProperty("date") + @JsonDeserialize(using = DateDeserializer.class) + public LocalDateTime date; @JsonProperty("temperature") public Float temperature; @JsonProperty("humidity") diff --git a/src/test/java/io/github/floriansw/ondus/api/SenseGuardIntegrationTest.java b/src/test/java/io/github/floriansw/ondus/api/SenseGuardIntegrationTest.java index bb1b5a1..76e76ad 100644 --- a/src/test/java/io/github/floriansw/ondus/api/SenseGuardIntegrationTest.java +++ b/src/test/java/io/github/floriansw/ondus/api/SenseGuardIntegrationTest.java @@ -69,9 +69,7 @@ public void applianceData() throws Exception { List withdrawals = ((ApplianceData) data).getData().getWithdrawals(); assertEquals(4, withdrawals.size()); assertEquals(0.8, withdrawals.get(0).waterconsumption, 0.0001); - assertEquals(7.7, withdrawals.get(0).maxflowrate, 0.0001); assertEquals(0, withdrawals.get(1).waterconsumption, 0.0001); - assertEquals(3.4, withdrawals.get(1).maxflowrate, 0.0001); List measurement = ((ApplianceData) data).getData().getMeasurement(); assertEquals(14.5, measurement.get(0).temperatureGuard, 0.0001); assertEquals(4.1, measurement.get(0).pressure, 0.0001);