-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
116 lines (97 loc) · 3.55 KB
/
node_helper.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
const NodeHelper = require("node_helper");
const Growatt = require("growatt");
const fetch = require("node-fetch");
const options = {
plantData: true,
weather: false,
totalData: false,
statusData: true,
deviceData: true,
deviceType: false,
historyLast: true,
historyAll: false,
};
module.exports = NodeHelper.create({
requiresVersion: "2.21.0",
start: function () {
console.log("Starting node helper for " + this.name);
},
stop: async function() {
if(this.growatt) {
const logout = await this.growatt.logout().catch((e) => {
console.log(e);
});
console.log("logout:", logout);
}
},
calcSpeed: function(watt) {
const MAX_WATT = 10000;
// 0.25 - 4.25s (peak at 10 000 W)
const exponentialWatt = Math.pow(Math.min(Math.abs(watt), MAX_WATT), 2);
const seconds = 4.25 - Math.min(exponentialWatt / 25000000, 4);
return `${seconds}s`;
},
convertGrowattData: function (data, { plantId, deviceSerial }) {
const production = Math.round(data[plantId].devices[deviceSerial].deviceData.pac);
const consumption = Math.round(
data[plantId].devices[deviceSerial].historyLast.pacToLocalLoad
);
const grid = consumption - production;
const speed = {
production: this.calcSpeed(production),
consumption: this.calcSpeed(consumption),
grid: this.calcSpeed(grid),
};
return {
production,
consumption,
grid, // negative = feeding back to grid
speed,
};
},
getConnection: async function (payload) {
if(this.growatt && this.growatt.isConnected()) {
console.log("Found existing connection, reusing");
return this.growatt;
}
console.log("Trying to connect to Growatt.")
try {
const growatt = new Growatt({});
const login = await growatt.login(payload.username, payload.password)
this.growatt = growatt;
console.log("login:", login);
} catch(e) {
console.log("Error", e);
}
return this.growatt;
},
getGrowattData: async function (payload) {
const growatt = await this.getConnection(payload);
if(!growatt) {
console.log("No connection, will not fetch.")
return null;
}
const data = await growatt.getAllPlantData(options).catch((e) => {
console.log(e);
});
const growattData = this.convertGrowattData(data, payload);
//console.log({ growattData });
this.sendSocketNotification("GROWATT_DATA", growattData);
},
getElectricityPrice: async function (payload) {
const year = new Date().toISOString().slice(0, 4);
const monthAndDay = new Date().toISOString().slice(5, 10);
const res = await fetch(`https://www.elprisetjustnu.se/api/v1/prices/${year}/${monthAndDay}_${payload.area}.json`);
const data = await res.json();
const electricityPrice = data[new Date().getHours()].SEK_per_kWh;
//console.log({ electricityPrice });
this.sendSocketNotification("ELECTRICITY_PRICE", { electricityPrice });
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_GROWATT_DATA") {
this.getGrowattData(payload);
} else if (notification === "GET_ELECTRICITY_PRICE") {
this.getElectricityPrice(payload);
}
},
});