-
Notifications
You must be signed in to change notification settings - Fork 14
/
peppy.py
4040 lines (3287 loc) · 158 KB
/
peppy.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2016-2024 Peppy Player [email protected]
#
# This file is part of Peppy Player.
#
# Peppy Player 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.
#
# Peppy Player 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 Peppy Player. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import subprocess
import pygame
import importlib
import time
import logging
import socket
from datetime import datetime
from threading import RLock, Thread
from subprocess import Popen
from event.dispatcher import EventDispatcher
from player.proxy import Proxy, MPD_NAME, SHAIRPORT_SYNC_NAME, RASPOTIFY_NAME, BLUETOOTH_SINK_NAME
from player.player import Player
from screensaver.screensaverdispatcher import ScreensaverDispatcher
from ui.state import State
from ui.screen.radiogroup import RadioGroupScreen
from ui.screen.home import HomeScreen
from ui.screen.language import LanguageScreen
from ui.screen.saver import SaverScreen
from ui.screen.about import AboutScreen
from ui.screen.black import BlackScreen
from ui.player.radioplayer import RadioPlayerScreen
from ui.player.streamplayer import StreamPlayerScreen
from ui.browser.radio import RadioBrowserScreen
from ui.browser.stream import StreamBrowserScreen
from ui.browser.file import FileBrowserScreen
from ui.player.fileplayer import FilePlayerScreen
from ui.screen.equalizer import EqualizerScreen
from ui.screen.timer import TimerScreen
from ui.layout.borderlayout import BorderLayout
from ui.screen.screen import PERCENT_TOP_HEIGHT, PERCENT_TITLE_FONT
from util.config import *
from util.util import Util, LABELS, PLAYER_RUNNING, PLAYER_SLEEPING
from util.keys import *
from ui.player.bookplayer import BookPlayer
from ui.screen.booktrack import BookTrack
from ui.screen.bookgenre import BookGenre
from ui.screen.bookgenrebooks import BookGenreBooks
from ui.screen.booknew import BookNew
from websiteparser.loyalbooks.loyalbooksparser import LoyalBooksParser
from websiteparser.loyalbooks.constants import *
from websiteparser.siteparser import BOOK_URL, FILE_NAME
from util.volumecontrol import VolumeControl
from ui.screen.podcasts import PodcastsScreen
from ui.screen.podcastepisodes import PodcastEpisodesScreen
from ui.player.podcastplayer import PodcastPlayerScreen
from ui.player.airplayplayer import AirplayPlayerScreen
from ui.player.spotifyconnect import SpotifyConnectScreen
from ui.screen.network import NetworkScreen
from ui.screen.wifi import WiFiScreen
from ui.screen.bluetooth import BluetoothScreen
from ui.screen.keyboard import KeyboardScreen
from ui.screen.collection import CollectionScreen
from ui.screen.topic import TopicScreen
from ui.screen.topicdetail import TopicDetailScreen
from ui.screen.latinabc import LatinAbcScreen
from ui.screen.collectionplayer import CollectionPlayerScreen
from ui.screen.collectionbrowser import CollectionBrowserScreen
from ui.screen.info import InfoScreen
from ui.screen.switch import SwitchScreen
from ui.screen.imageviewer import ImageViewer
from ui.player.bluetoothsink import BluetoothSinkScreen
from ui.screen.yastream import YaStreamScreen
from ui.player.yaplaylistplayer import YaPlaylistPlayerScreen
from ui.player.yasearchplayer import YaSearchPlayerScreen
from ui.browser.yaplaylist import YaPlaylistScreen
from ui.browser.yasearch import YaSearchScreen
from ui.browser.archivefiles import ArchiveFilesBrowserScreen
from ui.browser.archiveitems import ArchiveItemsBrowserScreen
from subprocess import Popen, check_output
from ui.browser.jukebox import JukeboxBrowserScreen
from ui.player.archiveplayer import ArchivePlayerScreen
from util.fileutil import FILE_PLAYLIST
from ui.browser.browser import BrowserScreen
from ui.browser.search import RadioSearchScreen
from ui.player.radiobrowserplayer import RadioBrowserPlayerScreen
from ui.screen.searchby import SearchByScreen
from ui.browser.favorites import FavoritesScreen
from ui.screen.catalog import CatalogScreen
from ui.browser.catalogbase import CatalogBase
from ui.browser.catalogalbumtracks import CatalogAlbumTracks
from ui.player.catalogplayer import CatalogPlayerScreen
from ui.browser.cataloggenres import CatalogGenres
from ui.browser.cataloggenreartists import CatalogGenreArtists
from util.serviceutil import SERVICE_QOBUZ, SERVICE_DEEZER, SERVICE_SPOTIFY
class Peppy(object):
""" Main class """
lock = RLock()
def __init__(self):
""" Initializer """
self.util = Util()
self.config = self.util.config
self.util.connected_to_internet = self.check_internet_connectivity()
self.util.init_utilities()
self.use_web = self.config[USAGE][USE_WEB]
self.players = {}
self.volume_control = VolumeControl(self.util)
self.voice_assistant = None
if self.config[USAGE][USE_VOICE_ASSISTANT]:
language = self.util.get_voice_assistant_language_code(self.config[CURRENT][LANGUAGE])
if language:
try:
from voiceassistant.voiceassistant import VoiceAssistant
self.voice_assistant = VoiceAssistant(self.util)
self.voice_assistant.start()
except Exception as e:
logging.debug(e)
self.player_screens = {
KEY_PLAY_SITE: self.go_site_playback,
KEY_STATIONS: self.go_stations,
KEY_PLAY_FILE: self.go_file_playback,
STREAM: self.go_stream,
KEY_PODCAST_PLAYER: self.go_podcast_player,
KEY_AIRPLAY_PLAYER: self.go_airplay,
KEY_SPOTIFY_CONNECT_PLAYER: self.go_spotify_connect,
KEY_BLUETOOTH_SINK_PLAYER: self.go_bluetooth_sink,
KEY_PLAY_COLLECTION: self.go_collection_playback,
KEY_YA_PLAYLIST_PLAYER: self.go_ya_playlist_player,
KEY_YA_SEARCH_PLAYER: self.go_ya_search_player,
KEY_JUKEBOX_BROWSER: self.go_jukebox,
KEY_ARCHIVE_PLAYER: self.go_archive,
KEY_RADIO_BROWSER_PLAYER: self.go_radio_browser_player,
KEY_CATALOG_NEW_RELEASE_PLAYER: self.go_catalog_new_release_player,
KEY_CATALOG_BESTSELLER_PLAYER: self.go_catalog_bestseller_player,
KEY_CATALOG_GENRE_PLAYER: self.go_catalog_genre_player
}
try:
self.util.switch_util.switch_power()
except:
pass
if self.config[LINUX_PLATFORM]:
from util.diskmanager import DiskManager
from util.nasmanager import NasManager
self.disk_manager = DiskManager(self)
self.nas_manager = NasManager(self)
if self.config[DISK_MOUNT][MOUNT_AT_STARTUP]:
self.disk_manager.mount_all_usb_disks()
self.nas_manager.mount_all_nases()
if self.config[DISK_MOUNT][MOUNT_AT_PLUG]:
self.disk_manager.start_observer()
self.util.samba_util.start_sharing()
if self.config[DSI_DISPLAY_BACKLIGHT][USE_DSI_DISPLAY] and self.config[BACKLIGHTER]:
screen_brightness = int(self.config[DSI_DISPLAY_BACKLIGHT][SCREEN_BRIGHTNESS])
self.config[BACKLIGHTER].brightness = screen_brightness
if self.config[BACKLIGHTER].power == False:
self.config[BACKLIGHTER].power = True
if self.config[LINUX_PLATFORM] and self.config[USAGE][USE_BLUETOOTH]:
bluetooth_util = self.util.get_bluetooth_util()
if self.config[CURRENT][MODE]:
if self.config[CURRENT][MODE] == BLUETOOTH_SINK:
bluetooth_util.connect_bluetooth_sink()
else:
bluetooth_util.connect_device(remove_previous=False)
s = self.config[SCRIPTS][SCRIPT_PLAYER_START]
if s != None and len(s.strip()) != 0:
self.util.run_script(s)
layout = BorderLayout(self.util.screen_rect)
layout.set_percent_constraints(PERCENT_TOP_HEIGHT, PERCENT_TOP_HEIGHT, 0, 0)
self.config[MAXIMUM_FONT_SIZE] = int((layout.TOP.h * PERCENT_TITLE_FONT)/100.0)
try:
values = self.config[CURRENT][EQUALIZER]
if values:
self.util.set_equalizer(values)
except:
pass
if self.use_web:
try:
f = open(os.devnull, 'w')
sys.stdout = sys.stderr = f
from web.server.webserver import WebServer
self.web_server = WebServer(self.util, self)
except Exception as e:
logging.debug(e)
self.use_web = False
self.screensaver_dispatcher = ScreensaverDispatcher(self.util, self.web_server)
about = AboutScreen(self.util)
about.add_listener(self.go_home)
self.screens = {KEY_ABOUT : about}
self.current_player_screen = None
self.initial_player_name = self.config[AUDIO][PLAYER_NAME]
self.current_audio_file = None
if self.config[AUDIO][PLAYER_NAME] == MPD_NAME:
self.start_audio()
if self.config[USAGE][USE_VU_METER]:
self.util.load_screensaver(VUMETER)
else:
if self.config[USAGE][USE_VU_METER]:
self.util.load_screensaver(VUMETER)
self.start_audio()
if self.use_web:
self.screensaver_dispatcher.add_start_listener(self.web_server.start_screensaver_to_json)
self.screensaver_dispatcher.add_stop_listener(self.web_server.stop_screensaver_to_json)
self.event_dispatcher = EventDispatcher(self.screensaver_dispatcher, self.util, self.volume_control)
self.current_screen = None
self.current_mode = self.config[CURRENT][MODE]
disabled_modes = self.util.get_disabled_modes()
if self.current_mode in disabled_modes:
self.go_home(None)
return
if not self.config[CURRENT][MODE] or not self.config[USAGE][USE_AUTO_PLAY]:
if self.config[CURRENT][MODE] == JUKEBOX:
self.go_jukebox(None)
else:
self.go_home(None)
elif self.config[CURRENT][MODE] == RADIO and self.config[HOME_MENU][RADIO]:
state = State()
state.source = INIT
self.go_stations(state)
elif self.config[CURRENT][MODE] == AUDIO_FILES and self.config[HOME_MENU][AUDIO_FILES]:
state = State()
state.folder = self.config[FILE_PLAYBACK][CURRENT_FOLDER].replace('\\\\', '\\')
state.file_name = self.config[FILE_PLAYBACK][CURRENT_FILE]
state.url = state.folder + os.sep + state.file_name
state.playback_mode = self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYBACK_MODE]
if self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYBACK_MODE] == FILE_PLAYLIST:
state.url = state.folder + os.sep + self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYLIST]
state.playlist_track_number = self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYLIST_INDEX]
self.wait_for_file(state.url)
self.go_file_playback(state)
elif self.config[CURRENT][MODE] == STREAM and self.config[HOME_MENU][STREAM]:
self.go_stream()
elif self.config[CURRENT][MODE] == AUDIOBOOKS and self.config[HOME_MENU][AUDIOBOOKS]:
state = State()
state.name = self.config[AUDIOBOOKS][BROWSER_BOOK_TITLE]
state.book_url = self.config[AUDIOBOOKS][BROWSER_BOOK_URL]
state.img_url = self.config[AUDIOBOOKS][BROWSER_IMAGE_URL]
state.file_name = self.config[AUDIOBOOKS][BROWSER_TRACK_FILENAME]
state.source = INIT
self.go_site_playback(state)
elif self.config[CURRENT][MODE] == PODCASTS and self.config[HOME_MENU][PODCASTS]:
state = State()
state.podcast_url = self.config[PODCASTS][PODCAST_URL]
state.name = self.config[PODCASTS][PODCAST_EPISODE_NAME]
state.url = self.config[PODCASTS][PODCAST_EPISODE_URL]
state.episode_time = self.config[PODCASTS][PODCAST_EPISODE_TIME]
state.source = INIT
self.go_podcast_player(state)
elif self.config[CURRENT][MODE] == AIRPLAY and self.config[HOME_MENU][AIRPLAY]:
self.reconfigure_player(SHAIRPORT_SYNC_NAME)
self.go_airplay()
elif self.config[CURRENT][MODE] == SPOTIFY_CONNECT and self.config[HOME_MENU][SPOTIFY_CONNECT]:
self.reconfigure_player(RASPOTIFY_NAME)
self.go_spotify_connect()
elif self.config[CURRENT][MODE] == COLLECTION and self.config[HOME_MENU][COLLECTION]:
state = State()
state.topic = self.config[COLLECTION_PLAYBACK][COLLECTION_TOPIC]
state.folder = self.config[COLLECTION_PLAYBACK][COLLECTION_FOLDER]
state.file_name = self.config[COLLECTION_PLAYBACK][COLLECTION_FILE]
state.url = self.config[COLLECTION_PLAYBACK][COLLECTION_URL]
state.track_time = self.config[COLLECTION_PLAYBACK][COLLECTION_TRACK_TIME]
state.source = INIT
self.wait_for_file(state.url)
self.go_collection_playback(state)
elif self.config[CURRENT][MODE] == BLUETOOTH_SINK and self.config[HOME_MENU][BLUETOOTH_SINK]:
self.reconfigure_player(BLUETOOTH_SINK_NAME)
self.go_bluetooth_sink()
elif self.config[CURRENT][MODE] == YA_STREAM and self.config[HOME_MENU][YA_STREAM]:
state = State()
state.id = self.config[YA_STREAM][YA_STREAM_ID]
state.l_name = self.config[YA_STREAM][YA_STREAM_NAME]
state.url = self.config[YA_STREAM][YA_STREAM_URL]
state.image_path = self.config[YA_STREAM][YA_THUMBNAIL_PATH]
state.time = self.config[YA_STREAM][YA_PLAYLIST_TIME]
state.source = INIT
self.go_ya_playlist_player(state)
elif self.config[CURRENT][MODE] == JUKEBOX and self.config[HOME_MENU][JUKEBOX]:
state = State()
state.source = INIT
self.go_jukebox(state)
elif self.config[CURRENT][MODE] == ARCHIVE and self.config[HOME_MENU][ARCHIVE]:
state = State()
state.item = self.config[ARCHIVE][ITEM]
state.file = self.config[ARCHIVE][FILE]
state.file_time = self.config[ARCHIVE][FILE_TIME]
state.source = INIT
self.go_archive(state)
elif self.config[CURRENT][MODE] == RADIO_BROWSER and self.config[HOME_MENU][RADIO_BROWSER]:
state = State()
state.id = self.config[RADIO_BROWSER][FAVORITE_STATION_ID]
state.name = state.l_name = state.comparator_item = self.config[RADIO_BROWSER][FAVORITE_STATION_NAME]
state.url = self.config[RADIO_BROWSER][FAVORITE_STATION_URL]
state.image_path = self.config[RADIO_BROWSER][FAVORITE_STATION_LOGO]
state.index = 0
state.source = INIT
self.go_radio_browser_player(state)
else:
self.go_home(None)
self.player_state = PLAYER_RUNNING
self.run_timer_thread = False
self.start_timer_thread()
def wait_for_file(self, url):
""" Wait for the file which can be unavailable at the moment after switching the hard drive on
Makes 6 attempts 5 seconds each
:param url: the file URL
"""
attempts = 6
timeout = 5
for _ in range(attempts):
file_available = os.path.exists(url)
if file_available:
logging.debug("File {} is available".format(url))
break
else:
logging.debug("File {} is unavailable".format(url))
time.sleep(timeout)
continue
def check_internet_connectivity(self):
""" Exit if Internet is not available after 3 attempts 3 seconds each """
attempts = 3
timeout = 3
for n in range(attempts):
internet_available = self.is_internet_available(timeout)
if internet_available:
break
else:
if n == (attempts - 1):
logging.error("Internet is not available")
return False
else:
time.sleep(timeout)
continue
return True
def is_internet_available(self, timeout):
""" Check that Internet is available. The solution was taken from here:
https://stackoverflow.com/questions/3764291/checking-network-connection
:param timeout: request timeout
:return: True - Internet available, False - not available
"""
dns_server = self.config[USAGE][USE_DNS_IP].strip()
dns_server_port = 53
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((dns_server, dns_server_port))
return True
except Exception as e:
logging.error("Connection error: {0}".format(e))
return False
def start_audio(self):
""" Starts audio server and client """
if self.config[AUDIO][PLAYER_NAME] in self.players.keys():
self.player = self.players[self.config[AUDIO][PLAYER_NAME]]
if hasattr(self.player, "proxy"):
self.player.proxy.start()
self.player.start_client()
self.volume_control.set_player(self.player)
return
folder = None
try:
folder = self.config[AUDIO][SERVER_FOLDER]
except:
pass
start_cmd = None
try:
start_cmd = self.config[AUDIO][SERVER_START_COMMAND]
except:
pass
stream_server_parameters = None
if self.config[USAGE][USE_STREAM_SERVER]:
try:
stream_server_parameters = self.config[AUDIO][STREAM_SERVER_PARAMETERS]
except:
pass
stream_client_parameters = None
try:
stream_client_parameters = self.config[AUDIO][STREAM_CLIENT_PARAMETERS]
except:
pass
if stream_client_parameters:
if start_cmd:
start_cmd = start_cmd + " " + stream_client_parameters
else:
start_cmd = stream_client_parameters
client_name = self.config[AUDIO][CLIENT_NAME]
linux = self.config[LINUX_PLATFORM]
stop_cmd = self.config[AUDIO][SERVER_STOP_COMMAND]
self.proxy = Proxy(client_name, linux, folder, start_cmd, stop_cmd, self.config[PLAYER_SETTINGS][VOLUME], stream_server_parameters)
self.proxy_process = self.proxy.start()
logging.debug("Audio Server Started")
p = "player." + client_name
try:
m = importlib.import_module(p)
except Exception as e:
logging.debug(e)
n = client_name.title()
player = getattr(m, n)()
player.set_platform(linux)
player.set_player_mode(self.config[CURRENT][MODE])
player.set_proxy(self.proxy_process, self.proxy)
player.set_util(self.util)
if self.config[VOLUME_CONTROL][VOLUME_CONTROL_TYPE] == VOLUME_CONTROL_TYPE_PLAYER:
player.set_player_volume_control(True)
else:
player.set_player_volume_control(False)
player.start_client()
if self.config[PLAYER_SETTINGS][PAUSE]:
player.pause()
self.players[self.config[AUDIO][PLAYER_NAME]] = player
self.player = player
self.volume_control.set_player(player)
def start_timer_thread(self):
""" Start timer thread """
if not self.config[HOME_NAVIGATOR][TIMER] or self.run_timer_thread:
return
sleep_selected = self.config[TIMER][SLEEP] and len(self.config[TIMER][SLEEP_TIME]) > 0
poweroff_selected = self.config[TIMER][POWEROFF] and len(self.config[TIMER][SLEEP_TIME]) > 0
if not sleep_selected and not poweroff_selected:
return
self.run_timer_thread = True
self.timer_thread = Thread(target=self.timer_thread)
self.timer_thread.start()
def timer_thread(self):
""" Timer thread function """
while self.run_timer_thread:
time.sleep(2)
with self.lock:
sleep_selected = self.config[TIMER][SLEEP]
poweroff_selected = self.config[TIMER][POWEROFF]
wake_up_selected = self.config[TIMER][WAKE_UP]
sleep_time = self.config[TIMER][SLEEP_TIME] + "00"
wake_up_time = self.config[TIMER][WAKE_UP_TIME] + "00"
current_time = datetime.now().strftime("%H%M%S")
if sleep_selected:
if wake_up_selected and self.player_state == PLAYER_SLEEPING and self.is_time_in_range(current_time, wake_up_time):
self.wake_up()
with self.lock:
self.player_state = PLAYER_RUNNING
if not self.is_time_in_range(current_time, sleep_time):
continue
if self.player_state == PLAYER_RUNNING:
self.sleep()
elif poweroff_selected:
if not self.is_time_in_range(current_time, sleep_time):
continue
logging.debug("poweroff")
self.run_timer_thread = False
self.shutdown()
def is_time_in_range(self, t1, t2):
""" Check if provided time (t1) is in range of 4 seconds (t1)
:param t1: current time
:param t2: time to compare to
:return: True - time in range, False - time out of range
"""
current_h_m = t1[0:4]
current_sec = t1[4:]
if current_h_m != t2[0:4]:
return False
if int(current_sec) < 3:
return True
else:
return False
def sleep(self, state=None):
""" Go to sleep mode
:param state: button state object
"""
if self.player_state == PLAYER_SLEEPING:
return
logging.debug("sleep")
with self.lock:
self.player_state = PLAYER_SLEEPING
self.player.stop()
if self.screensaver_dispatcher.saver_running:
self.screensaver_dispatcher.cancel_screensaver()
self.screensaver_dispatcher.current_delay = 0
if self.config[USAGE][USE_CLOCK_SCREENSAVER_IN_TIMER]:
self.screensaver_dispatcher.start_screensaver(CLOCK)
else:
self.go_black()
if self.config[DSI_DISPLAY_BACKLIGHT][USE_DSI_DISPLAY] and self.config[DSI_DISPLAY_BACKLIGHT][SLEEP_NOW_DISPLAY_POWER_OFF]:
self.config[BACKLIGHTER].power = False
self.util.run_script(self.config[SCRIPTS][SCRIPT_TIMER_START])
def wake_up(self, state=None):
""" Wake up from sleep mode
:param state: button state object
"""
if self.player_state == PLAYER_RUNNING:
return
logging.debug("wake up")
self.player_state = PLAYER_RUNNING
self.player.resume_playback()
self.set_current_screen(self.previous_screen_name)
self.screensaver_dispatcher.delay_counter = 0
self.screensaver_dispatcher.current_delay = self.screensaver_dispatcher.get_delay()
if self.use_web:
self.web_server.redraw_web_ui()
if self.config[DSI_DISPLAY_BACKLIGHT][USE_DSI_DISPLAY] and self.config[DSI_DISPLAY_BACKLIGHT][SLEEP_NOW_DISPLAY_POWER_OFF]:
self.config[BACKLIGHTER].power = True
self.util.run_script(self.config[SCRIPTS][SCRIPT_TIMER_STOP])
def exit_current_screen(self):
""" Complete action required to exit screen """
with self.lock:
cs = self.current_screen
if cs and self.screens and self.screens[cs]:
self.screens[cs].exit_screen()
def set_mode(self, state):
""" Set current mode (e.g. Radio, Language etc)
:param state: button state
"""
self.store_current_track_time(self.current_screen)
state.source = "home"
mode = state.genre
if self.current_mode != mode:
self.player.stop()
if self.current_mode == AIRPLAY or self.current_mode == SPOTIFY_CONNECT or self.current_mode == BLUETOOTH_SINK:
self.reconfigure_player(self.initial_player_name)
if self.current_mode == BLUETOOTH_SINK:
bluetooth_util = self.util.get_bluetooth_util()
bluetooth_util.disconnect_bluetooth_sink()
self.current_mode = mode
self.player.set_player_mode(mode)
state.change_mode = True
else:
state.change_mode = False
if mode == RADIO: self.go_stations(state)
elif mode == RADIO_BROWSER: self.go_radio_browser_player(state)
elif mode == AUDIO_FILES: self.go_file_playback(state)
elif mode == STREAM: self.go_stream(state)
elif mode == AUDIOBOOKS: self.go_audiobooks(state)
elif mode == PODCASTS: self.go_podcast_player(state)
elif mode == AIRPLAY:
self.reconfigure_player(SHAIRPORT_SYNC_NAME)
self.go_airplay(state)
elif mode == SPOTIFY_CONNECT:
self.reconfigure_player(RASPOTIFY_NAME)
self.go_spotify_connect(state)
elif mode == BLUETOOTH_SINK:
self.reconfigure_player(BLUETOOTH_SINK_NAME)
bluetooth_util = self.util.get_bluetooth_util()
bluetooth_util.connect_bluetooth_sink()
self.go_bluetooth_sink(state)
elif mode == COLLECTION: self.go_collection(state)
elif mode == YA_STREAM: self.go_ya_stream(state)
elif mode == JUKEBOX: self.go_jukebox(state)
elif mode == ARCHIVE: self.go_archive(state)
elif mode == CATALOG: self.go_catalog(state)
self.config[CURRENT][MODE] = mode
def go_player(self, state):
""" Go to the current player screen
:param state: button state
"""
try:
state.source = GO_PLAYER
self.player_screens[self.current_player_screen](state)
except:
pass
def go_favorites(self, state):
""" Go to the favorites screen
:param state: button state
"""
state.source = KEY_FAVORITES
self.go_stations(state)
def go_radio_browser_favorites(self, state):
""" Go to the radio browser favorites screen
:param state: button state
"""
state.source = KEY_FAVORITES
favorite_id = self.config[RADIO_BROWSER].get(FAVORITE_STATION_ID, None)
if not favorite_id:
self.go_favorites_screen(state)
else:
self.go_radio_browser_player(state)
def get_current_screen(self, key, state=None):
""" Return current screen by name
:param key: screen name
"""
s = None
self.exit_current_screen()
try:
if self.screens and self.screens[key]:
s = self.screens[key]
self.set_current_screen(key, state=state)
except KeyError:
pass
return s
def add_screen_observers(self, screen):
""" Add web obervers to the provided screen
:param screen: screen for observers
"""
screen.add_screen_observers(self.web_server.update_web_ui, self.web_server.redraw_web_ui)
def go_home(self, state):
""" Go to the Home Screen
:param state: button state
"""
if self.get_current_screen(KEY_HOME): return
listeners = self.get_home_screen_listeners()
home_screen = HomeScreen(self.util, listeners)
self.screens[KEY_HOME] = home_screen
self.set_current_screen(KEY_HOME)
if self.use_web:
self.add_screen_observers(home_screen)
def go_language(self, state):
""" Go to the Language Screen
:param state: button state
"""
if self.get_current_screen(LANGUAGE): return
listeners = {KEY_HOME: self.go_home, KEY_PLAYER: self.go_player}
language_screen = LanguageScreen(self.util, self.change_language, listeners)
self.screens[LANGUAGE] = language_screen
self.set_current_screen(LANGUAGE)
if self.use_web:
self.add_screen_observers(language_screen)
def change_language(self, state):
""" Change current language and go to the Home Screen
:param state: button state
"""
if state.name != self.config[CURRENT][LANGUAGE]:
self.config[LABELS].clear()
try:
stations = self.screens[KEY_STATIONS]
if stations:
self.player.remove_player_listener(stations.screen_title.set_text)
except KeyError:
pass
self.config[CURRENT][LANGUAGE] = state.name
self.config[LABELS] = self.util.get_labels()
try:
self.screensaver_dispatcher.current_screensaver.set_util(self.util)
except:
pass
self.config[AUDIOBOOKS][BROWSER_BOOK_URL] = ""
self.config[AUDIOBOOKS][BROWSER_IMAGE_URL] = ""
self.screens = {k : v for k, v in self.screens.items() if k == KEY_ABOUT}
self.current_screen = None
if self.config[USAGE][USE_VOICE_ASSISTANT]:
language = self.util.get_voice_assistant_language_code(state.name)
if language and self.voice_assistant != None:
self.voice_assistant.change_language()
self.go_home(state)
self.player.stop()
def go_file_browser(self, state=None):
""" Go to the File Browser Screen
:param state: button state
"""
if self.get_current_screen(AUDIO_FILES): return
file_player = self.screens[KEY_PLAY_FILE]
listeners = {}
listeners[KEY_HOME] = self.go_home
listeners[KEY_PLAY_FILE] = self.go_file_playback
listeners[GO_BACK] = file_player.restore_current_folder
listeners[KEY_SWITCH] = self.go_switch
listeners[KEY_IMAGE_VIEWER] = self.go_image_viewer
file_browser_screen = FileBrowserScreen(self.util, self.player.get_current_playlist, self.player.load_playlist, listeners)
file_player.add_play_listener(file_browser_screen.file_menu.select_item)
file_player.recursive_notifier = file_browser_screen.file_menu.change_folder
file_browser_screen.file_menu.add_playlist_size_listener(file_player.set_playlist_size)
file_browser_screen.file_menu.add_play_file_listener(file_player.play_button.draw_default_state)
self.player.add_player_listener(file_browser_screen.file_menu.update_playlist_menu)
self.screens[AUDIO_FILES] = file_browser_screen
self.set_current_screen(AUDIO_FILES)
if self.use_web:
self.add_screen_observers(file_browser_screen)
def go_image_viewer(self, state=None):
""" Go to the Image Viewer Screen
:param state: button state
"""
if self.get_current_screen(KEY_IMAGE_VIEWER, state):
return
listeners = {}
listeners[KEY_HOME] = self.go_home
listeners[KEY_BACK] = self.go_file_browser
image_viewer_screen = ImageViewer(self.util, listeners)
self.screens[KEY_IMAGE_VIEWER] = image_viewer_screen
self.set_current_screen(KEY_IMAGE_VIEWER, state=state)
if self.use_web:
self.add_screen_observers(image_viewer_screen)
def go_switch(self, state):
""" Go to the Switch Screen
:param state: button state
"""
if self.get_current_screen(KEY_SWITCH): return
listeners = {KEY_HOME: self.go_home, KEY_BACK: self.go_back, KEY_PLAYER: self.go_player}
switch_screen = SwitchScreen(self.util, listeners)
self.screens[KEY_SWITCH] = switch_screen
if hasattr(self, "disk_manager"):
switch_screen.menu.add_listener(self.disk_manager.poweroff_by_disk_name)
self.set_current_screen(KEY_SWITCH)
if self.use_web:
self.add_screen_observers(switch_screen)
def go_collection_browser(self, state=None):
""" Go to the File Browser Screen
:param state: button state
"""
self.exit_current_screen()
name = COLLECTION_TRACK
if self.get_current_screen(name, state=state): return
listeners = {}
listeners[KEY_HOME] = self.go_home
listeners[COLLECTION] = self.go_collection
listeners[COLLECTION_TOPIC] = self.go_topic
listeners[TOPIC_DETAIL] = self.go_topic_detail
listeners[KEY_PLAY_COLLECTION] = self.go_collection_playback
listeners[KEY_BACK] = self.go_back
listeners[KEY_PLAYER] = self.go_player
s = CollectionBrowserScreen(self.util, listeners)
self.screens[name] = s
self.set_current_screen(name, state=state)
file_player = self.screens[KEY_PLAY_COLLECTION]
file_player.add_play_listener(s.set_current)
if self.use_web:
self.add_screen_observers(s)
def go_file_playback(self, state=None):
""" Go to the File Player Screen
:param state: button state
"""
self.deactivate_current_player(KEY_PLAY_FILE)
try:
if self.screens[KEY_PLAY_FILE]:
if hasattr(state, "name") and (state.name == KEY_HOME or state.name == KEY_BACK):
self.set_current_screen(KEY_PLAY_FILE, True)
else:
self.set_current_screen(name=KEY_PLAY_FILE, state=state)
self.current_player_screen = KEY_PLAY_FILE
return
except Exception as e:
pass
if self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYBACK_MODE] == FILE_PLAYLIST:
self.config[FILE_PLAYBACK][CURRENT_FILE] = self.config[FILE_PLAYBACK][CURRENT_FILE]
self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYLIST_INDEX] = int(self.config[FILE_PLAYBACK][CURRENT_FILE_PLAYLIST_INDEX])
else:
if getattr(state, "url", None):
tokens = state.url.split(os.sep)
self.config[FILE_PLAYBACK][CURRENT_FILE] = tokens[len(tokens) - 1]
listeners = self.get_play_screen_listeners()
listeners[AUDIO_FILES] = self.go_file_browser
listeners[KEY_SEEK] = self.player.seek
screen = FilePlayerScreen(
listeners,
self.util,
self.player.get_current_playlist,
self.volume_control,
show_order=self.config[PLAYER_SCREEN][ENABLE_ORDER_BUTTON],
show_info=self.config[PLAYER_SCREEN][ENABLE_INFO_BUTTON]
)
self.screens[KEY_PLAY_FILE] = screen
self.current_player_screen = KEY_PLAY_FILE
screen.load_playlist = self.player.load_playlist
self.player.add_player_listener(screen.screen_title.set_text)
self.player.add_player_listener(screen.time_control.set_track_info)
self.player.add_player_listener(screen.update_arrow_button_labels)
self.player.add_end_of_track_listener(screen.end_of_track)
screen.add_play_listener(self.screensaver_dispatcher.change_image)
screen.add_play_listener(self.screensaver_dispatcher.change_image_folder)
f = getattr(state, "file_name", None)
if f == None and state != None:
state.file_name = self.config[FILE_PLAYBACK][CURRENT_FILE]
self.set_current_screen(KEY_PLAY_FILE, state=state)
state = State()
state.cover_art_folder = screen.center_button.state.cover_art_folder
self.screensaver_dispatcher.change_image_folder(state)
if self.use_web:
update = self.web_server.update_web_ui
redraw = self.web_server.redraw_web_ui
start = self.web_server.start_time_control_to_json
stop = self.web_server.stop_time_control_to_json
title_to_json = self.web_server.title_to_json
screen.add_screen_observers(update, redraw, start, stop, title_to_json)
self.web_server.add_player_listener(screen.time_control)
self.player.add_player_listener(self.web_server.update_player_listeners)
def go_file_info(self, state):
""" Go to the File Info Screen
:param state: button state
"""
try:
self.screens[KEY_FILE_INFO]
self.set_current_screen(name=KEY_FILE_INFO, state=state)
return
except:
pass
labels = [
self.config[LABELS][GENRE],
self.config[LABELS][ARTIST],
self.config[LABELS][ALBUM],
self.config[LABELS][DATE],
self.config[LABELS][FILE_SIZE],
self.config[LABELS][SAMPLE_RATE],
self.config[LABELS][CHANNELS],
self.config[LABELS][BITS_PER_SAMPLE],
self.config[LABELS][BIT_RATE]
]
meta_keys = [
Player.FILENAME, GENRE, ARTIST, ALBUM, DATE,
Player.FILESIZE, Player.SAMPLE_RATE, Player.CHANNELS, Player.BITS_PER_SAMPLE, Player.BITRATE
]
units = {
Player.FILESIZE: self.config[LABELS][BYTES],
Player.SAMPLE_RATE: self.config[LABELS][HZ],
Player.BITRATE: self.config[LABELS][KBPS]
}
screen = InfoScreen(KEY_FILE_INFO, self.util, self.go_back, labels, meta_keys, units, 14, 4, self.util.get_file_metadata)
self.screens[KEY_FILE_INFO] = screen
self.set_current_screen(name=KEY_FILE_INFO, state=state)
if self.use_web:
screen.add_screen_observer(self.web_server.redraw_web_ui)
def go_radio_info(self, state):
""" Go to the Radio Info Screen
:param state: button state
"""
try:
self.screens[KEY_RADIO_INFO]
self.set_current_screen(name=KEY_RADIO_INFO, state=state)
return
except:
pass
labels = [
self.config[LABELS][GENRE],
self.config[LABELS][ARTIST],
self.config[LABELS][SONG],
self.config[LABELS][SAMPLE_RATE],
self.config[LABELS][CHANNELS],
self.config[LABELS][BIT_RATE],
self.config[LABELS][CODEC]
]
meta_keys = [
Player.STATION,
GENRE,
ARTIST,
SONG,
Player.SAMPLE_RATE,
Player.CHANNELS,
Player.BITRATE,
Player.CODEC
]
units = {
Player.BITRATE: self.config[LABELS][KBPS],
Player.SAMPLE_RATE: self.config[LABELS][HZ]
}
screen = InfoScreen(KEY_RADIO_INFO, self.util, self.go_back, labels, meta_keys, units, 12, 3, self.screens[KEY_STATIONS].get_station_metadata)
self.screens[KEY_RADIO_INFO] = screen