projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Adds Arper, cleans up other stuff.
[python_utils.git]
/
presence.py
diff --git
a/presence.py
b/presence.py
old mode 100644
(file)
new mode 100755
(executable)
index
bd931f6
..
e5bc64f
--- a/
presence.py
+++ b/
presence.py
@@
-5,14
+5,11
@@
from collections import defaultdict
import enum
import logging
import re
import enum
import logging
import re
-import sys
from typing import Dict, List
import argparse_utils
import bootstrap
import config
from typing import Dict, List
import argparse_utils
import bootstrap
import config
-import dict_utils
-import exec_utils
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
@@
-51,10
+48,10
@@
class PresenceDetection(object):
# Note: list most important devices first.
self.devices_by_person: Dict[Person, List[str]] = {
Person.SCOTT: [
# Note: list most important devices first.
self.devices_by_person: Dict[Person, List[str]] = {
Person.SCOTT: [
- "3C:28:6D:10:6D:41",
- "
D4:61:2E:88:18:09",
- "6C:40:08:AE:DC:2E",
- "14:7D:DA:6A:20:D7",
+ "3C:28:6D:10:6D:41",
# pixel3
+ "
6C:40:08:AE:DC:2E", # laptop
+# "D4:61:2E:88:18:09", # watch
+# "14:7D:DA:6A:20:D7", # work laptop
],
Person.LYNN: [
"08:CC:27:63:26:14",
],
Person.LYNN: [
"08:CC:27:63:26:14",
@@
-77,17
+74,24
@@
class PresenceDetection(object):
"96:69:2C:88:7A:C3",
],
}
"96:69:2C:88:7A:C3",
],
}
+ self.weird_mac_at_cabin = False
self.location_ts_by_mac: Dict[
Location, Dict[str, datetime.datetime]
] = defaultdict(dict)
self.names_by_mac: Dict[str, str] = {}
self.location_ts_by_mac: Dict[
Location, Dict[str, datetime.datetime]
] = defaultdict(dict)
self.names_by_mac: Dict[str, str] = {}
- persisted_macs = config.config['presence_macs_file']
+ self.update()
+
+ def update(self) -> None:
+ from exec_utils import cmd
+ try:
+ persisted_macs = config.config['presence_macs_file']
+ except KeyError:
+ persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
- raw =
exec_utils.
cmd(
+ raw = cmd(
"ssh
[email protected]
'cat /home/scott/cron/persisted_mac_addresses.txt'"
)
self.parse_raw_macs_file(raw, Location.CABIN)
"ssh
[email protected]
'cat /home/scott/cron/persisted_mac_addresses.txt'"
)
self.parse_raw_macs_file(raw, Location.CABIN)
- # os.remove(filename)
def read_persisted_macs_file(
self, filename: str, location: Location
def read_persisted_macs_file(
self, filename: str, location: Location
@@
-102,11
+106,17
@@
class PresenceDetection(object):
lines = raw.split("\n")
# CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
lines = raw.split("\n")
# CC:F4:11:D7:FA:EE, 2240, 10.0.0.22 (side_deck_high_home), Google, 1611681990
+ cabin_count = 0
for line in lines:
line = line.strip()
if len(line) == 0:
continue
logger.debug(f'{location}> {line}')
for line in lines:
line = line.strip()
if len(line) == 0:
continue
logger.debug(f'{location}> {line}')
+ if "cabin_" in line:
+ continue
+ if location == Location.CABIN:
+ logger.debug('Cabin count: {cabin_count}')
+ cabin_count += 1
try:
(mac, count, ip_name, mfg, ts) = line.split(",")
except Exception as e:
try:
(mac, count, ip_name, mfg, ts) = line.split(",")
except Exception as e:
@@
-122,6
+132,9
@@
class PresenceDetection(object):
if match is not None:
name = match.group(2)
self.names_by_mac[mac] = name
if match is not None:
name = match.group(2)
self.names_by_mac[mac] = name
+ if cabin_count > 0:
+ logger.debug('Weird MAC at the cabin')
+ self.weird_mac_at_cabin = True
def is_anyone_in_location_now(self, location: Location) -> bool:
for person in Person:
def is_anyone_in_location_now(self, location: Location) -> bool:
for person in Person:
@@
-129,28
+142,38
@@
class PresenceDetection(object):
loc = self.where_is_person_now(person)
if location == loc:
return True
loc = self.where_is_person_now(person)
if location == loc:
return True
+ if location == location.CABIN and self.weird_mac_at_cabin:
+ return True
return False
def where_is_person_now(self, name: Person) -> Location:
return False
def where_is_person_now(self, name: Person) -> Location:
+ import dict_utils
+
if name is Person.UNKNOWN:
if name is Person.UNKNOWN:
- return Location.UNKNOWN
+ if self.weird_mac_at_cabin:
+ return Location.CABIN
+ else:
+ return Location.UNKNOWN
votes: Dict[Location, int] = {}
tiebreaks: Dict[Location, datetime.datetime] = {}
credit = 10000
for mac in self.devices_by_person[name]:
votes: Dict[Location, int] = {}
tiebreaks: Dict[Location, datetime.datetime] = {}
credit = 10000
for mac in self.devices_by_person[name]:
+ logger.debug(f'Looking for {name}... check for mac {mac}')
if mac not in self.names_by_mac:
continue
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]
if mac not in self.names_by_mac:
continue
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'I saw {mac} at {location} at {ts}')
tiebreaks[location] = ts
location = dict_utils.key_with_min_value(tiebreaks)
v = votes.get(location, 0)
votes[location] = v + credit
tiebreaks[location] = ts
location = dict_utils.key_with_min_value(tiebreaks)
v = votes.get(location, 0)
votes[location] = v + credit
+ logger.debug(f'{name}: {location} gets {credit} votes.')
credit = int(
credit * 0.667
) # Note: list most important devices first
credit = int(
credit * 0.667
) # Note: list most important devices first
- if credit
=
= 0:
+ if credit
<
= 0:
credit = 1
if len(votes) > 0:
item = dict_utils.item_with_max_value(votes)
credit = 1
if len(votes) > 0:
item = dict_utils.item_with_max_value(votes)
@@
-160,15
+183,12
@@
class PresenceDetection(object):
@bootstrap.initialize
def main() -> None:
@bootstrap.initialize
def main() -> None:
- config.parse()
p = PresenceDetection()
p = PresenceDetection()
-
- for loc in Location:
- print(f'{loc}: {p.is_anyone_in_location_now(loc)}')
-
- for u in Person:
- print(f'{u}: {p.where_is_person_now(u)}')
- sys.exit(0)
+ 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)}')
if __name__ == '__main__':
if __name__ == '__main__':