forked from robotstreamer/robotstreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_util.py
151 lines (106 loc) · 3.86 KB
/
robot_util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import time
import traceback
import ssl
import urllib.request
import getpass
import json
import _thread
ConfigFilename = "/home/pi/config_" + getpass.getuser() + ".json" #this is never used
KeepAlivePeriod = 6
numActiveSounds = 0
soundQueue = []
def times(lst, number):
return [x*number for x in lst]
def getWithRetry(url, secure=True):
for retryNumber in range(2000):
try:
print("GET", url)
if secure:
object = urllib.request.urlopen(url)
else:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
object = urllib.request.urlopen(url, context=ctx)
break
except:
print("could not open url", url)
traceback.print_exc()
time.sleep(2)
data = object.read()
encoding = object.info().get_content_charset('utf-8')
return data.decode(encoding)
def getNoRetry(url, secure=True):
#socket test has retry
try:
print("GET", url)
if secure:
object = urllib.request.urlopen(url)
else:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
object = urllib.request.urlopen(url, context=ctx)
except:
print("could not open url", url)
return
data = object.read()
encoding = object.info().get_content_charset('utf-8')
return data.decode(encoding)
def sendSerialCommand(ser, command):
print(ser.name) # check which port was really used
ser.nonblocking()
# loop to collect input
#s = "f"
#print "string:", s
print(str(command.lower()))
ser.write(command.lower() + "\r\n") # write a string
#ser.write(s)
ser.flush()
#while ser.in_waiting > 0:
# print "read:", ser.read()
#ser.close()
def makePOST(url, data):
params = json.dumps(data).encode('utf8')
req = urllib.request.Request(url,
data=params,
headers={'content-type': 'application/json'})
response = urllib.request.urlopen(req)
return response
def sendCameraAliveMessage(apiServer, cameraID, streamKey):
print("sending camera alive message")
url = '%s/v1/set_camera_status' % (apiServer)
print("url", url)
try:
response = makePOST(url, {'camera_id': cameraID,
'camera_status': 'online',
'stream_key': streamKey,
'type': 'robot_git'})
#todo:send unified stream key here
except:
print("could not make post to", url)
def getNumActiveSounds():
return numActiveSounds
def getSoundQueueSize():
return len(soundQueue)
def popSoundQueue():
return soundQueue.pop(0)
def playSound(command):
global numActiveSounds
numActiveSounds += 1
os.system(command)
numActiveSounds -= 1
def aplayFile(filename):
for hardwareNumber in (2, 0, 1):
_thread.start_new_thread(playSound, ('aplay -D plughw:%d,0 %s' % (hardwareNumber, filename),))
def handleSoundCommand(command, keyPosition, price):
print("command:", command, "key position:", keyPosition, "price:", price)
if len(command) >= 6:
if command[0:5] == "SOUND" and keyPosition == "down":
number = int(command[5:])
if price > 0:
soundQueue.append('/home/pi/sound/SOUND%d.WAV' % number)
else:
if getSoundQueueSize() < 2:
soundQueue.append('/home/pi/sound/SOUND%d.WAV' % number)