-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBeacon.py
64 lines (58 loc) · 2.19 KB
/
Beacon.py
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
from wsgiref.simple_server import make_server
import os
import urlparse
class RequestMatcher(object):
def __init__(self, paths, methods):
self.paths = paths
self.methods = methods
def check_request(self, request):
return (request.path in self.paths) and (request.method in self.methods)
class RequestBridge(object):
def __init__(self, environment):
self.path = environment["PATH_INFO"]
self.method = environment["REQUEST_METHOD"]
self.query_string = environment["QUERY_STRING"]
def router(environment, start_response):
global enabled
response = ""
request = RequestBridge(environment)
status_matcher = RequestMatcher(["/status"], ["GET"])
enable_matcher = RequestMatcher(["/enable"], ["GET", "POST"])
disable_matcher = RequestMatcher(["/disable"], ["GET", "POST"])
update_matcher = RequestMatcher(["/update"], ["GET"])
if status_matcher.check_request(request):
if enabled:
response = "Enabled"
else:
response = "Disabled"
elif enable_matcher.check_request(request):
os.system("sudo hciconfig hci0 leadv")
enabled = True
response = "Success"
elif disable_matcher.check_request(request):
os.system("sudo hciconfig hci0 noleadv")
enabled = False
response = "Success"
elif update_matcher.check_request(request):
query_dict = dict(urlparse.parse_qsl(request.query_string))
major_file = open("/home/pi/PiBeacon/Major.txt", "w")
minor_file = open("/home/pi/PiBeacon/Minor.txt", "w")
major_file.write(query_dict["major"][:2] + " " + query_dict["major"][2:])
minor_file.write(query_dict["minor"][:2] + " " + query_dict["minor"][2:])
major_file.close()
minor_file.close()
set_packet_data()
response = "Success"
start_response("200 OK", [("Content-type", "text/plain")])
return response
def set_packet_data():
major_file = open("/home/pi/PiBeacon/Major.txt", "r")
minor_file = open("/home/pi/PiBeacon/Minor.txt", "r")
os.system("sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 61 84 96 F1 C2 0A 4E 8F BA 2A A0 0C CE E4 45 65 " + major_file.read() + " " + minor_file.read() + " C8 00")
major_file.close()
minor_file.close()
set_packet_data()
os.system("sudo hciconfig hci0 leadv")
global enabled
enabled = True
make_server("", 80, router).serve_forever()