-
Notifications
You must be signed in to change notification settings - Fork 0
/
foosball_utils.py
62 lines (50 loc) · 1.85 KB
/
foosball_utils.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
#!/usr/bin/env python
import random
import httplib, urllib
import time
class DigitalFoosballSimulator(object):
def __init__ (self, ip="127.0.0.1", port="80"):
self.table = "main"
self.ip = ip
self.port = port
self.token = random.randint(1,65535)
self.context = ""
def _sendRequest(self, team, tag=None):
params = {'@token': self.token, '@table': self.table}
headers = {"Content-Type": "application/x-www-form-urlencoded",
"User-Agenet": "Arduino/DigitalKicker"
}
context = ""
try:
conn = httplib.HTTPConnection(self.ip, self.port)
conn.set_debuglevel(1)
if tag:
params['@tag'] = tag
context = self.context + "/events/rfid/%s" % team
else:
context = self.context + "/events/goals/%s" % team
params = urllib.urlencode(params)
conn.request("POST", context, params, headers)
response = conn.getresponse()
print response.status, response.reason
return response.status, response.reason
except Exception, e:
txt = 'exception when sending request\n params:%s\n headers:%s\n context:%s\n'%(params,headers,context)
print 'ERROR:',txt
return 'ERROR', txt
def sendVisitorGoal(self):
return self._sendRequest("visitors")
def sendHomeGoal(self):
return self._sendRequest("home")
def addRFIDVistor(self, id):
return self._sendRequest("visitors", id)
def addRFIDHome(self, id):
return self._sendRequest("home", id)
if __name__ == "__main__":
sim = DigitalFoosballSimulator()
sim.sendVisitorGoal()
for x in range(0):
time.sleep(2)
sim.sendHomeGoal()
time.sleep(2)
sim.sendVisitorGoal()