-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
43 lines (37 loc) · 1.11 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
"use strict";
/* Magic Mirror
* Node Helper: MMM-Fronius2
*
* By Beh ([email protected])
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const FroniusFetcher = require("./FroniusFetcher");
module.exports = NodeHelper.create({
initialize: function (config) {
if (typeof this.fetcher === "undefined") {
this.fetcher = new FroniusFetcher(config);
this.sendSocketNotification("MMM-Fronius2_INITIALIZED");
}
},
fetchData: async function () {
if (typeof this.fetcher === "undefined") return;
try {
const data = await this.fetcher.fetch();
this.sendSocketNotification("MMM-Fronius2_DATA", data);
} catch (error) {
if (error.message === "RequestTimeout") {
console.log("Data fetch from Fronius power converter timed out.");
this.sendSocketNotification("MMM-Fronius2_ERROR_FETCH_TIMEOUT");
}
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-Fronius2_INIT") {
this.initialize(payload);
}
if (notification === "MMM-Fronius2_FETCH_DATA") {
this.fetchData();
}
},
});