forked from roramirez/MMM-ImagesPhotos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-ImagesPhotos.js
173 lines (132 loc) · 4.27 KB
/
MMM-ImagesPhotos.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
168
169
170
171
172
173
/* global Module */
/* Magic Mirror
* Module: MMM-ImagesPhotos
*
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed.
*/
Module.register("MMM-ImagesPhotos",{
defaults: {
opacity: 0.9,
animationSpeed: 500,
updateInterval: 5000,
getInterval: 60000,
maxWidth: "100%",
maxHeight: "100%",
retryDelay: 2500,
show: "photo",
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function() {
var self = this;
this.image = {};
this.current = {url: "", album: ""};
this.loaded = false;
this.lastPhotoIndex = -1;
// Should start photo updater only if configured
this.sendSocketNotification("INIT");
if (this.config.show == "photo") {
this.sendSocketNotification("SET_CONFIG", this.config);
Log.info("sent SET_CONFIG message to node_helper");
};
this.getImageFromNodeHelper("/MMM-ImagesPhotos/update");
Log.info(`got ${this.image.url} from nodehelper at initialization`)
},
getImageFromNodeHelper: function(urlAppHelper) {
Log.info("calling node helper get image at initialization");
// var urlAppHelper = "/MMM-ImagesPhotos/update";
var self = this;
var photosRequest = new XMLHttpRequest();
photosRequest.open("GET", urlAppHelper, true);
photosRequest.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.image = JSON.parse(this.response);
Log.info(`returned ${self.image.url}`);
self.updateDom(self.config.animationSpeed);
} else if (this.status === 401) {
Log.error(self.name, this.status);
} else {
Log.error(self.name, "Could not load photos.");
}
}
};
photosRequest.send();
Log.info(`returned outer scope ${self.image.url}`);
return self.image;
},
// Listen to server notifications on websocket
socketNotificationReceived: function(notification, payload) {
var self = this;
Log.info(this.config.show + "-" + this.name + " received a socket notification: " + notification + " - Payload: " + payload);
if (notification == "PUBLISHED") {
self.image = payload;
self.updateDom(self.config.animationSpeed);
}
},
notificationReceived: function(notification, payload, sender) {
Log.info(`notification received ${notification}`);
if (notification == "GAMEPAD_BUTTON_PRESSED") {
Log.info(`payload ${payload.button}`);
switch (payload.button) {
case 1:
window.close();
case 3:
Log.info("skip back image");
this.sendSocketNotification("SHOW_PREVIOUS_IMAGE", {});
break;
case 0:
Log.info("skip forward image");
this.sendSocketNotification("SHOW_NEXT_IMAGE", {})
break;
}
}
},
preload_image: function(im_url) {
let img = new Image();
img.src = im_url;
Log.info(`preloaded ${im_url}`);
},
getDom: function() {
var self = this;
var wrapper = document.createElement("div");
var photoImage = this.image;
// var current = this.current;
// const entries = Object.entries(current);
// Log.info(`entries from socket ${entries}`);
Log.info(`${this.config.show}: image url ${photoImage.url} and album name: ${photoImage.album}`)
this.preload_image(photoImage.next_url);
if (photoImage && this.config.show == "photo") {
var img = document.createElement("img");
img.src = photoImage.url;
img.addEventListener('load', function() {
// Calculate the aspect ratio.
const aspectRatio = img.width / img.height;
Log.info(`aspectratio: ${aspectRatio}`)
// Set the class of the div based on the aspect ratio.
if (aspectRatio > 1) {
wrapper.classList.add('image-holder-landscape');
} else {
wrapper.classList.add('image-holder-portrait');
}
// Append the image to the div.
wrapper.appendChild(img);
});
img.id = "mmm-images-photos";
img.style.maxWidth = this.config.maxWidth;
img.style.maxHeight = this.config.maxHeight;
img.style.opacity = self.config.opacity;
img.style.position = "sticky";
}
else if (photoImage && this.config.show == "album") {
var textdiv = document.createElement("div")
textdiv.classList.add("album")
textdiv.innerHTML = photoImage.album;
wrapper.appendChild(textdiv);
}
return wrapper;
},
getStyles: function() {
return ["MMM-ImagesPhotos.css"]
},
});