X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=kiosk.py;h=b1bdb1c7c7e9b6f8319b46288c5995fac95c95f2;hb=477c84bcdefc573278ec8ed8974d5be8baac562d;hp=bd7137335ea25e5dbae0d8f39a9226c53c9bb19a;hpb=7eae23537dcc61565a24d5c957d4325b7337b63a;p=kiosk.git diff --git a/kiosk.py b/kiosk.py index bd71373..b1bdb1c 100755 --- a/kiosk.py +++ b/kiosk.py @@ -2,37 +2,40 @@ import collections from datetime import datetime -import difflib +from difflib import SequenceMatcher import gc -import linecache import logging import os import re -import sys from threading import Thread import time -import traceback import tracemalloc -from typing import Optional, List -from queue import Queue, Empty +from typing import Dict, List, Optional +from queue import Queue import astral # type: ignore from astral.sun import sun # type: ignore import numpy as np +import pvporcupine import pytz +import bootstrap +import config +import datetime_utils +import file_utils + import constants import file_writer -import renderer -import renderer import renderer_catalog import chooser import listen -import logging -import pvporcupine import trigger_catalog -import utils + +cfg = config.add_commandline_args( + f'Kiosk Server ({__file__})', + 'A python server that runs a kiosk.' +) logger = logging.getLogger(__file__) @@ -42,6 +45,7 @@ def thread_janitor() -> None: gc_target = 0.0 gc.enable() + # Main janitor loop; dump the largest pigs and force regular gcs. while True: now = time.time() if now > tracemalloc_target: @@ -54,34 +58,37 @@ def thread_janitor() -> None: key_type = 'lineno' limit = 10 top_stats = snapshot.statistics(key_type) - print("janitor: Top %s lines" % limit) + logger.info(f'janitor: Top {limit} lines') for index, stat in enumerate(top_stats[:limit], 1): frame = stat.traceback[0] + # replace "/path/to/module/file.py" with "module/file.py" filename = os.sep.join(frame.filename.split(os.sep)[-2:]) - print("janitor: #%s: %s:%s: %.1f KiB" - % (index, filename, frame.lineno, stat.size / 1024)) - line = linecache.getline(frame.filename, frame.lineno).strip() - if line: - print('janitor: %s' % line) + logger.info( + f'janitor: #{index}: {filename}:{frame.lineno}: {stat.size / 1024:.1f} KiB' + ) other = top_stats[limit:] if other: size = sum(stat.size for stat in other) - print("janitor: %s other: %.1f KiB" % (len(other), size / 1024)) + logger.info( + f'janitor: {len(other)} others: {size/1024:.1f} KiB' + ) total = sum(stat.size for stat in top_stats) - print("janitor: Total allocated size: %.1f KiB" % (total / 1024)) + logger.info( + f'janitor: Total allocated size: {total / 1024:.1f} KiB' + ) if now > gc_target: - print("janitor: Running gc operation") - gc_target = now + 60.0 + logger.info('janitor: kicking off a manual gc operation now.') gc.collect() - time.sleep(10.0) + gc_target = now + 120.0 + time.sleep(30.0) def guess_page(command: str, page_chooser: chooser.chooser) -> str: - best_page = None - best_score = None - for page in page_chooser.get_page_list(): + + def normalize_page(page: str) -> str: + logger.debug(f'normalize_page input: {page}') page = page.replace('(', ' ') page = page.replace('_', ' ') page = page.replace(')', ' ') @@ -95,20 +102,32 @@ def guess_page(command: str, page_chooser: chooser.chooser) -> str: page = page.replace('mynorthwest', 'northwest news') page = page.replace('myq', 'myq garage door status') page = page.replace('gomenu', 'dinner menu') - page = page.replace('wsdot', 'traffic') + page = page.replace('gmaps-seattle-unwrapped', 'traffic') page = page.replace('gomenu', 'dinner menu') page = page.replace('WSJNews', 'news') page = page.replace('telma', 'telma cabin') page = page.replace('WSJBusiness', 'business news') page = re.sub(r'[0-9]+', '', page) - score = SequenceMatcher(None, command, page).ratio() + logger.debug(f'normalize_page output: {page}') + return page + + logger.info(f'No exact match for f{command}; trying to guess...') + best_page = None + best_score = None + for page in page_chooser.get_page_list(): + npage = normalize_page(page) + score = SequenceMatcher(None, command, npage).ratio() if best_score is None or score > best_score: best_page = page + best_score = score assert best_page is not None + logger.info(f'Best guess for f{command} => {best_page} (score = {best_score})') return best_page def process_command(command: str, page_history: List[str], page_chooser) -> str: + command = command.lower() + logger.info(f'Parsing verbal command: {command}') page = None if 'hold' in command: page = page_history[0] @@ -117,8 +136,15 @@ def process_command(command: str, page_history: List[str], page_chooser) -> str: elif 'skip' in command: while True: (page, _) = page_chooser.choose_next_page() - if page != page_history[0]: + if page == page_history[0]: + logger.debug(f'chooser: {page} is the same as last time! Try again.') + else: break + elif 'internal' in command: + if 'render' in command: + page = constants.render_stats_pagename + else: + page = constants.render_stats_pagename elif 'weather' in command: if 'telma' in command or 'cabin' in command: page = 'weather-telma_3_10800.html' @@ -164,7 +190,7 @@ def process_command(command: str, page_history: List[str], page_chooser) -> str: elif 'twitter' in command: page = 'twitter_10_3600.html' elif 'traffic' in command: - page = 'wsdot-bridges_3_none.html' + page = 'gmaps-seattle-unwrapped_5_none.html' elif 'front' in command and 'door' in command: page = 'hidden/frontdoor.html' elif 'driveway' in command: @@ -174,29 +200,32 @@ def process_command(command: str, page_history: List[str], page_chooser) -> str: else: page = guess_page(command, page_chooser) assert page is not None + logger.info(f'Parsed to {page}') return page def thread_change_current(command_queue: Queue) -> None: - page_history = [ "", "" ] + page_history = ["", ""] swap_page_target = 0.0 def filter_news_during_dinnertime(page: str) -> bool: now = datetime.now(tz=pytz.timezone("US/Pacific")) - is_dinnertime = now.hour >= 17 and now.hour <= 20 + is_dinnertime = now.hour >= 18 and now.hour <= 20 return not is_dinnertime or not ( "cnn" in page or "news" in page or "mynorthwest" in page or "seattle" in page - or "stranger" in page - or "twitter" in page or "wsj" in page ) + page_chooser = chooser.weighted_random_chooser_with_triggers( trigger_catalog.get_triggers(), [filter_news_during_dinnertime] ) + current_file = os.path.join(constants.pages_dir, "current.shtml") + emergency_file = os.path.join(constants.pages_dir, "reload_immediately.html") + # Main chooser loop while True: now = time.time() @@ -206,63 +235,88 @@ def thread_change_current(command_queue: Queue) -> None: command = command_queue.get(block=False) except Exception: command = None - pass + if command is not None: + logger.info(f'chooser: We got a verbal command ("{command}"), parsing it...') triggered = True page = process_command(command, page_history, page_chooser) - - # Else pick a page randomly. else: while True: (page, triggered) = page_chooser.choose_next_page() - if triggered or page != page_history[0]: + if triggered: + logger.info('chooser: A trigger is active...') break + else: + if page == page_history[0]: + logger.debug(f'chooser: {page} is the same as last time! Try again.') + else: + break if triggered: - print("chooser[%s] - WE ARE TRIGGERED." % utils.timestamp()) if page != page_history[0] or (swap_page_target - now) < 10.0: - print( - "chooser[%s] - EMERGENCY PAGE %s LOAD NEEDED" - % (utils.timestamp(), page) - ) -# try: - current = os.path.join(constants.pages_dir, "current.shtml") - with open(current, "w") as f: - emit_wrapped(f, page, override_refresh_sec = 40, command = command) - print(f'Wrote {current}') - - page_history.insert(0, page) - page_history = page_history[0:10] - swap_page_target = now + 40 -# except: -# print("chooser[%s] - page does not exist?!" % (utils.timestamp())) -# continue + logger.info(f'chooser: An emergency page reload to {page} is needed at this time.') + swap_page_target = now + constants.emergency_refresh_period_sec + + # Set current.shtml to the right page. + try: + with open(current_file, "w") as f: + emit( + f, + page, + override_refresh_sec = constants.emergency_refresh_period_sec, + command = command + ) + logger.debug(f'chooser: Wrote {current_file}.') + except Exception as e: + logger.exception(e) + logger.error(f'chooser: Unexpected exception; assuming {page} doesn\'t exist?!') + continue # Also notify XMLHTTP clients that they need to refresh now. - emergency_file = os.path.join(constants.pages_dir, "reload_immediately.html") with open(emergency_file, "w") as f: f.write(f'Reload, suckers... you HAVE to see {page}!') - print(f'Causing immediate page reload with {emergency_file}...') + logger.debug(f'chooser: Wrote {emergency_file}...') # Fix this hack... maybe read the webserver logs and see if it # actually was picked up? - time.sleep(3.0) + time.sleep(0.999) os.remove(emergency_file) - print(f'...and removed {emergency_file}') + logger.debug(f'chooser: ...and removed {emergency_file}.') elif now >= swap_page_target: assert page != page_history[0] - print("chooser[%s] - nominal choice of %s" % (utils.timestamp(), page)) -# try: - with open(os.path.join(constants.pages_dir, "current.shtml"), "w") as f: - emit_wrapped(f, page) - page_history.insert(0, page) - page_history = page_history[0:10] + logger.info( + f'chooser: Nominal choice of {page} as the next to show.' + ) swap_page_target = now + constants.refresh_period_sec -# except: -# print("chooser[%s] - page does not exist?!" % (utils.timestamp())) -# continue - time.sleep(1) + try: + with open(current_file, "w") as f: + emit(f, page) + logger.debug(f'chooser: Wrote {current_file}.') + except Exception as e: + logger.exception(e) + logger.error(f'chooser: Unexpected exception; assuming {page} doesn\'t exist?!') + continue + page_history.insert(0, page) + page_history = page_history[0:10] + time.sleep(0.5) + + +def emit(f, + filename: str, + *, + override_refresh_sec: int = None, + command: str = None): + if 'unwrapped' not in filename: + logger.debug(f'Emitting {filename} wrapped.') + emit_wrapped(f, filename, override_refresh_sec=override_refresh_sec, command=command) + else: + logger.debug(f'Emitting {filename} raw.') + emit_raw(f, filename) + + +def emit_raw(f, filename: str): + f.write(f'') def emit_wrapped(f, @@ -270,15 +324,16 @@ def emit_wrapped(f, *, override_refresh_sec: int = None, command: str = None) -> None: + def pick_background_color() -> str: - now = datetime.now(tz=pytz.timezone("US/Pacific")) + now = datetime_utils.now_pacific() city = astral.LocationInfo( "Bellevue", "USA", "US/Pacific", 47.610, -122.201 ) s = sun(city.observer, date=now, tzinfo=pytz.timezone("US/Pacific")) - sunrise_mod = utils.minute_number(s["sunrise"].hour, s["sunrise"].minute) - sunset_mod = utils.minute_number(s["sunset"].hour, s["sunset"].minute) - now_mod = utils.minute_number(now.hour, now.minute) + sunrise_mod = datetime_utils.minute_number(s["sunrise"].hour, s["sunrise"].minute) + sunset_mod = datetime_utils.minute_number(s["sunset"].hour, s["sunset"].minute) + now_mod = datetime_utils.minute_number(now.hour, now.minute) if now_mod < sunrise_mod or now_mod > (sunset_mod + 45): return "E6B8B8" elif now_mod < (sunrise_mod + 45) or now_mod > (sunset_mod + 120): @@ -290,12 +345,12 @@ def emit_wrapped(f, if override_refresh_sec is not None: return float(override_refresh_sec * 1000.0) now = datetime.now(tz=pytz.timezone("US/Pacific")) - if now.hour < 7: + if now.hour < 6: return float(constants.refresh_period_night_sec * 1000.0) else: return float(constants.refresh_period_sec * 1000.0) - age = utils.describe_age_of_file_briefly(f"pages/{filename}") + age = file_utils.describe_file_ctime(f"pages/{filename}") bgcolor = pick_background_color() if command is None: pageid = filename @@ -307,6 +362,7 @@ def emit_wrapped(f, Kitchen Kiosk + """ % constants.root_url) @@ -479,7 +533,8 @@ def emit_wrapped(f,