-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_server.py
104 lines (86 loc) · 2.76 KB
/
local_server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import subprocess
import thread
import time
from random import choice
import dropbox
import websocket
import settings
from index import Index
from keys import app_key, app_secret
access_token = ''
idx = None
light_on = False
last_message_time = time.time()
def play_audio_mac():
sound = "sounds/R2D2" + choice(["a", "b", "c", "e"]) + ".wav"
return_code = subprocess.call(["afplay", sound])
def play_audio_ubuntu(letter):
sound = "sounds/R2D2" + letter + ".wav"
import pygame
pygame.mixer.Sound(sound).play()
def on_message(ws, message):
global last_message_time
if time.time() - last_message_time < 0.25:
return
last_message_time = time.time()
print message
if access_token and "photo" in message:
date_time = time.strftime("IMG_%Y-%m-%d_%H:%M:%S.png", time.gmtime())
idx.take_picture(date_time)
f = open(settings.base_path + 'pics/' + date_time)
client = dropbox.client.DropboxClient(access_token)
response = client.put_file('/' + date_time, f)
print "Uploaded: ", response
play_audio_ubuntu("b")
elif "pen" in message:
print "Finding pen"
idx.point_at_obj("pen")
play_audio_ubuntu("c")
elif "arduino" in message:
print "Finding Arduino"
idx.point_at_obj("arduino")
play_audio_ubuntu("c")
elif "keys" in message:
print "Finding keys"
idx.point_at_obj("key")
play_audio_ubuntu("c")
elif "spot" in message:
print "Beep beep"
play_audio_ubuntu("a")
elif "lights" in message:
global light_on
light_on = not light_on
print "Flipping light to " + str(light_on)
idx.lazr.lamp(int(light_on))
play_audio_ubuntu("e")
#play_audio_mac()
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
global idx
idx = Index()
def run():
while idx.step():
pass
thread.start_new_thread(run, ())
if __name__ == "__main__":
f = open('db_accesstoken.txt', 'r')
access_token = f.readline()
if not access_token:
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()
print authorize_url
code = raw_input("Enter the authorization code here: ").strip()
access_token, user_id = flow.finish(code)
f = open('db_accesstoken.txt', 'w')
f.write(access_token)
f.close()
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://pythonscript.richiezeng.com:8888/ws",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()