|
|
@@ -1,5 +1,6 @@
|
|
|
package io.senslog.service;
|
|
|
|
|
|
+import io.senslog.core.ObjectMapper;
|
|
|
import io.senslog.database.DBException;
|
|
|
import io.senslog.database.DBRepositoryPool;
|
|
|
import io.senslog.database.model.DBObservation;
|
|
|
@@ -23,6 +24,25 @@ public class DataService {
|
|
|
// TODO where to get this value?
|
|
|
private static final boolean USE_TRACKS = false;
|
|
|
|
|
|
+ private static final ObjectMapper mapper;
|
|
|
+
|
|
|
+ static {
|
|
|
+ mapper = ObjectMapper.create()
|
|
|
+ // WSObservation <--> DBObservation
|
|
|
+ .register(WSObservation.class, DBObservation.class,
|
|
|
+ o -> new DBObservation(o.getUnitId(), o.getSensorId(), o.getValue(), o.getTimestamp())
|
|
|
+ )
|
|
|
+ .register(DBObservation.class, WSObservation.class,
|
|
|
+ o -> new WSObservation(o.getUnitId(), o.getSensorId(), o.getValue(), o.getTimestamp())
|
|
|
+ )
|
|
|
+ // WSPosition <--> DBPosition
|
|
|
+ .register(WSPosition.class, DBPosition.class,
|
|
|
+ p -> new DBPosition(p.getUnitId(), p.getLatitude(), p.getLongitude(),
|
|
|
+ p.getTimestamp(), p.getAltitude(), p.getSpeed(), p.getDop(), 4326
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
private final ObservationRepository observationRep;
|
|
|
private final PositionRepository positionRep;
|
|
|
private final SensorRepository unitRep;
|
|
|
@@ -54,32 +74,35 @@ public class DataService {
|
|
|
// TODO solve
|
|
|
}
|
|
|
|
|
|
- DBObservation dbObservation = new DBObservation(
|
|
|
- wsObservation.getUnitId(), wsObservation.getSensorId(),
|
|
|
- wsObservation.getValue(), wsObservation.getTimestamp()
|
|
|
- );
|
|
|
-
|
|
|
try {
|
|
|
+ DBObservation dbObservation = mapper.mapTo(wsObservation, DBObservation.class);
|
|
|
return observationRep.insert(dbObservation);
|
|
|
} catch (DBException e) {
|
|
|
throw new WSException(500, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public boolean saveObservations(List<WSObservation> wsObservations) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<DBObservation> dbObservations = new ArrayList<>(wsObservations.size());
|
|
|
+ for (WSObservation wsO : wsObservations) {
|
|
|
+ dbObservations.add(mapper.mapTo(wsO, DBObservation.class));
|
|
|
+ }
|
|
|
+ return observationRep.insert(dbObservations);
|
|
|
+ } catch (DBException e) {
|
|
|
+ throw new WSException(500, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public boolean savePosition(WSPosition wsPosition) {
|
|
|
|
|
|
if (USE_TRACKS) {
|
|
|
// TODO solve(wsPosition);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- DBPosition dbPosition = new DBPosition(
|
|
|
- wsPosition.getUnitId(), wsPosition.getLatitude(), wsPosition.getLongitude(),
|
|
|
- wsPosition.getTimestamp(), wsPosition.getAltitude(), wsPosition.getSpeed(),
|
|
|
- wsPosition.getDop(), 4326
|
|
|
- );
|
|
|
-
|
|
|
try {
|
|
|
+ DBPosition dbPosition = mapper.mapTo(wsPosition, DBPosition.class);
|
|
|
return positionRep.insert(dbPosition);
|
|
|
} catch (DBException e) {
|
|
|
throw new WSException(500, e.getMessage());
|
|
|
@@ -107,7 +130,7 @@ public class DataService {
|
|
|
|
|
|
List<WSObservation> result = new ArrayList<>(observations.size());
|
|
|
for (DBObservation dbO : observations) {
|
|
|
- result.add(new WSObservation(dbO.getUnitId(), dbO.getSensorId(), dbO.getValue(), dbO.getTimestamp()));
|
|
|
+ result.add(mapper.mapTo(dbO, WSObservation.class));
|
|
|
}
|
|
|
return result;
|
|
|
} catch (DBException e) {
|