3 from datetime import datetime
10 from threading import Thread
14 from typing import Optional, List
15 from queue import Queue, Empty
17 import astral # type: ignore
18 from astral.sun import sun # type: ignore
23 import renderer_catalog
28 import trigger_catalog
32 def thread_janitor() -> None:
34 tracemalloc_target = 0.0
40 if now > tracemalloc_target:
41 tracemalloc_target = now + 30.0
42 snapshot = tracemalloc.take_snapshot()
43 snapshot = snapshot.filter_traces((
44 tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
45 tracemalloc.Filter(False, "<unknown>"),
49 top_stats = snapshot.statistics(key_type)
50 print("janitor: Top %s lines" % limit)
51 for index, stat in enumerate(top_stats[:limit], 1):
52 frame = stat.traceback[0]
53 # replace "/path/to/module/file.py" with "module/file.py"
54 filename = os.sep.join(frame.filename.split(os.sep)[-2:])
55 print("janitor: #%s: %s:%s: %.1f KiB"
56 % (index, filename, frame.lineno, stat.size / 1024))
57 line = linecache.getline(frame.filename, frame.lineno).strip()
59 print('janitor: %s' % line)
61 other = top_stats[limit:]
63 size = sum(stat.size for stat in other)
64 print("janitor: %s other: %.1f KiB" % (len(other), size / 1024))
65 total = sum(stat.size for stat in top_stats)
66 print("janitor: Total allocated size: %.1f KiB" % (total / 1024))
68 print("janitor: Running gc operation")
69 gc_target = now + 60.0
74 def guess_page(command: str, page_chooser: chooser.chooser) -> str:
77 for page in page_chooser.get_page_list():
78 page = page.replace('(', ' ')
79 page = page.replace('_', ' ')
80 page = page.replace(')', ' ')
81 page = page.replace('.html', '')
82 page = page.replace('CNNNews', 'news')
83 page = page.replace('CNNTechnology', 'technology')
84 page = page.replace('gocostco', 'costco list')
85 page = page.replace('gohardware', 'hardware list')
86 page = page.replace('gohouse', 'house list honey do')
87 page = page.replace('gcal', 'google calendar events')
88 page = page.replace('mynorthwest', 'northwest news')
89 page = page.replace('myq', 'myq garage door status')
90 page = page.replace('gomenu', 'dinner menu')
91 page = page.replace('wsdot', 'traffic')
92 page = page.replace('gomenu', 'dinner menu')
93 page = page.replace('WSJNews', 'news')
94 page = page.replace('telma', 'telma cabin')
95 page = page.replace('WSJBusiness', 'business news')
96 page = re.sub(r'[0-9]+', '', page)
97 score = SequenceMatcher(None, command, page).ratio()
98 if best_score is None or score > best_score:
100 assert best_page is not None
104 def process_command(command: str, page_history: List[str]) -> str:
106 if 'hold' in command:
107 page = page_history[0]
108 elif 'back' in command:
109 page = page_history[1]
110 elif 'skip' in command:
112 (page, _) = page_chooser.choose_next_page()
113 if page != page_history[0]:
115 elif 'weather' in command:
116 if 'telma' in command or 'cabin' in command:
117 page = 'weather-telma_3_10800.html'
118 elif 'stevens' in command:
119 page = 'weather-stevens_3_10800.html'
121 page = 'weather-home_3_10800.html'
122 elif 'cabin' in command:
123 if 'list' in command:
124 page = 'Cabin-(gocabin)_2_3600.html'
126 page = 'hidden/cabin_driveway.html'
127 elif 'news' in command or 'headlines' in command:
128 page = 'cnn-CNNNews_4_25900.html'
129 elif 'clock' in command or 'time' in command:
130 page = 'clock_10_none.html'
131 elif 'countdown' in command or 'countdowns' in command:
132 page = 'countdown_3_7200.html'
133 elif 'costco' in command:
134 page = 'Costco-(gocostco)_2_3600.html'
135 elif 'calendar' in command or 'events' in command:
136 page = 'gcal_3_86400.html'
137 elif 'countdown' in command or 'countdowns' in command:
138 page = 'countdown_3_7200.html'
139 elif 'grocery' in command or 'groceries' in command:
140 page = 'Grocery-(gogrocery)_2_3600.html'
141 elif 'hardware' in command:
142 page = 'Hardware-(gohardware)_2_3600.html'
143 elif 'garage' in command:
144 page = 'myq_4_300.html'
145 elif 'menu' in command:
146 page = 'Menu-(gomenu)_2_3600.html'
147 elif 'cron' in command or 'health' in command:
148 page = 'periodic-health_6_300.html'
149 elif 'photo' in command or 'picture' in command:
150 page = 'photo_23_3600.html'
151 elif 'quote' in command or 'quotation' in command or 'quotes' in command:
152 page = 'quotes_4_10800.html'
153 elif 'stevens' in command:
154 page = 'stevens-conditions_1_86400.html'
155 elif 'stock' in command or 'stocks' in command:
156 page = 'stock_3_86400.html'
157 elif 'twitter' in command:
158 page = 'twitter_10_3600.html'
159 elif 'traffic' in command:
160 page = 'wsdot-bridges_3_none.html'
161 elif 'front' in command and 'door' in command:
162 page = 'hidden/frontdoor.html'
163 elif 'driveway' in command:
164 page = 'hidden/driveway.html'
165 elif 'backyard' in command:
166 page = 'hidden/backyard.html'
168 page = guess_page(command, page_chooser)
169 assert page is not None
173 def thread_change_current(command_queue: Queue) -> None:
174 page_history = [ "", "" ]
175 swap_page_target = 0.0
177 def filter_news_during_dinnertime(page: str) -> bool:
179 is_dinnertime = now.hour >= 17 and now.hour <= 20
180 return not is_dinnertime or not (
183 or "mynorthwest" in page
185 or "stranger" in page
189 page_chooser = chooser.weighted_random_chooser_with_triggers(
190 trigger_catalog.get_triggers(), [filter_news_during_dinnertime]
196 # Check for a verbal command.
199 command = command_queue.get(block=False)
203 if command is not None:
205 page = process_command(command, page_history)
207 # Else pick a page randomly.
210 (page, triggered) = page_chooser.choose_next_page()
211 if triggered or page != page_history[0]:
215 print("chooser[%s] - WE ARE TRIGGERED." % utils.timestamp())
216 if page != page_history[0] or (swap_page_target - now < 10.0):
218 "chooser[%s] - EMERGENCY PAGE %s LOAD NEEDED"
219 % (utils.timestamp(), page)
222 with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f:
223 emit_wrapped(f, page, override_refresh_sec = 40, command = command)
224 page_history.insert(0, page)
225 page_history = page_history[0:10]
226 swap_page_target = now + 40
228 print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
231 # Also notify XMLHTTP clients that they need to refresh now.
232 path = os.path.join(constants.pages_dir, "reload_immediately.html")
233 with open(path, "w") as f:
234 f.write("Reload, suckers!")
236 # Fix this hack... maybe read the webserver logs and see if it
237 # actually was picked up?
241 elif now >= swap_page_target:
242 assert page != page_history[0]
243 print("chooser[%s] - nominal choice of %s" % (utils.timestamp(), page))
245 with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f:
246 emit_wrapped(f, page)
247 page_history.insert(0, page)
248 page_history = page_history[0:10]
249 swap_page_target = now + constants.refresh_period_sec
251 print("chooser[%s] - page does not exist?!" % (utils.timestamp()))
259 override_refresh_sec: int = None,
260 command: str = None) -> None:
261 def pick_background_color() -> str:
262 city = astral.LocationInfo(
263 "Bellevue", "USA", "US/Pacific", 47.610, -122.201
265 s = sun(city.observer, date=self.dt, tzinfo=pytz.timezone("US/Pacific"))
266 sunrise_mod = utils.minute_number(s["sunrise"].hour, s["sunrise"].minute)
267 sunset_mod = utils.minute_number(s["sunset"].hour, s["sunset"].minute)
269 now_mod = utils.minute_number(now.hour, now.minute)
270 if now_mod < sunrise_mod or now_mod > (sunset_mod + 45):
272 elif now_mod < (sunrise_mod + 45) or now_mod > (sunset_mod + 120):
277 def get_refresh_period() -> float:
278 if override_refresh_sec is not None:
279 return float(override_refresh_sec * 1000.0)
282 return float(constants.refresh_period_night_sec * 1000.0)
284 return float(constants.refresh_period_sec * 1000.0)
286 age = utils.describe_age_of_file_briefly(f"pages/{filename}")
287 bgcolor = pick_background_color()
291 pageid = f'"{command}" -> {filename}'
296 <TITLE>Kitchen Kiosk</TITLE>
297 <LINK rel="stylesheet" type="text/css" href="style.css">
298 <SCRIPT TYPE="text/javascript">
300 // Zoom the 'contents' div to fit without scrollbars and then make
302 function zoomScreen() {
305 document.getElementById("content").style.zoom = z+"%%";
306 var body = document.body;
307 var html = document.documentElement;
308 var height = Math.max(body.scrollHeight,
313 var windowHeight = window.innerHeight;
314 var width = Math.max(body.scrollWidth,
319 var windowWidth = window.innerWidth;
320 var heightRatio = height / windowHeight;
321 var widthRatio = width / windowWidth;
323 if (heightRatio <= 1.0 && widthRatio <= 1.0) {
328 document.getElementById("content").style.visibility = "visible";
331 // Load IMG tags with DATA-SRC attributes late.
332 function lateLoadImages() {
333 var image = document.getElementsByTagName('img');
334 for (var i = 0; i < image.length; i++) {
335 if (image[i].getAttribute('DATA-SRC')) {
336 image[i].setAttribute('SRC', image[i].getAttribute('DATA-SRC'));
341 // Operate the clock at the top of the page.
342 function runClock() {
343 var today = new Date();
344 var h = today.getHours();
345 var ampm = h >= 12 ? 'pm' : 'am';
347 h = h ? h : 12; // the hour '0' should be '12'
348 var m = maybeAddZero(today.getMinutes());
350 if (today.getSeconds() %% 2 == 0) {
351 colon = "<FONT STYLE='color: #%s; font-size: 4vmin; font-weight: bold'>:</FONT>";
353 document.getElementById("time").innerHTML = h + colon + m + ampm;
354 document.getElementById("date").innerHTML = today.toDateString();
355 var t = setTimeout(function(){runClock()}, 1000);
358 // Helper method for running the clock.
359 function maybeAddZero(x) {
360 return (x < 10) ? "0" + x : x;
363 // Do something on page load.
364 function addLoadEvent(func) {
365 var oldonload = window.onload;
366 if (typeof window.onload != 'function') {
367 window.onload = func;
369 window.onload = function() {
378 // Sleep thread helper.
379 const sleep = (milliseconds) => {
380 return new Promise(resolve => setTimeout(resolve, milliseconds))
383 var loadedDate = new Date();
385 addLoadEvent(zoomScreen);
386 addLoadEvent(runClock);
387 addLoadEvent(lateLoadImages);
389 // Runs the countdown line at the bottom and is responsible for
390 // normal page reloads caused by the expiration of a timer.
391 (function countdown() {
394 var now = new Date();
395 var deltaMs = now.getTime() - loadedDate.getTime();
397 var remainingMs = (totalMs - deltaMs);
399 if (remainingMs > 0) {
400 var hr = document.getElementById("countdown");
401 var width = (remainingMs / (totalMs - 5000)) * 100.0;
403 hr.style.visibility = "visible";
404 hr.style.width = " ".concat(width, "%%");
405 hr.style.backgroundColor = "maroon";
408 // Reload unconditionally after 22 sec.
409 window.location.reload();
412 // Brief sleep before doing it all over again.
413 sleep(50).then(() => {
419 // Periodically checks for emergency reload events.
423 var xhr = new XMLHttpRequest();
425 'http://%s/kiosk/pages/reload_immediately.html');
428 if (xhr.status === 200) {
429 window.location.reload();
431 sleep(500).then(() => {
442 <TABLE style="height:100%%; width:100%%" BORDER=0>
445 <DIV id="date"> </DIV>
447 <TD ALIGN="center"><FONT COLOR=#bbbbbb>
448 <DIV id="info"></DIV></FONT>
451 <DIV id="time"> </DIV>
454 <TR STYLE="vertical-align:top">
456 <DIV ID="content" STYLE="zoom: 1; visibility: hidden;">
457 <!-- BEGIN main page contents. -->
458 <!--#include virtual=\"%s\"-->
459 <!-- END main page contents. -->
462 <DIV STYLE="position: absolute; top:1030px; width:99%%">
464 <FONT SIZE=2 COLOR=#bbbbbb>%s @ %s ago.</FONT>
466 <HR id="countdown" STYLE="width:0px;
473 background-color:#ffffff;">
481 get_refresh_period(),
491 def thread_invoke_renderers() -> None:
493 print(f"renderer[{utils.timestamp()}]: invoking all renderers in catalog...")
494 for r in renderer_catalog.get_renderers():
498 except Exception as e:
499 traceback.print_exc()
501 f"renderer[{utils.timestamp()}] unknown exception in {r.get_name()}, swallowing it."
503 delta = time.time() - now
506 f"renderer[{utils.timestamp()}]: Warning: {r.get_name()}'s rendering took {delta:5.2f}s."
509 f"renderer[{utils.timestamp()}]: thread having a little break for {constants.render_period_sec}s..."
511 time.sleep(constants.render_period_sec)
514 if __name__ == "__main__":
515 logging.basicConfig()
516 command_queue: Queue = Queue()
517 changer_thread: Optional[Thread] = None
518 renderer_thread: Optional[Thread] = None
519 janitor_thread: Optional[Thread] = None
520 hotword_thread: Optional[Thread] = None
522 if hotword_thread is None or not hotword_thread.is_alive():
523 keyword_paths = [pvporcupine.KEYWORD_PATHS[x] for x in ["bumblebee"]]
524 sensitivities = [0.7] * len(keyword_paths)
525 listener = listen.HotwordListener(
530 hotword_thread = Thread(target=listener.listen_forever, args=())
531 hotword_thread.start()
532 if changer_thread is None or not changer_thread.is_alive():
534 f"MAIN[{utils.timestamp()}] - (Re?)initializing chooser thread... (wtf?!)"
536 changer_thread = Thread(target=thread_change_current, args=(command_queue,))
537 changer_thread.start()
538 if renderer_thread is None or not renderer_thread.is_alive():
540 f"MAIN[{utils.timestamp()}] - (Re?)initializing render thread... (wtf?!)"
542 renderer_thread = Thread(target=thread_invoke_renderers, args=())
543 renderer_thread.start()
544 if janitor_thread is None or not janitor_thread.is_alive():
546 f"MAIN[{utils.timestamp()}] - (Re?)initializing janitor thread... (wtf?!)"
548 janitor_thread = Thread(target=thread_janitor, args=())
549 janitor_thread.start()