Page 1 of 1

Need some help with http.request function

Posted: 16 Mar 2015 21:17
by pz1
Since I am not a programmer I do need some help with combining data coming from two http.request calls. Within the function shown below in the code block, I do make calls to two solar power inverters. That works fine. I do get stuck at the end of the code where I want to add the power of both inverters into a new totalPower variable. With my limited JS knowledge I had hoped that if I declared the variables power1 and power2 immediately after the "var self = instance;", I could make the calculation totalPower=power1+power2 just before the end of the code.

That did not work. So the question is how do I refer to these power variables?

Code: Select all

PVLogger.prototype.fetchSolar = function (instance) {
	var self = instance;

	http.request({
		url : "http://" + self.config.pvlogger1 + "/status.xml",
		method : "GET",
		async : true,
		success : function (response) {
			try {
				var doc1 = response.data; // it is already ZXmlDocument
				power1 = parseFloat(doc1.findOne("/response/gauge_power/text()"));
				self.vDev.set("metrics:watts1", power1);
			} catch (e) {
				//error handling
			}
		},
		error : function () {
			//error handling
		}
	});
	http.request({
		url : "http://" + self.config.pvlogger2 + "/status.xml",
		method : "GET",
		async : true,
		success : function (response) {
			try {
				var doc2 = response.data; // it is already ZXmlDocument
				power2 = parseFloat(doc2.findOne("/response/gauge_power/text()"));
				self.vDev.set("metrics:watts2", power2);
			} catch (e) {
				//error handling
			}
		},
		error : function () {
			//error handling
		}
	});
	icon = "http://quadras:8083/user/solarok58px.png";
	self.vDev.set("metrics:icon", icon);
};

Re: Need some help with http.request function

Posted: 16 Mar 2015 23:05
by dolpheen
pz1, your http requests are asynchronous, so the functions in 'success' properies are called some time after and normally in random order (and also when you are returned from fetchSolar).
For a situation when you wait for several async actions (it may be 2, as in your case or more), it's a good practice to use 'promise' objects. There are some popular libs like 'q', 'when', etc. and may be z-way Javascript engine has native support for them. (it should be checked).

If not using promises, for your case I'd propose to create power1 and power2 with 'null' values at the beginning of fetchSolar function. Then in both 'success' functions check weather power1 or power2 has 'null' value (not read still) and if it's not 'null' then calculate 'totalpower'. It should work but it's not good practice :)

Re: Need some help with http.request function

Posted: 17 Mar 2015 00:40
by pz1
Thanks for this clear expose. I do see the problem.

May be I should forget about doing the calculus in this module, but rather do that in a separate module. My original plan was to make a simple PVLogger module for a single inverter, which just collects the xml and turn the data into a virtual device. So I would need two instances of that.
In addition I would need then to create a kind of math module in which I could do calculus with the virtual devices data. As that obviously is outside my league, so I'll first go for a dedicated module.

Re: Need some help with http.request function

Posted: 17 Mar 2015 10:36
by dolpheen
If I got your idea, one power meter per inverter is good for unification. Then your widget is just a power metering device, like any other metering z-wave devices.
It would be good to have some visualization tool to display configurable pie charts, logs and graphs for all metering devices in house. But (ex. 'logging charts' visualization), it's not clear whether there will be some kind of 'standard' for razberry custom visualization.

Re: Need some help with http.request function

Posted: 17 Mar 2015 10:57
by pz1
dolpheen wrote:If I got your idea, one power meter per inverter is good for unification. Then your widget is just a power metering device, like any other metering z-wave devices.
Exactly!
And in addition, analogous to LogicalRule, I would need a generic MathModule to combine various device signals.
It would be good to have some visualization tool to display configurable pie charts, logs and graphs for all metering devices in house. But (ex. 'logging charts' visualization), it's not clear whether there will be some kind of 'standard' for razberry custom visualization.
In principle yes. At the moment I do use OpenRemote as a visualisation platform. The UI designer there has a webview element that is reasonably convenient see for example the screenshots in my OR solar logging description. (I am in the process of moving that control logic from OR to Z-way)

Re: Need some help with http.request function

Posted: 23 Mar 2015 17:41
by pz1
pz1 wrote:
dolpheen wrote:If I got your idea, one power meter per inverter is good for unification. Then your widget is just a power metering device, like any other metering z-wave devices.
Exactly!
And in addition, analogous to LogicalRule, I would need a generic MathModule to combine various device signals.
Got the "primitive" PVLogger virtual devices working now, and combined two power inverters in my "toy" Mathematica module. (See Recipes for both modules)