-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
72 lines (55 loc) · 2.15 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const request = require("request");
const _ = require('lodash');
const Push = require( 'pushover-notifications' )
require("babel-polyfill");
module.exports.dailySolarPush = (event, context, callback) => {
var start_date = new Date();
start_date.setDate(start_date.getDate() - 30);
var start_date = start_date.toISOString().split('T')[0];
var end_date = new Date();
end_date.setDate(end_date.getDate() - 1);
var end_date = end_date.toISOString().split('T')[0];
const options = {
url: 'https://monitoringapi.solaredge.com/site/' + process.env.SITE_ID + '/energy.json?timeUnit=DAY&endDate=' + end_date + '&startDate=' + start_date + '&api_key=' + process.env.API_KEY
};
function calculatePercentage($oldFigure, $newFigure) {
var percentChange = (($oldFigure - $newFigure) / $oldFigure) * 100;
return Math.abs(percentChange).toFixed(0);
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body)
var values = _.map(json.energy.values, 'value')
var total = values.reduce(function(acc, val) { return acc + val; })
var average = (total/values.length).toFixed(2);
var last_value = values.slice(-1)[0];
var last_value_human = (last_value / 1000).toFixed(2);
var average_label = 'the average of the last 30 days'
var new_value_label = 'yesterday'
var difference = calculatePercentage(average, last_value)
if (last_value >= average) {
var difference_label = 'higher'
} else {
var difference_label = 'lower'
}
var message = "Hi Martijn, " + new_value_label + " your solar panels generated " + last_value_human +
"kWh. That's " + difference + "% " + difference_label + " compared " +
"to " + average_label + "."
var push = new Push( {
user: process.env.PUSHOVER_USER_TOKEN,
token: process.env.PUSHOVER_APP_TOKEN,
})
var msg = {
message: message,
title: "RusPower"
}
push.send( msg, function( err, result ) {
if ( err ) {
throw err
}
console.log( result )
})
}
})
};