forked from moritzmair/e3dc_grid_charge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (109 loc) · 3.29 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Author: Moritz Mair
//
// Please do not change anything in this file. Use the config.json file to configure
const https = require('https');
const fetch = require("node-fetch");
const fs = require('fs');
var cron = require('node-cron');
if(typeof URLSearchParams === 'undefined'){
URLSearchParams = require('url').URLSearchParams;
}
let rawdata = fs.readFileSync('config.json');
let config_file = JSON.parse(rawdata);
// e3dc
var E3dcRscp = require('./e3dc_rscp_lib/e3dcrscp.js');
e3dc = new E3dcRscp(config_file);
e3dc.initChannel();
var Webserver = require('./webserver.js');
server = new Webserver(config_file)
server.start();
const GRAPHQL_URL = 'https://api.tibber.com/v1-beta/gql';
refresh_epex();
cron.schedule('2 13,14,15 * * *', () => {
// run every day at 13,14,15:02
refresh_epex();
});
async function refresh_epex() {
console.log("getting price information");
const response = await fetch(GRAPHQL_URL, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer '+config_file.tibber_token
},
body: JSON.stringify({
query: `
{
viewer {
homes {
currentSubscription{
priceInfo{
today {
total
energy
tax
startsAt
}
tomorrow {
total
energy
tax
startsAt
}
}
}
}
}
}
`,
}),
});
const responseBody = await response.json();
price_info = responseBody.data.viewer.homes[0].currentSubscription.priceInfo
all_prices = price_info.today.concat(price_info.tomorrow);
now = new Date();
tomorrow = new Date(Date.now() + (3600 * 1000 * 24));
all_prices_night = all_prices.filter(function(ele){
datetime = new Date(ele.startsAt)
return (datetime > now && datetime < tomorrow);
});
hourly_prices = all_prices_night.map(a => ({...a}));
sorted_prices = all_prices_night.sort(function(a, b){return a.total - b.total});
identify_cheapest_hours(Date.now());
}
// E3/DC requires regular SET_POWER repetition, otherwise it will fall back to NORMAL mode
setInterval(function(){ decide_switch(); }, 1000*config_file.setpower_interval);
function decide_switch(){
d = new Date();
current_hour = d.getFullYear()+"-"+(d.getMonth()+1).toString().padStart(2, "0")+"-"+d.getDate().toString().padStart(2, "0")+"T"+d.getHours().toString().padStart(2, "0");
if(charging_hours.some((element) => element.includes(current_hour))){
console.log("-- start charging --")
start_charging();
}else{
console.log("-- stop charging --")
stop_charging();
}
}
function identify_cheapest_hours(now){
charging_hours = sorted_prices.slice(0, config_file.charging_hours);
charging_hours.forEach((element, index) => {
charging_hours[index] = element.startsAt;
});
}
const rscpEmsSetPowerMode = {
0: "NORMAL",
1: "IDLE",
2: "DISCHARGE",
3: "CHARGE",
4: "GRID_CHARGE",
};
function start_charging(){
e3dc.sendEmsSetPower(4, config_file.max_charge_power);
}
function stop_charging(){
e3dc.sendEmsSetPower(0, 5000);
}
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}