-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFroniusFetcher.js
75 lines (63 loc) · 2.08 KB
/
FroniusFetcher.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
"use strict";
const fetch = require("node-fetch");
// Once MagicMirror uses an electron version that uses Node 16, we can get this from globalThis
const AbortController = require("node-abort-controller").AbortController;
class FroniusFetcher {
constructor(config) {
this.requestTimeout = config.requestTimeout;
this.url = `http://${config.ip}/solar_api/v1/GetPowerFlowRealtimeData.fcgi`;
this.dummyData = config.dummyData;
}
async fetch() {
if (this.dummyData) {
return this.fetchDummyData();
}
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, this.requestTimeout);
try {
const response = await fetch(this.url, { signal: controller.signal });
const rawData = await response.json();
const convertedData = this.convertSiteData(rawData);
return convertedData;
} catch (error) {
if (error.type === "aborted" || error.code === "EHOSTUNREACH") {
throw new Error("RequestTimeout");
}
console.error(error);
} finally {
clearTimeout(timeout);
}
}
convertSiteData(rawData) {
const targetData = rawData.Body.Data.Site;
const convertedData = {
energyDay: targetData.E_Day,
energyTotal: targetData.E_Total,
energyYear: targetData.E_Year,
energyNow: targetData.P_PV,
meterLocation: targetData.Meter_Location,
mode: targetData.Mode,
powerAkku: targetData.P_Akku,
powerGrid: targetData.P_Grid,
powerLoad: targetData.P_Load,
autonomy: targetData.rel_Autonomy,
selfConsumption: targetData.rel_SelfConsumption,
};
return convertedData;
}
fetchDummyData() {
const convertedData = {
energyDay: this._getRandomArbitrary(1000, 50000),
energyTotal: this._getRandomArbitrary(50000, 5000000),
energyYear: this._getRandomArbitrary(20000, 500000),
energyNow: this._getRandomArbitrary(1, 10000),
};
return convertedData;
}
_getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
}
module.exports = FroniusFetcher;