-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
118 lines (103 loc) · 5.04 KB
/
scheduler.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
import datetime
import traceback
from PyQt5.QtCore import pyqtSlot
from debug.qthread_with_logging import QThreadWithLogging
from data.schedule import schedule as entire_schedule
from threading import Lock
from music_downloader import MusicDownloader
class Scheduler(QThreadWithLogging):
last = datetime.datetime.now()
def __init__(self, main_platform):
QThreadWithLogging.__init__(self)
self.main_platform = main_platform
self.schedule = []
self.name_for_static_alarm = []
self.name_for_static_alarm.append('입구폐쇄')
self.name_for_static_alarm.append('점호준비')
self.name_for_static_alarm.append('점호시작')
self.name_for_static_alarm.append('점호종료')
self.name_for_static_alarm.append('기숙사퇴실')
self.name_for_static_alarm.append('복귀체크')
self.tag_command_queue = []
self.prev = datetime.datetime(2020, 8, 1)
self.curr = datetime.datetime.now()
self.debug_all_around = False
self.music_downloader = MusicDownloader(self)
self.lock = Lock()
for grade in ['1', '2', '3']:
for meal in ['아침', '점심', '저녁']:
self.name_for_static_alarm.append(f'{grade}학년{meal}')
self.name_for_static_alarm.append('저녁외출')
self.name_for_static_alarm.append('점심외출')
@pyqtSlot()
def all_around_test(self):
self.log(f'lock to all_around_test')
with self.lock:
now = datetime.datetime.now()
self.prev = now.replace(hour=0, minute=0, second=0, microsecond=0)
self.curr = now.replace(hour=23, minute=59, second=59, microsecond=999999)
self.debug_all_around = True
self.log(f'unlock to all_around_test')
@pyqtSlot(str)
def push_tag_command(self, tag: str):
self.log(f'push {tag}')
self.tag_command_queue.append(tag)
def decode_tag(self, tag: str):
self.log(f'decode {tag}')
if tag == '기상송':
with self.main_platform.storage_manager.lock:
self.log(f'getlock 기상송')
self.main_platform.music_player.push_to_playlist(
self.main_platform.storage_manager.files_to_play)
elif tag == '기상송초기화':
with self.main_platform.storage_manager.lock:
self.main_platform.storage_manager.clear_internal_storage()
elif tag == '기상송다운로드':
with self.main_platform.storage_manager.lock:
self.main_platform.storage_manager.clear_internal_storage()
self.music_downloader.download()
elif tag == '아침운동체크':
self.main_platform.music_player.music_pause()
self.main_platform.music_player.play_unstoppable_music('audio/아침운동체크.mp3')
self.main_platform.music_player.music_resume()
elif tag in self.name_for_static_alarm:
self.main_platform.music_player.push_to_playlist([f'audio/{tag}.mp3'])
else:
self.log(f'unknown command {tag}')
self.log(f'finish {tag}')
def run(self):
if self.curr.date().weekday() == 5:
self.schedule = entire_schedule['토']
elif self.curr.date().weekday() == 6:
self.schedule = entire_schedule['일']
else:
self.schedule = entire_schedule['평일']
while not self.isFinished():
with self.lock:
if not self.debug_all_around:
self.curr = datetime.datetime.now()
if self.tag_command_queue:
self.decode_tag(self.tag_command_queue[0])
del self.tag_command_queue[0]
try:
if self.curr.date() != self.prev.date():
self.prev = self.curr
if self.curr.date().weekday() == 5:
self.schedule = entire_schedule['토']
elif self.curr.date().weekday() == 6:
self.schedule = entire_schedule['일']
else:
self.schedule = entire_schedule['평일']
is_trying_to_play = False
for certain_time in self.schedule:
if self.prev.time() < certain_time <= self.curr.time():
self.prev = datetime.datetime.combine(self.curr.date(), certain_time)
self.decode_tag(self.schedule[certain_time])
is_trying_to_play = True
break
if self.debug_all_around and not is_trying_to_play:
self.debug_all_around = False
self.prev = datetime.datetime.now()
self.curr = datetime.datetime.now()
except:
self.log(traceback.format_exc())