-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-FTP-image.js
179 lines (148 loc) · 4.83 KB
/
MMM-FTP-image.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
174
175
176
177
178
179
// ################################################################ \\
// # MMM-FTP-image # \\
// # FTP server image display module # \\
// ################################################################ \\
Module.register('MMM-FTP-image', {
defaults: {
// FTP server configuration
port: 21,
user: 'pi',
password: null,
host: 'localhost',
// FTP directory configuration
defaultDirPath: null, // Type: string | null => Default directory to retrieve images
dirPathsAuthorized: [], // Type: Array<string> => List of authorized directories
// Display configuration
opacity: 1.0,
width: '100%',
height: '100%',
imgChangeInterval: 10000, // Type: number (ms)
},
imgNameList: [], // Type: Array<{ mimeType: string; base64: string }>
imgBase64: new Object(), // Type: { base64: string; mimeType: string }
imageDisplayedNumber: 0,
imageLoadFinished: false,
finishAllImgInCurrentDirectory: false,
intervalInstance: null,
start: function () {
this.logMessage('Started.');
if (!this.config.password) {
this.logMessage('The password is not entered !', 'error');
}
this.getListImgNameFromFTPServer();
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'FTP_IMG_LIST_NAME':
this.logMessage('Images list received !');
this.imgNameList = payload;
if (!this.imageLoadFinished || this.finishAllImgInCurrentDirectory) {
this.scheduleImgUpdateInterval();
this.finishAllImgInCurrentDirectory = false;
}
break;
case 'FTP_IMG_BASE64':
this.logMessage('Images received !');
this.imgBase64 = payload;
this.incrementImageIndex();
this.updateDom();
break;
}
},
getDom: function () {
var wrapper = document.createElement('div');
if (this.error !== null) {
wrapper.innerHTML = this.translate(this.error);
}
if (!this.imageLoadFinished) {
wrapper.innerHTML = this.translate('LOADING');
return wrapper;
}
const image = this.imgBase64;
if (!image) {
this.logMessage(`Could not load image (index: ${this.imageDisplayedNumber})`);
wrapper.innerHTML = this.translate('ERROR LOADING');
return wrapper;
}
wrapper.appendChild(this.createImageElement(image));
return wrapper;
},
/**
* Send notification of node_helper for get name list from FTP server
*/
getListImgNameFromFTPServer: function () {
this.imageDisplayedNumber = 0;
// Send FTP_IMG for get img from FTP server
this.sendSocketNotification('FTP_IMG_CALL_LIST', {
host: this.config.host,
port: this.config.port,
user: this.config.user,
password: this.config.password,
defaultDirPath: this.config.defaultDirPath,
dirPathsAuthorized: this.config.dirPathsAuthorized,
finishAllImgInCurrentDirectory: this.finishAllImgInCurrentDirectory,
});
},
createImageElement: function (image) {
var element = document.createElement('img');
element.src = `data:${image.mimeType};base64, ${image.base64}`;
element.style.maxWidth = this.config.width;
element.style.maxHeight = this.config.height;
element.style.opacity = this.config.opacity;
return element;
},
/**
* Loop to reload image based on user defined interval time
*/
scheduleImgUpdateInterval: function () {
this.logMessage(`Scheduled update interval (${this.config.imgChangeInterval / 1000}s)...`);
const payload = {
host: this.config.host,
port: this.config.port,
user: this.config.user,
password: this.config.password,
defaultDirPath: this.config.defaultDirPath,
dirPathsAuthorized: this.config.dirPathsAuthorized,
finishAllImgInCurrentDirectory: this.finishAllImgInCurrentDirectory,
};
// Get first image
this.sendSocketNotification('FTP_IMG_CALL_BASE64', {
...payload,
fileName: this.imgNameList[this.imageDisplayedNumber].name,
});
this.imageLoadFinished = true;
this.finishAllImgInCurrentDirectory = false;
// Set interval to reload image
this.intervalInstance = setInterval(() => {
this.sendSocketNotification('FTP_IMG_CALL_BASE64', {
...payload,
fileName: this.imgNameList[this.imageDisplayedNumber].name,
});
}, this.config.imgChangeInterval);
},
incrementImageIndex: function () {
this.logMessage(`Current image index: ${this.imageDisplayedNumber}`);
if (this.imageDisplayedNumber === this.imgNameList.length - 1) {
clearInterval(this.intervalInstance);
// Wait 10s before call next directory
setTimeout(() => {
this.imgNameList = [];
this.finishAllImgInCurrentDirectory = true;
this.sendSocketNotification('FTP_IMG_CALL_NEXT_DIR');
this.getListImgNameFromFTPServer();
}, this.config.imgChangeInterval);
return;
}
this.imageDisplayedNumber++;
},
logMessage: function (message, type) {
switch (type) {
case 'erorr':
Log.error(`Module ${this.name} | ${message}`);
break;
default:
Log.info(`Module ${this.name} | ${message}`);
break;
}
},
});