-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdf_console.py
executable file
·367 lines (305 loc) · 13.5 KB
/
df_console.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/python3
"""
DigiFrame is a lightweight solution for digital picture frame.
Copyright (C) 2021 Awalon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os.path
import df_util
from df_util import df_logger
import datetime
import logging.handlers
import sys
import time
import tempfile
from PIL import Image, ImageDraw, ImageFont, ImageColor
import psutil
import subprocess
import threading
from multiprocessing.connection import Listener
# from RPi import GPIO
RESTART = False
TIME_REMIX = -1
TIME_RESYNC = -1
stop_control_interface = False
# config
# global CONFIG
CONFIG = df_util.Config()
def turn_off_cursor():
try:
terminal = open("/dev/tty1", "wb")
subprocess.run(["setterm", "-cursor", "off"],
stdout=terminal, stderr=log_pipe_err,
env={"TERM": "xterm-256color"})
except Exception as ex_cursor:
if df_logger:
df_logger.error('Can not disable cursor: %s' % ex_cursor)
def start_slideshow(splashshow):
if splashshow:
stop_splashshow(splashshow)
try:
cmd = ["fim", "--device", "/dev/fb0", "--vt", "1", "--no-commandline", "--quiet",
"--recursive", CONFIG.PICTURE_PATH, "--execute-commands-early", '_scale_style="h"', "--execute-commands",
'while(1){display;sleep "' + str(CONFIG.PICTURE_TIMEOUT) + '";next;}']
if CONFIG.RANDOM:
cmd.append("--random")
slideshow = subprocess.Popen(cmd, stdout=log_pipe_out, stderr=log_pipe_err)
time.sleep(5) # Give time for fim to start so when the screen is turned on there’s already a picture displayed
subprocess.run(["vcgencmd", "display_power", "1"], stdout=log_pipe_out, stderr=log_pipe_err)
return slideshow
except Exception as ex_slideshow:
df_logger.error('Can not start slideshow: %s' % ex_slideshow)
if CONFIG.DEBUG:
splashshow = subprocess.Popen(['true'], stdout=log_pipe_out, stderr=log_pipe_err)
time.sleep(5)
return splashshow
return None
def restart_slideshow(slideshow):
if slideshow is not None and slideshow.poll() is None:
slideshow.terminate()
return start_slideshow(None)
def stop_slideshow(slideshow):
try:
subprocess.run(["vcgencmd", "display_power", "0"])
except Exception as ex_power:
df_logger.error('Can not disable display power: %s' % ex_power)
if slideshow is not None and slideshow.poll() is None:
slideshow.terminate()
time.sleep(12) # My screen waits 10 seconds before switching to sleep mode
def get_time():
return datetime.datetime.now(tz=CONFIG.time_zone()).timestamp()
def sync_src():
if CONFIG.SYNC_MODE == CONFIG.SYNC_MODE_RCLONE:
df_logger.info('sync with rclone "'
+ CONFIG.SYNC_SOURCE + '" to "' + CONFIG.PICTURE_PATH + '" (interval: '
+ str(CONFIG.SYNC_INTERVAL) + ' sec)...')
try:
subprocess.run(["/usr/bin/rclone", "sync", CONFIG.SYNC_SOURCE, CONFIG.PICTURE_PATH],
stdout=log_pipe_out, stderr=log_pipe_err)
except Exception as ex_sync:
df_logger.error('Can not sync with rclone src: %s' % ex_sync)
elif CONFIG.SYNC_MODE == CONFIG.SYNC_MODE_RSYNC:
df_logger.info('sync with rsync "'
+ CONFIG.SYNC_SOURCE + '" to "' + CONFIG.PICTURE_PATH + '" (interval: '
+ str(CONFIG.SYNC_INTERVAL) + ' sec)...')
try:
subprocess.run(["/usr/bin/rsync", "-aP", "--no-owner", "--delete",
"-e ssh -i ~/.ssh/rsync-key -o PubkeyAuthentication=yes",
CONFIG.SYNC_SOURCE, CONFIG.PICTURE_PATH],
stdout=log_pipe_out, stderr=log_pipe_err)
except Exception as ex_sync:
df_logger.error('Can not sync with rsync src: %s' % ex_sync)
else: # CONFIG.SYNC_MODE_NONE
df_logger.info('sync is disabled (None)...')
file_hash, file_count = df_util.get_file_list_hash(CONFIG.PICTURE_PATH)
return file_hash, file_count, get_time()
def get_splash_with_host_info(splash_picture: str) -> str:
try:
if os.path.isfile(splash_picture):
sys_info = df_util.get_sys_stat()
font_file = os.path.join(df_util.script_path, 'static/fonts/anita-semi-square/Anita semi square.ttf')
# add information to default splash screen
img = Image.open(splash_picture)
draw = ImageDraw.Draw(img)
x = 40
y = 40
# host name
draw.rectangle([(0, y-5), (img.width, y+40)], fill='#212529cc', width=0)
font = ImageFont.truetype(font_file, 32)
draw.text((x, y), sys_info['sys_name'], CONFIG.WEB_THEME_COLOR, font=font)
y += 5
# host IPs
font = ImageFont.truetype(font_file, 24)
for net_if, info in sys_info['_net_if']:
if net_if != 'lo':
for address in info:
y += 40
draw.rectangle([(0, y-5), (img.width, y+35)],
fill='#2c3034cc', width=0)
draw.text((x, y),
'%s (%s): %s' % (net_if, address['family'], address['address']),
CONFIG.WEB_THEME_COLOR,
font=font)
# write new, temporary splash picture
temp_dir = tempfile.gettempdir()
splash_picture_with_host_info = os.path.join(temp_dir, 'host_info_%s' % os.path.basename(splash_picture))
img.save(splash_picture_with_host_info)
df_logger.info('splash screen with host information: %s' % splash_picture_with_host_info)
return splash_picture_with_host_info
except Exception as ex:
df_logger.error('Can not generate host info: %s' % ex)
pass
return splash_picture
def start_splashshow():
df_logger.info('splash screen...')
turn_off_cursor()
# kill old processes
for proc in psutil.process_iter():
try:
if proc.name() == 'fim':
df_logger.info('killing: ' + proc.name() + '[' + str(proc.pid) + ']')
proc.kill()
except psutil.NoSuchProcess:
pass
except psutil.AccessDenied as ex_denied:
df_logger.warning('Can not kill old process: %s' % ex_denied)
pass
except Exception as ex_kill:
df_logger.error('Can not kill old process: %s' % ex_kill)
try:
splash_time = 3
splash_picture = os.path.join(df_util.script_path, 'static/splash.png')
if CONFIG.SHOW_HOST_INFO:
splash_picture = get_splash_with_host_info(splash_picture)
splash_time += 7
cmd = ['fim', '--device', '/dev/fb0', '--vt', '1', '--no-commandline', '--quiet',
'-a', "--execute-commands-early", '_scale_style="h"', '--', splash_picture]
df_logger.debug("Splash cmd: %s" % ' '.join(cmd))
splashshow = subprocess.Popen(cmd,
stdout=log_pipe_out, stderr=log_pipe_err)
time.sleep(splash_time)
return splashshow
except Exception as ex_splash:
df_logger.error('Can not start splash screen: %s' % ex_splash)
if CONFIG.DEBUG:
splashshow = subprocess.Popen(['true'], stdout=log_pipe_out, stderr=log_pipe_err)
time.sleep(3)
return splashshow
pass
return None
def stop_splashshow(splashshow):
if splashshow is not None and splashshow.poll() is None:
splashshow.terminate()
def control_interface(thread_name):
global RESTART
global CONFIG
global TIME_REMIX, TIME_RESYNC
global stop_control_interface
while True:
if stop_control_interface:
break
try:
address = ('localhost', CONFIG.IPC_PORT) # family is deduced to be 'AF_INET'
listener = Listener(address, authkey=bytes(CONFIG.WEB_SECRET, 'utf-8'))
try:
conn = listener.accept()
while True:
if stop_control_interface:
break
df_logger.debug('[%s] connection accepted from: %s' % (thread_name, listener.last_accepted))
msg = conn.recv()
# reload configuration
if msg == 'c:reload:setting':
df_logger.debug('[%s] reload message received: %s' % (thread_name, msg))
CONFIG.reload()
RESTART = True
conn.send(200)
conn.close()
break
# query stats
if msg == 'q:remix:time':
df_logger.debug('[%s] reload message received: %s' % (thread_name, msg))
conn.send([200, TIME_REMIX])
if msg == 'q:resync:time':
df_logger.debug('[%s] reload message received: %s' % (thread_name, msg))
conn.send([200, TIME_RESYNC])
# close connection
elif msg == 'close':
conn.send(200)
conn.close()
break
except Exception as ex_ci:
df_logger.info('[%s] server error: %s' % (thread_name, str(ex_ci)))
finally:
listener.close()
except Exception as ex_li:
df_logger.info('[%s] server error, can not listen on interface: %s' % (thread_name, str(ex_li)))
def main():
global RESTART
global TIME_REMIX, TIME_RESYNC
RESTART = False
df_logger.info('init...')
# GPIO.setmode(GPIO.BCM)
# GPIO.setup(PIR_PIN, GPIO.IN)
# motion_detection_time = get_time()
slideshow = None
picture_path_hash = None
loop_time = 0
splashshow = start_splashshow()
turn_off_cursor()
while True:
if RESTART:
RESTART = False
slideshow = None
loop_time = 0
splashshow = start_splashshow()
# if GPIO.input(PIR_PIN):
# motion_detection_time = get_time()
if slideshow is None:
picture_path_hash, file_count, sync_time = sync_src()
TIME_RESYNC = sync_time + CONFIG.SYNC_INTERVAL
loop_time = file_count * CONFIG.PICTURE_TIMEOUT
TIME_REMIX = get_time() + loop_time
slideshow = start_slideshow(splashshow)
df_logger.info('started (slideshow hash: "' + picture_path_hash
+ '", remix playlist after ' + str(loop_time / 60) + ' minutes)...')
# sync photos
elif slideshow is not None and get_time() > TIME_RESYNC:
# sync with sync source
picture_path_hash_new, file_count, sync_time = sync_src()
TIME_RESYNC = sync_time + CONFIG.SYNC_INTERVAL
loop_time = file_count * CONFIG.PICTURE_TIMEOUT
TIME_REMIX = get_time() + loop_time
# restart if source files had changed
if picture_path_hash != picture_path_hash_new:
df_logger.info('source changed ("' + picture_path_hash + '" -> "' + picture_path_hash_new + '")...')
slideshow = restart_slideshow(slideshow)
picture_path_hash = picture_path_hash_new
df_logger.info('restarted (slideshow hash: ' + picture_path_hash + ')...')
# elif slideshow is not None and get_time() > (motion_detection_time + NO_MOTION_TIMEOUT):
# stop_slideshow(slideshow)
# slideshow = None
# restart and remix playlist after each loop
elif slideshow is not None and CONFIG.RANDOM and get_time() > TIME_REMIX:
df_logger.info('loop finished after ' + str(loop_time / 60) + ' minutes...')
TIME_REMIX = get_time() + loop_time
slideshow = restart_slideshow(slideshow)
time.sleep(2)
if __name__ == "__main__":
ipc = None
log_pipe_out = None
log_pipe_err = None
try:
# init logging
df_util.init_logger(CONFIG)
log_pipe_out = df_util.LogPipe(df_logger, logging.DEBUG)
log_pipe_err = df_util.LogPipe(df_logger, logging.WARNING)
# start ipc thread
ipc = threading.Thread(target=control_interface, args=('ipc',))
ipc.daemon = True
ipc.start()
# start slide show
main()
except KeyboardInterrupt:
df_logger.critical('Stopping user aborted with CTRL+C')
# GPIO.cleanup()
except Exception as ex:
df_logger.critical('Fatal error: %s', ex)
finally:
stop_control_interface = True
if ipc is not None:
ipc.join(1)
if log_pipe_out is not None:
log_pipe_out.close()
if log_pipe_err is not None:
log_pipe_err.close()
sys.exit()