From 9674c0acfb96ef903ffede2f323127135b418ce0 Mon Sep 17 00:00:00 2001 From: Giorgio Bertolotti Date: Fri, 24 May 2019 23:53:56 +0200 Subject: [PATCH] Fixed issues #10, #11, #12, #13 --- .../main/java/com/zem/pwswatcher/Widget.java | 14 +- .../WidgetConfigurationActivity.java | 14 +- .../zem/pwswatcher/WidgetUpdateService.java | 135 ++++++---- .../java/com/zem/pwswatcher/model/Source.java | 4 + pws_watcher/lib/pws_state.dart | 248 +++++++++++------- pws_watcher/lib/settings.dart | 7 +- 6 files changed, 271 insertions(+), 151 deletions(-) diff --git a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/Widget.java b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/Widget.java index 538dac0..c12fd08 100644 --- a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/Widget.java +++ b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/Widget.java @@ -78,8 +78,18 @@ public void onUpdate(Context context, AppWidgetManager widgetManager, int[] appW PendingIntent service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); long refreshRate = sharedPrefs.getLong("flutter.widget_refresh_interval", 15); manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), refreshRate * 60000, service); - WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id); - dataElaborator.execute(); + if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) { + WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id); + dataElaborator.execute(); + } else { + String originalSource = source.getUrl(); + source.setUrl(originalSource + "/realtime.txt"); + WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id); + dataElaborator.execute(); + source.setUrl(originalSource + "/realtime.xml"); + dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id); + dataElaborator.execute(); + } RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); widgetManager.updateAppWidget(id, views); Log.d("PWSWatcher", "Started Widget #" + id); diff --git a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetConfigurationActivity.java b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetConfigurationActivity.java index 8229672..754b6a5 100644 --- a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetConfigurationActivity.java +++ b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetConfigurationActivity.java @@ -102,8 +102,18 @@ public void onCreate(Bundle icicle) { } long refreshRate = sharedPrefs.getLong("flutter.widget_refresh_interval", 15); manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), refreshRate * 60000, service); - WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId); - dataElaborator.execute(); + if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) { + WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId); + dataElaborator.execute(); + } else { + String originalSource = source.getUrl(); + source.setUrl(originalSource + "/realtime.txt"); + WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId); + dataElaborator.execute(); + source.setUrl(originalSource + "/realtime.xml"); + dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId); + dataElaborator.execute(); + } widgetManager.updateAppWidget(mAppWidgetId, views); Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); diff --git a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetUpdateService.java b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetUpdateService.java index c7e23f2..1afa865 100644 --- a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetUpdateService.java +++ b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/WidgetUpdateService.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.StringReader; +import java.util.Arrays; import okhttp3.Call; import okhttp3.OkHttpClient; @@ -43,8 +44,18 @@ public int onStartCommand(Intent intent, int flags, int startId) { } } if (source != null && id != -1) { - DataElaborator dataElaborator = new DataElaborator(this, source, id); - dataElaborator.execute(); + if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) { + DataElaborator dataElaborator = new DataElaborator(this, source, id); + dataElaborator.execute(); + } else { + String originalSource = source.getUrl(); + source.setUrl(originalSource + "/realtime.txt"); + DataElaborator dataElaborator = new DataElaborator(this, source, id); + dataElaborator.execute(); + source.setUrl(originalSource + "/realtime.xml"); + dataElaborator = new DataElaborator(this, source, id); + dataElaborator.execute(); + } } return super.onStartCommand(intent, flags, startId); } @@ -62,12 +73,12 @@ public DataElaborator(Context context, Source source, int id) { @Override protected String doInBackground(String... params) { - OkHttpClient client = new OkHttpClient.Builder().build(); - Request request = new Request.Builder() - .url(this.source.getUrl()) - .build(); - Call call = client.newCall(request); try { + OkHttpClient client = new OkHttpClient.Builder().build(); + Request request = new Request.Builder() + .url(this.source.getUrl()) + .build(); + Call call = client.newCall(request); Response response = call.execute(); return response.body().string(); } catch (IOException e) { @@ -81,59 +92,85 @@ protected void onPostExecute(String resp) { if (resp == null) return; RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widget); + boolean done = false; try { if (this.source.getUrl().endsWith(".txt")) { - String[] values = resp.split(" "); - view.setTextViewText(R.id.tv_location, this.source.getName()); - view.setTextViewText(R.id.tv_temperature, values[2] + values[14]); - view.setTextViewText(R.id.tv_datetime, values[0] + " " + values[1]); + done = visualizeRealtimeTXT(resp, view); } else if (this.source.getUrl().endsWith(".xml")) { - XmlPullParserFactory parserFactory; - parserFactory = XmlPullParserFactory.newInstance(); - XmlPullParser parser = parserFactory.newPullParser(); - parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); - parser.setInput(new StringReader(resp)); - int eventType = parser.getEventType(); - String date = null, time = null, temp = null, tempunit = null; - while (eventType != XmlPullParser.END_DOCUMENT) { - String eltName = null; - switch (eventType) { - case XmlPullParser.START_TAG: - eltName = parser.getName(); - if ("misc".equals(eltName)) { - for (int i = 0; i < parser.getAttributeCount(); i++) { - if (parser.getAttributeName(i).equals("data") && parser.getAttributeValue(i).equals("station_location")) { - view.setTextViewText(R.id.tv_location, parser.getText()); - } + done = visualizeRealtimeXML(resp, view); + } + } catch (Exception e) { + e.printStackTrace(); + } + if(done) { + AppWidgetManager manager = AppWidgetManager.getInstance(context); + manager.updateAppWidget(this.id, view); + } + } + + private boolean visualizeRealtimeTXT(String resp, RemoteViews view) { + try { + String[] values = resp.split(" "); + view.setTextViewText(R.id.tv_location, this.source.getName()); + view.setTextViewText(R.id.tv_temperature, values[2] + values[14]); + view.setTextViewText(R.id.tv_datetime, values[0] + " " + values[1]); + return true; + } catch(Exception ignored) {} + return false; + } + + private boolean visualizeRealtimeXML(String resp, RemoteViews view) { + try { + XmlPullParserFactory parserFactory; + parserFactory = XmlPullParserFactory.newInstance(); + XmlPullParser parser = parserFactory.newPullParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); + parser.setInput(new StringReader(resp)); + int eventType = parser.getEventType(); + String date = null, time = null, temp = null, tempunit = null; + String[] attributes = {"misc","realtime","today","yesterday","record"}; + while (eventType != XmlPullParser.END_DOCUMENT) { + String eltName = null; + switch (eventType) { + case XmlPullParser.START_TAG: + eltName = parser.getName(); + if ("misc".equals(eltName)) { + for (int i = 0; i < parser.getAttributeCount(); i++) { + if (parser.getAttributeName(i).equals("data") && parser.getAttributeValue(i).equals("station_location")) { + view.setTextViewText(R.id.tv_location, parser.nextText()); } } - if ("data".equals(eltName)) { - for (int i = 0; i < parser.getAttributeCount(); i++) { - if (parser.getAttributeName(i).equals("realtime")) { - if (parser.getAttributeValue(i).equals("temp")) { - temp = parser.getText(); - } else if (parser.getAttributeValue(i).equals("tempunit")) { - tempunit = parser.getText(); - } else if (parser.getAttributeValue(i).equals("station_date")) { - date = parser.getText(); - } else if (parser.getAttributeValue(i).equals("station_time")) { - time = parser.getText(); - } + } + if ("data".equals(eltName)) { + for (int i = 0; i < parser.getAttributeCount(); i++) { + if (Arrays.asList(attributes).contains(parser.getAttributeName(i))) { + if (parser.getAttributeValue(i).equals("temp")) { + temp = parser.nextText(); + } else if (parser.getAttributeValue(i).equals("tempunit")) { + tempunit = parser.nextText(); + } else if (parser.getAttributeValue(i).equals("station_date")) { + date = parser.nextText(); + } else if (parser.getAttributeValue(i).equals("station_time")) { + time = parser.nextText(); + } else if (parser.getAttributeValue(i).equals("location")) { + view.setTextViewText(R.id.tv_location, parser.nextText()); + } else if (parser.getAttributeValue(i).equals("refresh_time")) { + date = parser.nextText(); } } } - break; - } - eventType = parser.next(); + } + break; } - view.setTextViewText(R.id.tv_temperature, ((temp != null) ? temp : "") + ((tempunit != null) ? tempunit : "")); - view.setTextViewText(R.id.tv_datetime, ((date != null) ? date + " " : "") + ((time != null) ? time : "")); + eventType = parser.next(); } - } catch (XmlPullParserException e) { - } catch (IOException e) { + view.setTextViewText(R.id.tv_temperature, ((temp != null) ? temp : "") + ((tempunit != null) ? tempunit : "")); + view.setTextViewText(R.id.tv_datetime, ((date != null) ? date + " " : "") + ((time != null) ? time : "")); + return true; + } catch(Exception e) { + e.printStackTrace(); } - AppWidgetManager manager = AppWidgetManager.getInstance(context); - manager.updateAppWidget(this.id, view); + return false; } } } diff --git a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/model/Source.java b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/model/Source.java index 68f7cd4..184c08e 100644 --- a/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/model/Source.java +++ b/pws_watcher/android/app/src/main/java/com/zem/pwswatcher/model/Source.java @@ -26,6 +26,10 @@ public String getUrl() { return url; } + public void setUrl(String url) { + this.url = url; + } + public String toJSON() throws JSONException { JSONObject root = new JSONObject(); root.put("id", this.id); diff --git a/pws_watcher/lib/pws_state.dart b/pws_watcher/lib/pws_state.dart index c532125..3dddb74 100644 --- a/pws_watcher/lib/pws_state.dart +++ b/pws_watcher/lib/pws_state.dart @@ -46,7 +46,7 @@ class _PWSStatusPageState extends State { var windunit = "km/h"; var rainunit = "mm"; var barunit = "mb"; - var tempunit = "C°"; + var tempunit = "°C"; var degunit = "°"; var humunit = "%"; @@ -222,7 +222,7 @@ class _PWSStatusPageState extends State { "You are offline.", textAlign: TextAlign.center, style: TextStyle( - fontSize: 18, color: Colors.white70), + fontSize: 20, color: Colors.white70), ), Container( height: @@ -252,8 +252,8 @@ class _PWSStatusPageState extends State { "$location", maxLines: 1, style: TextStyle( - fontSize: 18, - fontWeight: FontWeight.w100, + fontSize: 20, + fontWeight: FontWeight.normal, color: Colors.white70, ), ), @@ -267,8 +267,8 @@ class _PWSStatusPageState extends State { "$date $time", maxLines: 1, style: TextStyle( - fontSize: 14, - fontWeight: FontWeight.w100, + fontSize: 16, + fontWeight: FontWeight.normal, color: Colors.white70, ), ), @@ -286,7 +286,7 @@ class _PWSStatusPageState extends State { maxLines: 1, style: TextStyle( fontSize: 72, - fontWeight: FontWeight.w100, + fontWeight: FontWeight.bold, color: Colors.white, ), ), @@ -321,9 +321,9 @@ class _PWSStatusPageState extends State { "$windspeed", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -331,9 +331,9 @@ class _PWSStatusPageState extends State { "$windunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -351,9 +351,9 @@ class _PWSStatusPageState extends State { "$bar", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -361,9 +361,9 @@ class _PWSStatusPageState extends State { "$barunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -412,9 +412,9 @@ class _PWSStatusPageState extends State { "$winddir", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -432,9 +432,9 @@ class _PWSStatusPageState extends State { "$humidity", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -442,9 +442,9 @@ class _PWSStatusPageState extends State { "$humunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -493,9 +493,9 @@ class _PWSStatusPageState extends State { "$temperature", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -503,9 +503,9 @@ class _PWSStatusPageState extends State { "$tempunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -523,9 +523,9 @@ class _PWSStatusPageState extends State { "$windchill", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -533,9 +533,9 @@ class _PWSStatusPageState extends State { "$degunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -584,9 +584,9 @@ class _PWSStatusPageState extends State { "$rain", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -594,9 +594,9 @@ class _PWSStatusPageState extends State { "$rainunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -614,9 +614,9 @@ class _PWSStatusPageState extends State { "$dew", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -624,9 +624,9 @@ class _PWSStatusPageState extends State { "$degunit", maxLines: 1, style: TextStyle( - fontSize: 14, + fontSize: 16, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -673,9 +673,9 @@ class _PWSStatusPageState extends State { "$sunrise", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -704,9 +704,9 @@ class _PWSStatusPageState extends State { "$sunset", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -735,9 +735,9 @@ class _PWSStatusPageState extends State { "$moonrise", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -766,9 +766,9 @@ class _PWSStatusPageState extends State { "$moonset", maxLines: 1, style: TextStyle( - fontSize: 18, + fontSize: 20, fontWeight: - FontWeight.w100, + FontWeight.normal, color: Colors.white70, ), ), @@ -911,7 +911,7 @@ class _PWSStatusPageState extends State { windunit = "km/h"; rainunit = "mm"; barunit = "mb"; - tempunit = "C°"; + tempunit = "°C"; degunit = "°"; humunit = "%"; }); @@ -931,8 +931,10 @@ class _PWSStatusPageState extends State { if (map == null) return; _visualizeRealtimeTXT(map); }); - } else - return null; + } else { + _retrieveData(url + "/realtime.xml"); + return _retrieveData(url + "/realtime.txt"); + } } Future> _parseRealtimeXML(String url) async { @@ -955,7 +957,13 @@ class _PWSStatusPageState extends State { var variable; try { variable = elem.attributes - .firstWhere((attr) => attr.name.toString() == "realtime") + .firstWhere((attr) => [ + "misc", + "realtime", + "today", + "yesterday", + "record", + ].contains(attr.name.toString())) .value; } catch (Exception) {} if (variable != null) { @@ -1047,58 +1055,99 @@ class _PWSStatusPageState extends State { _visualizeRealtimeXML(map) { setState(() { - location = (map.containsKey("station_location") - ? map["station_location"] - : ((widget.source != null) ? widget.source.name : "Location")); - date = (map.containsKey("station_date") - ? map["station_date"] - : "--/--/----"); - time = - (map.containsKey("station_time") ? map["station_time"] : "--:--:--"); - windspeed = (map.containsKey("windspeed") ? map["windspeed"] : "-"); - bar = (map.containsKey("barometer") ? map["barometer"] : "-"); - winddir = (map.containsKey("winddir") ? map["winddir"] : "-"); - humidity = (map.containsKey("hum") ? map["hum"] : "-"); - temperature = (map.containsKey("temp") ? map["temp"] : "-"); - windchill = (map.containsKey("windchill") ? map["windchill"] : "-"); - rain = (map.containsKey("todaysrain") ? map["todaysrain"] : "-"); - dew = (map.containsKey("dew") ? map["dew"] : "-"); - sunrise = (map.containsKey("sunrise") ? map["sunrise"] : "--:--"); - sunset = (map.containsKey("sunset") ? map["sunset"] : "--:--"); - moonrise = (map.containsKey("moonrise") ? map["moonrise"] : "--:--"); - moonset = (map.containsKey("moonset") ? map["moonset"] : "--:--"); - windunit = (map.containsKey("windunit") ? map["windunit"] : "km/h"); - rainunit = (map.containsKey("rainunit") ? map["rainunit"] : "mm"); - barunit = (map.containsKey("barunit") ? map["barunit"] : "mb"); - tempunit = (map.containsKey("tempunit") ? map["tempunit"] : "C°"); - humunit = (map.containsKey("humunit") ? map["humunit"] : "%"); + if (map.containsKey("station_location")) + location = map["station_location"]; + else if (map.containsKey("location")) + location = map["location"]; + else if (widget.source != null) location = widget.source.name; + if (map.containsKey("station_date")) + date = map["station_date"]; + else if (map.containsKey("refresh_time")) + date = map["refresh_time"].substring(0, 10); + if (map.containsKey("station_time")) + time = map["station_time"]; + else if (map.containsKey("refresh_time")) + time = map["refresh_time"].substring(12); + if (map.containsKey("windspeed")) + windspeed = map["windspeed"]; + else if (map.containsKey("avg_windspeed")) + windspeed = map["avg_windspeed"]; + if (map.containsKey("barometer")) + bar = map["barometer"]; + else if (map.containsKey("press")) bar = map["press"]; + if (map.containsKey("winddir")) winddir = map["winddir"]; + if (map.containsKey("hum")) humidity = map["hum"]; + if (map.containsKey("temp")) temperature = map["temp"]; + if (map.containsKey("windchill")) + windchill = map["windchill"]; + else if (map.containsKey("wchill")) windchill = map["wchill"]; + if (map.containsKey("todaysrain")) + rain = map["todaysrain"]; + else if (map.containsKey("today_rainfall")) rain = map["today_rainfall"]; + if (map.containsKey("dew")) dew = map["dew"]; + if (map.containsKey("sunrise")) sunrise = map["sunrise"]; + if (map.containsKey("sunset")) sunset = map["sunset"]; + if (map.containsKey("moonrise")) moonrise = map["moonrise"]; + if (map.containsKey("moonset")) moonset = map["moonset"]; + if (_isNumeric(windspeed)) { + if (map.containsKey("windunit")) windunit = map["windunit"]; + } else + windunit = ""; + if (_isNumeric(rain)) { + if (map.containsKey("rainunit")) rainunit = map["rainunit"]; + } else + rainunit = ""; + if (_isNumeric(bar)) { + if (map.containsKey("barunit")) barunit = map["barunit"]; + } else + barunit = ""; + if (_isNumeric(temperature)) { + if (map.containsKey("tempunit")) tempunit = map["tempunit"]; + } else + tempunit = ""; + if (_isNumeric(humidity)) { + if (map.containsKey("humunit")) humunit = map["humunit"]; + } else + humunit = ""; + degunit = (_isNumeric(dew) ? "°" : ""); }); } _visualizeRealtimeTXT(map) { setState(() { - location = ((widget.source != null) ? widget.source.name : "Location"); - date = (map.containsKey("date") ? map["date"] : "--/--/----"); - time = (map.containsKey("timehhmmss") ? map["timehhmmss"] : "--:--:--"); - windspeed = (map.containsKey("wspeed") ? map["wspeed"] : "-"); - bar = (map.containsKey("press") ? map["press"] : "-"); - winddir = (map.containsKey("currentwdir") ? map["currentwdir"] : "-"); - humidity = (map.containsKey("hum") ? map["hum"] : "-"); - temperature = (map.containsKey("temp") ? map["temp"] : "-"); - windchill = (map.containsKey("wchill") ? map["wchill"] : "-"); - rain = (map.containsKey("rfall") ? map["rfall"] : "-"); - dew = (map.containsKey("dew") ? map["dew"] : "-"); - // these four properties cannot be retrieved from realtime.txt - sunrise = "--:--"; - sunset = "--:--"; - moonrise = "--:--"; - moonset = "--:--"; - windunit = (map.containsKey("windunit") ? map["windunit"] : "km/h"); - rainunit = (map.containsKey("rainunit") ? map["rainunit"] : "mm"); - barunit = (map.containsKey("pressunit") ? map["pressunit"] : "mb"); - tempunit = - (map.containsKey("tempunitnodeg") ? map["tempunitnodeg"] : "C°"); - humunit = (map.containsKey("humunit") ? map["humunit"] : "%"); + if (widget.source != null) location = widget.source.name; + if (map.containsKey("date")) date = map["date"]; + if (map.containsKey("timehhmmss")) time = map["timehhmmss"]; + if (map.containsKey("wspeed")) windspeed = map["wspeed"]; + if (map.containsKey("press")) bar = map["press"]; + if (map.containsKey("currentwdir")) winddir = map["currentwdir"]; + if (map.containsKey("hum")) humidity = map["hum"]; + if (map.containsKey("temp")) temperature = map["temp"]; + if (map.containsKey("wchill")) windchill = map["wchill"]; + if (map.containsKey("rfall")) rain = map["rfall"]; + if (map.containsKey("dew")) dew = map["dew"]; + // data about sunrise, sunset, moonrise and moonset cannot be retrieved from realtime.txt + if (_isNumeric(windspeed)) { + if (map.containsKey("windunit")) windunit = map["windunit"]; + } else + windunit = ""; + if (_isNumeric(rain)) { + if (map.containsKey("rainunit")) rainunit = map["rainunit"]; + } else + rainunit = ""; + if (_isNumeric(bar)) { + if (map.containsKey("pressunit")) barunit = map["pressunit"]; + } else + barunit = ""; + if (_isNumeric(temperature)) { + if (map.containsKey("tempunitnodeg")) tempunit = map["tempunitnodeg"]; + } else + tempunit = ""; + if (_isNumeric(humidity)) { + if (map.containsKey("humunit")) humunit = map["humunit"]; + } else + humunit = ""; + degunit = (_isNumeric(dew) ? "°" : ""); }); } @@ -1122,4 +1171,11 @@ class _PWSStatusPageState extends State { } else return null; } + + bool _isNumeric(String str) { + if (str == null) { + return false; + } + return double.tryParse(str) != null; + } } diff --git a/pws_watcher/lib/settings.dart b/pws_watcher/lib/settings.dart index a7398eb..c166081 100644 --- a/pws_watcher/lib/settings.dart +++ b/pws_watcher/lib/settings.dart @@ -508,7 +508,8 @@ class _SettingsPageState extends State { return "You must set a source url."; }, decoration: InputDecoration.collapsed( - hintText: "URL", border: UnderlineInputBorder()), + hintText: "Realtime file URL", + border: UnderlineInputBorder()), maxLines: 1, ), ), @@ -595,7 +596,9 @@ class _SettingsPageState extends State { return "You must set a source url."; }, decoration: InputDecoration.collapsed( - hintText: "URL", border: UnderlineInputBorder()), + hintText: "Realtime file URL", + border: UnderlineInputBorder(), + ), maxLines: 1, ), ),