MeteoSystem/app/src/main/java/cc/pogoda/mobile/meteosystem/dao/SummaryDao.java

105 wiersze
3.5 KiB
Java
Czysty Zwykły widok Historia

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;
/**
* 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() {
// 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();
// 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 {
Logger.info("[SummaryDao][Worker][station = " + station +"]");
2020-12-16 19:26:53 +00:00
response = consumer.getSummaryForStation(station).execute();
} catch (IOException e) {
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();
}
if (response == null) {
Logger.error("[SummaryDao][Worker][worker is done, response is null]");
}
else {
Logger.info("[SummaryDao][Worker][worker is done][response.code() = " + response.code() +"]");
}
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 {
// wait for the web service response
worker.join(11000);
2020-12-16 19:26:53 +00:00
// check if web service returned anything
2020-12-16 19:26:53 +00:00
if (response != null) {
// if yes get the response body
2020-12-16 19:26:53 +00:00
out = response.body();
if (out != null) {
// convert all quality factors from string representation to native format
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);
out.qnh_qf_native = QualityFactor.valueOf(out.qnh_qf);
}
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();
Logger.error("[SummaryDao][getStationSummary][station = " + station +"][InterruptedException]");
2020-12-16 19:26:53 +00:00
}
return out;
}
}