Need some help with http.request function
Posted: 16 Mar 2015 21:17
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?
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);
};