Read value from XML document

Discussions about RaZberry - Z-Wave board for Raspberry computer
dolpheen
Posts: 119
Joined: 10 Feb 2015 00:38

Re: Read value from XML document

Post by dolpheen »

Thank you for the info, pofs.
I'm also going to do some projects with XML. But now watching pz1 activities :)
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

Re: Read value from XML document

Post by pz1 »

dolpheen wrote: Did you try my example?
No I did not, because imho the problem is not in getting the XML in. It works in my earlier post about the Solar monitor (PVLogger) example. That works for a rather flat xml.

So I guess here there is something funny with the path selection, or I am missing a stupid typo again ;)
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Read value from XML document

Post by pz1 »

pz1 wrote: or I am missing a stupid typo again ;)
Looks like this was the case. After several searches here an in my code, I found the findOne had lost its capitalisation of the O
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Read value from XML document

Post by pz1 »

Got Wunderground Weather Services working now for both JSON and XML.

The essential part for XML based weather report is:

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_c = parseFloat(doc.findOne("/response/current_observation/temp_c/text()"));
				windgust = parseFloat(doc.findOne("/response/current_observation/wind_gust_kph/text()"));
				wind_degrees = parseFloat(doc.findOne("/response/current_observation/wind_degrees/text()"));
				observe_time = doc.findOne("/response/current_observation/local_time_rfc822/text()");
				max_temp = parseInt(doc.findOne("/response/forecast/simpleforecast/forecastdays/forecastday/high/celsius/text()"));
				icon = doc.findOne("/response/current_observation/icon_url/text()");
				
				self.vDev.set("metrics:level", temp_c);
				self.vDev.set("metrics:windgust", windgust);
				self.vDev.set("metrics:wind_degrees", wind_degrees);
				self.vDev.set("metrics:observe_time", observe_time);
				self.vDev.set("metrics:max_temp", max_temp);
				self.vDev.set("metrics:icon", icon);
			} catch (e) {
				self.controller.addNotification("error", "Can not parse weather information", "module");
			}
		},
		error : function () {
			self.controller.addNotification("error", "Can not fetch weather information", "module");
		}
	});
};
The call

Code: Select all

http://raspi-ip:8083/JS/Run/controller.devices.get("Wunderground_8").get("metrics")
produces:

Code: Select all

{"scaleTitle":"°C","title":"lauwersoog","probeTitle":"Temperature","level":6.9,"windgust":16.9,"wind_degrees":112,"observe_time":"Sat, 28 Feb 2015 19:29:37 +0100","icon":"http://icons.wxug.com/i/c/k/nt_cloudy.gif","max_temp":8}
So many thanks to Pofs and Dolpheen for their support
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Read value from XML document

Post by pz1 »

pz1 wrote:
pz1 wrote: Unfortunately, something seems to go wrong with the Virtual Device creation.

Code: Select all

http://rasp_IP:8083/JS/Run/controller.devices.get("PVLogger_17").get("metrics")
produces

Code: Select all

 {"scaleTitle":"W","title":"Solar Power","probeTitle":"Watt","gauge_power":473,"energy_today":0.548,"power":614,"energy":5}
I am surprised by getting the sensors twice, and with different values. I did not expect to get gauge_power and energy_today
After upgrade to 2.0.1 RC16 I do get as expected:

Code: Select all

{"scaleTitle":"W","title":"Solar Power","probeTitle":"Watt","level":2797,"energy":4.6}
I have been fiddling with the PVLogger module a bit. I tried to combine the output of two loggers into one module. No success. So I deleted and commented some of the added code. However if I do ask for the metrics now:

Code: Select all

http://quadras:8083/JS/Run/controller.devices.get("PVLogger_15").get("metrics")
I do get the following mess of values:

Code: Select all

{"scaleTitle":"W","title":"Solar Power","probeTitle":"Watt","level":251,"energy1":0.272,"level2":244,"energy2":0.25,"icon":"http://quadras:8083/user/solarok58px.png","kWh":null,"watts1":229,"kWh1":1.6,"watts2":381,"kWh2":1.3}
Names like energy1, level2, etc have been replaced by other names or are deleted. Based on the presently active code I would only expect to see:

Code: Select all

{"scaleTitle":"W","title":"Solar Power","probeTitle":"Watt","level":251,"icon":"http://quadras:8083/user/solarok58px.png","kWh1":1.6}
I am on RC19 in this test. I have removed the module, rebooted/shutdown, reinstalled module. Still this for me strange behaviour.
Present code in the attached ZIP (deleted)
pz1
Posts: 2053
Joined: 08 Apr 2012 13:44

Re: Read value from XML document

Post by pz1 »

pz1 wrote: I do get the following mess of values:

Code: Select all

{"scaleTitle":"W","title":"Solar Power","probeTitle":"Watt","level":251,"energy1":0.272,"level2":244,"energy2":0.25,"icon":"http://quadras:8083/user/solarok58px.png","kWh":null,"watts1":229,"kWh1":1.6,"watts2":381,"kWh2":1.3}
Ah, I do understand now. All these variables are permanently kept in storage/configjson-xxxxx.json. No clean-up if the module is changed. (Old modules still reside there to. I did mention that on Github)

Code: Select all

		"PVLogger_15" : {
			"deviceType" : "sensorMultilevel",
			"metrics" : {
				"scaleTitle" : "W",
				"title" : "Solar Power",
				"probeTitle" : "Watt",
				"level" : 98,
				"energy1" : 0.272,
				"level2" : 244,
				"energy2" : 0.25,
				"icon" : "http://quadras:8083/user/solarok58px.png",
				"kWh" : null,
				"watts1" : 229,
				"kWh1" : 0.172,
				"watts2" : 381,
				"kWh2" : 1.3
			},
			"location" : null,
			"tags" : [],
			"permanently_hidden" : false
		},
Problem will be solved in some future release (Github issue #190)
Post Reply