-
Notifications
You must be signed in to change notification settings - Fork 7
/
remoteSensors.py
65 lines (45 loc) · 1.65 KB
/
remoteSensors.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
65
import android, time, BaseHTTPServer, urlparse
print "\nStarting sensors...\n"
droid = android.Android()
droid.startSensing()
time.sleep(2)
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
# Disable logging DNS lookups
def address_string(self):
return str(self.client_address[0])
def do_GET(self):
url = urlparse.urlparse(self.path)
params = urlparse.parse_qs(url.query)
delay = 1
if 'delay' in params:
delay = float(params['delay'][0])
self.send_response(200)
self.send_header("Content-type", "application/x-javascript; charset=utf-8")
self.end_headers()
try:
while True:
sensors = droid.readSensors()
data = sensors['result']
if 'jsonp' in params:
self.wfile.write("%s(" % (params['jsonp'][0]))
output = []
for type in data:
output.append("\"%s\": \"%s\"" % (type, data[type]))
self.wfile.write("{%s}" % (','.join(output)))
if 'jsonp' in params:
self.wfile.write(');')
self.wfile.write("\n")
self.wfile.flush()
if 'continuous' not in params:
break
time.sleep(delay)
except socket.error, e:
print "Client disconnected.\n"
PORT = 8000
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.co.uk', 80))
sData = s.getsockname()
print "Serving at '%s:%s'" % (sData[0], PORT)
httpd.serve_forever()