A lot
This commit is contained in:
@@ -3,9 +3,14 @@ package com.flaremicro.visualforecast.datamart;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
@@ -22,11 +27,13 @@ import org.xml.sax.SAXException;
|
||||
import com.flaremicro.util.Util;
|
||||
import com.flaremicro.visualforecast.forecast.DayForecast;
|
||||
import com.flaremicro.visualforecast.forecast.ForecastDetails;
|
||||
import com.flaremicro.visualforecast.forecast.HourlyForecast;
|
||||
import com.flaremicro.visualforecast.forecast.TownForecast;
|
||||
import com.flaremicro.visualforecast.forecast.ValueCheck;
|
||||
|
||||
public class ForecastProcessor implements Runnable {
|
||||
private final TownInfo[] towns;
|
||||
private final CanadaDatamartProvider cdp;
|
||||
private ForecastDetails mostRecentForecast = null;
|
||||
private boolean running = false;
|
||||
private Thread self;
|
||||
@@ -34,9 +41,9 @@ public class ForecastProcessor implements Runnable {
|
||||
|
||||
private DatamartTranslation dmt = new DatamartTranslation();
|
||||
|
||||
public ForecastProcessor(TownInfo[] towns) {
|
||||
public ForecastProcessor(TownInfo[] towns, CanadaDatamartProvider cdp) {
|
||||
this.towns = towns;
|
||||
|
||||
this.cdp = cdp;
|
||||
try
|
||||
{
|
||||
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
@@ -70,20 +77,74 @@ public class ForecastProcessor implements Runnable {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void processHourlyForecast(TownForecast forecast, Document doc) {
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
NodeList hourlyForecast = doc.getElementsByTagName("hourlyForecast");
|
||||
ArrayList<HourlyForecast> hourlyForecastArray = new ArrayList<HourlyForecast>(12);
|
||||
for (int i = 0; i < hourlyForecast.getLength(); i++)
|
||||
{
|
||||
if (hourlyForecast.item(i).getNodeType() == Node.ELEMENT_NODE)
|
||||
{
|
||||
Element element = (Element) hourlyForecast.item(i);
|
||||
byte iconCode = dmt.icon(XMLUtils.getIntFromTag(element, "iconCode", ValueCheck.NO_DATA_INT));
|
||||
byte temp = XMLUtils.getByteFromTag(element, "temperature", ValueCheck.NO_DATA_BYTE);
|
||||
byte windChill = XMLUtils.getByteFromTag(element, "windChill", ValueCheck.NO_DATA_BYTE);
|
||||
float percip = XMLUtils.getFloatFromTag(element, "lop", ValueCheck.NO_DATA_FLOAT);
|
||||
String dateTimeUTC = XMLUtils.getStringFromAttribute(element, "dateTimeUTC");
|
||||
if (dateTimeUTC != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Date date = dateFormat.parse(dateTimeUTC);
|
||||
hourlyForecastArray.add(new HourlyForecast(date, iconCode, temp, (short) 0, percip, windChill));
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
forecast.setHourlyForecast(hourlyForecastArray.toArray(new HourlyForecast[0]));
|
||||
}
|
||||
|
||||
public void processForecasts() {
|
||||
System.out.println("Time to update");
|
||||
ForecastDetails forecastDetails = new ForecastDetails();
|
||||
ArrayList<TownForecast> townForecasts = new ArrayList<TownForecast>();
|
||||
for (TownInfo townInfo : towns)
|
||||
{
|
||||
DayForecast[] dayForecasts = new DayForecast[8];
|
||||
InputStream is = null;
|
||||
Document doc = null;
|
||||
try
|
||||
{
|
||||
|
||||
URL url = new URL("https://dd.weather.gc.ca/citypage_weather/xml/BC/" + townInfo.code + "_e.xml");
|
||||
URL url = new URL("https://dd.weather.gc.ca/citypage_weather/xml/" + townInfo.province + "/" + townInfo.code + "_e.xml");
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
is = url.openStream();
|
||||
Document doc = db.parse(is);
|
||||
doc = db.parse(is);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ParserConfigurationException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (SAXException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.cleanClose(is);
|
||||
}
|
||||
|
||||
if (doc != null)
|
||||
{
|
||||
NodeList nodeList = doc.getElementsByTagName("forecast");
|
||||
for (int i = 0; i < nodeList.getLength(); i++)
|
||||
{
|
||||
@@ -92,7 +153,8 @@ public class ForecastProcessor implements Runnable {
|
||||
Element node = (Element) nodeList.item(i);
|
||||
int dayIndex = getDayIndex(XMLUtils.getStringFromTagAttribute(node, "period", "textForecastName"));
|
||||
Element abbForecast = XMLUtils.getFistElement(node, "abbreviatedForecast");
|
||||
int iconIndex = XMLUtils.getIntFromTag(abbForecast, "iconCode", 0);
|
||||
int iconIndex = XMLUtils.getIntFromTag(abbForecast, "iconCode", ValueCheck.NO_DATA_INT);
|
||||
float percip = XMLUtils.getFloatFromTag(abbForecast, "pop", ValueCheck.NO_DATA_FLOAT);
|
||||
String textForecast = XMLUtils.getStringFromTag(abbForecast, "textSummary");
|
||||
|
||||
WeatherLines lines = dmt.weatherName(textForecast);
|
||||
@@ -109,11 +171,11 @@ public class ForecastProcessor implements Runnable {
|
||||
try
|
||||
{
|
||||
byte val = Byte.parseByte(n.getTextContent().trim());
|
||||
if(XMLUtils.getStringFromAttribute(n, "class").trim().equalsIgnoreCase("high"))
|
||||
if (XMLUtils.getStringFromAttribute(n, "class").trim().equalsIgnoreCase("high"))
|
||||
{
|
||||
hi = val;
|
||||
}
|
||||
else if(XMLUtils.getStringFromAttribute(n, "class").trim().equalsIgnoreCase("low"))
|
||||
else if (XMLUtils.getStringFromAttribute(n, "class").trim().equalsIgnoreCase("low"))
|
||||
{
|
||||
lo = val;
|
||||
}
|
||||
@@ -136,45 +198,49 @@ public class ForecastProcessor implements Runnable {
|
||||
{
|
||||
dayForecasts[dayIndex].hiTemp = hi;
|
||||
}
|
||||
|
||||
if (!ValueCheck.valueNoData(percip))
|
||||
{
|
||||
if (ValueCheck.valueNoData(dayForecasts[dayIndex].percipPercent))
|
||||
{
|
||||
dayForecasts[dayIndex].percipPercent = percip;
|
||||
}
|
||||
else
|
||||
{
|
||||
dayForecasts[dayIndex].percipPercent = (dayForecasts[dayIndex].percipPercent + percip) - (percip * dayForecasts[dayIndex].percipPercent) / 100F;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dayForecasts[dayIndex] = new DayForecast(hi, lo, icon, lines.line1, lines.line2, ValueCheck.NO_DATA_FLOAT);
|
||||
dayForecasts[dayIndex] = new DayForecast(hi, lo, icon, lines.line1, lines.line2, percip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ParserConfigurationException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (SAXException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.cleanClose(is);
|
||||
}
|
||||
for (int i = 0; i < dayForecasts.length; i++)
|
||||
{
|
||||
if (dayForecasts[i] == null)
|
||||
for (int i = 0; i < dayForecasts.length; i++)
|
||||
{
|
||||
dayForecasts[i] = new DayForecast();
|
||||
if (dayForecasts[i] == null)
|
||||
{
|
||||
dayForecasts[i] = new DayForecast();
|
||||
}
|
||||
}
|
||||
TownForecast tf = new TownForecast(townInfo.townName + ", " + townInfo.province, dayForecasts);
|
||||
townForecasts.add(tf);
|
||||
this.processHourlyForecast(tf, doc);
|
||||
}
|
||||
townForecasts.add(new TownForecast(townInfo.townName + ", " + townInfo.province, dayForecasts));
|
||||
}
|
||||
forecastDetails.setTownForecast(townForecasts.toArray(new TownForecast[0]));
|
||||
setMostRecentForecast(forecastDetails);
|
||||
}
|
||||
|
||||
public void begin() {
|
||||
if (!running)
|
||||
{
|
||||
running = true;
|
||||
new Thread(this).start();
|
||||
}
|
||||
}
|
||||
|
||||
public void end() {
|
||||
running = false;
|
||||
self.interrupt();
|
||||
@@ -201,8 +267,14 @@ public class ForecastProcessor implements Runnable {
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS));
|
||||
long hourInMillis = 60 * 60 * 1000;
|
||||
long startDateInMillis = System.currentTimeMillis();
|
||||
long millisSinceLastHourChange = startDateInMillis % hourInMillis;
|
||||
long millisToNextHourChange = hourInMillis - millisSinceLastHourChange;
|
||||
Thread.sleep(millisToNextHourChange);
|
||||
processForecasts();
|
||||
this.cdp.notifyForecastProviderUpdate();
|
||||
//Thread.sleep(TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS));
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user