Skip to content

Commit c42090d

Browse files
committed
Fix #17.
Also introduced immichTimeout for further customization in case requests timeout occasionally.
1 parent ec7a858 commit c42090d

5 files changed

Lines changed: 74 additions & 61 deletions

File tree

MMM-ImmichSlideShow.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Module.register('MMM-ImmichSlideShow', {
2626
apiKey: 'provide your API KEY',
2727
// Base Immich URL. /api will be appended to this URL to make API calls.
2828
immichUrl: 'provide your base Immich URL',
29+
// The amount of timeout for immich API calls
30+
immichTimeout: 6000,
2931
// Mode of operation:
3032
// memory = show recent photos. requires numDaystoInclude
3133
// album = show picture from album. requires albumId/albumName
@@ -624,8 +626,12 @@ Module.register('MMM-ImmichSlideShow', {
624626
suspend: function () {
625627
Log.debug(LOG_PREFIX + 'Suspend called...');
626628
// Hide the progress while paused
627-
const oldDiv = document.getElementsByClassName('progress-inner')[0];
628-
oldDiv.style.display = 'none';
629+
if (this.config.showProgressBar) {
630+
const oldDiv = document.getElementsByClassName('progress-inner')[0];
631+
if (oldDiv) {
632+
oldDiv.style.display = 'none';
633+
}
634+
}
629635

630636
this.sendSocketNotification(
631637
'IMMICHSLIDESHOW_SUSPEND'

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ The following properties can be configured:
132132
<br>This value is <b>REQUIRED</b>
133133
</td>
134134
</tr>
135+
<tr>
136+
<td><code>immichTimeout</code></td>
137+
<td>The timeout for Immich API calls in milliseconds.<br>
138+
<br><b>Example:</b> <code>10000</code>
139+
<br><b>Default value:</b> <code>6000</code>
140+
<br>This value is <b>OPTIONAL</b>
141+
</td>
142+
</tr>
135143
<tr>
136144
<td><code>mode</code></td>
137145
<td>The mode of operation for the module. Valid options are 'memory' or 'album' and depending on which is chosen, additional settings are required.<br>

immichApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const immichApi = {
3434
// create and axis instance
3535
this.http = axios.create({
3636
baseURL: config.immichUrl + this.baseUrl,
37-
timeout: 5000,
37+
timeout: config.immichTimeout,
3838
headers: {
3939
'x-api-key': config.apiKey,
4040
'Accept': 'application/json'

node_helper.js

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -203,70 +203,69 @@ module.exports = NodeHelper.create({
203203

204204
displayImage: async function(showCurrent = false) {
205205
Log.debug(LOG_PREFIX + 'displayImage called', showCurrent, this.lastImageLoaded ? 'has last image' : 'no last image');
206-
if (showCurrent && this.lastImageLoaded) {
207-
// Just send the current image
208-
this.sendSocketNotification(
209-
'IMMICHSLIDESHOW_DISPLAY_IMAGE',
210-
this.lastImageLoaded
211-
);
212-
return;
213-
}
206+
207+
// If we are not showing the current image, then fetch the next one.
208+
if (!(showCurrent && this.lastImageLoaded)) {
209+
210+
// if there are no images or all the images have been displayed or it is the next day, try loading the images again
211+
if (!this.imageList.length || this.index >= this.imageList.length || Date.now() - this.pictureDate > 86400000) {
212+
Log.debug(LOG_PREFIX + 'image list is empty or index out of range or list too old! fetching new image list...');
213+
// Force the index to 0 so that we start from the beginning
214+
// and calling this function again will not get stuck in a loop
215+
if (this.index >= this.imageList.length) {
216+
this.index = 0;
217+
}
218+
// Set the last Image to null so we cannot load it and have to progress
219+
this.lastImageLoaded = null;
220+
this.gatherImageList(this.config);
221+
return;
222+
}
214223

215-
// if there are no images or all the images have been displayed or it is the next day, try loading the images again
216-
if (!this.imageList.length || this.index >= this.imageList.length || Date.now() - this.pictureDate > 86400000) {
217-
Log.debug(LOG_PREFIX + 'image list is empty or index out of range or list too old! fetching new image list...');
218-
// Force the index to 0 so that we start from the beginning
219-
// and calling this function again will not get stuck in a loop
220-
if (this.index >= this.imageList.length) {
221-
this.index = 0;
224+
// If still do not have images, just call this function in 5 min
225+
if (!this.imageList.length) {
226+
Log.debug(LOG_PREFIX + 'image list is empty! setting timeout for next image...');
227+
// still no images, search again after 5 mins
228+
setTimeout(() => {
229+
this.displayImage();
230+
}, 300000);
231+
return;
222232
}
223-
// Set the last Image to null so we cannot load it and have to progress
224-
this.lastImageLoaded = null;
225-
this.gatherImageList(this.config);
226-
return;
227-
}
228-
// Log.debug(LOG_PREFIX + 'image list', this.imageList.length, this.imageList);
229-
if (!this.imageList.length) {
230-
Log.debug(LOG_PREFIX + 'image list is empty! setting timeout for next image...');
231-
// still no images, search again after 5 mins
232-
setTimeout(() => {
233-
this.displayImage();
234-
}, 300000);
235-
return;
236-
}
237233

238-
let image = this.imageList[this.index];
234+
let image = this.imageList[this.index];
235+
236+
Log.debug(LOG_PREFIX + 'reading image "' + image.originalPath + '"');
237+
238+
this.lastImageLoaded = {
239+
identifier: this.config.identifier,
240+
path: image.originalPath,
241+
exifInfo: image.exifInfo || {},
242+
people: [],
243+
data: null,
244+
imageId: image.id,
245+
index: this.index+1, // Index is zero based
246+
total: this.imageList.length
247+
};
239248

240-
Log.debug(LOG_PREFIX + 'reading image "' + image.originalPath + '"');
241-
242-
this.lastImageLoaded = {
243-
identifier: this.config.identifier,
244-
path: image.originalPath,
245-
exifInfo: image.exifInfo || {},
246-
people: [],
247-
data: null,
248-
imageId: image.id,
249-
index: this.index+1, // Index is zero based
250-
total: this.imageList.length
251-
};
252-
253-
// If there is no exif info available, or if we need people but no people are listed
254-
// then fetch it with a separate call based on the API version
255-
if (!image.exifInfo || image.exifInfo.length == 0 ||
256-
this.config.imageInfo.includes('people') && (!image.people || image.people.length == 0)) {
257-
const assetInfo = await immichApi.getAssetInfo(image.id);
258-
if (assetInfo) {
259-
this.lastImageLoaded.exifInfo = assetInfo.exifInfo;
260-
this.lastImageLoaded.people = assetInfo.people;
249+
// If there is no exif info available, or if we need people but no people are listed
250+
// then fetch it with a separate call based on the API version
251+
if (!image.exifInfo || image.exifInfo.length == 0 ||
252+
this.config.imageInfo.includes('people') && (!image.people || image.people.length == 0)) {
253+
const assetInfo = await immichApi.getAssetInfo(image.id);
254+
if (assetInfo) {
255+
this.lastImageLoaded.exifInfo = assetInfo.exifInfo;
256+
this.lastImageLoaded.people = assetInfo.people;
257+
}
261258
}
259+
this.lastImageLoaded.data = await immichApi.getBase64EncodedAsset(image.id);
262260
}
263261

264-
this.lastImageLoaded.data = await immichApi.getBase64EncodedAsset(image.id);
265-
266-
this.sendSocketNotification(
267-
'IMMICHSLIDESHOW_DISPLAY_IMAGE',
268-
this.lastImageLoaded
269-
);
262+
// Only send a notification if we have the new image loaded
263+
if (this.lastImageLoaded.data) {
264+
this.sendSocketNotification(
265+
'IMMICHSLIDESHOW_DISPLAY_IMAGE',
266+
this.lastImageLoaded
267+
);
268+
}
270269
},
271270

272271
getNextImage: function (showCurrent = false, reloadOnLoop = false) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mmm-immichslideshow",
3-
"version": "0.10.0",
3+
"version": "0.10.1",
44
"description": "A plugin for MagicMirror2 to show slideshows from Immich",
55
"author": "Pelaxa Inc.",
66
"license": "MIT",

0 commit comments

Comments
 (0)