Skip to content

Commit

Permalink
Added code to handle WMO Forecasts, squashed BoM bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
evilbunny2008 committed Nov 21, 2018
1 parent 3e63814 commit af7d52d
Show file tree
Hide file tree
Showing 51 changed files with 187 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.odiousapps.weewxweather"
minSdkVersion 19
targetSdkVersion 28
versionCode 6002
versionName "0.6.2"
versionCode 6003
versionName "0.6.3"
}
buildTypes {
release {
Expand Down
114 changes: 106 additions & 8 deletions app/src/main/java/com/odiousapps/weewxweather/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.RemoteViews;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
Expand Down Expand Up @@ -321,6 +322,97 @@ RemoteViews buildUpdate(Context context)
return views;
}

String[] processWMO(String data)
{
return processWMO(data, false);
}

String[] processWMO(String data, boolean showHeader)
{
if(data == null || data.equals(""))
return null;

String desc = "";
StringBuilder out = new StringBuilder();
boolean metric = GetBoolPref("metric", true);

try
{
JSONObject jobj = new JSONObject(data);

desc = jobj.getJSONObject("city").getString("cityName") + ", " + jobj.getJSONObject("city").getJSONObject("member").getString("memName");
String tmp = jobj.getJSONObject("city").getJSONObject("forecast").getString("issueDate");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
long mdate = sdf.parse(tmp).getTime();
sdf = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String date = sdf.format(mdate);

tmp = "<div style='font-size:12pt;'>" + date + "</div>";
out.append(tmp);
tmp = "<table style='width:100%;'>\n";
out.append(tmp);

JSONArray jarr = jobj.getJSONObject("city").getJSONObject("forecast").getJSONArray("forecastDay");
for(int i = 0; i < jarr.length(); i++)
{
JSONObject j = jarr.getJSONObject(i);

date = j.getString("forecastDate").trim();
sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
mdate = sdf.parse(date).getTime();
sdf = new SimpleDateFormat("EEEE", Locale.getDefault());
date = sdf.format(mdate);

String text = j.getString("weather");
String max = j.getString("maxTemp") + "&deg;C";
String min = j.getString("minTemp") + "&deg;C";
if(!metric)
{
max = j.getString("maxTempF") + "&deg;F";
min = j.getString("minTempF") + "&deg;F";
}

String code = j.getString("weatherIcon");
code = code.substring(0, 2);

tmp = "<tr><td style='width:10%;' rowspan='2'>" + "<img width='40px' src='file:///android_res/drawable/i" + code + ".png'></td>";
out.append(tmp);

tmp = "<td style='width:80%;'><b>" + date + "</b></td>";
out.append(tmp);

if(!max.equals(""))
tmp = "<td style='width:10%;text-align:right;'><b>" + max + "</b></td></tr>";
else
tmp = "<td style='width:10%;text-align:right;'><b>&nbsp;</b></td></tr>";
out.append(tmp);

tmp = "<tr><td>" + text + "</td>";
out.append(tmp);

if(!min.equals(""))
tmp = "<td style='text-align:right;'>" + min + "</td></tr>";
else
tmp = "<td style='text-align:right;'>&nbsp;</td></tr>";
out.append(tmp);

if(showHeader)
{
tmp = "<tr><td style='font-size:10pt;' colspan='5'>&nbsp;</td></tr>";
out.append(tmp);
}
}

out.append("</table>");
} catch (Exception e) {
e.printStackTrace();
return null;
}

return new String[]{out.toString(), desc};
}

String[] processBOM(String data)
{
return processBOM(data, false);
Expand All @@ -337,7 +429,7 @@ String[] processBOM(String data, boolean showHeader)
try
{
JSONObject jobj = new JSONObject(data);
desc = jobj.getString("description");
desc = jobj.getString("description") + ", Australia";

String tmp = jobj.getString("content");
tmp = tmp.substring(0, tmp.length() - 6);
Expand Down Expand Up @@ -368,16 +460,22 @@ String[] processBOM(String data, boolean showHeader)
}
}

for(int x = 0; x < j.getJSONArray("element").length(); x++)
try
{
if(j.getJSONArray("element").getJSONObject(x).getString("type").equals("forecast_icon_code"))
code = j.getJSONArray("element").getJSONObject(x).getString("content");
JSONArray jarr2 = j.getJSONArray("element");
for (int x = 0; x < jarr2.length(); x++)
{
if (jarr2.getJSONObject(x).getString("type").equals("forecast_icon_code"))
code = jarr2.getJSONObject(x).getString("content");

if(j.getJSONArray("element").getJSONObject(x).getString("type").equals("air_temperature_minimum"))
min = j.getJSONArray("element").getJSONObject(x).getString("content");
if (jarr2.getJSONObject(x).getString("type").equals("air_temperature_minimum"))
min = jarr2.getJSONObject(x).getString("content");

if(j.getJSONArray("element").getJSONObject(x).getString("type").equals("air_temperature_maximum"))
max = j.getJSONArray("element").getJSONObject(x).getString("content");
if (jarr2.getJSONObject(x).getString("type").equals("air_temperature_maximum"))
max = jarr2.getJSONObject(x).getString("content");
}
} catch (JSONException e) {
code = j.getJSONObject("element").getString("content");
}

date = j.getString("start-time-local").trim();
Expand Down
44 changes: 43 additions & 1 deletion app/src/main/java/com/odiousapps/weewxweather/Forecast.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import com.github.rongi.rotate_layout.layout.RotateLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -32,6 +35,8 @@
import java.net.URL;
import java.net.URLConnection;

import fr.arnaudguyon.xmltojsonlib.XmlToJson;

class Forecast
{
private Common common;
Expand Down Expand Up @@ -492,9 +497,36 @@ public void run()
}
in.close();

String tmp = sb.toString().trim();
if(common.GetStringPref("fctype", "Yahoo").equals("bom.gov.au"))
{
try
{
JSONObject jobj = new XmlToJson.Builder(tmp).build().toJson();
if(jobj == null)
return;

jobj = jobj.getJSONObject("product");
String content = jobj.getJSONObject("amoc").getJSONObject("issue-time-local").getString("content");
JSONArray area = jobj.getJSONObject("forecast").getJSONArray("area");
for (int i = 0; i < area.length(); i++)
{
JSONObject o = area.getJSONObject(i);
if (o.getString("description").equals(common.GetStringPref("bomtown", "")))
{
o.put("content", content);
tmp = o.toString();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

Common.LogMessage("updating rss cache");
common.SetIntPref("rssCheck", curtime);
common.SetStringPref("forecastData", sb.toString().trim());
common.SetStringPref("forecastData", tmp);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -573,6 +605,13 @@ private void generateForecast()
updateForecast(content[0], content[1]);
break;
}
case "wmo.int":
{
String[] content = common.processWMO(data, true);
if(content != null && content.length >= 2)
updateForecast(content[0], content[1]);
break;
}
}
}

Expand Down Expand Up @@ -606,6 +645,9 @@ public void run()
case "bom.gov.au":
im.setImageResource(R.drawable.bom);
break;
case "wmo.int":
im.setImageResource(R.drawable.wmo);
break;
}
}
}
18 changes: 17 additions & 1 deletion app/src/main/java/com/odiousapps/weewxweather/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ public void onFocusChange(View v, boolean hasFocus)
"Weather Icons from <a href='https://www.flaticon.com/'>FlatIcon</a> and " +
"is licensed under <a href='http://creativecommons.org/licenses/by/3.0/'>CC 3.0 BY</a><br><br>" +
"Forecasts supplied by <a href='https://www.yahoo.com/?ilc=401'>Yahoo!</a>, <a href='https://weatherzone.com.au'>weatherzone</a> and " +
"<a href='https://hjelp.yr.no/hc/en-us/articles/360001940793-Free-weather-data-service-from-Yr'>yr.no</a><br><br>" +
"<a href='https://hjelp.yr.no/hc/en-us/articles/360001940793-Free-weather-data-service-from-Yr'>yr.no</a>" +
"<a href='https://bom.gov.au'>Bureau of Meteorology</a><br><br>" +
"weeWX Weather App v" + common.getAppversion() + " is by <a href='https://odiousapps.com'>OdiousApps</a>.</body</html>";

