Files
VF1000-CanadaDatamartProvider/src/com/flaremicro/visualforecast/datamart/CanadaDatamartProvider.java
Vulpovile 61281e6167 A lot
2025-04-09 21:56:21 -07:00

125 lines
4.0 KiB
Java

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.Collections;
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
{
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;
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()))
{
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();
String supportedDisplaysString = propertyManager.getStringNoSet("displays-enabled.code."+code, "");
if(supportedDisplaysString.trim().length() <= 0)
supportedDisplaysString = propertyManager.getString("displays-enabled.town."+town.toLowerCase().replaceAll("\\s", "_")+"."+province.toLowerCase().replace("\\s", "_"), "");
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));
TownInfo townInfo = new TownInfo(code, town, province, latitude, longitude, priority);
if(supportedDisplaysString.trim().length() > 0)
{
String[] displays = supportedDisplaysString.split(",");
for(int i = 0; i < displays.length; i++)
{
displays[i] = displays[i].trim();
}
townInfo.setSupportedDisplays(displays);
}
towns.add(townInfo);
}
}
}
catch (IOException e)
{
e.printStackTrace();
ready = true;
}
finally
{
Util.cleanClose(bufferedReader);
}
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();
}
@Override
public boolean isForecastReady() {
return ready;
}
@Override
public void deinit() {
forecastProcessor.end();
propertyManager.store();
}
}