Initial Commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package com.flaremicro.visualforecast.datamart;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.flaremicro.util.Util;
|
||||
import com.flaremicro.visualforecast.PropertyManager;
|
||||
import com.flaremicro.visualforecast.api.ForecastProvider;
|
||||
import com.flaremicro.visualforecast.forecast.ForecastDetails;
|
||||
|
||||
public class CanadaDatamartProvider extends ForecastProvider {
|
||||
boolean ready = false;
|
||||
PropertyManager propertyManager;
|
||||
ForecastProcessor forecastProcessor;
|
||||
|
||||
ArrayList<TownInfo> towns = new ArrayList<TownInfo>();
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
propertyManager = super.getOwnPropertyManager();
|
||||
String byCode = propertyManager.getString("towns-by-code", "").toLowerCase();
|
||||
String byNameAndProvince = propertyManager.getString("towns-by-name-and-province", "").toLowerCase();
|
||||
|
||||
if(byCode.isEmpty() && byNameAndProvince.isEmpty())
|
||||
{
|
||||
ready = true;
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedReader bufferedReader = null;
|
||||
try
|
||||
{
|
||||
HashSet<String> byCodes = new HashSet<String>(Arrays.asList(byCode.split(",")));
|
||||
HashSet<String> byNameAndProvinces = new HashSet<String>(Arrays.asList(byNameAndProvince.split(";")));
|
||||
URL url = new URL("https://dd.weather.gc.ca/citypage_weather/docs/site_list_towns_en.csv");
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
String line;
|
||||
int skip = 2;
|
||||
while ((line = bufferedReader.readLine()) != null)
|
||||
{
|
||||
if (skip > 0)
|
||||
{
|
||||
skip--;
|
||||
continue;
|
||||
}
|
||||
String[] data = line.trim().split(",");
|
||||
if (byCodes.contains(data[0].toLowerCase()) || byNameAndProvinces.contains(data[1].toLowerCase() + "," + data[2].toLowerCase()))
|
||||
{
|
||||
String code = data[0].trim();
|
||||
String town = data[1].trim();
|
||||
String province = data[2].trim();
|
||||
float latitude = Float.parseFloat(data[3].trim().substring(0, data[3].length()-1));
|
||||
float longitude = Float.parseFloat(data[4].trim().substring(0, data[4].length()-1));
|
||||
towns.add(new TownInfo(code, town, province, latitude, longitude));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
ready = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.cleanClose(bufferedReader);
|
||||
}
|
||||
forecastProcessor = new ForecastProcessor(towns.toArray(new TownInfo[0]));
|
||||
forecastProcessor.processForecasts();
|
||||
ready = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForecastDetails getForecast() {
|
||||
return forecastProcessor.getMostRecentForecast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForecastReady() {
|
||||
return ready;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deinit() {
|
||||
propertyManager.store();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user