X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=base_presence.py;h=f18b870daceaa4e97cf47d5c7b68f90da1a2ed24;hb=bf611fe56efcdce0dada32d292b1587057ab6dd0;hp=f774dbce270dc0b9c13c409ca9019d3a8e921166;hpb=36fea7f15ed17150691b5b3ead75450e575229ef;p=python_utils.git diff --git a/base_presence.py b/base_presence.py index f774dbc..f18b870 100755 --- a/base_presence.py +++ b/base_presence.py @@ -1,20 +1,19 @@ #!/usr/bin/env python3 import datetime -from collections import defaultdict import logging import re -from typing import Dict, List, Set import warnings +from collections import defaultdict +from typing import Dict, List, Optional, Set # Note: this module is fairly early loaded. Be aware of dependencies. import argparse_utils import bootstrap import config +import site_config from type.locations import Location from type.people import Person -import site_config - logger = logging.getLogger(__name__) @@ -68,14 +67,12 @@ class PresenceDetection(object): ], } self.run_location = site_config.get_location() - logger.debug(f"run_location is {self.run_location}") + logger.debug("base_presence run_location is %s", self.run_location) self.weird_mac_at_cabin = False - self.location_ts_by_mac: Dict[ - Location, Dict[str, datetime.datetime] - ] = defaultdict(dict) + self.location_ts_by_mac: Dict[Location, Dict[str, datetime.datetime]] = defaultdict(dict) self.names_by_mac: Dict[str, str] = {} self.dark_locations: Set[Location] = set() - self.last_update = None + self.last_update: Optional[datetime.datetime] = None def maybe_update(self) -> None: if self.last_update is None: @@ -85,12 +82,10 @@ class PresenceDetection(object): delta = now - self.last_update if ( delta.total_seconds() - > config.config[ - 'presence_tolerable_staleness_seconds' - ].total_seconds() + > config.config['presence_tolerable_staleness_seconds'].total_seconds() ): logger.debug( - f"It's been {delta.total_seconds()}s since last update; refreshing now." + "It's been %ss since last update; refreshing now.", delta.total_seconds() ) self.update() @@ -146,9 +141,7 @@ class PresenceDetection(object): warnings.warn(msg, stacklevel=2) self.dark_locations.add(Location.HOUSE) - def read_persisted_macs_file( - self, filename: str, location: Location - ) -> None: + def read_persisted_macs_file(self, filename: str, location: Location) -> None: if location is Location.UNKNOWN: return with open(filename, "r") as rf: @@ -164,22 +157,22 @@ class PresenceDetection(object): line = line.strip() if len(line) == 0: continue - logger.debug(f'{location}> {line}') + logger.debug('%s> %s', location, line) if "cabin_" in line: continue if location == Location.CABIN: - logger.debug(f'Cabin count: {cabin_count}') + logger.debug('Cabin count: %d', cabin_count) cabin_count += 1 try: - (mac, count, ip_name, mfg, ts) = line.split(",") + (mac, _, ip_name, _, ts) = line.split(",") # type: ignore except Exception as e: - logger.error(f'SKIPPED BAD LINE> {line}') logger.exception(e) + logger.error('SKIPPED BAD LINE> %s', line) continue mac = mac.strip() - (self.location_ts_by_mac[location])[ - mac - ] = datetime.datetime.fromtimestamp(int(ts.strip())) + (self.location_ts_by_mac[location])[mac] = datetime.datetime.fromtimestamp( + int(ts.strip()) + ) ip_name = ip_name.strip() match = re.match(r"(\d+\.\d+\.\d+\.\d+) +\(([^\)]+)\)", ip_name) if match is not None: @@ -192,9 +185,7 @@ class PresenceDetection(object): def is_anyone_in_location_now(self, location: Location) -> bool: self.maybe_update() if location in self.dark_locations: - raise Exception( - f"Can't see {location} right now; answer undefined." - ) + raise Exception(f"Can't see {location} right now; answer undefined.") for person in Person: if person is not None: loc = self.where_is_person_now(person) @@ -210,7 +201,7 @@ class PresenceDetection(object): msg = f"Can't see {self.dark_locations} right now; answer confidence impacted" logger.warning(msg) warnings.warn(msg, stacklevel=2) - logger.debug(f'Looking for {name}...') + logger.debug('Looking for %s...', name) if name is Person.UNKNOWN: if self.weird_mac_at_cabin: @@ -223,37 +214,39 @@ class PresenceDetection(object): votes: Dict[Location, int] = {} tiebreaks: Dict[Location, datetime.datetime] = {} credit = 10000 + location = None for mac in self.devices_by_person[name]: 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})' + 'Looking for %s... check for mac %s (%s)', + name, 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'Seen {mac} ({mac_name}) at {location} since {ts}' + 'Seen %s (%s) at %s since %s', + mac, mac_name, location, ts ) tiebreaks[location] = ts ( 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.2 - ) # Note: list most important devices first + logger.debug('%s: %s gets %d votes.', name, most_recent_location, bonus) + credit = int(credit * 0.2) # Note: list most important devices first if credit <= 0: credit = 1 if len(votes) > 0: (location, value) = dict_utils.item_with_max_value(votes) if value > 2001: + assert location return location return Location.UNKNOWN