|
@@ -2,9 +2,12 @@ package cz.senslog.watchdog.provider.ws;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import cz.senslog.watchdog.config.WebServiceConfig;
|
|
import cz.senslog.watchdog.config.WebServiceConfig;
|
|
|
|
|
+import cz.senslog.watchdog.domain.Sensor;
|
|
|
|
|
+import cz.senslog.watchdog.domain.Unit;
|
|
|
import cz.senslog.watchdog.provider.DataProvider;
|
|
import cz.senslog.watchdog.provider.DataProvider;
|
|
|
import cz.senslog.watchdog.domain.ObservationInfo;
|
|
import cz.senslog.watchdog.domain.ObservationInfo;
|
|
|
import cz.senslog.watchdog.provider.Record;
|
|
import cz.senslog.watchdog.provider.Record;
|
|
|
|
|
+import cz.senslog.watchdog.util.Tuple;
|
|
|
import cz.senslog.watchdog.util.http.HttpClient;
|
|
import cz.senslog.watchdog.util.http.HttpClient;
|
|
|
import cz.senslog.watchdog.util.http.HttpRequest;
|
|
import cz.senslog.watchdog.util.http.HttpRequest;
|
|
|
import cz.senslog.watchdog.util.http.HttpResponse;
|
|
import cz.senslog.watchdog.util.http.HttpResponse;
|
|
@@ -19,12 +22,17 @@ import java.util.*;
|
|
|
|
|
|
|
|
import static cz.senslog.watchdog.util.json.BasicJson.jsonToObject;
|
|
import static cz.senslog.watchdog.util.json.BasicJson.jsonToObject;
|
|
|
import static java.time.format.DateTimeFormatter.ofPattern;
|
|
import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
|
|
+import static java.util.Collections.emptyList;
|
|
|
|
|
+import static java.util.Collections.emptyMap;
|
|
|
|
|
|
|
|
public class WebServiceDataProvider implements DataProvider {
|
|
public class WebServiceDataProvider implements DataProvider {
|
|
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger(WebServiceDataProvider.class);
|
|
private static final Logger logger = LogManager.getLogger(WebServiceDataProvider.class);
|
|
|
|
|
|
|
|
- private static final DateTimeFormatter PATTERN = ofPattern("yyyy-MM-dd HH:mm:ssX");
|
|
|
|
|
|
|
+ private static final String DEFAULT_NAME = "unknown";
|
|
|
|
|
+ private static final Tuple<String, Map<Long, String>> DEFAULT_UNIT_INFO = Tuple.of(DEFAULT_NAME, emptyMap());
|
|
|
|
|
+
|
|
|
|
|
+ private static final DateTimeFormatter TIMESTAMP_PATTERN = ofPattern("yyyy-MM-dd HH:mm:ssX");
|
|
|
|
|
|
|
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
private final WebServiceConfig config;
|
|
private final WebServiceConfig config;
|
|
@@ -33,9 +41,60 @@ public class WebServiceDataProvider implements DataProvider {
|
|
|
this.config = config;
|
|
this.config = config;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public List<Record> getLastRecords() {
|
|
|
|
|
|
|
|
|
|
|
|
+ /* Map<unitId, Tuple<unitName, Map<sensorId, sensorName>>> */
|
|
|
|
|
+ private Map<Long, Tuple<String, Map<Long, String>>> loadUnitsInfo() {
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
|
|
+ .url(URLBuilder.newBuilder(config.getUrl(), "/DataService")
|
|
|
|
|
+ .addParam("Operation", "GetUnits")
|
|
|
|
|
+ .addParam("group", config.getGroup())
|
|
|
|
|
+ .addParam("user", config.getUser())
|
|
|
|
|
+ .build()
|
|
|
|
|
+ ).build();
|
|
|
|
|
+ logger.info("Getting new data from the server: {}.", request.getUrl());
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = httpClient.send(request);
|
|
|
|
|
+ logger.info("Received data with the status '{}' from the server {}.", response.getStatus(), request.getUrl());
|
|
|
|
|
+
|
|
|
|
|
+ if (response.isError()) {
|
|
|
|
|
+ logger.error("code: {}, message: {}", response.getStatus(), response.getBody());
|
|
|
|
|
+ return emptyMap();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<Long, Tuple<String, Map<Long, String>>> unitsInfo = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ final Type unitInfoType = new TypeToken<Collection<Map<String, Object>>>() {}.getType();
|
|
|
|
|
+ List<Map<String, Object>> unitInfoList = jsonToObject(response.getBody(), unitInfoType);
|
|
|
|
|
+ for (Map<String, Object> unitInfo : unitInfoList) {
|
|
|
|
|
+ Object unitObj = unitInfo.get("unit");
|
|
|
|
|
+ if (unitObj instanceof Map) {
|
|
|
|
|
+ Map<?, ?> unitMap = (Map<?, ?>)unitObj;
|
|
|
|
|
+ String unitName = unitMap.get("description").toString();
|
|
|
|
|
+ long unitId = ((Double)unitMap.get("unitId")).longValue();
|
|
|
|
|
+
|
|
|
|
|
+ Map<Long, String> resultSensorInfo = new HashMap<>();
|
|
|
|
|
+ Object sensorsObj = unitInfo.get("sensors");
|
|
|
|
|
+ if (sensorsObj instanceof List) {
|
|
|
|
|
+ List<?> sensorsList = (List<?>)sensorsObj;
|
|
|
|
|
+ for (Object sensorInfoObj : sensorsList) {
|
|
|
|
|
+ if (sensorInfoObj instanceof Map) {
|
|
|
|
|
+ Map<?, ?> sensorMap = (Map<?, ?>)sensorInfoObj;
|
|
|
|
|
+ String sensorName = sensorMap.get("sensorName").toString();
|
|
|
|
|
+ long sensorId = ((Double)sensorMap.get("sensorId")).longValue();
|
|
|
|
|
+ resultSensorInfo.put(sensorId, sensorName);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ unitsInfo.put(unitId, Tuple.of(unitName, resultSensorInfo));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return unitsInfo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<Map<String, Object>> loadLastObservations() {
|
|
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
.url(URLBuilder.newBuilder(config.getUrl(), "/SensorService")
|
|
.url(URLBuilder.newBuilder(config.getUrl(), "/SensorService")
|
|
|
.addParam("Operation", "GetLastObservations")
|
|
.addParam("Operation", "GetLastObservations")
|
|
@@ -48,24 +107,36 @@ public class WebServiceDataProvider implements DataProvider {
|
|
|
HttpResponse response = httpClient.send(request);
|
|
HttpResponse response = httpClient.send(request);
|
|
|
logger.info("Received data with the status '{}' from the server {}.", response.getStatus(), request.getUrl());
|
|
logger.info("Received data with the status '{}' from the server {}.", response.getStatus(), request.getUrl());
|
|
|
|
|
|
|
|
- if (response.isOk()) {
|
|
|
|
|
- String jsonBody = response.getBody();
|
|
|
|
|
- Type lastObsType = new TypeToken<Collection<Map<String, Object>>>() {}.getType();
|
|
|
|
|
- List<Map<String, Object>> lastObservations = jsonToObject(jsonBody, lastObsType);
|
|
|
|
|
- List<Record> observations = new ArrayList<>(lastObservations.size());
|
|
|
|
|
- for (Map<String, Object> observationMap : lastObservations) {
|
|
|
|
|
- long unitId = ((Double)observationMap.get("unitId")).longValue();
|
|
|
|
|
- long sensorId = ((Double)observationMap.get("sensorId")).longValue();
|
|
|
|
|
- String timestampStr = observationMap.get("timeStamp").toString();
|
|
|
|
|
- OffsetDateTime timestamp = OffsetDateTime.parse(timestampStr, PATTERN);
|
|
|
|
|
- observations.add(new ObservationInfo(unitId, sensorId, timestamp));
|
|
|
|
|
- }
|
|
|
|
|
- return observations;
|
|
|
|
|
- } else {
|
|
|
|
|
|
|
+ if (response.isError()) {
|
|
|
logger.error("code: {}, message: {}", response.getStatus(), response.getBody());
|
|
logger.error("code: {}, message: {}", response.getStatus(), response.getBody());
|
|
|
|
|
+ return emptyList();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return Collections.emptyList();
|
|
|
|
|
|
|
+ final Type lastObsType = new TypeToken<Collection<Map<String, Object>>>() {}.getType();
|
|
|
|
|
+ return jsonToObject(response.getBody(), lastObsType);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Record> getLastRecords() {
|
|
|
|
|
+
|
|
|
|
|
+ // Map<unitId, Tuple<unitName, Map<sensorId, sensorName>>>
|
|
|
|
|
+ Map<Long, Tuple<String, Map<Long, String>>> unitsInfo = loadUnitsInfo();
|
|
|
|
|
+ List<Map<String, Object>> lastObservations = loadLastObservations();
|
|
|
|
|
+
|
|
|
|
|
+ List<Record> observations = new ArrayList<>(lastObservations.size());
|
|
|
|
|
+ for (Map<String, Object> observationMap : lastObservations) {
|
|
|
|
|
+
|
|
|
|
|
+ long unitId = ((Double)observationMap.get("unitId")).longValue();
|
|
|
|
|
+ long sensorId = ((Double)observationMap.get("sensorId")).longValue();
|
|
|
|
|
+ String timestampStr = observationMap.get("timeStamp").toString();
|
|
|
|
|
+ OffsetDateTime timestamp = OffsetDateTime.parse(timestampStr, TIMESTAMP_PATTERN);
|
|
|
|
|
+
|
|
|
|
|
+ Tuple<String, Map<Long, String>> unitInfo = unitsInfo.getOrDefault(unitId, DEFAULT_UNIT_INFO);
|
|
|
|
|
+ Unit unit = new Unit(unitId, unitInfo.getItem1());
|
|
|
|
|
+ Sensor sensor = new Sensor(sensorId, unitInfo.getItem2().getOrDefault(sensorId, DEFAULT_NAME));
|
|
|
|
|
+ observations.add(new ObservationInfo(unit, sensor, timestamp));
|
|
|
|
|
+ }
|
|
|
|
|
+ return observations;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|