tv.setText(Html.fromHtml(lines));
Expand Down Expand Up @@ -679,6 +680,11 @@ protected PasswordAuthentication getPasswordAuthentication()
Common.LogMessage("fctype=" + fctype);
Common.LogMessage("bomtown=" + bomtown);
break;
case "wmo.int":
forecast = "https://worldweather.wmo.int/en/json/" + forecast.trim() + "_en.xml";
Common.LogMessage("forecast=" + forecast);
Common.LogMessage("fctype=" + fctype);
break;
default:
common.SetStringPref("lastError", "forecast type " + fctype + " is invalid, check your settings file and try again.");
handlerForecast.sendEmptyMessage(0);
Expand Down Expand Up @@ -717,6 +723,8 @@ protected PasswordAuthentication getPasswordAuthentication()
}
in.close();

boolean found = false;

String tmp = sb.toString().trim();
if(fctype.equals("bom.gov.au"))
{
Expand All @@ -730,9 +738,17 @@ protected PasswordAuthentication getPasswordAuthentication()
{
o.put("content", content);
tmp = o.toString();
found = true;
break;
}
}

if(!found)
{
common.SetStringPref("lastError", "Unable to match '" + common.GetStringPref("bomtown", "") + "'. Make sure you selected the right state file ID and a town where the BoM produces forecasts.");
handlerForecast.sendEmptyMessage(0);
return;
}
}

