This commit is contained in:
Flare Microsystems
2024-03-15 23:45:13 -07:00
parent 37c506249a
commit 88a566dcbe
5 changed files with 182 additions and 49 deletions

View File

@@ -6,6 +6,7 @@ import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import com.flaremicro.util.Util;
@@ -35,8 +36,11 @@ public class CanadaDatamartProvider extends ForecastProvider {
BufferedReader bufferedReader = null;
try
{
HashSet<String> byCodes = new HashSet<String>(Arrays.asList(byCode.split(",")));
HashSet<String> byNameAndProvinces = new HashSet<String>(Arrays.asList(byNameAndProvince.split(";")));
ArrayList<String> byCodesArray = new ArrayList<String>(Arrays.asList(byCode.split(",")));
ArrayList<String> byNameAndProvincesArray = new ArrayList<String>(Arrays.asList(byNameAndProvince.split(";")));
HashSet<String> byCodes = new HashSet<String>(byCodesArray);
HashSet<String> byNameAndProvinces = new HashSet<String>(byNameAndProvincesArray);
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;
@@ -51,12 +55,21 @@ public class CanadaDatamartProvider extends ForecastProvider {
String[] data = line.trim().split(",");
if (byCodes.contains(data[0].toLowerCase()) || byNameAndProvinces.contains(data[1].toLowerCase() + "," + data[2].toLowerCase()))
{
int priority = Integer.MAX_VALUE;
if(byCodes.contains(data[0].toLowerCase()))
{
priority = byCodesArray.indexOf(data[0]);
}
else if(byNameAndProvinces.contains(data[1].toLowerCase() + "," + data[2].toLowerCase()))
{
priority = byNameAndProvincesArray.indexOf(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));
towns.add(new TownInfo(code, town, province, latitude, longitude, priority));
}
}
}
@@ -69,11 +82,15 @@ public class CanadaDatamartProvider extends ForecastProvider {
{
Util.cleanClose(bufferedReader);
}
forecastProcessor = new ForecastProcessor(towns.toArray(new TownInfo[0]));
Collections.sort(towns);
forecastProcessor = new ForecastProcessor(towns.toArray(new TownInfo[0]), this);
forecastProcessor.processForecasts();
this.getRenderPanel().notifyForecastProviderUpdate();
forecastProcessor.begin();
this.notifyForecastProviderUpdate();
ready = true;
}
@Override
public ForecastDetails getForecast() {
return forecastProcessor.getMostRecentForecast();
@@ -86,6 +103,7 @@ public class CanadaDatamartProvider extends ForecastProvider {
@Override
public void deinit() {
forecastProcessor.end();
propertyManager.store();
}