2021-12-10 10:21:06 +00:00
|
|
|
package cc.pogoda.mobile.meteosystem.dao;
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-01-06 21:06:53 +00:00
|
|
|
import org.tinylog.Logger;
|
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2021-12-10 10:21:06 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.type.web.QualityFactor;
|
|
|
|
import cc.pogoda.mobile.meteosystem.type.web.Summary;
|
|
|
|
import cc.pogoda.mobile.meteosystem.web.RestClientConfig;
|
|
|
|
import cc.pogoda.mobile.meteosystem.web.SummaryConsumer;
|
2020-12-16 19:26:53 +00:00
|
|
|
import retrofit2.Response;
|
|
|
|
|
2021-01-12 21:43:14 +00:00
|
|
|
/**
|
|
|
|
* This DAO downloads the measurements summary data for wx station given by its name
|
|
|
|
*/
|
2020-12-16 19:26:53 +00:00
|
|
|
public class SummaryDao {
|
|
|
|
|
|
|
|
RestClientConfig restClient;
|
|
|
|
|
|
|
|
Response<Summary> response = null;
|
|
|
|
|
|
|
|
String station;
|
|
|
|
|
|
|
|
class Worker implements Runnable {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-01-12 21:43:14 +00:00
|
|
|
// create a new instance of factory class. This could be refactored to static invocation
|
2020-12-16 19:26:53 +00:00
|
|
|
restClient = new RestClientConfig();
|
|
|
|
|
2021-01-12 21:43:14 +00:00
|
|
|
// create a new instance of Retrofit with OkHttp client with GSON parser
|
2020-12-16 19:26:53 +00:00
|
|
|
SummaryConsumer consumer = restClient.getWeatherStationClient().create(SummaryConsumer.class);
|
|
|
|
|
|
|
|
try {
|
2022-05-08 15:26:47 +00:00
|
|
|
Logger.info("[SummaryDao][Worker][station = " + station +"]");
|
2022-05-03 17:56:00 +00:00
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
response = consumer.getSummaryForStation(station).execute();
|
|
|
|
} catch (IOException e) {
|
2022-05-03 17:56:00 +00:00
|
|
|
Logger.error("[SummaryDao][Worker][IOException][e = " + e.getLocalizedMessage() +"]");
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
Logger.error("[SummaryDao][Worker][RuntimeException][e = " + e.getLocalizedMessage() +"]");
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2022-01-06 21:06:53 +00:00
|
|
|
Logger.error("[SummaryDao][Worker][Exception][e = " + e.getLocalizedMessage() +"]");
|
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-05-03 17:56:00 +00:00
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
Logger.error("[SummaryDao][Worker][worker is done, response is null]");
|
|
|
|
}
|
|
|
|
else {
|
2022-05-08 15:26:47 +00:00
|
|
|
Logger.info("[SummaryDao][Worker][worker is done][response.code() = " + response.code() +"]");
|
2022-05-03 17:56:00 +00:00
|
|
|
}
|
2020-12-16 19:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Summary getStationSummary(String station) {
|
|
|
|
|
|
|
|
Summary out = null;
|
|
|
|
|
|
|
|
this.station = station;
|
|
|
|
|
|
|
|
Thread worker = new Thread(new Worker());
|
|
|
|
|
|
|
|
worker.start();
|
|
|
|
|
|
|
|
try {
|
2021-01-12 21:43:14 +00:00
|
|
|
// wait for the web service response
|
2022-05-03 17:56:00 +00:00
|
|
|
worker.join(11000);
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2021-01-12 21:43:14 +00:00
|
|
|
// check if web service returned anything
|
2020-12-16 19:26:53 +00:00
|
|
|
if (response != null) {
|
2021-01-12 21:43:14 +00:00
|
|
|
// if yes get the response body
|
2020-12-16 19:26:53 +00:00
|
|
|
out = response.body();
|
2021-01-02 13:30:46 +00:00
|
|
|
|
|
|
|
if (out != null) {
|
2021-01-12 21:43:14 +00:00
|
|
|
// convert all quality factors from string representation to native format
|
2021-01-02 13:30:46 +00:00
|
|
|
out.temperature_qf_native = QualityFactor.valueOf(out.temperature_qf);
|
|
|
|
out.wind_qf_native = QualityFactor.valueOf(out.wind_qf);
|
|
|
|
out.humidity_qf_native = QualityFactor.valueOf(out.humidity_qf);
|
2021-01-12 21:43:14 +00:00
|
|
|
out.qnh_qf_native = QualityFactor.valueOf(out.qnh_qf);
|
2021-01-02 13:30:46 +00:00
|
|
|
}
|
2022-05-03 17:56:00 +00:00
|
|
|
else {
|
|
|
|
Logger.error("[SummaryDao][getStationSummary][station = " + station +"][response.code() = " + response.code() +"][response body is nulll, probably HTTP error" +
|
|
|
|
"]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Logger.error("[SummaryDao][getStationSummary][station = " + station +"][response is null!!]");
|
2020-12-16 19:26:53 +00:00
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
2022-05-03 17:56:00 +00:00
|
|
|
Logger.error("[SummaryDao][getStationSummary][station = " + station +"][InterruptedException]");
|
2020-12-16 19:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|