idotmatrix-api-client 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- idotmatrix/__init__.py +15 -0
- idotmatrix/apps/__init__.py +0 -0
- idotmatrix/apps/digital_picture_frame.py +541 -0
- idotmatrix/client.py +284 -0
- idotmatrix/connection_manager.py +702 -0
- idotmatrix/const.py +30 -0
- idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
- idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
- idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
- idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
- idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
- idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
- idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
- idotmatrix/fonts/16-dogica.ttf +0 -0
- idotmatrix/fonts/16-dogicabold.ttf +0 -0
- idotmatrix/fonts/16-dogicapixel.ttf +0 -0
- idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
- idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
- idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
- idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
- idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
- idotmatrix/fonts/CC-BY-SA-4.0-License.txt +428 -0
- idotmatrix/fonts/ErbosDraco-License.txt +3 -0
- idotmatrix/fonts/HomeVideo-License.txt +2 -0
- idotmatrix/fonts/Jersey10-Regular-License.txt +93 -0
- idotmatrix/fonts/MayfairNbpBold-License.txt +3 -0
- idotmatrix/fonts/MinecraftStandard-License.txt +5 -0
- idotmatrix/fonts/PixelOperator-License.txt +121 -0
- idotmatrix/fonts/PixelifySans-License.txt +93 -0
- idotmatrix/fonts/PressStart2P-Regular-License.txt +93 -0
- idotmatrix/fonts/Rain-DRM3-License.txt +44 -0
- idotmatrix/fonts/Rain-DRM3.otf +0 -0
- idotmatrix/fonts/dogica_license.txt +94 -0
- idotmatrix/modules/__init__.py +64 -0
- idotmatrix/modules/chronograph.py +50 -0
- idotmatrix/modules/clock.py +101 -0
- idotmatrix/modules/common.py +176 -0
- idotmatrix/modules/countdown.py +70 -0
- idotmatrix/modules/eco.py +63 -0
- idotmatrix/modules/effect.py +79 -0
- idotmatrix/modules/fullscreen_color.py +55 -0
- idotmatrix/modules/gif.py +403 -0
- idotmatrix/modules/graffiti.py +71 -0
- idotmatrix/modules/image.py +383 -0
- idotmatrix/modules/music_sync.py +67 -0
- idotmatrix/modules/scoreboard.py +38 -0
- idotmatrix/modules/system.py +65 -0
- idotmatrix/modules/text.py +232 -0
- idotmatrix/protocol.py +52 -0
- idotmatrix/screensize.py +8 -0
- idotmatrix/transport.py +137 -0
- idotmatrix/util/__init__.py +0 -0
- idotmatrix/util/color_utils.py +64 -0
- idotmatrix/util/command_ack.py +23 -0
- idotmatrix/util/device_info.py +38 -0
- idotmatrix/util/device_material.py +16 -0
- idotmatrix/util/file_watch.py +57 -0
- idotmatrix/util/image_utils.py +139 -0
- idotmatrix/util/upload_ack.py +42 -0
- idotmatrix_api_client-0.2.0.dist-info/METADATA +375 -0
- idotmatrix_api_client-0.2.0.dist-info/RECORD +63 -0
- idotmatrix_api_client-0.2.0.dist-info/WHEEL +4 -0
- idotmatrix_api_client-0.2.0.dist-info/licenses/LICENSE +674 -0
idotmatrix/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""
|
|
2
|
+
idotmatrix.
|
|
3
|
+
|
|
4
|
+
Library to configure any iDotMatrix compatible 16x16 or 32x32 pixel display without the chinese iDotMatrix android / iOS app.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
__author__ = "Kalle Minkner, Jon-Mailes Graeffe"
|
|
8
|
+
__credits__ = (
|
|
9
|
+
"everyone who thankfully helped with the reverse-engineering. You are awesome!"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
from .client import IDotMatrixClient
|
|
13
|
+
from .screensize import ScreenSize
|
|
14
|
+
|
|
15
|
+
__all__ = ["IDotMatrixClient", "ScreenSize"]
|
|
File without changes
|
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import logging
|
|
3
|
+
import re
|
|
4
|
+
import signal
|
|
5
|
+
from asyncio import sleep, Task
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from os import PathLike
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from random import shuffle
|
|
10
|
+
from typing import List
|
|
11
|
+
|
|
12
|
+
from watchdog.observers.inotify import InotifyObserver
|
|
13
|
+
from watchdog.observers.polling import PollingObserver
|
|
14
|
+
|
|
15
|
+
from idotmatrix.client import IDotMatrixClient
|
|
16
|
+
from idotmatrix.connection_manager import ConnectionListener
|
|
17
|
+
from idotmatrix.util.file_watch import ImageFileEventHandler
|
|
18
|
+
from idotmatrix.util.image_utils import ResizeMode
|
|
19
|
+
|
|
20
|
+
FilesystemObserver = InotifyObserver | PollingObserver
|
|
21
|
+
|
|
22
|
+
IMAGE_FILE_EXTENSIONS = {".png", ".jpg", ".jpeg"}
|
|
23
|
+
ANIMATION_FILE_EXTENSIONS = {".gif"}
|
|
24
|
+
SUPPORTED_FILE_EXTENSIONS = IMAGE_FILE_EXTENSIONS.union(ANIMATION_FILE_EXTENSIONS)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PictureFrameGif:
|
|
28
|
+
def __init__(self, file_path: PathLike | str):
|
|
29
|
+
self.file_path = file_path
|
|
30
|
+
self.duration_per_frame_in_ms = None
|
|
31
|
+
|
|
32
|
+
def __eq__(self, other):
|
|
33
|
+
if isinstance(other, PictureFrameGif):
|
|
34
|
+
return self.file_path == other.file_path
|
|
35
|
+
if isinstance(other, (PathLike, str)):
|
|
36
|
+
return self.file_path == other
|
|
37
|
+
return False
|
|
38
|
+
|
|
39
|
+
def __str__(self):
|
|
40
|
+
return f"PictureFrameGif(file_path={self.file_path}, duration_per_frame_in_ms={self.duration_per_frame_in_ms})"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class PictureFrameImage:
|
|
44
|
+
def __init__(self, file_path: PathLike | str):
|
|
45
|
+
self.file_path = file_path
|
|
46
|
+
|
|
47
|
+
def __eq__(self, other):
|
|
48
|
+
if isinstance(other, PictureFrameImage):
|
|
49
|
+
return self.file_path == other.file_path
|
|
50
|
+
if isinstance(other, (PathLike, str)):
|
|
51
|
+
return self.file_path == other
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
def __str__(self):
|
|
55
|
+
return f"PictureFrameImage(file_path={self.file_path})"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
DEFAULT_INTERVAL_SECONDS = 30
|
|
59
|
+
HEARTBEAT_INTERVAL_SECONDS = 60
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class FileObserverType(Enum):
|
|
63
|
+
INOTIFY = "inotify"
|
|
64
|
+
POLLING = "polling"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class DigitalPictureFrame:
|
|
68
|
+
"""
|
|
69
|
+
A class to manage a digital picture frame that can display images and GIFs in a slideshow format.
|
|
70
|
+
"""
|
|
71
|
+
logging = logging.getLogger(__name__)
|
|
72
|
+
|
|
73
|
+
def __init__(
|
|
74
|
+
self,
|
|
75
|
+
device_client: IDotMatrixClient,
|
|
76
|
+
images: List[PictureFrameImage | PictureFrameGif | PathLike | str] = None,
|
|
77
|
+
resize_mode: ResizeMode = ResizeMode.FIT,
|
|
78
|
+
interval_seconds: int = DEFAULT_INTERVAL_SECONDS,
|
|
79
|
+
shuffle_images: bool = False,
|
|
80
|
+
):
|
|
81
|
+
"""
|
|
82
|
+
Initializes the DigitalPictureFrame with a device client and optional images.
|
|
83
|
+
Args:
|
|
84
|
+
device_client (IDotMatrixClient): The client to communicate with the digital picture frame device.
|
|
85
|
+
images (List[PictureFrameImage | PictureFrameGif | PathLike | str]): A list of images or GIFs to display.
|
|
86
|
+
resize_mode (ResizeMode): The mode to use for resizing images (ResizeMode.FIT, ResizeMode.FILL, ResizeMode.STRETCH).
|
|
87
|
+
interval_seconds (int): The time in seconds between image changes in the slideshow. Defaults to 30 seconds.
|
|
88
|
+
shuffle_images (bool): Whether to shuffle the images in the slideshow. Defaults to False.
|
|
89
|
+
"""
|
|
90
|
+
self.device_client: IDotMatrixClient = device_client
|
|
91
|
+
self.device_client.set_auto_reconnect(True)
|
|
92
|
+
self.device_client.set_heartbeat(HEARTBEAT_INTERVAL_SECONDS)
|
|
93
|
+
self._setup_connection_listener()
|
|
94
|
+
|
|
95
|
+
self.resize_mode: ResizeMode = resize_mode
|
|
96
|
+
|
|
97
|
+
if not images:
|
|
98
|
+
images = []
|
|
99
|
+
self.images: List[PictureFrameImage | PictureFrameGif | PathLike | str] = images
|
|
100
|
+
self.interval_seconds: int = interval_seconds
|
|
101
|
+
self._shuffle_images = shuffle_images
|
|
102
|
+
|
|
103
|
+
self._filesystem_observers: List[FilesystemObserver] = []
|
|
104
|
+
|
|
105
|
+
self._current_slideshow_index: int = -1 # Start with -1 to ensure the first call to next() works correctly
|
|
106
|
+
self._last_set_image: PictureFrameImage | PictureFrameGif | PathLike | str | None = ""
|
|
107
|
+
|
|
108
|
+
self._slideshow_task: Task | None = None
|
|
109
|
+
|
|
110
|
+
self._is_paused: bool = False
|
|
111
|
+
|
|
112
|
+
def _setup_connection_listener(self):
|
|
113
|
+
"""
|
|
114
|
+
Due to the device not beeing the most stable, and the connection also being lost sometimes,
|
|
115
|
+
we need to ensure that the device is always in a valid state when the connection is re-established.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
async def on_device_connected():
|
|
119
|
+
self.logging.debug("Device connected, resetting state and resuming slideshow.")
|
|
120
|
+
await self.resume_slideshow()
|
|
121
|
+
|
|
122
|
+
async def on_device_disconnected():
|
|
123
|
+
self.logging.debug("Device disconnected, resetting state and pausing slideshow.")
|
|
124
|
+
self._last_set_image = None
|
|
125
|
+
await self.pause_slideshow()
|
|
126
|
+
|
|
127
|
+
connection_listener = ConnectionListener(
|
|
128
|
+
on_connected=on_device_connected,
|
|
129
|
+
on_disconnected=on_device_disconnected,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
self.device_client.add_connection_listener(connection_listener)
|
|
133
|
+
|
|
134
|
+
def set_interval(self, interval: int):
|
|
135
|
+
"""
|
|
136
|
+
Sets the interval between two images/GIFs for the slideshow.
|
|
137
|
+
"""
|
|
138
|
+
if interval <= 0:
|
|
139
|
+
raise ValueError("Interval must be greater than 0 seconds.")
|
|
140
|
+
self.logging.info(f"Setting slideshow interval to {interval} seconds")
|
|
141
|
+
self.interval_seconds = interval
|
|
142
|
+
|
|
143
|
+
def watch_folders(
|
|
144
|
+
self,
|
|
145
|
+
folders: List[PathLike | str],
|
|
146
|
+
recursive: bool = False,
|
|
147
|
+
observer_type: FileObserverType = FileObserverType.INOTIFY
|
|
148
|
+
):
|
|
149
|
+
"""
|
|
150
|
+
Adds the given folders to the watchlist and displays any image or GIF in them in the slideshow.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
folders (List[PathLike | str]): The folders to watch.
|
|
154
|
+
recursive (bool): Whether to watch subdirectories recursively. Defaults to False.
|
|
155
|
+
observer_type (FileObserverType): The type of file observer to use. Defaults to FileObserverType.INOTIFY.
|
|
156
|
+
"""
|
|
157
|
+
if not isinstance(folders, list):
|
|
158
|
+
raise ValueError("Folders must be a list of PathLike or str.")
|
|
159
|
+
|
|
160
|
+
for folder in folders:
|
|
161
|
+
self.add_folder(folder, recursive)
|
|
162
|
+
self.watch_folder(folder, recursive, observer_type)
|
|
163
|
+
|
|
164
|
+
def watch_folder(
|
|
165
|
+
self,
|
|
166
|
+
folder: PathLike | str,
|
|
167
|
+
recursive: bool = False,
|
|
168
|
+
observer_type: FileObserverType = FileObserverType.INOTIFY
|
|
169
|
+
):
|
|
170
|
+
"""
|
|
171
|
+
Adds the given folder to the watchlist and displays any image or GIF in them in the slideshow.
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
folder (PathLike | str): The folder to watch.
|
|
175
|
+
recursive (bool): Whether to watch subdirectories recursively. Defaults to False.
|
|
176
|
+
observer_type (FileObserverType): The type of file observer to use. Defaults to FileObserverType.INOTIFY.
|
|
177
|
+
"""
|
|
178
|
+
if not isinstance(folder, (PathLike, str)):
|
|
179
|
+
raise ValueError("Folder must be of type PathLike or str.")
|
|
180
|
+
|
|
181
|
+
self._add_folder_watch(
|
|
182
|
+
folder=Path(folder),
|
|
183
|
+
recursive=recursive,
|
|
184
|
+
observer_type=observer_type,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
self.logging.info(f"Watching folder: {folder}")
|
|
188
|
+
|
|
189
|
+
def add_folder(
|
|
190
|
+
self,
|
|
191
|
+
folder: PathLike | str,
|
|
192
|
+
recursive: bool = False
|
|
193
|
+
):
|
|
194
|
+
"""
|
|
195
|
+
Adds all images and GIFs in the given folder to the slideshow.
|
|
196
|
+
Args:
|
|
197
|
+
folder (PathLike | str): The folder to add images from.
|
|
198
|
+
recursive (bool): Whether to add images from subdirectories recursively. Defaults to False.
|
|
199
|
+
"""
|
|
200
|
+
if not isinstance(folder, (PathLike, str)):
|
|
201
|
+
raise ValueError("Folder must be of type PathLike or str.")
|
|
202
|
+
|
|
203
|
+
folder_path = Path(folder)
|
|
204
|
+
if not folder_path.is_dir():
|
|
205
|
+
raise ValueError(f"The provided path '{folder}' is not a directory.")
|
|
206
|
+
|
|
207
|
+
self.logging.info(f"Adding images from folder: {folder_path} (recursive={recursive})")
|
|
208
|
+
files = folder_path.rglob("*") if recursive else folder_path.glob("*")
|
|
209
|
+
for file in files:
|
|
210
|
+
if file.is_file():
|
|
211
|
+
self._add_image_file(file)
|
|
212
|
+
|
|
213
|
+
def _add_image_file(self, file: Path):
|
|
214
|
+
"""
|
|
215
|
+
Adds a file to the slideshow as image or GIF, depending on its extension. Other files are ignored.
|
|
216
|
+
"""
|
|
217
|
+
if file.suffix.lower() in IMAGE_FILE_EXTENSIONS:
|
|
218
|
+
self.add_image(PictureFrameImage(file))
|
|
219
|
+
elif file.suffix.lower() in ANIMATION_FILE_EXTENSIONS:
|
|
220
|
+
self.add_image(PictureFrameGif(file))
|
|
221
|
+
|
|
222
|
+
def add_image(self, image: PictureFrameImage | PictureFrameGif | PathLike | str):
|
|
223
|
+
"""
|
|
224
|
+
Adds an image or GIF to the slideshow.
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
image (PictureFrameImage | PictureFrameGif | PathLike | str): The image or GIF to add.
|
|
228
|
+
"""
|
|
229
|
+
if not isinstance(image, (PictureFrameImage, PictureFrameGif, PathLike, str)):
|
|
230
|
+
raise ValueError("Image must be of type PictureFrameImage, PictureFrameGif, PathLike, or str.")
|
|
231
|
+
|
|
232
|
+
self.images.append(image)
|
|
233
|
+
self.logging.info(f"Added image: {image}")
|
|
234
|
+
|
|
235
|
+
def remove_image(self, image: PictureFrameImage | PictureFrameGif | PathLike | str):
|
|
236
|
+
"""
|
|
237
|
+
Removes an image or GIF from the slideshow.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
image (PictureFrameImage | PictureFrameGif | PathLike | str): The image or GIF to remove.
|
|
241
|
+
"""
|
|
242
|
+
if image in self.images:
|
|
243
|
+
index = self.images.index(image)
|
|
244
|
+
self.images.pop(index)
|
|
245
|
+
if index <= self._current_slideshow_index:
|
|
246
|
+
# keep pointing at the same image, so that the next one is not skipped
|
|
247
|
+
self._current_slideshow_index -= 1
|
|
248
|
+
self.logging.info(f"Removed image: {image}")
|
|
249
|
+
else:
|
|
250
|
+
self.logging.warning(f"Image not found in slideshow: {image}")
|
|
251
|
+
|
|
252
|
+
def shuffle_images(self):
|
|
253
|
+
"""
|
|
254
|
+
Shuffles the images in the slideshow.
|
|
255
|
+
"""
|
|
256
|
+
if not self.images:
|
|
257
|
+
return
|
|
258
|
+
|
|
259
|
+
self.logging.info("Shuffling images in slideshow")
|
|
260
|
+
shuffle(self.images)
|
|
261
|
+
|
|
262
|
+
if self._last_set_image is not None:
|
|
263
|
+
current_image = self._get_current_image()
|
|
264
|
+
if current_image == self._last_set_image:
|
|
265
|
+
# if, after shuffling, the current image is still the same as the last set image,
|
|
266
|
+
# simply advance the slideshow index by one, to get a different image.
|
|
267
|
+
# if the list of images is empty, this will simply do nothing.
|
|
268
|
+
# if there is only one image, this will also do nothing,
|
|
269
|
+
self._advance_slideshow_index()
|
|
270
|
+
|
|
271
|
+
async def start_slideshow(self, interval: int = None) -> Task:
|
|
272
|
+
"""
|
|
273
|
+
Starts a slideshow of images on the digital picture frame.
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
interval (int): Time in seconds between image changes. Defaults to 5 seconds.
|
|
277
|
+
"""
|
|
278
|
+
image_count = len(self.images)
|
|
279
|
+
self.logging.info(f"Starting slideshow containing {image_count} items with interval: {interval} seconds")
|
|
280
|
+
if interval is not None:
|
|
281
|
+
self.set_interval(interval)
|
|
282
|
+
|
|
283
|
+
async def async_signal_handler():
|
|
284
|
+
"""
|
|
285
|
+
Handles the signal to stop the slideshow gracefully.
|
|
286
|
+
"""
|
|
287
|
+
await self.stop_slideshow()
|
|
288
|
+
# TODO: doing it like this is probably a bad idea,
|
|
289
|
+
# since it will also kill anything else that is currently running,
|
|
290
|
+
# or even fail to stop the loop,
|
|
291
|
+
# but I haven't gotten anything else to work yet :(
|
|
292
|
+
asyncio.get_event_loop().stop()
|
|
293
|
+
|
|
294
|
+
def signal_handler(signum):
|
|
295
|
+
signame = signal.Signals(signum).name
|
|
296
|
+
logging.info(f'Signal handler called with signal {signame} ({signum})')
|
|
297
|
+
asyncio.ensure_future(async_signal_handler())
|
|
298
|
+
|
|
299
|
+
asyncio.get_event_loop().add_signal_handler(signal.SIGINT, lambda: signal_handler(signal.SIGINT))
|
|
300
|
+
asyncio.get_event_loop().add_signal_handler(signal.SIGTERM, lambda: signal_handler(signal.SIGTERM))
|
|
301
|
+
self._slideshow_task = self._start_slideshow_task()
|
|
302
|
+
await asyncio.sleep(0)
|
|
303
|
+
return self._slideshow_task
|
|
304
|
+
|
|
305
|
+
async def pause_slideshow(self):
|
|
306
|
+
"""
|
|
307
|
+
Pauses the slideshow.
|
|
308
|
+
"""
|
|
309
|
+
if self.is_slideshow_running():
|
|
310
|
+
self.logging.info("Pausing slideshow")
|
|
311
|
+
self._is_paused = True
|
|
312
|
+
|
|
313
|
+
async def resume_slideshow(self):
|
|
314
|
+
"""
|
|
315
|
+
Resumes the slideshow if it was paused.
|
|
316
|
+
"""
|
|
317
|
+
if not self.is_slideshow_running():
|
|
318
|
+
self.logging.info("Resuming slideshow")
|
|
319
|
+
self._is_paused = False
|
|
320
|
+
|
|
321
|
+
async def stop_slideshow(self):
|
|
322
|
+
"""
|
|
323
|
+
Stops the slideshow.
|
|
324
|
+
"""
|
|
325
|
+
if self._slideshow_task:
|
|
326
|
+
self.logging.info("Stopping slideshow")
|
|
327
|
+
self._slideshow_task.cancel()
|
|
328
|
+
self._slideshow_task = None
|
|
329
|
+
|
|
330
|
+
# we don't have a way to know what the device was showing before the slideshow started,
|
|
331
|
+
# so we simply show the clock
|
|
332
|
+
await self.device_client.clock.show(show_date=False)
|
|
333
|
+
await self.device_client.disconnect()
|
|
334
|
+
|
|
335
|
+
asyncio.get_event_loop().remove_signal_handler(signal.SIGINT)
|
|
336
|
+
asyncio.get_event_loop().remove_signal_handler(signal.SIGTERM)
|
|
337
|
+
|
|
338
|
+
def is_slideshow_running(self) -> bool:
|
|
339
|
+
"""
|
|
340
|
+
Checks if the slideshow is currently running.
|
|
341
|
+
|
|
342
|
+
Returns:
|
|
343
|
+
bool: True if the slideshow is running, False otherwise.
|
|
344
|
+
"""
|
|
345
|
+
return self._slideshow_task is not None and not self._slideshow_task.done() and not self._is_paused
|
|
346
|
+
|
|
347
|
+
def _start_slideshow_task(self) -> Task:
|
|
348
|
+
"""
|
|
349
|
+
Starts the slideshow task that uploads images to the device at specified intervals.
|
|
350
|
+
"""
|
|
351
|
+
return asyncio.create_task(self._slideshow_task_main_loop())
|
|
352
|
+
|
|
353
|
+
async def _slideshow_task_main_loop(self):
|
|
354
|
+
"""
|
|
355
|
+
Internal method to handle the slideshow loop.
|
|
356
|
+
"""
|
|
357
|
+
while True:
|
|
358
|
+
try:
|
|
359
|
+
if not self.device_client.screen_size_known:
|
|
360
|
+
# connecting does not detect it when the client is connected on the first command
|
|
361
|
+
await self.device_client.detect_screen_size()
|
|
362
|
+
|
|
363
|
+
# initialize by connecting to the device and showing a black screen
|
|
364
|
+
await self._show_black_screen()
|
|
365
|
+
|
|
366
|
+
# start the slideshow loop
|
|
367
|
+
await self._slideshow_task_inner_loop()
|
|
368
|
+
except asyncio.CancelledError:
|
|
369
|
+
self.logging.info("Slideshow loop cancelled.")
|
|
370
|
+
break
|
|
371
|
+
except Exception as ex:
|
|
372
|
+
self.logging.error(f"Unexpected error in slideshow loop: {ex}")
|
|
373
|
+
# wait a bit before retrying to avoid rapid reconnection attempts
|
|
374
|
+
await sleep(10)
|
|
375
|
+
|
|
376
|
+
async def _slideshow_task_inner_loop(self):
|
|
377
|
+
while True:
|
|
378
|
+
try:
|
|
379
|
+
if self._is_paused:
|
|
380
|
+
await sleep(1)
|
|
381
|
+
continue
|
|
382
|
+
await self.next()
|
|
383
|
+
except Exception as ex:
|
|
384
|
+
self.logging.error(f"Error switching to next image in slideshow: {ex}")
|
|
385
|
+
continue
|
|
386
|
+
await sleep(self.interval_seconds)
|
|
387
|
+
|
|
388
|
+
async def next(self):
|
|
389
|
+
"""
|
|
390
|
+
Switches to the next image in the slideshow.
|
|
391
|
+
|
|
392
|
+
This can be used to manually advance to the next image in the slideshow, but keep in mind
|
|
393
|
+
that if a slideshow has been started, it will also automatically advance to the next image
|
|
394
|
+
independently.
|
|
395
|
+
"""
|
|
396
|
+
if not self.images:
|
|
397
|
+
if self._last_set_image is not None:
|
|
398
|
+
self.logging.warning("No images in slideshow to display.")
|
|
399
|
+
await self._show_black_screen()
|
|
400
|
+
return
|
|
401
|
+
self._advance_slideshow_index()
|
|
402
|
+
if self._shuffle_images and self._current_slideshow_index == 0:
|
|
403
|
+
self.shuffle_images()
|
|
404
|
+
next_image = self.images[self._current_slideshow_index]
|
|
405
|
+
if next_image != self._last_set_image:
|
|
406
|
+
try:
|
|
407
|
+
image_path = await self._switch_to(next_image)
|
|
408
|
+
except Exception as ex:
|
|
409
|
+
self.logging.error(f"Failed to switch to image: {next_image} ({ex}). Skipping this image.")
|
|
410
|
+
if isinstance(ex, FileNotFoundError):
|
|
411
|
+
# the file is gone, so there is no point in trying it again on every round
|
|
412
|
+
self.remove_image(next_image)
|
|
413
|
+
return
|
|
414
|
+
if len(self.images) > 1:
|
|
415
|
+
self.logging.info(f"Displaying image '{image_path}'.")
|
|
416
|
+
else:
|
|
417
|
+
self.logging.info(f"Displaying image '{image_path}' (only image in slideshow).")
|
|
418
|
+
else:
|
|
419
|
+
if len(self.images) > 1:
|
|
420
|
+
self.logging.info(f"Skipping image '{next_image}' as it is already being displayed currently.")
|
|
421
|
+
|
|
422
|
+
async def _switch_to(self, image: PictureFrameImage | PictureFrameGif | PathLike | str) -> str:
|
|
423
|
+
if isinstance(image, PictureFrameImage):
|
|
424
|
+
image_path = image.file_path
|
|
425
|
+
await self._set_image(image_path)
|
|
426
|
+
elif isinstance(image, PictureFrameGif):
|
|
427
|
+
image_path = image.file_path
|
|
428
|
+
await self._set_gif(
|
|
429
|
+
file_path=image_path,
|
|
430
|
+
duration_per_frame_in_ms=image.duration_per_frame_in_ms
|
|
431
|
+
)
|
|
432
|
+
elif isinstance(image, (PathLike, str)):
|
|
433
|
+
# If it's a string or PathLike, treat it as a file path
|
|
434
|
+
if isinstance(image, PathLike):
|
|
435
|
+
image_path = image.__fspath__()
|
|
436
|
+
else:
|
|
437
|
+
image_path = image
|
|
438
|
+
if image_path.lower().endswith('.gif'):
|
|
439
|
+
await self._set_gif(image_path)
|
|
440
|
+
else:
|
|
441
|
+
await self._set_image(image_path)
|
|
442
|
+
else:
|
|
443
|
+
raise ValueError(
|
|
444
|
+
f"Unsupported image type: {type(image)}. Must be PictureFrameImage, PictureFrameGif, or a file path."
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
self._last_set_image = image
|
|
448
|
+
return image_path
|
|
449
|
+
|
|
450
|
+
async def _set_image(
|
|
451
|
+
self,
|
|
452
|
+
file_path: PathLike | str
|
|
453
|
+
):
|
|
454
|
+
self.logging.debug(f"Setting image file: {file_path}")
|
|
455
|
+
await self.device_client.image.show_image_file(
|
|
456
|
+
file_path=file_path,
|
|
457
|
+
resize_mode=self.resize_mode,
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
async def _set_gif(
|
|
461
|
+
self,
|
|
462
|
+
file_path: PathLike | str,
|
|
463
|
+
duration_per_frame_in_ms: int = None
|
|
464
|
+
):
|
|
465
|
+
self.logging.debug(f"Setting GIF file: {file_path} ({duration_per_frame_in_ms} ms per frame)")
|
|
466
|
+
await self.device_client.gif.upload_gif_file(
|
|
467
|
+
file_path=file_path,
|
|
468
|
+
resize_mode=self.resize_mode,
|
|
469
|
+
duration_per_frame_in_ms=duration_per_frame_in_ms,
|
|
470
|
+
)
|
|
471
|
+
# give the device some time to process the GIF
|
|
472
|
+
await sleep(3)
|
|
473
|
+
|
|
474
|
+
def _add_folder_watch(
|
|
475
|
+
self,
|
|
476
|
+
folder: Path,
|
|
477
|
+
recursive: bool = False,
|
|
478
|
+
observer_type: FileObserverType = FileObserverType.INOTIFY,
|
|
479
|
+
):
|
|
480
|
+
"""
|
|
481
|
+
Adds a folder to the watchlist and sets up file observers to monitor changes in the folder.
|
|
482
|
+
Args:
|
|
483
|
+
folder (Path): The folder to watch.
|
|
484
|
+
recursive (bool): Whether to watch subdirectories recursively. Defaults to False.
|
|
485
|
+
observer_type (FileObserverType): The type of file observer to use. Defaults to FileObserverType.INOTIFY.
|
|
486
|
+
"""
|
|
487
|
+
self.file_regex = re.compile(rf"^.*({'|'.join(SUPPORTED_FILE_EXTENSIONS)})$", re.IGNORECASE)
|
|
488
|
+
|
|
489
|
+
observers = self._setup_file_observers(
|
|
490
|
+
observer_type=observer_type,
|
|
491
|
+
source_directories=[Path(folder)],
|
|
492
|
+
recursive=recursive,
|
|
493
|
+
file_filter=self.file_regex,
|
|
494
|
+
)
|
|
495
|
+
self._filesystem_observers.extend(observers)
|
|
496
|
+
|
|
497
|
+
self.logging.info(f"Added folder to watchlist: {folder}")
|
|
498
|
+
|
|
499
|
+
def _setup_file_observers(
|
|
500
|
+
self,
|
|
501
|
+
observer_type: FileObserverType,
|
|
502
|
+
source_directories: List[Path],
|
|
503
|
+
recursive: bool = False,
|
|
504
|
+
file_filter: re.Pattern | None = None,
|
|
505
|
+
) -> List[FilesystemObserver]:
|
|
506
|
+
observers = []
|
|
507
|
+
|
|
508
|
+
for directory in source_directories:
|
|
509
|
+
if observer_type == FileObserverType.INOTIFY:
|
|
510
|
+
observer = InotifyObserver()
|
|
511
|
+
elif observer_type == FileObserverType.POLLING:
|
|
512
|
+
observer = PollingObserver()
|
|
513
|
+
else:
|
|
514
|
+
raise ValueError(f"Unexpected file observer type {observer_type}")
|
|
515
|
+
|
|
516
|
+
def on_file_moved(old, new):
|
|
517
|
+
self.remove_image(old)
|
|
518
|
+
self._add_image_file(new)
|
|
519
|
+
|
|
520
|
+
event_handler = ImageFileEventHandler(
|
|
521
|
+
file_filter=file_filter,
|
|
522
|
+
on_created=self._add_image_file,
|
|
523
|
+
on_deleted=lambda x: self.remove_image(x),
|
|
524
|
+
on_moved=on_file_moved
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
observer.schedule(event_handler, str(directory), recursive=recursive)
|
|
528
|
+
observer.start()
|
|
529
|
+
observers.append(observer)
|
|
530
|
+
|
|
531
|
+
return observers
|
|
532
|
+
|
|
533
|
+
def _advance_slideshow_index(self):
|
|
534
|
+
self._current_slideshow_index = (self._current_slideshow_index + 1) % len(self.images)
|
|
535
|
+
|
|
536
|
+
async def _show_black_screen(self):
|
|
537
|
+
self._last_set_image = None
|
|
538
|
+
await self.device_client.color.show_color(color="black")
|
|
539
|
+
|
|
540
|
+
def _get_current_image(self) -> PictureFrameImage | PictureFrameGif | PathLike | str | None:
|
|
541
|
+
return self.images[self._current_slideshow_index % len(self.images)] if self.images else None
|