forked from vinnyspb/nexa-controller-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresence_controller.py
More file actions
66 lines (56 loc) · 2.82 KB
/
presence_controller.py
File metadata and controls
66 lines (56 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import httplib
import base64
import time
from datetime import datetime
class PresenceController:
def __init__(self, router_host, router_uri, router_username, router_password, monitored_mac_addresses,
search_prefix_for_mac, search_suffix_for_mac):
self._router_host = router_host
self._router_uri = router_uri
self._router_username = router_username
self._router_password = router_password
self._search_prefix_for_mac = search_prefix_for_mac
self._search_suffix_for_mac = search_suffix_for_mac
# dict MAC address -> last seen timestamp
self._monitored_mac_addresses = dict(zip(monitored_mac_addresses,
[datetime.fromtimestamp(0)]*len(monitored_mac_addresses)))
def _check_connected_devices(self):
# base64 encode the username and password
auth = base64.encodestring('%s:%s' % (self._router_username, self._router_password)).replace('\n', '')
webservice = httplib.HTTP(self._router_host)
# write your headers
webservice.putrequest("GET", self._router_uri)
webservice.putheader("Host", self._router_host)
webservice.putheader("User-Agent", "NEXA-Controller")
webservice.putheader("Content-type", "text/html; charset=\"UTF-8\"")
webservice.putheader("Content-length", "0")
# write the Authorization header like: 'Basic base64encode(username + ':' + password)
webservice.putheader("Authorization", "Basic %s" % auth)
webservice.endheaders()
# get the response
statuscode, statusmessage, header = webservice.getreply()
if statuscode != 200:
raise Exception("Can't connect to router: " + statusmessage)
router_web_page = webservice.getfile().read()
for device in self._monitored_mac_addresses.keys():
search_for_message = self._search_prefix_for_mac + device + self._search_suffix_for_mac
if search_for_message in router_web_page:
self._monitored_mac_addresses[device] = datetime.now()
def dispatch(self):
successful_result = False
while not successful_result:
try:
self._check_connected_devices()
successful_result = True
except Exception as e:
print str(e)
print "Sleeping 60 seconds..."
time.sleep(60)
current_time = datetime.now()
for device in self._monitored_mac_addresses.keys():
time_diff = current_time - self._monitored_mac_addresses[device]
if time_diff.total_seconds() < 5*60:
return True
print "PresenceController: No devices from the list are alive for the last 5 minutes, disabling:" +\
str(self._monitored_mac_addresses)
return False