You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 2
Next »
public class RmoTimeSeriesServerParser implements ServerParser<TimeSeriesContentHandler>, TimeSeriesHeadersConsumer, PeriodConsumer {
private static final Logger log = Logger.getLogger(RmoTimeSeriesServerParser.class);
private FastDateFormat dateFormat = null;
private FastDateFormat timeFormat = null;
private TimeSeriesHeader[] headers = null;
private Period period = null;
@SuppressWarnings({"AssignmentToCollectionOrArrayFieldFromParameter"})
@Override
public void setTimeSeriesHeaders(TimeSeriesHeader[] timeSeriesHeaders) {
this.headers = timeSeriesHeaders;
}
@Override
public void setPeriod(Period period) {
this.period = period;
}
// http://000.000.00.00:8002/query?stations=1300H014,B,1300H013,1300H016,1300H017&start=2007-10-01&end=2007-10-10&startTime=00:00:00&endTime=00:00:00
@Override
public void parse(URL url, String username, String password, TimeSeriesContentHandler contentHandler) throws IOException {
if (period == Period.ANY_TIME)
throw new IllegalArgumentException("contentHandler.getWantedPeriod() == Period.ANY_TIME");
dateFormat = FastDateFormat.getInstance("yyyy-MM-dd", contentHandler.getDefaultTimeZone(), Locale.US, dateFormat);
timeFormat = FastDateFormat.getInstance("HH:mm:ss", contentHandler.getDefaultTimeZone(), Locale.US, timeFormat);
Date startDate = period.getStartDate();
Date endDate = period.getEndDate();
String periodQuery = "&start=" + dateFormat.format(startDate) + "&end=" + dateFormat.format(endDate)
+ "&startTime=" + timeFormat.format(startDate) + "&endTime=" + timeFormat.format(endDate);
PiTimeSeriesParser piTimeSeriesParser = new PiTimeSeriesParser();
for (int i = 0; i < headers.length; i++) {
TimeSeriesHeader header = headers[i];
String locationId = header.getLocationId();
URL fullUrl = new URL(url.toExternalForm() + "/query?stations=" + locationId + periodQuery);
if (log.isDebugEnabled()) log.debug(fullUrl.toExternalForm());
try {
InputStream stream = fullUrl.openStream();
try {
IOUtils.parse(stream, url.toString(), piTimeSeriesParser, contentHandler);
} finally {
stream.close();
}
} catch (ConnectException e) {
log.warn("Can not connect to " + fullUrl + ' ' + e.getMessage(), e);
return;
} catch (Exception e) {
log.warn("Can not parse data for " + header + ' ' + e.getMessage(), e);
}
}
}
}