|
|
@@ -2,26 +2,160 @@ package io.senslog.database.repository;
|
|
|
|
|
|
import io.senslog.database.DBConnection;
|
|
|
import io.senslog.database.DBException;
|
|
|
-import cz.hsrs.db.model.Observation;
|
|
|
+import io.senslog.database.model.DBObservation;
|
|
|
+
|
|
|
+import java.time.OffsetDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
|
|
|
public class ObservationRepository implements AbstractRepository {
|
|
|
|
|
|
+ private static final DateTimeFormatter TIMESTAMP_FORMATTER = ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSX");
|
|
|
+
|
|
|
private final DBConnection connection;
|
|
|
|
|
|
public ObservationRepository(DBConnection connection) {
|
|
|
this.connection = connection;
|
|
|
}
|
|
|
|
|
|
- public long insert(Observation observation) throws DBException {
|
|
|
+ public boolean insert(DBObservation observation) throws DBException {
|
|
|
+ if (observation == null) {
|
|
|
+ throw new DBException("Nothing to insert. Observation is empty.");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ connection.get().<Exception>useHandle(h -> h.inTransaction(t -> t.createUpdate(
|
|
|
+ "INSERT INTO observations(time_stamp, observed_value, sensor_id, unit_id) " +
|
|
|
+ "VALUES (:timestamp, :value, :sensorId, :unitId)"
|
|
|
+ )
|
|
|
+ .bind("timestamp", observation.getTimestamp())
|
|
|
+ .bind("value", observation.getValue())
|
|
|
+ .bind("sensorId", observation.getSensorId())
|
|
|
+ .bind("unitId", observation.getUnitId())
|
|
|
+ .execute()));
|
|
|
+ return true; // TODO return observationId in future
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new DBException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean insert(List<DBObservation> observations) throws DBException {
|
|
|
+ if (observations == null || observations.isEmpty()) {
|
|
|
+ throw new DBException("Nothing to insert. List of observation is empty.");
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<DBObservation> selectLastObservationsBy(String groupName) throws DBException {
|
|
|
+ if (groupName == null || groupName.isEmpty()) {
|
|
|
+ throw new DBException("Name of group was not specified.");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return connection.get().<List<DBObservation>, Exception>withHandle(h -> h.createQuery(
|
|
|
+ "SELECT observation_id AS id, TO_CHAR(time_stamp, 'yyyy-MM-dd HH:mm:ss.SSSSSSOF') AS timestamp, " +
|
|
|
+ "gid AS gid, observed_value AS value, o.sensor_id AS sensorId, o.unit_id AS unitId " +
|
|
|
+ "FROM groups g, units_to_groups utg, units_to_sensors uts " +
|
|
|
+ "LEFT JOIN observations o ON uts.last_obs = o.time_stamp " +
|
|
|
+ "WHERE g.group_name = :group " +
|
|
|
+ "AND g.id = utg.group_id " +
|
|
|
+ "AND utg.unit_id = uts.unit_id " +
|
|
|
+ "AND uts.unit_id = o.unit_id " +
|
|
|
+ "AND uts.sensor_id = o.sensor_id " +
|
|
|
+ "ORDER BY uts.unit_id, uts.sensor_id"
|
|
|
+ )
|
|
|
+ .bind("group", groupName)
|
|
|
+ .map((rs, ctx) -> new DBObservation(
|
|
|
+ rs.getLong("id"),
|
|
|
+ rs.getLong("unitId"),
|
|
|
+ rs.getLong("sensorId"),
|
|
|
+ rs.getLong("value"),
|
|
|
+ rs.getInt("gid"),
|
|
|
+ OffsetDateTime.parse(rs.getString("timestamp"), TIMESTAMP_FORMATTER)
|
|
|
+ )
|
|
|
+ ).list());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new DBException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<DBObservation> selectLastObservationsBy(String groupName, long sensorId) throws DBException {
|
|
|
+ if (groupName == null || groupName.isEmpty()) {
|
|
|
+ throw new DBException("Name of group was not specified.");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return connection.get().<List<DBObservation>, Exception>withHandle(h -> h.createQuery(
|
|
|
+ "SELECT observation_id AS id, TO_CHAR(time_stamp, 'yyyy-MM-dd HH:mm:ss.SSSSSSOF') AS timestamp, " +
|
|
|
+ "gid AS gid, observed_value AS value, o.unit_id AS unitId " +
|
|
|
+ "FROM groups g, units_to_groups utg, units_to_sensors uts " +
|
|
|
+ "LEFT JOIN observations o ON uts.last_obs = o.time_stamp " +
|
|
|
+ "WHERE g.group_name = :group " +
|
|
|
+ "AND g.id = utg.group_id " +
|
|
|
+ "AND utg.unit_id = uts.unit_id " +
|
|
|
+ "AND uts.sensor_id = :sensorId " +
|
|
|
+ "AND uts.unit_id = o.unit_id " +
|
|
|
+ "AND uts.sensor_id = o.sensor_id " +
|
|
|
+ "ORDER BY uts.unit_id, uts.sensor_id"
|
|
|
+ )
|
|
|
+ .bind("group", groupName)
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
+ .map((rs, ctx) -> new DBObservation(
|
|
|
+ rs.getLong("id"),
|
|
|
+ rs.getLong("unitId"),
|
|
|
+ sensorId,
|
|
|
+ rs.getLong("value"),
|
|
|
+ rs.getInt("gid"),
|
|
|
+ OffsetDateTime.parse(rs.getString("timestamp"), TIMESTAMP_FORMATTER)
|
|
|
+ )
|
|
|
+ ).list());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new DBException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<DBObservation> selectLastObservationsBy(long unitId) throws DBException {
|
|
|
+ try {
|
|
|
+ return connection.get().<List<DBObservation>, Exception>withHandle(h -> h.createQuery(
|
|
|
+ "SELECT observation_id AS id, TO_CHAR(time_stamp, 'yyyy-MM-dd HH:mm:ss.SSSSSSOF') AS timestamp, " +
|
|
|
+ "gid AS gid, observed_value AS value, o.sensor_id AS sensorId " +
|
|
|
+ "FROM units_to_sensors uts " +
|
|
|
+ "LEFT JOIN observations o ON uts.last_obs = o.time_stamp " +
|
|
|
+ "WHERE uts.unit_id = :unitId AND uts.sensor_id = o.sensor_id"
|
|
|
+ )
|
|
|
+ .bind("unitId", unitId)
|
|
|
+ .map((rs, ctx) -> new DBObservation(
|
|
|
+ rs.getLong("id"),
|
|
|
+ unitId,
|
|
|
+ rs.getLong("sensorId"),
|
|
|
+ rs.getLong("value"),
|
|
|
+ rs.getInt("gid"),
|
|
|
+ OffsetDateTime.parse(rs.getString("timestamp"), TIMESTAMP_FORMATTER))
|
|
|
+ ).list());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new DBException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<DBObservation> selectLastObservationsBy(long unitId, long sensorId) throws DBException {
|
|
|
try {
|
|
|
- connection.get().<Exception>useHandle(h -> h.inTransaction(t -> t.execute(
|
|
|
- "INSERT INTO observations(time_stamp, observed_value, sensor_id, unit_id) VALUES (?, ?, ?, ?)",
|
|
|
- observation.getTimeStamp(), observation.getObservedValue(), observation.getSensorId(), observation.getUnitId()
|
|
|
- )
|
|
|
- ));
|
|
|
+ return connection.get().<List<DBObservation>, Exception>withHandle(h -> h.createQuery(
|
|
|
+ "SELECT observation_id AS id, TO_CHAR(time_stamp, 'yyyy-MM-dd HH:mm:ss.SSSSSSOF') AS timestamp, " +
|
|
|
+ "gid AS gid, observed_value AS value " +
|
|
|
+ "FROM units_to_sensors uts "
|
|
|
+ + "LEFT JOIN observations o ON uts.last_obs = o.time_stamp "
|
|
|
+ + "WHERE uts.unit_id = :unitId AND uts.sensor_id = :sensorId AND uts.sensor_id = o.sensor_id"
|
|
|
+ )
|
|
|
+ .bind("unitId", unitId)
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
+ .map((rs, ctx) -> new DBObservation(
|
|
|
+ rs.getLong("id"),
|
|
|
+ unitId, sensorId,
|
|
|
+ rs.getLong("value"),
|
|
|
+ rs.getInt("gid"),
|
|
|
+ OffsetDateTime.parse(rs.getString("timestamp"), TIMESTAMP_FORMATTER))
|
|
|
+ ).list());
|
|
|
} catch (Exception e) {
|
|
|
throw new DBException(e.getMessage());
|
|
|
}
|
|
|
- return 0L; // observation_id
|
|
|
}
|
|
|
}
|