Discussions about RaZberry - Z-Wave board for Raspberry computer
pz1
Posts: 2053 Joined: 08 Apr 2012 13:44
Post
by pz1 » 28 Feb 2015 14:41
My next try is with a deeper XML representation, where the path to the values involves more steps. Here is a partially collapsed view on some XML weather data
Loog.PNG (28.13 KiB) Viewed 9958 times
I use the following code to parse:
Code: Select all
Wunderground.prototype.fetchWeather = function (instance) {
var self = instance;
http.request({
url : "http://api.wunderground.com/api/" + self.config.key + "/conditions/forecast/q/" + self.config.country + "/" + self.config.city + ".xml",
method : "GET",
async : true,
success : function (response) {
var doc = response.data; // it is already ZXmlDocument
try {
temp = parseFloat(doc.findone("/response/current_observation/temp_c/text()"));
console.log(temp);
self.vDev.set("metrics:level", temp);
} catch (e) {
self.controller.addNotification("error", "Can not parse weather information", "module");
}
},
error : function () {
self.controller.addNotification("error", "Can not fetch weather information", "module");
}
});
};
I do get the "Can not parse weather information" error message here. Console log does not get any values written to it.
note: This code is derived from my Wunderground module that works correctly when based on JSON, except for a nasty naming issue that I try to avoid by using the XML data representation.
pofs
Posts: 688 Joined: 25 Mar 2011 19:03
Post
by pofs » 28 Feb 2015 14:53
Probably response content type is not really an XML (or parse has failed), so response.data is not an xml document and has no findone() method. Try dumping response object first.
pz1
Posts: 2053 Joined: 08 Apr 2012 13:44
Post
by pz1 » 28 Feb 2015 15:08
In my Altova XMLSpy tool the retrieved code qualifies as
valid XML.
Code: Select all
<response>
<version>0.1</version>
<termsofService>http://www.wunderground.com/weather/api/d/terms.html</termsofService>
<features>
<feature>conditions</feature>
<feature>forecast</feature>
</features>
<current_observation>
<image>
<url>http://icons.wxug.com/graphics/wu2/logo_130x80.png</url>
<title>Weather Underground</title>
<link>http://www.wunderground.com</link>
</image>
<display_location>
<full>Lauwersoog, Netherlands</full>
<city>Lauwersoog</city>
<state></state>
<state_name>Netherlands</state_name>
<country>NL</country>
<country_iso3166>NL</country_iso3166>
<zip>00000</zip>
<magic>1</magic>
<wmo>06277</wmo>
<latitude>53.41999817</latitude>
<longitude>6.19999981</longitude>
<elevation>0.00000000</elevation>
</display_location>
<observation_location>
<full>Kollum, Kollum, FRIESLAND</full>
<city>Kollum, Kollum</city>
<state>FRIESLAND</state>
<country>NETHERLANDS</country>
<country_iso3166>NL</country_iso3166>
<latitude>53.286213</latitude>
<longitude>6.149729</longitude>
<elevation>0 ft</elevation>
</observation_location>
<estimated>
</estimated>
<station_id>IFRIESLA71</station_id>
<observation_time>Last Updated on February 28, 1:00 PM CET</observation_time>
<observation_time_rfc822>Sat, 28 Feb 2015 13:00:08 +0100</observation_time_rfc822>
<observation_epoch>1425124808</observation_epoch>
<local_time_rfc822>Sat, 28 Feb 2015 13:00:24 +0100</local_time_rfc822>
<local_epoch>1425124824</local_epoch>
<local_tz_short>CET</local_tz_short>
<local_tz_long>Europe/Amsterdam</local_tz_long>
<local_tz_offset>+0100</local_tz_offset>
<weather>Clear</weather>
<temperature_string>43.2 F (6.2 C)</temperature_string>
<temp_f>43.2</temp_f>
<temp_c>6.2</temp_c>
<relative_humidity>75%</relative_humidity>
<wind_string>From the South at 18.8 MPH Gusting to 25.7 MPH</wind_string>
<wind_dir>South</wind_dir>
<wind_degrees>180</wind_degrees>
<wind_mph>18.8</wind_mph>
<wind_gust_mph>25.7</wind_gust_mph>
<wind_kph>30.3</wind_kph>
<wind_gust_kph>41.4</wind_gust_kph>
<pressure_mb>1013</pressure_mb>
<pressure_in>29.92</pressure_in>
<pressure_trend>0</pressure_trend>
<dewpoint_string>36 F (2 C)</dewpoint_string>
<dewpoint_f>36</dewpoint_f>
<dewpoint_c>2</dewpoint_c>
<heat_index_string>NA</heat_index_string>
<heat_index_f>NA</heat_index_f>
<heat_index_c>NA</heat_index_c>
<windchill_string>35 F (2 C)</windchill_string>
<windchill_f>35</windchill_f>
<windchill_c>2</windchill_c>
<feelslike_string>35 F (2 C)</feelslike_string>
<feelslike_f>35</feelslike_f>
<feelslike_c>2</feelslike_c>
<visibility_mi>6.2</visibility_mi>
<visibility_km>10.0</visibility_km>
<solarradiation></solarradiation>
<UV>-1</UV>
<precip_1hr_string>0.00 in ( 0 mm)</precip_1hr_string>
<precip_1hr_in>0.00</precip_1hr_in>
<precip_1hr_metric> 0</precip_1hr_metric>
<precip_today_string>0.00 in (0 mm)</precip_today_string>
<precip_today_in>0.00</precip_today_in>
<precip_today_metric>0</precip_today_metric>
<icon>clear</icon>
<icon_url>http://icons.wxug.com/i/c/k/clear.gif</icon_url>
<forecast_url>http://www.wunderground.com/global/stations/06277.html</forecast_url>
<history_url>http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IFRIESLA71</history_url>
<ob_url>http://www.wunderground.com/cgi-bin/findweather/getForecast?query=53.286213,6.149729</ob_url>
</current_observation>
<forecast>
<txt_forecast>
<date>11:35 AM CET</date>
<forecastdays>
<forecastday>
<period>0</period>
<icon>cloudy</icon>
<icon_url>http://icons.wxug.com/i/c/k/cloudy.gif</icon_url>
<title>Saturday</title>
<fcttext><![CDATA[Generally cloudy. High 44F. Winds S at 15 to 25 mph.]]></fcttext>
<fcttext_metric><![CDATA[Cloudy skies. High 7C. Winds S at 25 to 40 km/h.]]></fcttext_metric>
<pop>0</pop>
</forecastday>
<forecastday>
<period>1</period>
<icon>nt_rain</icon>
<icon_url>http://icons.wxug.com/i/c/k/nt_rain.gif</icon_url>
<title>Saturday Night</title>
<fcttext><![CDATA[Windy with periods of rain. Low 41F. Winds S at 25 to 35 mph. Chance of rain 90%.]]></fcttext>
<fcttext_metric><![CDATA[Windy with periods of rain. Low around 5C. Winds S at 30 to 50 km/h. Chance of rain 90%.]]></fcttext_metric>
<pop>90</pop>
</forecastday>
<forecastday>
<period>2</period>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<title>Sunday</title>
<fcttext><![CDATA[Becoming partly cloudy after some morning light rain. Windy at times. High near 45F. Winds WSW at 25 to 35 mph. Chance of rain 70%.]]></fcttext>
<fcttext_metric><![CDATA[Windy with light rain early. A mix of sun and clouds in the afternoon. High 7C. Winds WSW at 30 to 50 km/h. Chance of rain 70%.]]></fcttext_metric>
<pop>70</pop>
</forecastday>
<forecastday>
<period>3</period>
<icon>nt_chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/nt_chancerain.gif</icon_url>
<title>Sunday Night</title>
<fcttext><![CDATA[Showers in the evening, then partly cloudy overnight. Low 37F. Winds WSW at 15 to 25 mph. Chance of rain 60%.]]></fcttext>
<fcttext_metric><![CDATA[Showers in the evening, then partly cloudy overnight. Low 3C. Winds WSW at 25 to 40 km/h. Chance of rain 60%.]]></fcttext_metric>
<pop>60</pop>
</forecastday>
<forecastday>
<period>4</period>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<title>Monday</title>
<fcttext><![CDATA[Rain showers, with increasing winds in the afternoon. High 41F. Winds W at 20 to 30 mph. Chance of rain 60%.]]></fcttext>
<fcttext_metric><![CDATA[Overcast with showers. Becoming windy in the afternoon. High around 5C. Winds W at 30 to 50 km/h. Chance of rain 60%.]]></fcttext_metric>
<pop>60</pop>
</forecastday>
<forecastday>
<period>5</period>
<icon>nt_chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/nt_chancerain.gif</icon_url>
<title>Monday Night</title>
<fcttext><![CDATA[Windy. Some showers in the evening then partly cloudy overnight. Low 36F. Winds W at 20 to 30 mph. Chance of rain 50%.]]></fcttext>
<fcttext_metric><![CDATA[Windy. Rain showers early with clearing late. Low 2C. Winds W at 30 to 50 km/h. Chance of rain 50%.]]></fcttext_metric>
<pop>50</pop>
</forecastday>
<forecastday>
<period>6</period>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<title>Tuesday</title>
<fcttext><![CDATA[Steady light rain in the morning. Showers continuing in the afternoon. High 41F. Winds WSW at 15 to 25 mph. Chance of rain 60%.]]></fcttext>
<fcttext_metric><![CDATA[Light rain early...then remaining cloudy with showers in the afternoon. High near 5C. Winds WSW at 25 to 40 km/h. Chance of rain 60%.]]></fcttext_metric>
<pop>60</pop>
</forecastday>
<forecastday>
<period>7</period>
<icon>nt_partlycloudy</icon>
<icon_url>http://icons.wxug.com/i/c/k/nt_partlycloudy.gif</icon_url>
<title>Tuesday Night</title>
<fcttext><![CDATA[A few clouds. Low 37F. Winds WSW at 15 to 25 mph.]]></fcttext>
<fcttext_metric><![CDATA[A few clouds. Low 3C. Winds WSW at 25 to 40 km/h.]]></fcttext_metric>
<pop>20</pop>
</forecastday>
</forecastdays>
</txt_forecast>
<simpleforecast>
<forecastdays>
<forecastday>
<date>
<epoch>1425146400</epoch>
<pretty_short>7:00 PM CET</pretty_short>
<pretty>7:00 PM CET on February 28, 2015</pretty>
<day>28</day>
<month>2</month>
<year>2015</year>
<yday>58</yday>
<hour>19</hour>
<min>00</min>
<sec>0</sec>
<isdst>0</isdst>
<monthname>February</monthname>
<monthname_short>Feb</monthname_short>
<weekday_short>Sat</weekday_short>
<weekday>Saturday</weekday>
<ampm>PM</ampm>
<tz_short>CET</tz_short>
<tz_long>Europe/Amsterdam</tz_long>
</date>
<period>1</period>
<high>
<fahrenheit>44</fahrenheit>
<celsius>7</celsius>
</high>
<low>
<fahrenheit>41</fahrenheit>
<celsius>5</celsius>
</low>
<conditions>Overcast</conditions>
<icon>cloudy</icon>
<icon_url>http://icons.wxug.com/i/c/k/cloudy.gif</icon_url>
<skyicon></skyicon>
<pop>0</pop>
<qpf_allday>
<in>0.17</in>
<mm>4</mm>
</qpf_allday>
<qpf_day>
<in>0.00</in>
<mm>0</mm>
</qpf_day>
<qpf_night>
<in>0.17</in>
<mm>4</mm>
</qpf_night>
<snow_allday>
<in>0.0</in>
<cm>0.0</cm>
</snow_allday>
<snow_day>
<in>0.0</in>
<cm>0.0</cm>
</snow_day>
<snow_night>
<in>0.0</in>
<cm>0.0</cm>
</snow_night>
<maxwind>
<mph>25</mph>
<kph>40</kph>
<dir>S</dir>
<degrees>184</degrees>
</maxwind>
<avewind>
<mph>19</mph>
<kph>31</kph>
<dir>S</dir>
<degrees>184</degrees>
</avewind>
<avehumidity>72</avehumidity>
<maxhumidity>0</maxhumidity>
<minhumidity>0</minhumidity>
</forecastday>
<forecastday>
<date>
<epoch>1425232800</epoch>
<pretty_short>7:00 PM CET</pretty_short>
<pretty>7:00 PM CET on March 01, 2015</pretty>
<day>1</day>
<month>3</month>
<year>2015</year>
<yday>59</yday>
<hour>19</hour>
<min>00</min>
<sec>0</sec>
<isdst>0</isdst>
<monthname>March</monthname>
<monthname_short>Mar</monthname_short>
<weekday_short>Sun</weekday_short>
<weekday>Sunday</weekday>
<ampm>PM</ampm>
<tz_short>CET</tz_short>
<tz_long>Europe/Amsterdam</tz_long>
</date>
<period>2</period>
<high>
<fahrenheit>45</fahrenheit>
<celsius>7</celsius>
</high>
<low>
<fahrenheit>37</fahrenheit>
<celsius>3</celsius>
</low>
<conditions>Chance of Rain</conditions>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<skyicon></skyicon>
<pop>70</pop>
<qpf_allday>
<in>0.12</in>
<mm>3</mm>
</qpf_allday>
<qpf_day>
<in>0.02</in>
<mm>1</mm>
</qpf_day>
<qpf_night>
<in>0.10</in>
<mm>3</mm>
</qpf_night>
<snow_allday>
<in>0.0</in>
<cm>0.0</cm>
</snow_allday>
<snow_day>
<in>0.0</in>
<cm>0.0</cm>
</snow_day>
<snow_night>
<in>0.0</in>
<cm>0.0</cm>
</snow_night>
<maxwind>
<mph>35</mph>
<kph>56</kph>
<dir>WSW</dir>
<degrees>248</degrees>
</maxwind>
<avewind>
<mph>25</mph>
<kph>40</kph>
<dir>WSW</dir>
<degrees>248</degrees>
</avewind>
<avehumidity>78</avehumidity>
<maxhumidity>0</maxhumidity>
<minhumidity>0</minhumidity>
</forecastday>
<forecastday>
<date>
<epoch>1425319200</epoch>
<pretty_short>7:00 PM CET</pretty_short>
<pretty>7:00 PM CET on March 02, 2015</pretty>
<day>2</day>
<month>3</month>
<year>2015</year>
<yday>60</yday>
<hour>19</hour>
<min>00</min>
<sec>0</sec>
<isdst>0</isdst>
<monthname>March</monthname>
<monthname_short>Mar</monthname_short>
<weekday_short>Mon</weekday_short>
<weekday>Monday</weekday>
<ampm>PM</ampm>
<tz_short>CET</tz_short>
<tz_long>Europe/Amsterdam</tz_long>
</date>
<period>3</period>
<high>
<fahrenheit>41</fahrenheit>
<celsius>5</celsius>
</high>
<low>
<fahrenheit>36</fahrenheit>
<celsius>2</celsius>
</low>
<conditions>Chance of Rain</conditions>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<skyicon></skyicon>
<pop>60</pop>
<qpf_allday>
<in>0.08</in>
<mm>2</mm>
</qpf_allday>
<qpf_day>
<in>0.08</in>
<mm>2</mm>
</qpf_day>
<qpf_night>
<in>0.00</in>
<mm>0</mm>
</qpf_night>
<snow_allday>
<in>0.0</in>
<cm>0.0</cm>
</snow_allday>
<snow_day>
<in>0.0</in>
<cm>0.0</cm>
</snow_day>
<snow_night>
<in>0.0</in>
<cm>0.0</cm>
</snow_night>
<maxwind>
<mph>30</mph>
<kph>48</kph>
<dir>W</dir>
<degrees>265</degrees>
</maxwind>
<avewind>
<mph>21</mph>
<kph>34</kph>
<dir>W</dir>
<degrees>265</degrees>
</avewind>
<avehumidity>83</avehumidity>
<maxhumidity>0</maxhumidity>
<minhumidity>0</minhumidity>
</forecastday>
<forecastday>
<date>
<epoch>1425405600</epoch>
<pretty_short>7:00 PM CET</pretty_short>
<pretty>7:00 PM CET on March 03, 2015</pretty>
<day>3</day>
<month>3</month>
<year>2015</year>
<yday>61</yday>
<hour>19</hour>
<min>00</min>
<sec>0</sec>
<isdst>0</isdst>
<monthname>March</monthname>
<monthname_short>Mar</monthname_short>
<weekday_short>Tue</weekday_short>
<weekday>Tuesday</weekday>
<ampm>PM</ampm>
<tz_short>CET</tz_short>
<tz_long>Europe/Amsterdam</tz_long>
</date>
<period>4</period>
<high>
<fahrenheit>41</fahrenheit>
<celsius>5</celsius>
</high>
<low>
<fahrenheit>37</fahrenheit>
<celsius>3</celsius>
</low>
<conditions>Chance of Rain</conditions>
<icon>chancerain</icon>
<icon_url>http://icons.wxug.com/i/c/k/chancerain.gif</icon_url>
<skyicon></skyicon>
<pop>60</pop>
<qpf_allday>
<in>0.09</in>
<mm>2</mm>
</qpf_allday>
<qpf_day>
<in>0.09</in>
<mm>2</mm>
</qpf_day>
<qpf_night>
<in>0.00</in>
<mm>0</mm>
</qpf_night>
<snow_allday>
<in>0.0</in>
<cm>0.0</cm>
</snow_allday>
<snow_day>
<in>0.0</in>
<cm>0.0</cm>
</snow_day>
<snow_night>
<in>0.0</in>
<cm>0.0</cm>
</snow_night>
<maxwind>
<mph>25</mph>
<kph>40</kph>
<dir>WSW</dir>
<degrees>241</degrees>
</maxwind>
<avewind>
<mph>18</mph>
<kph>29</kph>
<dir>WSW</dir>
<degrees>241</degrees>
</avewind>
<avehumidity>81</avehumidity>
<maxhumidity>0</maxhumidity>
<minhumidity>0</minhumidity>
</forecastday>
</forecastdays>
</simpleforecast>
</forecast>
</response>
dolpheen
Posts: 119 Joined: 10 Feb 2015 00:38
Post
by dolpheen » 28 Feb 2015 15:15
pz1 , in your 'try' section you use findOne() method of 'doc' variable that is simple a string, according to previous post you must create a ZXmlDocument object from your recieved data.
Is 'response.data' automatically created as a ZXmlDocument object?
Example
Code: Select all
var xmlDoc = new ZXmlDocument(doc);
parseInt(doc.findOne("/response/current_observation/temp_c/text()"));
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
dolpheen
Posts: 119 Joined: 10 Feb 2015 00:38
Post
by dolpheen » 28 Feb 2015 15:29
Corrrected, xmlDoc of course
Code: Select all
var xmlDoc = new ZXmlDocument(doc);
parseInt(xmlDoc.findOne("/response/current_observation/temp_c/text()"));
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
pz1
Posts: 2053 Joined: 08 Apr 2012 13:44
Post
by pz1 » 28 Feb 2015 15:31
dolpheen wrote: Is 'response.data' automatically created as a ZXmlDocument object?
From the example in this post of 2 June 2014(
viewtopic.php?f=3422&t=19972#p50963 ) I understood it is created as a ZXmlDocument object by the
http.request
dolpheen
Posts: 119 Joined: 10 Feb 2015 00:38
Post
by dolpheen » 28 Feb 2015 15:40
Then, I guess, you can use debugger or log to console the recieved data to see what type is that.
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
pz1
Posts: 2053 Joined: 08 Apr 2012 13:44
Post
by pz1 » 28 Feb 2015 15:58
dolpheen wrote: log to console the recieved data to see what type is that.
I had written the
doc variable to log in the past and now again. It still produces the same valid XML I get when using the call string in a browser.
dolpheen
Posts: 119 Joined: 10 Feb 2015 00:38
Post
by dolpheen » 28 Feb 2015 16:04
pz1 wrote: dolpheen wrote: It still produces the same valid XML I get when using the call string in a browser.
So, it's just a string and not a ZXmlDocument object? Did you try my example?
Razberry B+ 2.0.1-rc25 on ZW500 + 15 devices / Razberry B 2.0.1-rc25 on ZW300 for test
pofs
Posts: 688 Joined: 25 Mar 2011 19:03
Post
by pofs » 28 Feb 2015 16:17
dolpheen wrote: Is 'response.data' automatically created as a ZXmlDocument object?
Yes, it is, in case of certain response content types (like "text/xml" and some others). You may also force http.request() to use specific content type instead of taking "content-type" header from server response.