Make it so that I can verbally scroll chrome.
[kiosk.git] / kiosk.py
1 #!/usr/bin/env python3
2
3 import collections
4 from datetime import datetime
5 from difflib import SequenceMatcher
6 import gc
7 import logging
8 import os
9 import re
10 from threading import Thread
11 import time
12 import tracemalloc
13 from typing import Dict, List, Optional
14 from queue import Queue
15
16 import astral  # type: ignore
17 from astral.sun import sun  # type: ignore
18 import numpy as np
19 import pvporcupine
20 import pytz
21
22 from pyutils import (
23     bootstrap,
24     config,
25 )
26 from pyutils.datetimes import datetime_utils
27 from pyutils.files import file_utils
28
29 import kiosk_constants
30 import file_writer
31 import renderer_catalog
32 import chooser
33 import listen
34 import trigger_catalog
35
36
37 cfg = config.add_commandline_args(
38     f'Kiosk Server ({__file__})',
39     'A python server that runs a kiosk.'
40 )
41 logger = logging.getLogger(__file__)
42
43
44 def thread_janitor() -> None:
45     tracemalloc.start()
46     tracemalloc_target = 0.0
47     gc_target = 0.0
48     gc.enable()
49
50     # Main janitor loop; dump the largest pigs and force regular gcs.
51     while True:
52         now = time.time()
53         if now > tracemalloc_target:
54             tracemalloc_target = now + 30.0
55             snapshot = tracemalloc.take_snapshot()
56             snapshot = snapshot.filter_traces((
57                 tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
58                 tracemalloc.Filter(False, "<unknown>"),
59             ))
60             key_type = 'lineno'
61             limit = 10
62             top_stats = snapshot.statistics(key_type)
63             logger.info(f'janitor: Top {limit} lines')
64             for index, stat in enumerate(top_stats[:limit], 1):
65                 frame = stat.traceback[0]
66
67                 # replace "/path/to/module/file.py" with "module/file.py"
68                 filename = os.sep.join(frame.filename.split(os.sep)[-2:])
69                 logger.info(
70                     f'janitor: #{index}: {filename}:{frame.lineno}: {stat.size / 1024:.1f} KiB'
71                 )
72
73             other = top_stats[limit:]
74             if other:
75                 size = sum(stat.size for stat in other)
76                 logger.info(
77                     f'janitor: {len(other)} others: {size/1024:.1f} KiB'
78                 )
79             total = sum(stat.size for stat in top_stats)
80             logger.info(
81                 f'janitor: Total allocated size: {total / 1024:.1f} KiB'
82             )
83         if now > gc_target:
84             logger.info('janitor: kicking off a manual gc operation now.')
85             gc.collect()
86             gc_target = now + 120.0
87         time.sleep(30.0)
88
89
90 def guess_page(command: str, page_chooser: chooser.chooser) -> str:
91
92     def normalize_page(page: str) -> str:
93         logger.debug(f'normalize_page input: {page}')
94         page = page.replace('(', ' ')
95         page = page.replace('_', ' ')
96         page = page.replace(')', ' ')
97         page = page.replace('.html', '')
98         page = page.replace('CNNNews', 'news')
99         page = page.replace('CNNTechnology', 'technology')
100         page = page.replace('gocostco', 'costco list')
101         page = page.replace('gohardware', 'hardware list')
102         page = page.replace('gohouse', 'house list honey do')
103         page = page.replace('gcal', 'google calendar events')
104         page = page.replace('mynorthwest', 'northwest news')
105         page = page.replace('myq', 'myq garage door status')
106         page = page.replace('gomenu', 'dinner menu')
107         page = page.replace('gmaps-seattle-unwrapped', 'traffic')
108         page = page.replace('gomenu', 'dinner menu')
109         page = page.replace('WSJNews', 'news')
110         page = page.replace('telma', 'telma cabin')
111         page = page.replace('WSJBusiness', 'business news')
112         page = re.sub(r'[0-9]+', '', page)
113         logger.debug(f'normalize_page output: {page}')
114         return page
115
116     logger.info(f'No exact match for f{command}; trying to guess...')
117     best_page = None
118     best_score = None
119     for page in page_chooser.get_page_list():
120         npage = normalize_page(page)
121         score = SequenceMatcher(None, command, npage).ratio()
122         if best_score is None or score > best_score:
123             best_page = page
124             best_score = score
125     assert best_page is not None
126     logger.info(f'Best guess for f{command} => {best_page} (score = {best_score})')
127     return best_page
128
129
130 def process_command(command: str, page_history: List[str], page_chooser) -> str:
131     command = command.lower()
132     logger.info(f'Parsing verbal command: {command}')
133     page = None
134     if 'hold' in command:
135         page = page_history[0]
136     elif 'down' in command:
137         os.system(
138             "xdotool search --onlyvisible \"chrom\" windowactivate click --repeat 8 5"
139         )
140         return None
141     elif 'up' in command:
142         os.system(
143             "xdotool search --onlyvisible \"chrom\" windowactivate click --repeat 8 4"
144         )
145         return None
146     elif 'back' in command:
147         page = page_history[1]
148     elif 'skip' in command:
149         while True:
150             (page, _) = page_chooser.choose_next_page()
151             if page == page_history[0]:
152                 logger.debug(f'chooser: {page} is the same as last time!  Try again.')
153             else:
154                 break
155     elif 'internal' in command:
156         if 'render' in command:
157             page = kiosk_constants.render_stats_pagename
158         else:
159             page = kiosk_constants.render_stats_pagename
160     elif 'weather' in command:
161         if 'telma' in command or 'cabin' in command:
162             page = 'weather-telma_3_10800.html'
163         elif 'stevens' in command:
164             page = 'weather-stevens_3_10800.html'
165         else:
166             page = 'weather-home_3_10800.html'
167     elif 'cabin' in command:
168         if 'list' in command:
169             page = 'Cabin-(gocabin)_2_3600.html'
170         else:
171             page = 'hidden/cabin_driveway.html'
172     elif 'news' in command or 'headlines' in command:
173         page = 'cnn-CNNNews_4_25900.html'
174     elif 'clock' in command or 'time' in command:
175         page = 'clock_10_none.html'
176     elif 'countdown' in command or 'countdowns' in command:
177         page = 'countdown_3_7200.html'
178     elif 'costco' in command:
179         page = 'Costco-(gocostco)_2_3600.html'
180     elif 'calendar' in command or 'events' in command:
181         page = 'gcal_3_86400.html'
182     elif 'countdown' in command or 'countdowns' in command:
183         page = 'countdown_3_7200.html'
184     elif 'grocery' in command or 'groceries' in command:
185         page = 'Grocery-(gogrocery)_2_3600.html'
186     elif 'hardware' in command:
187         page = 'Hardware-(gohardware)_2_3600.html'
188     elif 'garage' in command:
189         page = 'myq_4_300.html'
190     elif 'menu' in command:
191         page = 'Menu-(gomenu)_2_3600.html'
192     elif 'cron' in command or 'health' in command:
193         page = 'periodic-health_6_300.html'
194     elif 'photo' in command or 'picture' in command:
195         page = 'photo_23_3600.html'
196     elif 'quote' in command or 'quotation' in command or 'quotes' in command:
197         page = 'quotes_4_10800.html'
198     elif 'stevens' in command:
199         page = 'stevens-conditions_1_86400.html'
200     elif 'stock' in command or 'stocks' in command:
201         page = 'stock_3_86400.html'
202     elif 'twitter' in command:
203         page = 'twitter_10_3600.html'
204     elif 'traffic' in command:
205         page = 'gmaps-seattle-unwrapped_5_none.html'
206     elif 'front' in command and 'door' in command:
207         page = 'hidden/frontdoor.html'
208     elif 'driveway' in command:
209         page = 'hidden/driveway.html'
210     elif 'backyard' in command:
211         page = 'hidden/backyard.html'
212     else:
213         page = guess_page(command, page_chooser)
214     assert page is not None
215     logger.info(f'Parsed to {page}')
216     return page
217
218
219 def thread_change_current(command_queue: Queue) -> None:
220     page_history = ["", ""]
221     swap_page_target = 0.0
222
223     def filter_news_during_dinnertime(page: str) -> bool:
224         now = datetime.now(tz=pytz.timezone("US/Pacific"))
225         is_dinnertime = now.hour >= 18 and now.hour <= 20
226         return not is_dinnertime or not (
227             "cnn" in page
228             or "news" in page
229             or "mynorthwest" in page
230             or "seattle" in page
231             or "wsj" in page
232         )
233
234     page_chooser = chooser.weighted_random_chooser_with_triggers(
235         trigger_catalog.get_triggers(), [filter_news_during_dinnertime]
236     )
237     current_file = os.path.join(kiosk_constants.pages_dir, "current.shtml")
238     emergency_file = os.path.join(kiosk_constants.pages_dir, "reload_immediately.html")
239
240     # Main chooser loop
241     while True:
242         now = time.time()
243
244         # Check for a verbal command.
245         command = None
246         try:
247             command = command_queue.get(block=False)
248         except Exception:
249             command = None
250
251         if command is not None:
252             logger.info(f'chooser: We got a verbal command ("{command}"), parsing it...')
253             page = process_command(command, page_history, page_chooser)
254
255         if page:
256             triggered = True
257         else:
258             while True:
259                 (page, triggered) = page_chooser.choose_next_page()
260                 if triggered:
261                     logger.info('chooser: A trigger is active...')
262                     break
263                 else:
264                     if page == page_history[0]:
265                         logger.debug(f'chooser: {page} is the same as last time! Try again.')
266                     else:
267                         break
268
269         if triggered:
270             if page != page_history[0] or (swap_page_target - now) < 10.0:
271                 logger.info(f'chooser: An emergency page reload to {page} is needed at this time.')
272                 swap_page_target = now + kiosk_constants.emergency_refresh_period_sec
273
274                 # Set current.shtml to the right page.
275                 try:
276                     with open(current_file, "w") as f:
277                         emit(
278                             f,
279                             page,
280                             override_refresh_sec = kiosk_constants.emergency_refresh_period_sec,
281                             command = command
282                         )
283                     logger.debug(f'chooser: Wrote {current_file}.')
284                 except Exception as e:
285                     logger.exception(e)
286                     logger.error(f'chooser: Unexpected exception; assuming {page} doesn\'t exist?!')
287                     continue
288
289                 # Also notify XMLHTTP clients that they need to refresh now.
290                 with open(emergency_file, "w") as f:
291                     f.write(f'Reload, suckers... you HAVE to see {page}!')
292                 logger.debug(f'chooser: Wrote {emergency_file}...')
293
294                 # Fix this hack... maybe read the webserver logs and see if it
295                 # actually was picked up?
296                 time.sleep(0.999)
297                 os.remove(emergency_file)
298                 logger.debug(f'chooser: ...and removed {emergency_file}.')
299
300         elif now >= swap_page_target:
301             assert page != page_history[0]
302             logger.info(
303                 f'chooser: Nominal choice of {page} as the next to show.'
304             )
305             swap_page_target = now + kiosk_constants.refresh_period_sec
306             try:
307                 with open(current_file, "w") as f:
308                     emit(f, page)
309                 logger.debug(f'chooser: Wrote {current_file}.')
310             except Exception as e:
311                 logger.exception(e)
312                 logger.error(f'chooser: Unexpected exception; assuming {page} doesn\'t exist?!')
313                 continue
314         page_history.insert(0, page)
315         page_history = page_history[0:10]
316         time.sleep(0.5)
317
318
319 def emit(f,
320          filename: str,
321          *,
322          override_refresh_sec: int = None,
323          command: str = None):
324     if 'unwrapped' not in filename:
325         logger.debug(f'Emitting {filename} wrapped.')
326         emit_wrapped(f, filename, override_refresh_sec=override_refresh_sec, command=command)
327     else:
328         logger.debug(f'Emitting {filename} raw.')
329         emit_raw(f, filename)
330
331
332 def emit_raw(f, filename: str):
333     f.write(f'<!--#include virtual="{filename}"-->')
334
335
336 def emit_wrapped(f,
337                  filename: str,
338                  *,
339                  override_refresh_sec: int = None,
340                  command: str = None) -> None:
341
342     def pick_background_color() -> str:
343         now = datetime_utils.now_pacific()
344         city = astral.LocationInfo(
345             "Bellevue", "USA", "US/Pacific", 47.610, -122.201
346         )
347         s = sun(city.observer, date=now, tzinfo=pytz.timezone("US/Pacific"))
348         sunrise_mod = datetime_utils.minute_number(s["sunrise"].hour, s["sunrise"].minute)
349         sunset_mod = datetime_utils.minute_number(s["sunset"].hour, s["sunset"].minute)
350         now_mod = datetime_utils.minute_number(now.hour, now.minute)
351         if now_mod < sunrise_mod or now_mod > (sunset_mod + 45):
352             return "E6B8B8"
353         elif now_mod < (sunrise_mod + 45) or now_mod > (sunset_mod + 120):
354             return "EECDCD"
355         else:
356             return "FFFFFF"
357
358     def get_refresh_period() -> float:
359         if override_refresh_sec is not None:
360             return float(override_refresh_sec * 1000.0)
361         now = datetime.now(tz=pytz.timezone("US/Pacific"))
362         if now.hour < 6:
363             return float(kiosk_constants.refresh_period_night_sec * 1000.0)
364         else:
365             return float(kiosk_constants.refresh_period_sec * 1000.0)
366
367     age = file_utils.describe_file_ctime(f"pages/{filename}")
368     bgcolor = pick_background_color()
369     if command is None:
370         pageid = filename
371     else:
372         pageid = f'"{command}" -> {filename}'
373
374     f.write(
375 """
376 <HEAD>
377   <TITLE>Kitchen Kiosk</TITLE>
378   <LINK rel="stylesheet" type="text/css" href="style.css">
379   <meta name="google" content="notranslate">
380   <SCRIPT TYPE="text/javascript">
381
382   // Zoom the 'contents' div to fit without scrollbars and then make
383   // it visible.
384   function zoomScreen() {
385     z = 285;
386     do {
387       document.getElementById("content").style.zoom = z+"%";
388       var body = document.body;
389       var html = document.documentElement;
390       var height = Math.max(body.scrollHeight,
391                             body.offsetHeight,
392                             html.clientHeight,
393                             html.scrollHeight,
394                             html.offsetHeight);
395       var windowHeight = window.innerHeight;
396       var width = Math.max(body.scrollWidth,
397                            body.offsetWidth,
398                            html.clientWidth,
399                            html.scrollWidth,
400                            html.offsetWidth);
401       var windowWidth = window.innerWidth;
402       var heightRatio = height / windowHeight;
403       var widthRatio = width / windowWidth;
404
405       if (heightRatio <= 1.0 && widthRatio <= 1.0) {
406         break;
407       }
408       z -= 4;
409     } while(z >= 70);
410     document.getElementById("content").style.visibility = "visible";
411   }
412
413   // Load IMG tags with DATA-SRC attributes late.
414   function lateLoadImages() {
415     var image = document.getElementsByTagName('img');
416     for (var i = 0; i < image.length; i++) {
417       if (image[i].getAttribute('DATA-SRC')) {
418         image[i].setAttribute('SRC', image[i].getAttribute('DATA-SRC'));
419       }
420     }
421   }
422 """
423     )
424     f.write(
425 """
426   // Operate the clock at the top of the page.
427   function runClock() {
428     var today = new Date();
429     var h = today.getHours();
430     var ampm = h >= 12 ? 'pm' : 'am';
431     h = h %% 12;
432     h = h ? h : 12; // the hour '0' should be '12'
433     var m = maybeAddZero(today.getMinutes());
434     var colon = ":";
435     if (today.getSeconds() %% 2 == 0) {
436       colon = "<FONT STYLE='color: #%s; font-size: 4vmin; font-weight: bold'>:</FONT>";
437     }
438     document.getElementById("time").innerHTML = h + colon + m + ampm;
439     document.getElementById("date").innerHTML = today.toDateString();
440     var t = setTimeout(function(){runClock()}, 1000);
441   }
442 """ % bgcolor
443     )
444     f.write(
445 """
446   // Helper method for running the clock.
447   function maybeAddZero(x) {
448     return (x < 10) ? "0" + x : x;
449   }
450
451   // Do something on page load.
452   function addLoadEvent(func) {
453     var oldonload = window.onload;
454     if (typeof window.onload != 'function') {
455       window.onload = func;
456     } else {
457       window.onload = function() {
458         if (oldonload) {
459           oldonload();
460         }
461         func();
462       }
463     }
464   }
465
466   // Sleep thread helper.
467   const sleep = (milliseconds) => {
468     return new Promise(resolve => setTimeout(resolve, milliseconds))
469   }
470
471   var loadedDate = new Date();
472
473   addLoadEvent(zoomScreen);
474   addLoadEvent(runClock);
475   addLoadEvent(lateLoadImages);
476 """
477     )
478     f.write(
479 """
480   // Runs the countdown line at the bottom and is responsible for
481   // normal page reloads caused by the expiration of a timer.
482   (function countdown() {
483     setTimeout(
484       function() {
485         var now = new Date();
486         var deltaMs = now.getTime() - loadedDate.getTime();
487         var totalMs = %d;
488         var remainingMs = (totalMs - deltaMs);
489
490         if (remainingMs > 0) {
491           var hr = document.getElementById("countdown");
492           var width = (remainingMs / (totalMs - 5000)) * 100.0;
493           if (width <= 100) {
494             hr.style.visibility = "visible";
495             hr.style.width = " ".concat(width, "%%");
496             hr.style.backgroundColor = "maroon";
497           }
498         } else {
499           // Reload unconditionally after 22 sec.
500           window.location.reload(true);
501         }
502
503         // Brief sleep before doing it all over again.
504         sleep(50).then(() => {
505           countdown();
506         });
507       }, 50)
508   })();
509 """ % get_refresh_period()
510     )
511     f.write(
512 """
513   function check_reload() {
514     var xhr = new XMLHttpRequest();
515     xhr.open('GET',
516              '%s/reload_immediately.html');
517     xhr.onload =
518       function() {
519         if (xhr.status === 200) {
520           window.location.reload(true);
521         }
522       };
523     xhr.send();
524   }
525
526   // Periodically checks for emergency reload events.
527   setInterval(check_reload, 500);
528   </SCRIPT>
529 </HEAD>
530 """ % kiosk_constants.root_url)
531     f.write(f'<BODY BGCOLOR="#{bgcolor}">')
532     f.write(
533 """
534     <TABLE style="height:100%; width:100%" BORDER=0>
535     <TR HEIGHT=30>
536         <TD ALIGN="left">
537             <DIV id="date">&nbsp;</DIV>
538         </TD>
539         <TD ALIGN="center"><FONT COLOR=#bbbbbb>
540             <DIV id="info"></DIV></FONT>
541         </TD>
542         <TD ALIGN="right">
543             <DIV id="time">&nbsp;</DIV>
544         </TD>
545     </TR>
546     <TR STYLE="vertical-align:top">
547         <TD COLSPAN=3>
548             <DIV ID="content" STYLE="zoom: 1; visibility: hidden;">
549               <!-- BEGIN main page contents. -->
550 """
551     )
552     f.write(f'<!--#include virtual="{filename}"-->')
553     f.write(
554 """
555             <!-- END main page contents. -->
556             </DIV>
557             <BR>
558             <DIV STYLE="position: absolute; top:1030px; width:99%">
559             <P ALIGN="right">
560 """
561     )
562     f.write(f'<FONT SIZE=2 COLOR=#bbbbbb>{pageid} @ {age} ago.</FONT>')
563     f.write(
564 """
565             </P>
566             <HR id="countdown" STYLE="width:0px;
567                                       text-align:left;
568                                       margin:0;
569                                       border:none;
570                                       border-width:0;
571                                       height:5px;
572                                       visibility:hidden;
573                                       background-color:#ffffff;">
574             </DIV>
575         </TD>
576     </TR>
577     </TABLE>
578 </BODY>"""
579     )
580
581
582 def renderer_update_internal_stats_page(
583         last_render: Dict[str, datetime],
584         render_counts: collections.Counter,
585         render_times: Dict[str, np.array],
586 ) -> None:
587     logger.info(
588         'renderer: Updating internal render statistics page.'
589     )
590     with file_writer.file_writer(kiosk_constants.render_stats_pagename) as f:
591         f.write(
592 '''
593 <CENTER>
594 <TABLE BORDER=0 WIDTH=95%>
595     <TR>
596     <TH><B>Renderer Name</B></TH>
597     <TH><B>Last Run</B></TH>
598     <TH><B>Num Invocations</B></TH>
599     <TH><B>Render Latency</B></TH>
600     </TR>
601 ''')
602         for n, r in enumerate(renderer_catalog.get_renderers()):
603             if n % 2 == 0:
604                 style = 'style="margin: 0; padding: 0; background: #c6b0b0;"'
605             else:
606                 style = 'style="margin: 0; padding: 0; background: #eeeeee;"'
607             name = r.get_name()
608             last = last_render.get(name, None)
609             if last is None:
610                 last = 'never'
611             else:
612                 last = last.strftime('%Y/%m/%d %I:%M:%S%P')
613             count = render_counts.get(name, 0)
614             latency = render_times.get(name, np.array([]))
615             p25 = p50 = p75 = p90 = p99 = 'N/A'
616             try:
617                 p25 = np.percentile(latency, 25)
618                 p50 = np.percentile(latency, 50)
619                 p75 = np.percentile(latency, 75)
620                 p90 = np.percentile(latency, 90)
621                 p99 = np.percentile(latency, 99)
622                 f.write(
623 f'''
624     <TR>
625     <TD {style}>{name}&nbsp;</TD>
626     <TD {style}>&nbsp;{last}&nbsp;</TD>
627     <TD {style}><CENTER>&nbsp;{count}&nbsp;</CENTER></TD>
628     <TD {style}>&nbsp;p25={p25:5.2f}, p50={p50:5.2f}, p75={p75:5.2f}, p90={p90:5.2f}, p99={p99:5.2f}</TD>
629     </TR>
630 '''
631                 )
632             except IndexError:
633                 pass
634         f.write('</TABLE>')
635
636
637 def thread_invoke_renderers() -> None:
638     render_times: Dict[str, np.array] = {}
639     render_counts: collections.Counter = collections.Counter()
640     last_render: Dict[str, datetime] = {}
641
642     # Touch the internal render page now to signal that we're alive.
643     renderer_update_internal_stats_page(last_render, render_counts, render_times)
644
645     # Main renderer loop
646     while True:
647         logger.info(
648             'renderer: invoking all overdue renderers in catalog...'
649         )
650         for r in renderer_catalog.get_renderers():
651             name = r.get_name()
652             now = time.time()
653             logger.info(f'renderer: Invoking {name}\'s render method.')
654             try:
655                 r.render()
656             except Exception as e:
657                 logger.exception(e)
658                 logger.error(
659                     f'renderer: Unexpected and unhandled exception ({e}) in {name}, swallowing it.'
660                 )
661                 continue
662
663             # Increment the count of render operations per renderer.
664             render_counts[name] += 1
665
666             # Keep track of the last time we invoked each renderer.
667             last_render[name] = datetime_utils.now_pacific()
668
669             # Record how long each render operation takes and warn if very long.
670             delta = time.time() - now
671             times = render_times.get(name, np.array([]))
672             times = np.insert(times, 0, delta)
673             render_times[name] = times
674             if delta > 1.0:
675                 hdr = 'renderer: '
676                 logger.warning(
677 f'''
678 {hdr} Warning: {name}'s rendering took {delta:5.2f}s.
679 {hdr} FYI: {name}'s render times: p25={np.percentile(times, 25):5.2f}, p50={np.percentile(times, 50):5.2f}, p75={np.percentile(times, 75):5.2f}, p90={np.percentile(times, 90):5.2f}, p99={np.percentile(times, 99):5.2f}
680 '''
681                 )
682
683         # Update a page about internal stats of renderers.
684         renderer_update_internal_stats_page(last_render, render_counts, render_times)
685         logger.info('renderer: having a little nap...')
686         time.sleep(kiosk_constants.render_period_sec)
687
688
689 @bootstrap.initialize
690 def main() -> None:
691     command_queue: Queue = Queue()
692     changer_thread: Optional[Thread] = None
693     renderer_thread: Optional[Thread] = None
694     janitor_thread: Optional[Thread] = None
695     hotword_thread: Optional[Thread] = None
696     while True:
697         if hotword_thread is None or not hotword_thread.is_alive():
698             if hotword_thread is None:
699                 logger.info('watchdog: Starting up the hotword detector thread...')
700             else:
701                 logger.warning(
702                     'watchdog: The hotword detector thread seems to have died; restarting it and hoping for the best.'
703                 )
704             keyword_paths = [pvporcupine.KEYWORD_PATHS[x] for x in ["bumblebee"]]
705             sensitivities = [0.7] * len(keyword_paths)
706             listener = listen.HotwordListener(
707                 command_queue,
708                 keyword_paths,
709                 sensitivities,
710             )
711             hotword_thread = Thread(target=listener.listen_forever, args=())
712             hotword_thread.start()
713
714         if changer_thread is None or not changer_thread.is_alive():
715             if changer_thread is None:
716                 logger.info('watchdog: Starting up the current page changer thread...')
717             else:
718                 logger.warning(
719                     'watchdog: The current page changer thread seems to have died; restarting it and hoping for the best.'
720                 )
721             changer_thread = Thread(target=thread_change_current, args=(command_queue,))
722             changer_thread.start()
723
724         if renderer_thread is None or not renderer_thread.is_alive():
725             if renderer_thread is None:
726                 logger.info('watchdog: Starting up the page renderer thread...')
727             else:
728                 logger.warning(
729                     'watchdog: The page renderer thread seems to have died; restarting it and hoping for the best.'
730                 )
731             renderer_thread = Thread(target=thread_invoke_renderers, args=())
732             renderer_thread.start()
733
734         if janitor_thread is None or not janitor_thread.is_alive():
735             if janitor_thread is None:
736                 logger.info('watchdog: Starting up the memory janitor thread...')
737             else:
738                 logger.warning(
739                     'watchdog: The memory janitor thread seems to have died; restarting it and hoping for the best.'
740                 )
741             janitor_thread = Thread(target=thread_janitor, args=())
742             janitor_thread.start()
743
744         # Have a little break and then check to make sure all threads are still alive.
745         logger.debug('watchdog: having a little nap.')
746         time.sleep(kiosk_constants.check_threads_period_sec)
747
748
749 if __name__ == "__main__":
750     main()