/
WeatherFetcher.java
142 lines (115 loc) · 3.95 KB
/
WeatherFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package no.vindsiden;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import no.vindsiden.configuration.Configuration;
import no.vindsiden.domain.Measurement;
import no.vindsiden.domain.WeatherStation;
import no.vindsiden.domain.WeatherStationComparator;
import org.apache.commons.httpclient.HttpException;
/**
* @author Erik Mohn - mohn.erik@gmail.com
*/
public class WeatherFetcher {
private Configuration configuration;
private VindsidenHttpClient httpClient;
private List<WeatherStation> weatherStations;
private List<WeatherStation> failedWeatherStations;
private boolean inErrorHandling = false;
public WeatherFetcher(Configuration config) {
this.configuration = config;
this.httpClient = new VindsidenHttpClient();
this.weatherStations = config.getWeatherStationList();
this.failedWeatherStations = new ArrayList<WeatherStation>();
}
public void execute() {
logStart();
validate();
processWeatherStations();
if (failedWeatherStationsExists()) {
log("Will do error handling of: " + failedWeatherStations);
executeErrorHandling();
}
logFinish();
}
private void logStart() {
printFrame("STARTING EXECTUION OF WEATHERFETCHER");
}
private void printFrame(String string) {
log("---------------------------------------------");
log("--- " + string);
log("---------------------------------------------");
}
private void logFinish() {
printFrame("EXECTUION OF WEATHERFETCHER FINISHED");
}
private void validate() {
if ( weatherStations == null || weatherStations.size() == 0) {
throw new RuntimeException("No weatherStations configured!");
}
}
private void processWeatherStations() {
List<WeatherStation> weatherStations = (inErrorHandling) ? failedWeatherStations : this.weatherStations;
Collections.sort(weatherStations, new WeatherStationComparator());
for (WeatherStation weatherStation : weatherStations) {
try {
if(weatherStation.isEnabled()) {
processWeatherStation(weatherStation);
}
} catch (Exception e) {
log("Error occured while processing: " + weatherStation);
if (!inErrorHandling) {
failedWeatherStations.add(weatherStation);
}
e.printStackTrace();
}
}
}
private void processWeatherStation(WeatherStation weatherStation) throws Exception {
List<Measurement> measurements = weatherStation.fetchMeasurement();
for (Measurement measurement : measurements) {
String name = (measurements.size() == 1 ) ? weatherStation.getName() : measurement.getStationName();
Instant latestUpdate = null;
if (measurement.getTimestamp() != null) {
latestUpdate = httpClient.getLatestUpdateTimestamp(measurement);
}
if (latestUpdate == null || measurement.getTimestamp().isAfter(latestUpdate)) {
httpClient.sendHttpRequest(measurement);
log( Instant.now() + " Executed HTTP request, for " + name + " : " + measurement.toVindSidenUrl());
} else {
log( Instant.now() + " Did not send data, due to outdated timestamp: " + measurement.getTimestamp() + " " + name + " : " + measurement.toVindSidenUrl());
}
}
}
private void executeErrorHandling() {
inErrorHandling = true;
try {
Thread.sleep(configuration.getTimeToSleepBeforeErrorHandling());
processWeatherStations();
} catch (InterruptedException e) {
log("Thread.sleep Interrupted");
e.printStackTrace();
} finally {
clearFailedWeatherStations();
}
}
private boolean failedWeatherStationsExists() {
return failedWeatherStations.size() != 0;
}
private void clearFailedWeatherStations() {
failedWeatherStations.clear();
}
public void setHttpClient(VindsidenHttpClient httpClient) {
this.httpClient = httpClient;
}
protected void log(String log) {
System.out.println(log);
}
}