diff --git a/README.md b/README.md index 4d22acd..83a752c 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,26 @@ # PVoutput-NodeJS -PVoutput nodejs project to send data to pvoutput. +PVoutput nodejs project to interact with the pvoutput API. Based on http://pvoutput.org/help.html#api-getting-started ## Installation ```bash -npm install https://github.com/CONNCTED/PVoutput-NodeJS --save +npm install pvoutput-nodejs --save ``` ## Usage -### Create a new file 'my-pvoutput.js' ```javascript var PVoutputAPI = require('pvoutput-nodejs'); var pvoutput = new PVoutputAPI({ debug: false, - apiKey: "xxx", - systemId: "xxx" + apiKey: 'xxx', + systemId: 'xxx' }); - -module.exports = pvoutput; ``` -### In another nodejs file +### Sending Output to PVOutput ```javascript -var pvoutput = require('./my-pvoutput'); - var timestamp = 1451936700000; var solarWatt = 6000; var solarWattHour = 400; @@ -33,6 +28,69 @@ var consumptionWatt = 12000; var consumptionWattHour = 650; pvoutput.send(timestamp, solarWatt, solarWattHour, consumptionWatt, consumptionWattHour, function(statusCode, body) { - console.log(body); + console.log(body); +}); +``` + +### Getting Status +```javascript +pvoutput.getStatus().then(function(results) { + console.log(results); + + /* returns + { date: Sat Jun 11 2016 00:00:00 GMT+0200 (CEST), + time: '18:30', + energyGeneration: 3900, + powerGeneration: 1381, + energyConsumption: NaN, + powerConsumption: NaN, + efficiency: 0.511, + temperature: NaN, + voltage: 136.6 } + */ +}) +.catch(function(err) { + console.log(err); +}); +``` + +### Getting Outputs +```javascript +pvoutput.getOutputs().then(function(results) { + console.log(results); + + /* returns: + [ { date: Sat Jun 11 2016 00:00:00 GMT+0200 (CEST), + energyGenerated: 3900, + efficiency: 1.444, + energyExported: 0, + energyUsed: 0, + peakPower: 2219, + peakTime: '16:50', + condition: 'Cloudy', + minTemperature: NaN, + maxTemperature: NaN, + peakEnergyImport: NaN, + offPeakEnergyImport: NaN, + shoulderEnergyImport: NaN, + highShoulderEnergyImport: NaN }, + { date: Fri Jun 10 2016 00:00:00 GMT+0200 (CEST), + energyGenerated: 4200, + efficiency: 1.556, + energyExported: 0, + energyUsed: 0, + peakPower: NaN, + peakTime: undefined, + condition: 'Fine', + minTemperature: NaN, + maxTemperature: NaN, + peakEnergyImport: NaN, + offPeakEnergyImport: NaN, + shoulderEnergyImport: NaN, + highShoulderEnergyImport: NaN } ] + */ +}) +.catch(function(err) { + console.log(err); }); -``` \ No newline at end of file +``` diff --git a/example/my-pvoutput.js b/example/my-pvoutput.js index 93e2b18..d849738 100644 --- a/example/my-pvoutput.js +++ b/example/my-pvoutput.js @@ -7,4 +7,31 @@ var pvoutput = new PVoutputAPI({ systemId: "xxx" }); -module.exports = pvoutput; \ No newline at end of file +// Example sending data +var moment = require('moment'); +var now = moment().utc().valueOf(); +var timestamp = now; +var solarWatt = 6000; +var solarWattHour = 400; +var consumptionWatt = 12000; +var consumptionWattHour = 650; + +pvoutput.send(timestamp, solarWatt, solarWattHour, consumptionWatt, consumptionWattHour, function(statusCode, body) { + console.log(body); +}); + +// Example: get status +pvoutput.getStatus().then(function(results) { + console.log(results); +}) +.catch(function(err) { + console.log(err); +}); + +//Example: get outputs +pvoutput.getOutput().then(function(results) { + console.log(results); +}) +.catch(function(err) { + console.log(err); +}); diff --git a/example/package.json b/example/package.json index 25431ad..516dda4 100644 --- a/example/package.json +++ b/example/package.json @@ -2,7 +2,7 @@ "name": "pvoutput-nodejs-example", "version": "0.0.1", "scripts": { - "start": "node ./pvoutput-api-test.js" + "start": "node ./pvoutput.js" }, "dependencies": { "moment": "^2.10.6", diff --git a/example/pvoutput-api-test.js b/example/pvoutput-api-test.js deleted file mode 100644 index 5778da8..0000000 --- a/example/pvoutput-api-test.js +++ /dev/null @@ -1,15 +0,0 @@ -var pvoutput = require('./my-pvoutput'); -var moment = require('moment'); - -var now = moment().utc().valueOf(); - -var timestamp = now; - -var solarWatt = 6000; -var solarWattHour = 400; -var consumptionWatt = 12000; -var consumptionWattHour = 650; - -pvoutput.send(timestamp, solarWatt, solarWattHour, consumptionWatt, consumptionWattHour, function(statusCode, body) { - console.log(body); -}); \ No newline at end of file diff --git a/package.json b/package.json index 6099070..ba67ea5 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "./pvoutput-api.js", "dependencies": { "moment": "^2.10.6", - "request": "^2.67.0" + "request": "^2.67.0", + "request-promise": "^3.0.0" } } diff --git a/pvoutput-api.js b/pvoutput-api.js index 66ba105..7b1d189 100644 --- a/pvoutput-api.js +++ b/pvoutput-api.js @@ -1,4 +1,5 @@ var request = require('request'); +var rp = require('request-promise'); var querystring = require('querystring'); var moment = require('moment'); @@ -31,16 +32,137 @@ function PvoutputAPI(settings) { var query = querystring.stringify(params); - var options = { url: 'http://pvoutput.org/service/r2/addstatus.jsp?' + query }; - request.get(options, function (err, httpResponse, body) { + request.get(options, function(err, httpResponse, body) { callback(httpResponse.statusCode, body, options.url); }); }; + /* + * Get status of PV installation + * + * Returns results: + * + * Date yyyymmdd 2016-06-1120100830 + * Time hh:mm 19:0714:10 + * Energy Generation number watt hours 12936 + * Power Generation number watt 202 + * Energy Consumption number watt hours 19832 + * Power Consumption number watt 459 + * Efficiency number kWh/kW 5.280 + * Temperature decimal celsius 15.3 + * Voltage decimal volts 240.1 + */ + this.getStatus = function() { + var params = { + key: apiKey, + sid: systemId, + }; + + var query = querystring.stringify(params); + + var url = 'http://pvoutput.org/service/r2/getstatus.jsp?' + query; + + return rp({ + uri: url, + simple: false + }).then(function(body) { + var results = body.split(','); + results = filterNanValues(results); + + return { + date: moment(results[0]).toDate(), + time: results[1], + energyGeneration: parseInt(results[2]), + powerGeneration: parseInt(results[3]), + energyConsumption: parseInt(results[4]), + powerConsumption: parseInt(results[5]), + efficiency: parseFloat(results[6]), + temperature: parseFloat(results[7]), + voltage: parseFloat(results[8]) + }; + }) + .catch(function(err) { + return err; + }); + + }; + + /* + * Get output of PV installation + * + * Returns results: + * + * Date yyyymmdd 2016-06-1120110327 + * Energy Generated number watt hours 4413 + * Efficiency number kWh/kW 0.460 + * Energy Exported number watt hours 0 + * Energy Used number watt hours 21859 + * Peak Power number watts 2070 + * Peak Time 19:21hh:mm 11:00 + * Condition -.textShowers + * Min. Temperature number degrees celsius -3 + * Max. Temperature number degrees celsius 6 + * Peak Energy Import number watt hours 4220 + * Off-Peak Energy Import number watt hours 7308 + * Shoulder Energy Import number watt hours 2030 + * High-Shoulder Energy Import number watt hours 3888') + */ + this.getOutput = function() { + var params = { + key: apiKey, + sid: systemId, + }; + + var query = querystring.stringify(params); + + var url = 'http://pvoutput.org/service/r2/getoutput.jsp?' + query; + + return rp({ + uri: url, + simple: false + }).then(function(body) { + var dayOutputs = body.split(';'); + return dayOutputs.map(function(day) { + var dayValues = day.split(','); + dayValues = filterNanValues(dayValues); + return { + date: moment(dayValues[0]).toDate(), + energyGenerated: parseInt(dayValues[1]), + efficiency: parseFloat(dayValues[2]), + energyExported: parseInt(dayValues[3]), + energyUsed: parseInt(dayValues[4]), + peakPower: parseInt(dayValues[5]), + peakTime: dayValues[6], + condition: dayValues[7], + minTemperature: parseFloat(dayValues[8]), + maxTemperature: parseFloat(dayValues[9]), + peakEnergyImport: parseInt(dayValues[10]), + offPeakEnergyImport: parseInt(dayValues[11]), + shoulderEnergyImport: parseInt(dayValues[12]), + highShoulderEnergyImport: parseInt(dayValues[13]), + }; + }); + + }) + .catch(function(err) { + return err; + }); + + }; + +} + +function filterNanValues(results) { + return results.map(function(result) { + if (result === 'NaN') { + return undefined; + } + return result; + }); } -module.exports = PvoutputAPI; \ No newline at end of file +module.exports = PvoutputAPI;