-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
42 lines (36 loc) · 1.37 KB
/
Copy pathnode_helper.js
File metadata and controls
42 lines (36 loc) · 1.37 KB
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
/* global __dirname */
const NodeHelper = require("node_helper")
const fs = require("fs")
const path = require("path")
const logStream = fs.createWriteStream("/home/user/slideshow.log", { flags: "a" }); // 'a' = append
console.log = function (message, ...optionalParams) {
const timestamp = new Date().toISOString();
const fullMessage = [timestamp, message, ...optionalParams].join(" ") + "\n";
logStream.write(fullMessage);
};
module.exports = NodeHelper.create({
imagePaths: [],
currentIndex: 0,
start: function () {
console.log("[MMM-StyledSlideshow]" + `Starting node helper: ${this.name}`);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "NEXT_IMAGE") {
this.sendSocketNotification("NEXT_PICTURE", this.imagePaths[this.currentIndex]);
console.log("[MMM-StyledSlideshow" + this.imagePaths[this.currentIndex])
this.currentIndex = (this.currentIndex + 1) % this.imagePaths.length;
}
else if (notification == "GET_PATHS"){
this.getFiles(payload);
}
},
getFiles: function(folderPath){
this.imagePaths = []
const modulePath = path.join(__dirname, folderPath);
fs.readdir(modulePath, (err, files) => {
if (err) return console.error(err);
this.imagePaths = files.map(f => folderPath + "/" + f);
console.log("[MMM-StyledSlideshow]" + this.imagePaths)
});
},
})