X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;ds=inline;f=presence.py;h=b310183b4da6c1fdf002bee0c559abd9a9fd0ca1;hb=2c54bfde335f3631f045a871c540c9d63c5bb081;hp=682855d7491245a232e2d1ec3414e67eef5dae2a;hpb=11eeb8574b7b4620ac6fd440cb251f8aa2458f5b;p=python_utils.git diff --git a/presence.py b/presence.py old mode 100644 new mode 100755 index 682855d..b310183 --- a/presence.py +++ b/presence.py @@ -7,7 +7,9 @@ import logging import re from typing import Dict, List +# Note: this module is fairly early loaded. Be aware of dependencies. import argparse_utils +import bootstrap import config logger = logging.getLogger(__name__) @@ -47,10 +49,10 @@ class PresenceDetection(object): # Note: list most important devices first. self.devices_by_person: Dict[Person, List[str]] = { Person.SCOTT: [ - "3C:28:6D:10:6D:41", - "D4:61:2E:88:18:09", - "6C:40:08:AE:DC:2E", - "14:7D:DA:6A:20:D7", + "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", @@ -82,7 +84,10 @@ class PresenceDetection(object): def update(self) -> None: from exec_utils import cmd - persisted_macs = config.config['presence_macs_file'] + 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'" @@ -111,6 +116,7 @@ class PresenceDetection(object): if "cabin_" in line: continue if location == Location.CABIN: + logger.debug('Cabin count: {cabin_count}') cabin_count += 1 try: (mac, count, ip_name, mfg, ts) = line.split(",") @@ -128,6 +134,7 @@ class PresenceDetection(object): name = match.group(2) self.names_by_mac[mac] = name if cabin_count > 0: + logger.debug('Weird MAC at the cabin') self.weird_mac_at_cabin = True def is_anyone_in_location_now(self, location: Location) -> bool: @@ -152,15 +159,18 @@ 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 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}') 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.') credit = int( credit * 0.667 ) # Note: list most important devices first @@ -170,3 +180,17 @@ class PresenceDetection(object): item = dict_utils.item_with_max_value(votes) return item[0] return Location.UNKNOWN + + +@bootstrap.initialize +def main() -> None: + p = PresenceDetection() + for person in Person: + print(f'{person} => {p.where_is_person_now(person)}') + print() + for location in Location: + print(f'{location} => {p.is_anyone_in_location_now(location)}') + + +if __name__ == '__main__': + main()