|
|
@@ -1,11 +1,8 @@
|
|
|
package cz.senslog.watchdog.provider.ws;
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
-import cz.senslog.watchdog.config.WebServiceDataProviderConfig;
|
|
|
-import cz.senslog.watchdog.domain.Sensor;
|
|
|
-import cz.senslog.watchdog.domain.Unit;
|
|
|
+import cz.senslog.watchdog.config.WSSensLogDataProviderConfig;
|
|
|
import cz.senslog.watchdog.provider.*;
|
|
|
-import cz.senslog.watchdog.domain.ObservationInfo;
|
|
|
import cz.senslog.watchdog.util.Tuple;
|
|
|
import cz.senslog.watchdog.util.http.HttpClient;
|
|
|
import cz.senslog.watchdog.util.http.HttpRequest;
|
|
|
@@ -24,9 +21,9 @@ import static cz.senslog.watchdog.util.json.BasicJson.jsonToObject;
|
|
|
import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
import static java.util.Collections.*;
|
|
|
|
|
|
-public class WebServiceDataProvider implements DataProvider {
|
|
|
+public class WSSensLogDataProvider implements DataProvider {
|
|
|
|
|
|
- private static final Logger logger = LogManager.getLogger(WebServiceDataProvider.class);
|
|
|
+ private static final Logger logger = LogManager.getLogger(WSSensLogDataProvider.class);
|
|
|
|
|
|
private static final String ERROR_KEY = "__error";
|
|
|
|
|
|
@@ -36,13 +33,12 @@ public class WebServiceDataProvider implements DataProvider {
|
|
|
private static final DateTimeFormatter TIMESTAMP_PATTERN = ofPattern("yyyy-MM-dd HH:mm:ssX");
|
|
|
|
|
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
|
|
- private final WebServiceDataProviderConfig config;
|
|
|
+ private final WSSensLogDataProviderConfig config;
|
|
|
|
|
|
- public WebServiceDataProvider(WebServiceDataProviderConfig config) {
|
|
|
+ public WSSensLogDataProvider(WSSensLogDataProviderConfig config) {
|
|
|
this.config = config;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/* Map<unitId, Tuple<unitName, Map<sensorId, sensorName>>> */
|
|
|
private Map<String, Tuple<String, Map<String, String>>> loadUnitsInfo() {
|
|
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
|
|
@@ -122,31 +118,8 @@ public class WebServiceDataProvider implements DataProvider {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Record> getLastRecords() {
|
|
|
-
|
|
|
- // Map<unitId, Tuple<unitName, Map<sensorId, sensorName>>>
|
|
|
- Map<String, Tuple<String, Map<String, String>>> unitsInfo = loadUnitsInfo();
|
|
|
- List<Map<String, Object>> lastObservations = loadLastObservations();
|
|
|
-
|
|
|
- List<Record> observations = new ArrayList<>(lastObservations.size());
|
|
|
- for (Map<String, Object> obMap : lastObservations) {
|
|
|
-
|
|
|
- String unitId = obMap.getOrDefault("unitId", "").toString();
|
|
|
- String sensorId = obMap.getOrDefault("sensorId", "").toString();
|
|
|
- OffsetDateTime timestamp = OffsetDateTime.parse(obMap.get("timeStamp").toString(), TIMESTAMP_PATTERN);
|
|
|
-
|
|
|
- Tuple<String, Map<String, String>> unitInfo = unitsInfo.getOrDefault(unitId, DEFAULT_UNIT_INFO);
|
|
|
- Unit unit = new Unit(Long.parseLong(unitId), unitInfo.getItem1());
|
|
|
- Sensor sensor = new Sensor(Long.parseLong(sensorId), unitInfo.getItem2().getOrDefault(sensorId, DEFAULT_NAME));
|
|
|
- observations.add(new ObservationInfo(unit, sensor, timestamp));
|
|
|
- }
|
|
|
- return observations;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
public ProvidedData getLastData() {
|
|
|
|
|
|
- // Map<unitId, Tuple<unitName, Map<sensorId, sensorName>>>
|
|
|
Map<String, Tuple<String, Map<String, String>>> unitsInfo = loadUnitsInfo();
|
|
|
List<Map<String, Object>> lastObservations = loadLastObservations();
|
|
|
|
|
|
@@ -163,21 +136,22 @@ public class WebServiceDataProvider implements DataProvider {
|
|
|
String source = String.format("%s: %s", config.getId(), config.getBaseUrl());
|
|
|
ProvidedData providedData = new ProvidedData(new ProviderInfo(source, WEB_SERVICE, isServerAlive), errorMessages);
|
|
|
|
|
|
- for (Map<String, Object> obMap : lastObservations) {
|
|
|
- long unitId = ((Double) obMap.get("unitId")).longValue();
|
|
|
- long sensorId = ((Double) obMap.get("sensorId")).longValue();
|
|
|
- OffsetDateTime timestamp = OffsetDateTime.parse(obMap.get("timeStamp").toString(), TIMESTAMP_PATTERN);
|
|
|
-
|
|
|
- String unitIdStr = String.format("%d", unitId);
|
|
|
- String sensorIdStr = String.format("%d", sensorId);
|
|
|
+ if (isServerAlive) {
|
|
|
+ for (Map<String, Object> obMap : lastObservations) {
|
|
|
+ long unitId = ((Double) obMap.get("unitId")).longValue();
|
|
|
+ long sensorId = ((Double) obMap.get("sensorId")).longValue();
|
|
|
+ OffsetDateTime timestamp = OffsetDateTime.parse(obMap.get("timeStamp").toString(), TIMESTAMP_PATTERN);
|
|
|
|
|
|
- Tuple<String, Map<String, String>> unitInfo = unitsInfo.getOrDefault(unitIdStr, DEFAULT_UNIT_INFO);
|
|
|
- String unitName = unitInfo.getItem1();
|
|
|
- String sensorName = unitInfo.getItem2().getOrDefault(sensorIdStr, DEFAULT_NAME);
|
|
|
+ String unitIdStr = String.format("%d", unitId);
|
|
|
+ String sensorIdStr = String.format("%d", sensorId);
|
|
|
|
|
|
- providedData.computeIfAbsent(unitIdStr, k -> new ProvidedObject(k, unitName, null))
|
|
|
- .addNode(sensorIdStr, new ProvidedObject(sensorIdStr, sensorName, timestamp));
|
|
|
+ Tuple<String, Map<String, String>> unitInfo = unitsInfo.getOrDefault(unitIdStr, DEFAULT_UNIT_INFO);
|
|
|
+ String unitName = unitInfo.getItem1();
|
|
|
+ String sensorName = unitInfo.getItem2().getOrDefault(sensorIdStr, DEFAULT_NAME);
|
|
|
|
|
|
+ providedData.computeIfAbsent(unitIdStr, k -> new ProvidedObject(k, unitName, null))
|
|
|
+ .addNode(sensorIdStr, new ProvidedObject(sensorIdStr, sensorName, timestamp));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return providedData;
|