4 from collections import defaultdict
8 from typing import Dict, List
10 # Note: this module is fairly early loaded. Be aware of dependencies.
15 logger = logging.getLogger(__name__)
17 cfg = config.add_commandline_args(
18 f"Presence Detection ({__file__})",
19 "Args related to detection of human beings in locations.",
22 "--presence_macs_file",
23 type=argparse_utils.valid_filename,
24 default = "/home/scott/cron/persisted_mac_addresses.txt",
26 help="The location of persisted_mac_addresses.txt to use."
30 class Person(enum.Enum):
41 class Location(enum.Enum):
47 class PresenceDetection(object):
48 def __init__(self) -> None:
49 # Note: list most important devices first.
50 self.devices_by_person: Dict[Person, List[str]] = {
52 "3C:28:6D:10:6D:41", # pixel3
53 "6C:40:08:AE:DC:2E", # laptop
54 # "D4:61:2E:88:18:09", # watch
55 # "14:7D:DA:6A:20:D7", # work laptop
65 Person.AARON_AND_DANA: [
78 self.weird_mac_at_cabin = False
79 self.location_ts_by_mac: Dict[
80 Location, Dict[str, datetime.datetime]
82 self.names_by_mac: Dict[str, str] = {}
85 def update(self) -> None:
86 from exec_utils import cmd
88 persisted_macs = config.config['presence_macs_file']
90 persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
91 self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
95 self.parse_raw_macs_file(raw, Location.CABIN)
97 def read_persisted_macs_file(
98 self, filename: str, location: Location
100 if location is Location.UNKNOWN:
102 with open(filename, "r") as rf:
104 self.parse_raw_macs_file(lines, location)
106 def parse_raw_macs_file(self, raw: str, location: Location) -> None:
107 lines = raw.split("\n")
109 # CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
115 logger.debug(f'{location}> {line}')
118 if location == Location.CABIN:
119 logger.debug('Cabin count: {cabin_count}')
122 (mac, count, ip_name, mfg, ts) = line.split(",")
123 except Exception as e:
124 logger.error(f'SKIPPED BAD LINE> {line}')
128 (self.location_ts_by_mac[location])[
130 ] = datetime.datetime.fromtimestamp(int(ts.strip()))
131 ip_name = ip_name.strip()
132 match = re.match(r"(\d+\.\d+\.\d+\.\d+) +\(([^\)]+)\)", ip_name)
133 if match is not None:
134 name = match.group(2)
135 self.names_by_mac[mac] = name
137 logger.debug('Weird MAC at the cabin')
138 self.weird_mac_at_cabin = True
140 def is_anyone_in_location_now(self, location: Location) -> bool:
141 for person in Person:
142 if person is not None:
143 loc = self.where_is_person_now(person)
146 if location == location.CABIN and self.weird_mac_at_cabin:
150 def where_is_person_now(self, name: Person) -> Location:
153 if name is Person.UNKNOWN:
154 if self.weird_mac_at_cabin:
155 return Location.CABIN
157 return Location.UNKNOWN
158 votes: Dict[Location, int] = {}
159 tiebreaks: Dict[Location, datetime.datetime] = {}
161 for mac in self.devices_by_person[name]:
162 logger.debug(f'Looking for {name}... check for mac {mac}')
163 if mac not in self.names_by_mac:
165 for location in self.location_ts_by_mac:
166 if mac in self.location_ts_by_mac[location]:
167 ts = (self.location_ts_by_mac[location])[mac]
168 logger.debug(f'I saw {mac} at {location} at {ts}')
169 tiebreaks[location] = ts
170 location = dict_utils.key_with_min_value(tiebreaks)
171 v = votes.get(location, 0)
172 votes[location] = v + credit
173 logger.debug(f'{name}: {location} gets {credit} votes.')
176 ) # Note: list most important devices first
180 item = dict_utils.item_with_max_value(votes)
182 return Location.UNKNOWN
185 @bootstrap.initialize
187 p = PresenceDetection()
188 for person in Person:
189 print(f'{person} => {p.where_is_person_now(person)}')
191 for location in Location:
192 print(f'{location} => {p.is_anyone_in_location_now(location)}')
195 if __name__ == '__main__':