X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=presence.py;h=5fad457aa1251c88ab66b2b693dd25dc00fbbeab;hb=b29be4f1750fd20bd2eada88e751dfae85817882;hp=b310183b4da6c1fdf002bee0c559abd9a9fd0ca1;hpb=eb9e6df32ed696158bf34dba6464277b648f5c74;p=python_utils.git diff --git a/presence.py b/presence.py index b310183..5fad457 100755 --- a/presence.py +++ b/presence.py @@ -5,6 +5,7 @@ from collections import defaultdict import enum import logging import re +import sys from typing import Dict, List # Note: this module is fairly early loaded. Be aware of dependencies. @@ -51,16 +52,14 @@ class PresenceDetection(object): Person.SCOTT: [ "3C:28:6D:10:6D:41", # pixel3 "6C:40:08:AE:DC:2E", # laptop -# "D4:61:2E:88:18:09", # watch -# "14:7D:DA:6A:20:D7", # work laptop ], Person.LYNN: [ - "08:CC:27:63:26:14", - "B8:31:B5:9A:4F:19", + "08:CC:27:63:26:14", # motog7 + "B8:31:B5:9A:4F:19", # laptop ], Person.ALEX: [ - "0C:CB:85:0C:8B:AE", - "D0:C6:37:E3:36:9A", + "0C:CB:85:0C:8B:AE", # phone + "D0:C6:37:E3:36:9A", # laptop ], Person.AARON_AND_DANA: [ "98:B6:E9:E5:5A:7C", @@ -83,16 +82,24 @@ class PresenceDetection(object): self.update() def update(self) -> None: - from exec_utils import cmd + from exec_utils import cmd_with_timeout try: persisted_macs = config.config['presence_macs_file'] except KeyError: persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt' self.read_persisted_macs_file(persisted_macs, Location.HOUSE) - raw = cmd( - "ssh scott@meerkat.cabin 'cat /home/scott/cron/persisted_mac_addresses.txt'" - ) - self.parse_raw_macs_file(raw, Location.CABIN) + try: + raw = cmd_with_timeout( + "ssh scott@meerkat.cabin 'cat /home/scott/cron/persisted_mac_addresses.txt'", + timeout_seconds=10.0, + ) + self.parse_raw_macs_file(raw, Location.CABIN) + except Exception as e: + logger.exception(e) + logger.error( + 'Unable to fetch MAC Addresses from meerkat; can\'t do proper presence detection.' + ) + sys.exit(1) def read_persisted_macs_file( self, filename: str, location: Location @@ -149,6 +156,7 @@ class PresenceDetection(object): def where_is_person_now(self, name: Person) -> Location: import dict_utils + logger.debug(f'Looking for {name}...') if name is Person.UNKNOWN: if self.weird_mac_at_cabin: @@ -159,26 +167,30 @@ class PresenceDetection(object): tiebreaks: Dict[Location, datetime.datetime] = {} credit = 10000 for mac in self.devices_by_person[name]: - logger.debug(f'Looking for {name}... check for mac {mac}') if mac not in self.names_by_mac: continue + mac_name = self.names_by_mac[mac] + logger.debug(f'Looking for {name}... check for mac {mac} ({mac_name})') for location in self.location_ts_by_mac: if mac in self.location_ts_by_mac[location]: ts = (self.location_ts_by_mac[location])[mac] - logger.debug(f'I saw {mac} at {location} at {ts}') + logger.debug(f'Seen {mac} ({mac_name}) at {location} since {ts}') tiebreaks[location] = ts - location = dict_utils.key_with_min_value(tiebreaks) - v = votes.get(location, 0) - votes[location] = v + credit - logger.debug(f'{name}: {location} gets {credit} votes.') + + (most_recent_location, first_seen_ts) = dict_utils.item_with_max_value(tiebreaks) + bonus = credit + v = votes.get(most_recent_location, 0) + votes[most_recent_location] = v + bonus + logger.debug(f'{name}: {location} gets {bonus} votes.') credit = int( - credit * 0.667 + credit * 0.2 ) # Note: list most important devices first if credit <= 0: credit = 1 if len(votes) > 0: - item = dict_utils.item_with_max_value(votes) - return item[0] + (location, value) = dict_utils.item_with_max_value(votes) + if value > 2001: + return location return Location.UNKNOWN