4 from collections import defaultdict
8 from typing import Dict, List
14 logger = logging.getLogger(__name__)
16 cfg = config.add_commandline_args(
17 f"Presence Detection ({__file__})",
18 "Args related to detection of human beings in locations.",
21 "--presence_macs_file",
22 type=argparse_utils.valid_filename,
23 default = "/home/scott/cron/persisted_mac_addresses.txt",
25 help="The location of persisted_mac_addresses.txt to use."
29 class Person(enum.Enum):
40 class Location(enum.Enum):
46 class PresenceDetection(object):
47 def __init__(self) -> None:
48 # Note: list most important devices first.
49 self.devices_by_person: Dict[Person, List[str]] = {
64 Person.AARON_AND_DANA: [
77 self.weird_mac_at_cabin = False
78 self.location_ts_by_mac: Dict[
79 Location, Dict[str, datetime.datetime]
81 self.names_by_mac: Dict[str, str] = {}
84 def update(self) -> None:
85 from exec_utils import cmd
86 persisted_macs = config.config['presence_macs_file']
87 self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
91 self.parse_raw_macs_file(raw, Location.CABIN)
93 def read_persisted_macs_file(
94 self, filename: str, location: Location
96 if location is Location.UNKNOWN:
98 with open(filename, "r") as rf:
100 self.parse_raw_macs_file(lines, location)
102 def parse_raw_macs_file(self, raw: str, location: Location) -> None:
103 lines = raw.split("\n")
105 # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
111 logger.debug(f'{location}> {line}')
114 if location == Location.CABIN:
115 logger.debug('Cabin count: {cabin_count}')
118 (mac, count, ip_name, mfg, ts) = line.split(",")
119 except Exception as e:
120 logger.error(f'SKIPPED BAD LINE> {line}')
124 (self.location_ts_by_mac[location])[
126 ] = datetime.datetime.fromtimestamp(int(ts.strip()))
127 ip_name = ip_name.strip()
128 match = re.match(r"(\d+\.\d+\.\d+\.\d+) +\(([^\)]+)\)", ip_name)
129 if match is not None:
130 name = match.group(2)
131 self.names_by_mac[mac] = name
133 logger.debug('Weird MAC at the cabin')
134 self.weird_mac_at_cabin = True
136 def is_anyone_in_location_now(self, location: Location) -> bool:
137 for person in Person:
138 if person is not None:
139 loc = self.where_is_person_now(person)
142 if location == location.CABIN and self.weird_mac_at_cabin:
146 def where_is_person_now(self, name: Person) -> Location:
149 if name is Person.UNKNOWN:
150 if self.weird_mac_at_cabin:
151 return Location.CABIN
153 return Location.UNKNOWN
154 votes: Dict[Location, int] = {}
155 tiebreaks: Dict[Location, datetime.datetime] = {}
157 for mac in self.devices_by_person[name]:
158 logger.debug(f'Looking for {name}... check for mac {mac}')
159 if mac not in self.names_by_mac:
161 for location in self.location_ts_by_mac:
162 if mac in self.location_ts_by_mac[location]:
163 ts = (self.location_ts_by_mac[location])[mac]
164 logger.debug(f'I saw {mac} at {location} at {ts}')
165 tiebreaks[location] = ts
166 location = dict_utils.key_with_min_value(tiebreaks)
167 v = votes.get(location, 0)
168 votes[location] = v + credit
169 logger.debug('{name}: {location} gets {credit} votes.')
172 ) # Note: list most important devices first
176 item = dict_utils.item_with_max_value(votes)
178 return Location.UNKNOWN
181 @bootstrap.initialize
183 p = PresenceDetection()
184 for person in Person:
185 print(f'{person} => {p.where_is_person_now(person)}')
187 for location in Location:
188 print(f'{location} => {p.is_anyone_in_location_now(location)}')
191 if __name__ == '__main__':