-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
55 lines (46 loc) · 1.78 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
const NodeHelper = require("node_helper");
//const fetch = require("node-fetch");
module.exports = NodeHelper.create({
config: null, // Initially, no config is set
start: function () {},
socketNotificationReceived: function (notification, payload) {
if (notification === "SET_CONFIG") {
this.config = payload;
this.startFetchingData();
} else if (notification === "GET_FRONIUS_DATA") {
if (!this.config) return;
this.getFroniusData();
}
},
startFetchingData: function () {
if (this.config && this.config.InverterIP) {
this.fetchInterval = setInterval(() => {
this.getFroniusData();
}, 60000); // Fetch data every 60 seconds
}
},
getFroniusData: function () {
if (!this.config || !this.config.InverterIP) return;
const url = `http://${this.config.InverterIP}/solar_api/v1/GetPowerFlowRealtimeData.fcgi`;
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error!`);
}
return response.json();
})
.then(data => {
const siteData = data.Body.Data.Site;
const inverterData = data.Body.Data.Inverters ? data.Body.Data.Inverters["1"] : {};
const result = {
P_Akku: siteData.P_Akku || 0,
P_Grid: siteData.P_Grid || 0,
P_Load: siteData.P_Load || 0,
P_PV: siteData.P_PV || 0,
Inverters: { "1": { SOC: inverterData.SOC || 0 } },
};
this.sendSocketNotification("FRONIUS_DATA", result);
})
.catch(() => {});
},
});