Browse Source

Added API 'sensorIdUnitsGET'

Lukas Cerny 1 year ago
parent
commit
f791a7ddfb

+ 9 - 4
src/main/java/cz/senslog/telemetry/database/domain/CampaignUnit.java

@@ -6,16 +6,17 @@ import java.time.OffsetDateTime;
 public class CampaignUnit {
 
     private final long unitId;
-    private final String description;
+    private final String description, name;
     private final OffsetDateTime fromTime, toTime;
 
 
-    public static CampaignUnit of(long unitId, String description, OffsetDateTime fromTime, OffsetDateTime toTime) {
-        return new CampaignUnit(unitId, description, fromTime, toTime);
+    public static CampaignUnit of(long unitId, String name, String description, OffsetDateTime fromTime, OffsetDateTime toTime) {
+        return new CampaignUnit(unitId, name, description, fromTime, toTime);
     }
 
-    private CampaignUnit(long unitId, String description, OffsetDateTime fromTime, OffsetDateTime toTime) {
+    private CampaignUnit(long unitId, String name, String description, OffsetDateTime fromTime, OffsetDateTime toTime) {
         this.unitId = unitId;
+        this.name = name;
         this.description = description;
         this.fromTime = fromTime;
         this.toTime = toTime;
@@ -25,6 +26,10 @@ public class CampaignUnit {
         return unitId;
     }
 
+    public String getName() {
+        return name;
+    }
+
     public String getDescription() {
         return description;
     }

+ 13 - 1
src/main/java/cz/senslog/telemetry/database/repository/MapLogRepository.java

@@ -223,6 +223,7 @@ public class MapLogRepository implements SensLogRepository {
 
     private static final Function<Row, CampaignUnit> ROW_TO_CAMPAIGN_UNIT = (row) -> CampaignUnit.of(
             row.getLong("unit_id"),
+            row.getString("name"),
             row.getString("description"),
             row.getOffsetDateTime("from_time"),
             row.getOffsetDateTime("to_time")
@@ -230,7 +231,7 @@ public class MapLogRepository implements SensLogRepository {
 
     @Override
     public Future<List<CampaignUnit>> findUnitsByCampaignId(long campaignId) {
-        return client.preparedQuery("SELECT u.unit_id, u.description, utc.from_time, utc.to_time " +
+        return client.preparedQuery("SELECT u.unit_id, u.name, u.description, utc.from_time, utc.to_time " +
                         "FROM maplog.unit_to_campaign AS utc " +
                         "JOIN maplog.campaign c on c.campaign_id = utc.camp_id " +
                         "JOIN maplog.unit u on u.unit_id = utc.unit_id " +
@@ -293,6 +294,17 @@ public class MapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<List<Unit>> findUnitsBySensorId(long sensorId) {
+        return client.preparedQuery("SELECT u.unit_id, u.name, u.imei, u.description FROM maplog.unit_to_sensor AS uts " +
+                        "JOIN maplog.unit u on u.unit_id = uts.unit_id " +
+                        "WHERE uts.sensor_id = $1")
+                .execute(Tuple.of(sensorId))
+                .map(rs -> StreamSupport.stream(rs.spliterator(), false)
+                        .map(ROW_TO_UNIT).collect(Collectors.toList())
+                );
+    }
+
+    @Override
     public Future<List<Long>> findUnitIdsByCampaignId(long campaignId) {
         return client.preparedQuery("SELECT unit_id FROM maplog.unit_to_campaign WHERE camp_id = $1")
                 .execute(Tuple.of(campaignId))

+ 5 - 0
src/main/java/cz/senslog/telemetry/database/repository/MockMapLogRepository.java

@@ -62,6 +62,11 @@ public class MockMapLogRepository implements SensLogRepository {
     }
 
     @Override
+    public Future<List<Unit>> findUnitsBySensorId(long sensorId) {
+        return Future.succeededFuture(Collections.emptyList());
+    }
+
+    @Override
     public Future<Sensor> findSensorByIOAndUnitId(int ioID, long unitId) {
         return Future.succeededFuture(Sensor.of(12345, "no name", ioID));
     }

+ 1 - 0
src/main/java/cz/senslog/telemetry/database/repository/SensLogRepository.java

@@ -29,6 +29,7 @@ public interface SensLogRepository {
 
     Future<List<Sensor>> allSensors();
     Future<Sensor> findSensorById(long sensorId);
+    Future<List<Unit>> findUnitsBySensorId(long sensorId);
     Future<Sensor> findSensorByIOAndUnitId(int ioID, long unitId);
 
 

+ 1 - 0
src/main/java/cz/senslog/telemetry/server/HttpVertxServer.java

@@ -69,6 +69,7 @@ public final class HttpVertxServer extends AbstractVerticle {
 
                     openAPIRouterBuilder.operation("sensorsGET").handler(apiHandler::sensorsGET);
                     openAPIRouterBuilder.operation("sensorIdGET").handler(apiHandler::sensorIdGET);
+                    openAPIRouterBuilder.operation("sensorIdUnitsGET").handler(apiHandler::sensorIdUnitsGET);
 
                     Router mainRouter = openAPIRouterBuilder.createRouter();
 //                    mainRouter.route().handler(LoggerHandler.create());

+ 23 - 1
src/main/java/cz/senslog/telemetry/server/OpenAPIHandler.java

@@ -122,7 +122,8 @@ public class OpenAPIHandler {
                         units.stream().map(u -> (navigationLinks ? JsonObject.of(
                                 "Unit@NavigationLink", String.format("%s/units/%d",host, u.getUnitId())
                         ) : JsonObject.of()).mergeIn(JsonObject.of(
-                                "id", u.getUnitId(),
+                                "unitId", u.getUnitId(),
+                                "name", u.getUnitId(),
                                 "description", u.getDescription(),
                                 "fromTime", DATE_TIME_FORMATTER.apply(u.getFromTime(), zone),
                                 "toTime", DATE_TIME_FORMATTER.apply(u.getToTime(), zone)
@@ -563,4 +564,25 @@ public class OpenAPIHandler {
                         ))).collect(toList())).encode()))
                 .onFailure(th -> rc.fail(400, th));
     }
+
+    public void sensorIdUnitsGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        long sensorId = Long.parseLong(rc.pathParam("sensorId"));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findUnitsBySensorId(sensorId)
+                .onSuccess(units -> rc.response().end(new JsonArray(
+                        units.stream().map(u -> (navigationLinks ? JsonObject.of(
+                                "Unit@NavigationLink", String.format("%s/units/%d",host, u.getUnitId())
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "unitId", u.getUnitId(),
+                                "name", u.getName(),
+                                "description", u.getDescription()
+                        ))).collect(toList())
+                ).encode()))
+                .onFailure(th -> rc.fail(400, th));
+    }
 }

+ 32 - 23
src/main/resources/openAPISpec.yaml

@@ -7,7 +7,7 @@ servers:
   - url: https://theros.wirelessinfo.cz
 paths:
   /info:
-    get:
+    get: # done
       operationId: infoGET
       summary: Information about running instance
       responses:
@@ -25,7 +25,7 @@ paths:
                 $ref: "#/components/schemas/Error"
 
   /campaigns:
-    get:
+    get: # done
       operationId: campaignsGET
       summary: Publish info about all campaigns
       parameters:
@@ -63,7 +63,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}:
-    get:
+    get: # done
       operationId: campaignIdGET
       summary: Publish info about a campaign
       parameters:
@@ -106,7 +106,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}/units:
-    get:
+    get: # done
       operationId: campaignIdUnitsGET
       summary: Publish info about the campaign's units
       parameters:
@@ -151,7 +151,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}/units/observations:
-    get:
+    get: # done
       operationId: campaignIdUnitsObservationsGET
       summary: Publish info about all data of units merged together within the campaign
       parameters:
@@ -211,7 +211,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}/units/observations/locations:
-    get:
+    get: # done
       operationId: campaignIdUnitsObservationsLocationsGET
       summary: Publish info about all data of units merged together within the campaign
       parameters:
@@ -315,7 +315,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}/units/{unitId}/observations:
-    get:
+    get: # done
       operationId: campaignIdUnitIdObservationsGET
       summary: Publish info about all data of the unit within the campaign
       parameters:
@@ -383,7 +383,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /campaigns/{campaignId}/units/{unitId}/observations/location:
-    get:
+    get: # done
       operationId: campaignIdUnitIdLocationsGET
       summary: Publish locations of the unit within the campaign
       parameters:
@@ -560,7 +560,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /units:
-    get:
+    get: # done
       operationId: unitsGET
       summary: Publish info about all units
       parameters:
@@ -587,7 +587,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /units/{unitId}:
-    get:
+    get: # done
       operationId: unitIdGET
       summary: Publish info about the unit
       parameters:
@@ -613,7 +613,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /units/{unitId}/sensors:
-    get:
+    get: # done
       operationId: unitIdSensorsGET
       summary: Publish info about sensors assigned to the unit
       parameters:
@@ -647,7 +647,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /units/{unitId}/campaigns:
-    get:
+    get: # done
       operationId: unitIdCampaignsGET
       summary: Publish info about campaigns where the unit was/is assigned
       parameters:
@@ -687,7 +687,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /sensors:
-    get:
+    get: # done
       operationId: sensorsGET
       summary: Publish info about all sensors
       parameters:
@@ -714,7 +714,7 @@ paths:
                 $ref: '#/components/schemas/Error'
 
   /sensors/{sensorId}:
-    get:
+    get: # done
       operationId: sensorIdGET
       summary: Publish info about the sensor
       parameters:
@@ -745,18 +745,24 @@ paths:
               schema:
                 $ref: '#/components/schemas/Error'
 
-  /sensors/{id}/units:
+  /sensors/{sensorId}/units:
     get:
       operationId: sensorIdUnitsGET
       summary: Publish info about units to whom the sensor is assigned
       parameters:
         - in: path
-          name: id
+          name: sensorId
           schema:
             type: integer
             format: int64
           required: true
           description: Numeric ID of the sensor to get
+        - in: query
+          name: navigationLinks
+          schema:
+            type: boolean
+            default: true
+          description: Option to disable @NavigationLinks in a response
       responses:
         200:
           description: JSON Array of info about the units
@@ -914,17 +920,20 @@ components:
     CampaignUnitBasicInfo:
       type: object
       required:
-        - id
+        - unitId
+        - name
         - fromTime
         - toTime
       properties:
         Unit@NavigationLink:
           type: string
           format: uri
-        id:
+        unitId:
           description: Identifier of the unit
           type: integer
           format: int64
+        name:
+          type: string
         description:
           type: string
         fromTime:
@@ -937,7 +946,8 @@ components:
           format: date-time
       example:
         Unit@NavigationLink: "<domain>/units/25"
-        id: 25
+        unitId: 25
+        name: "Unit name"
         description: "Purpose of the Unit 25"
         fromTime: "2023-01-25 15:35:32Z"
         toTime: "2023-03-20 10:35:32Z"
@@ -1515,14 +1525,13 @@ components:
     SensorUnitBasicInfo:
       type: object
       required:
-        - Unit@NavigationLink
-        - id
+        - unitId
         - name
       properties:
         Unit@NavigationLink:
           type: string
           format: uri
-        id:
+        unitId:
           description: Identifier of the unit
           type: integer
           format: int64
@@ -1532,7 +1541,7 @@ components:
           type: string
       example:
         Unit@NavigationLink: "<domain>/units/25"
-        id: 25
+        unitId: 25
         name: "Mobile Unit"
         description: "Mobile Unit 25"