Переглянути джерело

Added API 'unitIdSensorsGET'

Lukas Cerny 1 рік тому
батько
коміт
1659263928

+ 1 - 3
sql/init.sql

@@ -121,11 +121,9 @@ ALTER TABLE ONLY maplog.phenomenon ALTER COLUMN phenomenon_id SET DEFAULT nextva
 
 CREATE TABLE maplog.sensor (
     sensor_id BIGINT NOT NULL PRIMARY KEY,
-    sensor_name CHARACTER VARYING(100) UNIQUE,
+    sensor_name CHARACTER VARYING(100) UNIQUE NOT NULL,
     sensor_type TEXT,
     io_id INTEGER,
-    min_range TEXT,
-    max_range TEXT,
     phenomenon_id INTEGER NOT NULL
 );
 ALTER TABLE maplog.sensor OWNER TO senslog;

+ 17 - 3
src/main/java/cz/senslog/telemetry/database/domain/Sensor.java

@@ -4,16 +4,24 @@ public class Sensor {
 
     private final long sensorId;
     private final String name;
+    private final String type;
     private final int ioID;
+    private final int phenomenonId;
+
+    public static Sensor of(long sensorId, String name, String type, int ioID, int phenomenonId) {
+        return new Sensor(sensorId, name, type, ioID, phenomenonId);
+    }
 
     public static Sensor of(long sensorId, String name, int ioId) {
-        return new Sensor(sensorId, name, ioId);
+        return of(sensorId, name, null, ioId, -1);
     }
 
-    private Sensor(long sensorId, String name, int ioID) {
+    private Sensor(long sensorId, String name, String type, int ioID, int phenomenonId) {
         this.sensorId = sensorId;
         this.name = name;
+        this.type = type;
         this.ioID = ioID;
+        this.phenomenonId = phenomenonId;
     }
 
     public long getSensorId() {
@@ -24,9 +32,15 @@ public class Sensor {
         return name;
     }
 
+    public String getType() {
+        return type;
+    }
+
     public int getIoID() {
         return ioID;
     }
 
-
+    public int getPhenomenonId() {
+        return phenomenonId;
+    }
 }

+ 7 - 3
src/main/java/cz/senslog/telemetry/database/repository/MapLogRepository.java

@@ -136,12 +136,15 @@ public class MapLogRepository implements SensLogRepository {
     private static final Function<Row, Sensor> ROW_TO_SENSOR = (row) -> Sensor.of(
             row.getLong("sensor_id"),
             row.getString("sensor_name"),
-            row.getInteger("io_id")
+            row.getString("sensor_type"),
+            row.getInteger("io_id"),
+            row.getInteger("phenomenon_id")
     );
 
     @Override
     public Future<Sensor> findSensorByIOAndUnitId(int ioID, long unitId) {
-        return client.preparedQuery("SELECT s.sensor_id, s.sensor_name, s.io_id FROM maplog.sensor AS s " +
+        return client.preparedQuery("SELECT s.sensor_id, s.sensor_name, s.sensor_type, s.io_id, s.phenomenon_id " +
+                        "FROM maplog.sensor AS s " +
                         "JOIN maplog.unit_to_sensor uts ON s.sensor_id = uts.sensor_id " +
                         "WHERE s.io_id = $1 AND uts.unit_id = $2")
                 .execute(Tuple.of(ioID, unitId))
@@ -151,7 +154,8 @@ public class MapLogRepository implements SensLogRepository {
 
     @Override
     public Future<List<Sensor>> findSensorsByUnitId(long unitId) {
-        return client.preparedQuery("SELECT s.sensor_id, s.sensor_name, s.io_id FROM maplog.unit_to_sensor AS uts " +
+        return client.preparedQuery("SELECT s.sensor_id, s.sensor_name, s.sensor_type, s.io_id, s.phenomenon_id " +
+                        "FROM maplog.unit_to_sensor AS uts " +
                         "JOIN maplog.sensor s on s.sensor_id = uts.sensor_id " +
                         "WHERE UTS.unit_id = $1")
                 .execute(Tuple.of(unitId))

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

@@ -63,6 +63,7 @@ public final class HttpVertxServer extends AbstractVerticle {
                     openAPIRouterBuilder.operation("campaignIdUnitIdLocationsGET").handler(apiHandler::campaignIdUnitIdLocationsGET);
 
                     openAPIRouterBuilder.operation("unitIdGET").handler(apiHandler::unitIdGET);
+                    openAPIRouterBuilder.operation("unitIdSensorsGET").handler(apiHandler::unitIdSensorsGET);
 
                     Router mainRouter = openAPIRouterBuilder.createRouter();
 //                    mainRouter.route().handler(LoggerHandler.create());

+ 21 - 0
src/main/java/cz/senslog/telemetry/server/OpenAPIHandler.java

@@ -458,4 +458,25 @@ public class OpenAPIHandler {
                         )).encode())
                         .onFailure(th -> rc.fail(400, th)));
     }
+
+    public void unitIdSensorsGET(RoutingContext rc) {
+        String host =  hostURLFull(rc.request());
+
+        long unitId = Long.parseLong(rc.pathParam("unitId"));
+
+        List<String> paramNavigationLinks = rc.queryParam("navigationLinks");
+        boolean navigationLinks = paramNavigationLinks.isEmpty() ? DEFAULT_NAVIGATION_LINKS : parseBoolean(paramNavigationLinks.get(0));
+
+        repo.findSensorsByUnitId(unitId)
+                .onSuccess(sensors -> rc.response().end(new JsonArray(
+                        sensors.stream().map(s -> (navigationLinks ? JsonObject.of(
+                                "Sensor@NavigationLink", String.format("%s/sensors/%d",host, s.getSensorId())
+                        ) : JsonObject.of()).mergeIn(JsonObject.of(
+                                "id", s.getSensorId(),
+                                "name", s.getName(),
+                                "type", s.getType()
+                        ))).collect(toList())
+                ).encode()))
+                .onFailure(th -> rc.fail(400, th));
+    }
 }

+ 10 - 11
src/main/resources/openAPISpec.yaml

@@ -605,18 +605,24 @@ paths:
               schema:
                 $ref: '#/components/schemas/Error'
 
-  /units/{id}/sensors:
+  /units/{unitId}/sensors:
     get:
       operationId: unitIdSensorsGET
       summary: Publish info about sensors assigned to the unit
       parameters:
         - in: path
-          name: id
+          name: unitId
           schema:
             type: integer
             format: int64
           required: true
           description: Numeric ID of the unit 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 of the sensors
@@ -1360,15 +1366,9 @@ components:
     UnitSensorBasicInfo:
       type: object
       required:
-        - Unit@NavigationLink
-        - Sensor@NavigationLink
         - id
         - name
       properties:
-        Unit@NavigationLink:
-          description: Navigation link to the unit
-          type: string
-          format: uri
         Sensor@NavigationLink:
           description: Navigation link to detail info of the sensor
           type: string
@@ -1379,14 +1379,13 @@ components:
           format: int64
         name:
           type: string
-        description:
+        type:
           type: string
       example:
-        Unit@NavigationLink: "<domain>/units/25"
         Sensor@NavigationLink: "<domain>/sensors/105"
         id: 105
         name: "Sensor 105"
-        description: "Description of the sensor 105"
+        type: "temperature"
 
     UnitCampaignBasicInfo:
       type: object