4 from collections import defaultdict
9 from typing import Dict, List
17 logger = logging.getLogger(__name__)
19 cfg = config.add_commandline_args(
20 f"Presence Detection ({__file__})",
21 "Args related to detection of human beings in locations.",
24 "--presence_macs_file",
25 type=argparse_utils.valid_filename,
26 default = "/home/scott/cron/persisted_mac_addresses.txt",
28 help="The location of persisted_mac_addresses.txt to use."
32 class Person(enum.Enum):
43 class Location(enum.Enum):
49 class PresenceDetection(object):
50 def __init__(self) -> None:
51 # Note: list most important devices first.
52 self.devices_by_person: Dict[Person, List[str]] = {
67 Person.AARON_AND_DANA: [
80 self.weird_mac_at_cabin = False
81 self.location_ts_by_mac: Dict[
82 Location, Dict[str, datetime.datetime]
84 self.names_by_mac: Dict[str, str] = {}
85 persisted_macs = config.config['presence_macs_file']
86 self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
90 self.parse_raw_macs_file(raw, Location.CABIN)
92 def read_persisted_macs_file(
93 self, filename: str, location: Location
95 if location is Location.UNKNOWN:
97 with open(filename, "r") as rf:
99 self.parse_raw_macs_file(lines, location)
101 def parse_raw_macs_file(self, raw: str, location: Location) -> None:
102 lines = raw.split("\n")
104 # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
110 logger.debug(f'{location}> {line}')
113 if location == Location.CABIN:
116 (mac, count, ip_name, mfg, ts) = line.split(",")
117 except Exception as e:
118 logger.error(f'SKIPPED BAD LINE> {line}')
122 (self.location_ts_by_mac[location])[
124 ] = datetime.datetime.fromtimestamp(int(ts.strip()))
125 ip_name = ip_name.strip()
126 match = re.match(r"(\d+\.\d+\.\d+\.\d+) +\(([^\)]+)\)", ip_name)
127 if match is not None:
128 name = match.group(2)
129 self.names_by_mac[mac] = name
131 self.weird_mac_at_cabin = True
133 def is_anyone_in_location_now(self, location: Location) -> bool:
134 for person in Person:
135 if person is not None:
136 loc = self.where_is_person_now(person)
139 if location == location.CABIN and self.weird_mac_at_cabin:
143 def where_is_person_now(self, name: Person) -> Location:
144 if name is Person.UNKNOWN:
145 if self.weird_mac_at_cabin:
146 return Location.CABIN
148 return Location.UNKNOWN
149 votes: Dict[Location, int] = {}
150 tiebreaks: Dict[Location, datetime.datetime] = {}
152 for mac in self.devices_by_person[name]:
153 if mac not in self.names_by_mac:
155 for location in self.location_ts_by_mac:
156 if mac in self.location_ts_by_mac[location]:
157 ts = (self.location_ts_by_mac[location])[mac]
158 tiebreaks[location] = ts
159 location = dict_utils.key_with_min_value(tiebreaks)
160 v = votes.get(location, 0)
161 votes[location] = v + credit
164 ) # Note: list most important devices first
168 item = dict_utils.item_with_max_value(votes)
170 return Location.UNKNOWN
173 @bootstrap.initialize
176 p = PresenceDetection()
179 print(f'{loc}: {p.is_anyone_in_location_now(loc)}')
182 print(f'{u}: {p.where_is_person_now(u)}')
186 if __name__ == '__main__':