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]] = {
51 "3C:28:6D:10:6D:41", # pixel3
52 "6C:40:08:AE:DC:2E", # laptop
53 # "D4:61:2E:88:18:09", # watch
54 # "14:7D:DA:6A:20:D7", # work laptop
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
87 persisted_macs = config.config['presence_macs_file']
89 persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
90 self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
94 self.parse_raw_macs_file(raw, Location.CABIN)
96 def read_persisted_macs_file(
97 self, filename: str, location: Location
99 if location is Location.UNKNOWN:
101 with open(filename, "r") as rf:
103 self.parse_raw_macs_file(lines, location)
105 def parse_raw_macs_file(self, raw: str, location: Location) -> None:
106 lines = raw.split("\n")
108 # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
114 logger.debug(f'{location}> {line}')
117 if location == Location.CABIN:
118 logger.debug('Cabin count: {cabin_count}')
121 (mac, count, ip_name, mfg, ts) = line.split(",")
122 except Exception as e:
123 logger.error(f'SKIPPED BAD LINE> {line}')
127 (self.location_ts_by_mac[location])[
129 ] = datetime.datetime.fromtimestamp(int(ts.strip()))
130 ip_name = ip_name.strip()
131 match = re.match(r"(\d+\.\d+\.\d+\.\d+) +\(([^\)]+)\)", ip_name)
132 if match is not None:
133 name = match.group(2)
134 self.names_by_mac[mac] = name
136 logger.debug('Weird MAC at the cabin')
137 self.weird_mac_at_cabin = True
139 def is_anyone_in_location_now(self, location: Location) -> bool:
140 for person in Person:
141 if person is not None:
142 loc = self.where_is_person_now(person)
145 if location == location.CABIN and self.weird_mac_at_cabin:
149 def where_is_person_now(self, name: Person) -> Location:
152 if name is Person.UNKNOWN:
153 if self.weird_mac_at_cabin:
154 return Location.CABIN
156 return Location.UNKNOWN
157 votes: Dict[Location, int] = {}
158 tiebreaks: Dict[Location, datetime.datetime] = {}
160 for mac in self.devices_by_person[name]:
161 logger.debug(f'Looking for {name}... check for mac {mac}')
162 if mac not in self.names_by_mac:
164 for location in self.location_ts_by_mac:
165 if mac in self.location_ts_by_mac[location]:
166 ts = (self.location_ts_by_mac[location])[mac]
167 logger.debug(f'I saw {mac} at {location} at {ts}')
168 tiebreaks[location] = ts
169 location = dict_utils.key_with_min_value(tiebreaks)
170 v = votes.get(location, 0)
171 votes[location] = v + credit
172 logger.debug(f'{name}: {location} gets {credit} votes.')
175 ) # Note: list most important devices first
179 item = dict_utils.item_with_max_value(votes)
181 return Location.UNKNOWN
184 @bootstrap.initialize
186 p = PresenceDetection()
187 for person in Person:
188 print(f'{person} => {p.where_is_person_now(person)}')
190 for location in Location:
191 print(f'{location} => {p.is_anyone_in_location_now(location)}')
194 if __name__ == '__main__':