-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-ImageFileWatcher.js
167 lines (140 loc) · 5.69 KB
/
MMM-ImageFileWatcher.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Magic Mirror
* Module: MMM-ImageFileWatcher
*
* By Bas Knol
* MIT Licensed.
*/
Module.register("MMM-ImageFileWatcher", {
defaults: {
imagePaths: [],
//If zero do nothing, otherwise set height to a pixel value
fixedHeight: 500,
//If zero do nothing, otherwise set width to a pixel value
fixedWidth: 0,
//Duration to show the images in milliseconds
showtime: 10 * 1000,
//Hide modules if we show images
hideModules: [],
//List of valid file extensions, seperated by commas
validImageFileExtensions: 'bmp,jpg,jpeg,gif,png',
//Process notifications from MMM-PushBulletNotifications
processPushBulletNotifications: false
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
console.log("MMM-ImageFileWatcher module started!");
this.lock = false;
this.imageList = [];
this.sendSocketNotification("START", this.config);
},
getDom: function () {
var self = this;
var wrapper = document.createElement("div");
//Check if we have images in list and if we are currently not showing an image (=lock)
if (this.lock === false && this.imageList.length > 0) {
//Showing image, set lock
this.lock = true;
//Create image element
var image = document.createElement("img");
//Get first image from array
image.src = this.imageList.shift();
var imageStyle = '';
//Set fixed image height
if (this.config.fixedHeight > 0) {
imageStyle += 'height:' + this.config.fixedHeight + 'px;';
}
//Set fixed image width
if (this.config.fixedWidth > 0) {
imageStyle += 'width:' + this.config.fixedWidth + 'px;';
}
//Set image style
if (imageStyle != '') {
image.style = imageStyle;
}
wrapper.appendChild(image);
//Hide modules if we need to
if (self.config.hideModules.length > 0) {
var hideOptions = { lockString: this.identifier };
//Loop over modules
MM.getModules().enumerate(function (module) {
//Check if we need to hide module and module is not already hidden
if (self.config.hideModules.includes(module.name) && !module.lockStrings.includes(self.identifier)) {
//Hide module with lockstring
module.hide(0, hideOptions);
}
});
}
//Show image for configured time
this.sleep(this.config.showtime).then(() => {
//Finished showing image. Unlock and update DOM
self.lock = false;
self.updateDom();
});
}
else {
//Show modules if modules were hidden
if (self.config.hideModules.length > 0) {
var showOptions = { lockString: this.identifier };
//Loop over modules
MM.getModules().enumerate(function (module) {
//Check if module is in hidelist en module is hidden based on lockstring
if (self.config.hideModules.includes(module.name) && module.lockStrings.includes(self.identifier)) {
//Show module, pass lockstring
module.show(1000, showOptions);
}
});
}
}
return wrapper;
},
//sleep time expects milliseconds
sleep: function (time) {
return new Promise((resolve) => setTimeout(resolve, time));
},
getScripts: function() {
return [];
},
getStyles: function () {
return [];
},
//Check if we can handle this type of file based on extension, is it an image?
checkValidImageFileExtension: function (filename, extensions) {
var extList = extensions.split(',');
for (var extIndex = 0; extIndex < extList.length; extIndex++) {
if (filename.toLowerCase().endsWith(extList[extIndex])) {
return true;
}
}
return false;
},
addImage: function (imagePath) {
if (imagePath) {
//Add image to list
this.imageList.push(imagePath);
//Check if image is currently shown, if not update DOM
if (!this.lock) {
this.updateDom();
}
}
},
socketNotificationReceived: function (notification, payload) {
console.log(notification + " " + payload);
//Image is added to watch folder
if (notification === "FILE_ADDED") {
this.addImage(payload);
}
},
notificationReceived: function (notification, payload, sender) {
//Notification received from MMM-PushBulletNotifications: file uploaded
if (this.config.processPushBulletNotifications
&& notification === 'PUSHBULLET_FILE_UPLOAD'
&& sender && sender.name === 'MMM-PushBulletNotifications'
&& this.checkValidImageFileExtension(payload.file_name, this.config.validImageFileExtensions)) {
//Logging
console.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
console.log(sender.name + ' file upload: ' + payload.file_name);
//Add image to list, pass image_url to get online PusBullet image
this.addImage(payload.image_url);
}
},
});