Common.LogMessage("updating rss cache");
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/odiousapps/weewxweather/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,25 @@ public void run()
});
break;
}
case "wmo.int":
{
String[] content = common.processWMO(data);
if(content == null || content.length <= 0)
return;

String wmo = "<img src='wmo.png' height='29px'/><br/>";
final String fc = "<html><body style='text-align:center'>" + wmo + content[0] + "</body></html>";

wv.post(new Runnable()
{
@Override
public void run()
{
wv.loadDataWithBaseURL("file:///android_res/drawable/", fc, "text/html", "utf-8", null);
}
});
break;
}
}
}
}
Expand Down
Binary file added app/src/main/res/drawable/i01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i21a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i21b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i22a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/i22b.png
Binary file added app/src/main/res/drawable/i23.png
Binary file added app/src/main/res/drawable/i23a.png
Binary file added app/src/main/res/drawable/i23b.png
Binary file added app/src/main/res/drawable/i24.png
Binary file added app/src/main/res/drawable/i24a.png
Binary file added app/src/main/res/drawable/i24b.png
Binary file added app/src/main/res/drawable/i25.png
Binary file added app/src/main/res/drawable/i25a.png
Binary file added app/src/main/res/drawable/i25b.png
Binary file added app/src/main/res/drawable/i26.png
Binary file added app/src/main/res/drawable/i27.png
Binary file added app/src/main/res/drawable/i28.png
Binary file added app/src/main/res/drawable/i29.png
Binary file added app/src/main/res/drawable/i30.png
Binary file added app/src/main/res/drawable/i31.png
Binary file added app/src/main/res/drawable/i32.png
Binary file added app/src/main/res/drawable/i33.png
Binary file added app/src/main/res/drawable/i34.png
Binary file added app/src/main/res/drawable/i35.png
Binary file added app/src/main/res/drawable/wmo.png

0 comments on commit af7d52d

Please sign in to comment.