-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsensor_recorder.py
More file actions
54 lines (47 loc) · 1.53 KB
/
sensor_recorder.py
File metadata and controls
54 lines (47 loc) · 1.53 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
#!/usr/bin/python2.6
from __future__ import with_statement
from util import get_last_fileid
import android
import json
import time
OUTPUT_FILENAME_TEMPLATE = 'sensor_output_{0}.json'
def record():
# Start location messages
droid = android.Android()
droid.startSensingTimed(1, 50)
droid.makeToast("Starting sensing")
# Show a dialog box
droid.dialogCreateInput("Sensor Recorder running", "Add message to log")
droid.dialogSetPositiveButtonText("Log message")
droid.dialogSetNegativeButtonText("Exit")
droid.dialogShow()
# Loop until the user exits
out_filename = OUTPUT_FILENAME_TEMPLATE.format(get_last_fileid() + 1)
with open(out_filename, 'w') as out_file:
running = True
time.sleep(10)
while running:
res = droid.eventWait(1000).result
print res
if res == None:
print "SensorListener timeout"
elif res['name'] == "dialog":
# User saving a comment
if (res[u'data'][u'which'] == u'positive'):
droid.makeToast("Saving log message")
message = '# {0}'.format(res[u'data'][u'value'])
print message
out_file.write('{0}\n'.format(message))
droid.dialogShow()
else:
print "User requested exit"
running = False
elif res['name'] == "sensors":
# We've got what we're looking for!
print(res)
out_file.write(json.dumps(res) + '\n')
# Shutdown
droid.stopLocating()
droid.makeToast("All done - saved to {0}".format(out_filename))
if __name__ == '__main__':
record()