projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix dimmer level JSON command.
[python_utils.git]
/
base_presence.py
diff --git
a/base_presence.py
b/base_presence.py
index ad852f9f7ebce82d5a76c6d7f1436a78670489e7..8be4d936d7a5e1e9be2027e8b3ad5b7ea47b98de 100755
(executable)
--- a/
base_presence.py
+++ b/
base_presence.py
@@
-1,5
+1,10
@@
#!/usr/bin/env python3
#!/usr/bin/env python3
+"""This is a module dealing with trying to guess a person's location
+based on the location of certain devices (e.g. phones, laptops)
+belonging to that person. It works with networks I run that log
+device MAC addresses active."""
+
import datetime
import logging
import re
import datetime
import logging
import re
@@
-38,6
+43,9
@@
cfg.add_argument(
class PresenceDetection(object):
class PresenceDetection(object):
+ """See above. This is a base class for determining a person's
+ location on networks I administer."""
+
def __init__(self) -> None:
# Note: list most important devices first.
self.devices_by_person: Dict[Person, List[str]] = {
def __init__(self) -> None:
# Note: list most important devices first.
self.devices_by_person: Dict[Person, List[str]] = {
@@
-67,11
+75,9
@@
class PresenceDetection(object):
],
}
self.run_location = site_config.get_location()
],
}
self.run_location = site_config.get_location()
- logger.debug(
f"run_location is {self.run_location}"
)
+ logger.debug(
"base_presence run_location is %s", self.run_location
)
self.weird_mac_at_cabin = False
self.weird_mac_at_cabin = False
- self.location_ts_by_mac: Dict[
- Location, Dict[str, datetime.datetime]
- ] = defaultdict(dict)
+ self.location_ts_by_mac: Dict[Location, Dict[str, datetime.datetime]] = defaultdict(dict)
self.names_by_mac: Dict[str, str] = {}
self.dark_locations: Set[Location] = set()
self.last_update: Optional[datetime.datetime] = None
self.names_by_mac: Dict[str, str] = {}
self.dark_locations: Set[Location] = set()
self.last_update: Optional[datetime.datetime] = None
@@
-87,7
+93,7
@@
class PresenceDetection(object):
> config.config['presence_tolerable_staleness_seconds'].total_seconds()
):
logger.debug(
> config.config['presence_tolerable_staleness_seconds'].total_seconds()
):
logger.debug(
- f"It's been {delta.total_seconds()}s since last update; refreshing now."
+ "It's been %ss since last update; refreshing now.", delta.total_seconds()
)
self.update()
)
self.update()
@@
-159,17
+165,17
@@
class PresenceDetection(object):
line = line.strip()
if len(line) == 0:
continue
line = line.strip()
if len(line) == 0:
continue
- logger.debug(
f'{location}> {line}'
)
+ logger.debug(
'%s> %s', location, line
)
if "cabin_" in line:
continue
if location == Location.CABIN:
if "cabin_" in line:
continue
if location == Location.CABIN:
- logger.debug(
f'Cabin count: {cabin_count}'
)
+ logger.debug(
'Cabin count: %d', cabin_count
)
cabin_count += 1
try:
cabin_count += 1
try:
- (mac,
count, ip_name, mfg, ts) = line.split(",")
+ (mac,
_, ip_name, _, ts) = line.split(",") # type: ignore
except Exception as e:
except Exception as e:
- logger.error(f'SKIPPED BAD LINE> {line}')
logger.exception(e)
logger.exception(e)
+ logger.error('SKIPPED BAD LINE> %s', line)
continue
mac = mac.strip()
(self.location_ts_by_mac[location])[mac] = datetime.datetime.fromtimestamp(
continue
mac = mac.strip()
(self.location_ts_by_mac[location])[mac] = datetime.datetime.fromtimestamp(
@@
-200,12
+206,10
@@
class PresenceDetection(object):
def where_is_person_now(self, name: Person) -> Location:
self.maybe_update()
if len(self.dark_locations) > 0:
def where_is_person_now(self, name: Person) -> Location:
self.maybe_update()
if len(self.dark_locations) > 0:
- msg = (
- f"Can't see {self.dark_locations} right now; answer confidence impacted"
- )
+ msg = f"Can't see {self.dark_locations} right now; answer confidence impacted"
logger.warning(msg)
warnings.warn(msg, stacklevel=2)
logger.warning(msg)
warnings.warn(msg, stacklevel=2)
- logger.debug(
f'Looking for {name}...'
)
+ logger.debug(
'Looking for %s...', name
)
if name is Person.UNKNOWN:
if self.weird_mac_at_cabin:
if name is Person.UNKNOWN:
if self.weird_mac_at_cabin:
@@
-218,31
+222,33
@@
class PresenceDetection(object):
votes: Dict[Location, int] = {}
tiebreaks: Dict[Location, datetime.datetime] = {}
credit = 10000
votes: Dict[Location, int] = {}
tiebreaks: Dict[Location, datetime.datetime] = {}
credit = 10000
+ location = None
for mac in self.devices_by_person[name]:
if mac not in self.names_by_mac:
continue
mac_name = self.names_by_mac[mac]
for mac in self.devices_by_person[name]:
if mac not in self.names_by_mac:
continue
mac_name = self.names_by_mac[mac]
- logger.debug(
f'Looking for {name}... check for mac {mac} ({mac_name})'
)
+ logger.debug(
'Looking for %s... check for mac %s (%s)', name, mac, mac_name
)
for location in self.location_ts_by_mac:
if mac in self.location_ts_by_mac[location]:
ts = (self.location_ts_by_mac[location])[mac]
for location in self.location_ts_by_mac:
if mac in self.location_ts_by_mac[location]:
ts = (self.location_ts_by_mac[location])[mac]
- logger.debug(
f'Seen {mac} ({mac_name}) at {location} since {ts}'
)
+ logger.debug(
'Seen %s (%s) at %s since %s', mac, mac_name, location, ts
)
tiebreaks[location] = ts
(
most_recent_location,
tiebreaks[location] = ts
(
most_recent_location,
-
first_seen_ts
,
+
_
,
) = dict_utils.item_with_max_value(tiebreaks)
bonus = credit
v = votes.get(most_recent_location, 0)
votes[most_recent_location] = v + bonus
) = dict_utils.item_with_max_value(tiebreaks)
bonus = credit
v = votes.get(most_recent_location, 0)
votes[most_recent_location] = v + bonus
- logger.debug(
f'{name}: {location} gets {bonus} votes.'
)
+ logger.debug(
'%s: %s gets %d votes.', name, most_recent_location, bonus
)
credit = int(credit * 0.2) # Note: list most important devices first
if credit <= 0:
credit = 1
if len(votes) > 0:
(location, value) = dict_utils.item_with_max_value(votes)
if value > 2001:
credit = int(credit * 0.2) # Note: list most important devices first
if credit <= 0:
credit = 1
if len(votes) > 0:
(location, value) = dict_utils.item_with_max_value(votes)
if value > 2001:
+ assert location
return location
return Location.UNKNOWN
return location
return Location.UNKNOWN
@@
-253,10
+259,8
@@
def main() -> None:
for person in Person:
print(f'{person} => {p.where_is_person_now(person)}')
print()
for person in Person:
print(f'{person} => {p.where_is_person_now(person)}')
print()
-
-
-# for location in Location:
-# print(f'{location} => {p.is_anyone_in_location_now(location)}')
+ for location in Location:
+ print(f'{location} => {p.is_anyone_in_location_now(location)}')
if __name__ == '__main__':
if __name__ == '__main__':