|
|
@@ -1,6 +1,7 @@
|
|
|
package cz.senslog.telemetry.server;
|
|
|
|
|
|
import cz.senslog.telemetry.app.Application;
|
|
|
+import cz.senslog.telemetry.database.DataNotFoundException;
|
|
|
import cz.senslog.telemetry.database.SortType;
|
|
|
import cz.senslog.telemetry.database.repository.SensLogRepository;
|
|
|
import cz.senslog.telemetry.utils.TernaryCondition;
|
|
|
@@ -15,8 +16,7 @@ import org.apache.logging.log4j.Logger;
|
|
|
import java.time.OffsetDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
import java.util.List;
|
|
|
-import java.util.function.BiFunction;
|
|
|
-import java.util.function.Function;
|
|
|
+import java.util.function.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.lang.Boolean.parseBoolean;
|
|
|
@@ -47,6 +47,16 @@ public class OpenAPIHandler {
|
|
|
return String.format("%s://%s", req.scheme(), req.host());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private static final BiConsumer<Throwable, Consumer<Integer>> throwableToStatusCode = (th, handler) -> {
|
|
|
+ if (th instanceof DataNotFoundException) {
|
|
|
+ handler.accept(404);
|
|
|
+ } else {
|
|
|
+ handler.accept(400);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
public void info(RoutingContext rc) {
|
|
|
rc.response().end(JsonObject.of(
|
|
|
"name", Application.PROJECT_NAME,
|
|
|
@@ -101,8 +111,8 @@ public class OpenAPIHandler {
|
|
|
"description", c.getDescription(),
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(c.getFromTime(), zone),
|
|
|
"toTime", DATE_TIME_FORMATTER.apply(c.getToTime(), zone)
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitsGET(RoutingContext rc) {
|
|
|
@@ -127,9 +137,8 @@ public class OpenAPIHandler {
|
|
|
"description", u.getDescription(),
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(u.getFromTime(), zone),
|
|
|
"toTime", DATE_TIME_FORMATTER.apply(u.getToTime(), zone)
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitsObservationsGET(RoutingContext rc) {
|
|
|
@@ -205,9 +214,8 @@ public class OpenAPIHandler {
|
|
|
"latitude", o.getLocation().getLatitude(),
|
|
|
"altitude", o.getLocation().getAltitude()),
|
|
|
"observedValues", o.getObservedValues()
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdObservationsGET(RoutingContext rc) {
|
|
|
@@ -283,9 +291,8 @@ public class OpenAPIHandler {
|
|
|
"latitude", o.getLocation().getLatitude(),
|
|
|
"altitude", o.getLocation().getAltitude()),
|
|
|
"observedValues", o.getObservedValues()
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitsObservationsLocationsGET(RoutingContext rc) {
|
|
|
@@ -345,9 +352,8 @@ public class OpenAPIHandler {
|
|
|
l.getLocation().getLatitude(),
|
|
|
l.getLocation().getAltitude()
|
|
|
)
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdLocationsGET(RoutingContext rc) {
|
|
|
@@ -426,9 +432,8 @@ public class OpenAPIHandler {
|
|
|
l.getLocation().getLatitude(),
|
|
|
l.getLocation().getAltitude()
|
|
|
)
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
} else {
|
|
|
// TODO implement filter
|
|
|
/*
|
|
|
@@ -456,8 +461,8 @@ public class OpenAPIHandler {
|
|
|
"name", u.getName(),
|
|
|
"imei", u.getImei(),
|
|
|
"description", u.getDescription()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void unitIdSensorsGET(RoutingContext rc) {
|
|
|
@@ -476,9 +481,8 @@ public class OpenAPIHandler {
|
|
|
"sensorId", s.getSensorId(),
|
|
|
"name", s.getName(),
|
|
|
"type", s.getType()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void unitsGET(RoutingContext rc) {
|
|
|
@@ -496,7 +500,7 @@ public class OpenAPIHandler {
|
|
|
"name", u.getName(),
|
|
|
"description", u.getDescription()
|
|
|
))).collect(toList())).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void unitIdCampaignsGET(RoutingContext rc) {
|
|
|
@@ -520,7 +524,7 @@ public class OpenAPIHandler {
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(c.getFromTime(), zone),
|
|
|
"toTime", DATE_TIME_FORMATTER.apply(c.getToTime(), zone)
|
|
|
))).collect(toList())).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void sensorIdGET(RoutingContext rc) {
|
|
|
@@ -543,8 +547,8 @@ public class OpenAPIHandler {
|
|
|
"description", s.getDescription(),
|
|
|
"type", s.getType(),
|
|
|
"phenomenon", s.getPhenomenon().getName()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void sensorsGET(RoutingContext rc) {
|
|
|
@@ -562,7 +566,7 @@ public class OpenAPIHandler {
|
|
|
"name", s.getName(),
|
|
|
"description", s.getDescription()
|
|
|
))).collect(toList())).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void sensorIdUnitsGET(RoutingContext rc) {
|
|
|
@@ -581,9 +585,8 @@ public class OpenAPIHandler {
|
|
|
"unitId", u.getUnitId(),
|
|
|
"name", u.getName(),
|
|
|
"description", u.getDescription()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void phenomenonIdGET(RoutingContext rc) {
|
|
|
@@ -603,8 +606,8 @@ public class OpenAPIHandler {
|
|
|
"name", p.getName(),
|
|
|
"uom", p.getUom(),
|
|
|
"uomLink", p.getUomLink()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void phenomenonsGET(RoutingContext rc) {
|
|
|
@@ -621,7 +624,7 @@ public class OpenAPIHandler {
|
|
|
"id", p.getId(),
|
|
|
"name", p.getName()
|
|
|
))).collect(toList())).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void phenomenonIdSensorsGET(RoutingContext rc) {
|
|
|
@@ -640,9 +643,8 @@ public class OpenAPIHandler {
|
|
|
"sensorId", s.getSensorId(),
|
|
|
"name", s.getName(),
|
|
|
"type", s.getType()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdGET(RoutingContext rc) {
|
|
|
@@ -672,8 +674,8 @@ public class OpenAPIHandler {
|
|
|
"description", u.getDescription(),
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(u.getFromTime(), zone),
|
|
|
"toTime", DATE_TIME_FORMATTER.apply(u.getToTime(), zone)
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdSensorsGET(RoutingContext rc) {
|
|
|
@@ -693,9 +695,8 @@ public class OpenAPIHandler {
|
|
|
"sensorId", s.getSensorId(),
|
|
|
"name", s.getName(),
|
|
|
"type", s.getType()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdSensorIdGET(RoutingContext rc) {
|
|
|
@@ -722,8 +723,8 @@ public class OpenAPIHandler {
|
|
|
"description", s.getDescription(),
|
|
|
"type", s.getType(),
|
|
|
"phenomenon", s.getPhenomenon().getName()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void campaignIdUnitIdSensorIdObservationsGET(RoutingContext rc) {
|
|
|
@@ -800,9 +801,8 @@ public class OpenAPIHandler {
|
|
|
"longitude", o.getLocation().getLongitude(),
|
|
|
"latitude", o.getLocation().getLatitude(),
|
|
|
"altitude", o.getLocation().getAltitude())
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driversGET(RoutingContext rc) {
|
|
|
@@ -819,7 +819,7 @@ public class OpenAPIHandler {
|
|
|
"id", d.getId(),
|
|
|
"name", d.getName()
|
|
|
))).collect(toList())).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdGET(RoutingContext rc) {
|
|
|
@@ -838,8 +838,8 @@ public class OpenAPIHandler {
|
|
|
) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
"id", d.getId(),
|
|
|
"name", d.getName()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdUnitsGET(RoutingContext rc) {
|
|
|
@@ -864,9 +864,8 @@ public class OpenAPIHandler {
|
|
|
"unitId", u.getUnitId(),
|
|
|
"name", u.getName(),
|
|
|
"description", u.getDescription()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdUnitIdGET(RoutingContext rc) {
|
|
|
@@ -888,8 +887,8 @@ public class OpenAPIHandler {
|
|
|
"name", u.getName(),
|
|
|
"imei", u.getImei(),
|
|
|
"description", u.getDescription()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdUnitIdActionsGET(RoutingContext rc) {
|
|
|
@@ -908,9 +907,8 @@ public class OpenAPIHandler {
|
|
|
) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
"id", a.getId(),
|
|
|
"name", a.getName()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdActionsGET(RoutingContext rc) {
|
|
|
@@ -928,9 +926,8 @@ public class OpenAPIHandler {
|
|
|
) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
"id", a.getId(),
|
|
|
"name", a.getName()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdActionIdGET(RoutingContext rc) {
|
|
|
@@ -950,8 +947,8 @@ public class OpenAPIHandler {
|
|
|
) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
"id", a.getId(),
|
|
|
"name", a.getName()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdActionIdUnitsGET(RoutingContext rc) {
|
|
|
@@ -971,9 +968,8 @@ public class OpenAPIHandler {
|
|
|
"unitId", u.getUnitId(),
|
|
|
"name", u.getName(),
|
|
|
"description", u.getDescription()
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdActionIdUnitIdGET(RoutingContext rc) {
|
|
|
@@ -997,8 +993,8 @@ public class OpenAPIHandler {
|
|
|
"name", u.getName(),
|
|
|
"imei", u.getImei(),
|
|
|
"description", u.getDescription()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdUnitIdActionIdGET(RoutingContext rc) {
|
|
|
@@ -1020,8 +1016,8 @@ public class OpenAPIHandler {
|
|
|
) : JsonObject.of()).mergeIn(JsonObject.of(
|
|
|
"id", a.getId(),
|
|
|
"name", a.getName()
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void driverIdUnitIdActionIdEventsGET(RoutingContext rc) {
|
|
|
@@ -1045,9 +1041,8 @@ public class OpenAPIHandler {
|
|
|
"id", e.getId(),
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(e.getFromTime(), zone),
|
|
|
"description", DATE_TIME_FORMATTER.apply(e.getToTime(), zone)
|
|
|
- ))).collect(toList())
|
|
|
- ).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ ))).collect(toList())).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void eventIdGET(RoutingContext rc) {
|
|
|
@@ -1076,8 +1071,8 @@ public class OpenAPIHandler {
|
|
|
"unitId", e.getUnitId(),
|
|
|
"fromTime", DATE_TIME_FORMATTER.apply(e.getFromTime(), zone),
|
|
|
"description", DATE_TIME_FORMATTER.apply(e.getToTime(), zone)
|
|
|
- )).encode())
|
|
|
- .onFailure(th -> rc.fail(400, th)));
|
|
|
+ )).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void eventIdObservationsGET(RoutingContext rc) {
|
|
|
@@ -1152,9 +1147,8 @@ public class OpenAPIHandler {
|
|
|
"latitude", o.getLocation().getLatitude(),
|
|
|
"altitude", o.getLocation().getAltitude()),
|
|
|
"observedValues", o.getObservedValues()
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
|
|
|
public void eventIdLocationsGET(RoutingContext rc) {
|
|
|
@@ -1228,8 +1222,7 @@ public class OpenAPIHandler {
|
|
|
l.getLocation().getLatitude(),
|
|
|
l.getLocation().getAltitude()
|
|
|
)
|
|
|
- )).collect(toList()))
|
|
|
- )).encode()))
|
|
|
- .onFailure(th -> rc.fail(400, th));
|
|
|
+ )).collect(toList())))).encode()))
|
|
|
+ .onFailure(th -> throwableToStatusCode.accept(th, code -> rc.fail(code, th)));
|
|
|
}
|
|
|
}
|