X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=presence.py;h=682855d7491245a232e2d1ec3414e67eef5dae2a;hb=c79ecbf708a63a54a9c3e8d189b65d4794930082;hp=bd931f63933c553a67fc970da6478cb19e03eff9;hpb=497fb9e21f45ec08e1486abaee6dfa7b20b8a691;p=python_utils.git diff --git a/presence.py b/presence.py index bd931f6..682855d 100644 --- a/presence.py +++ b/presence.py @@ -5,14 +5,10 @@ from collections import defaultdict import enum import logging import re -import sys from typing import Dict, List import argparse_utils -import bootstrap import config -import dict_utils -import exec_utils logger = logging.getLogger(__name__) @@ -77,17 +73,21 @@ class PresenceDetection(object): "96:69:2C:88:7A:C3", ], } + self.weird_mac_at_cabin = False self.location_ts_by_mac: Dict[ Location, Dict[str, datetime.datetime] ] = defaultdict(dict) self.names_by_mac: Dict[str, str] = {} + self.update() + + def update(self) -> None: + from exec_utils import cmd persisted_macs = config.config['presence_macs_file'] self.read_persisted_macs_file(persisted_macs, Location.HOUSE) - raw = exec_utils.cmd( + raw = cmd( "ssh scott@meerkat.cabin 'cat /home/scott/cron/persisted_mac_addresses.txt'" ) self.parse_raw_macs_file(raw, Location.CABIN) - # os.remove(filename) def read_persisted_macs_file( self, filename: str, location: Location @@ -102,11 +102,16 @@ class PresenceDetection(object): lines = raw.split("\n") # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990 + cabin_count = 0 for line in lines: line = line.strip() if len(line) == 0: continue logger.debug(f'{location}> {line}') + if "cabin_" in line: + continue + if location == Location.CABIN: + cabin_count += 1 try: (mac, count, ip_name, mfg, ts) = line.split(",") except Exception as e: @@ -122,6 +127,8 @@ class PresenceDetection(object): if match is not None: name = match.group(2) self.names_by_mac[mac] = name + if cabin_count > 0: + self.weird_mac_at_cabin = True def is_anyone_in_location_now(self, location: Location) -> bool: for person in Person: @@ -129,11 +136,18 @@ class PresenceDetection(object): loc = self.where_is_person_now(person) if location == loc: return True + if location == location.CABIN and self.weird_mac_at_cabin: + return True return False def where_is_person_now(self, name: Person) -> Location: + import dict_utils + if name is Person.UNKNOWN: - return Location.UNKNOWN + if self.weird_mac_at_cabin: + return Location.CABIN + else: + return Location.UNKNOWN votes: Dict[Location, int] = {} tiebreaks: Dict[Location, datetime.datetime] = {} credit = 10000 @@ -150,26 +164,9 @@ class PresenceDetection(object): credit = int( credit * 0.667 ) # Note: list most important devices first - if credit == 0: + if credit <= 0: credit = 1 if len(votes) > 0: item = dict_utils.item_with_max_value(votes) return item[0] return Location.UNKNOWN - - -@bootstrap.initialize -def main() -> None: - config.parse() - p = PresenceDetection() - - for loc in Location: - print(f'{loc}: {p.is_anyone_in_location_now(loc)}') - - for u in Person: - print(f'{u}: {p.where_is_person_now(u)}') - sys.exit(0) - - -if __name__ == '__main__': - main()