-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-helper.js
71 lines (63 loc) · 2.52 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
/* MagicMirror²
* Node Helper: MMM-WeatherEffects
*
* By Christian Gillinger
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const fs = require("fs");
const path = require("path");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
this.loadWeatherKeywords();
},
loadWeatherKeywords: function() {
const keywordsPath = path.join(__dirname, "weatherKeywords.json");
try {
// Initial load of keywords
this.readAndSendKeywords(keywordsPath);
// Watch for file changes
fs.watch(keywordsPath, (eventType) => {
if (eventType === "change") {
console.log("MMM-WeatherEffects: weatherKeywords.json changed, reloading...");
this.readAndSendKeywords(keywordsPath);
}
});
} catch (err) {
console.error("MMM-WeatherEffects: Error setting up keyword monitoring:", err);
// Send default keywords if file operations fail
this.sendSocketNotification("WEATHER_KEYWORDS_LOADED", {
rain: ["rain", "showers", "drizzle", "precipitation"],
snow: ["snow", "sleet", "blizzard", "flurries"]
});
}
},
readAndSendKeywords: function(filePath) {
try {
const data = fs.readFileSync(filePath, "utf8");
const keywords = JSON.parse(data);
if (keywords.effects && keywords.effects.rain && keywords.effects.snow) {
this.sendSocketNotification("WEATHER_KEYWORDS_LOADED", {
rain: keywords.effects.rain,
snow: keywords.effects.snow
});
console.log("MMM-WeatherEffects: Successfully loaded keywords:", keywords.effects);
} else {
throw new Error("Invalid keywords format");
}
} catch (err) {
console.error("MMM-WeatherEffects: Error reading keywords:", err);
// Send default keywords on error
this.sendSocketNotification("WEATHER_KEYWORDS_LOADED", {
rain: ["rain", "showers", "drizzle", "precipitation"],
snow: ["snow", "sleet", "blizzard", "flurries"]
});
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === "REQUEST_KEYWORDS") {
this.loadWeatherKeywords();
}
}
});