-
Notifications
You must be signed in to change notification settings - Fork 1
/
githubrpi.py
163 lines (148 loc) · 5.33 KB
/
githubrpi.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
151
152
153
154
155
156
157
158
159
160
161
import json
import time
import requests
from sseclient import SSEClient as EventSource
server_url = 'http://192.168.43.215:5000'
events_url = server_url
class CycleController:
"""
Class for Cycle Controller
"""
PHASE_RED = 0
PHASE_YELLOW = 1
PHASE_GREEN = 2
PHASE_ORANGE = 3
def __init__(self, id, name):
self.id = id
self.name = name
self.rfid = None
self.ride_id = None
self.source = None
self.set_phase(CycleController.PHASE_RED)
def with_requests(self, url):
"""Get a streaming response for the given event feed using requests."""
data = {
'id': self.id,
'ride_id': self.ride
}
return requests.get(url = url, data = data, stream=True)
def set_phase(self, phase):
p = ["Red", "Yellow", "Green", "Orange"]
print("Phase : {0}".format(p[phase]))
self.phase = phase
# TODO: Store the object to a file
# for later restoration.
def init_source(self):
response = self.with_requests(events_url + '/events')
self.source = EventSource(response)
def listen(self):
global server_url, event_url
print "gone to listen"
self.init_source()
print "Listening to events"
for event in self.source.events():
print("Got event {0}".format(event.event))
if event.event == 'user_request':
response = json.loads(event.data)
print response
self.rfid = response["rfid"]
self.ride_id = response["ride_id"]
self.set_phase(CycleController.PHASE_YELLOW)
self.source.close()
self.source = None
self.init_ride()
elif event.event == 'post_ride':
response = json.loads(event.data)
# If user wants to continue
if response["status"] == 'continue':
self.set_phase(CycleController.PHASE_YELLOW)
self.source.close()
self.source = None
self.init_ride()
elif response["status"] == 'timeout' or \
response["status"] == 'stop':
self.set_phase(CycleController.PHASE_RED)
print "Listening to events"
def init_ride(self):
# TODO: Start & Wait for RFID
# If timeout , go to red phase.
# ...
print("Getting rfid")
user_rfid = "F100" # Get this from RFID
_ = raw_input("Insert RFID?")
if user_rfid == self.rfid:
# Tell server to start ride
print("Ride Accepted")
data = {
'status': 'Accepted',
'ride_id': self.ride_id,
'cycle_id': self.id,
}
try:
r = requests.post(
url=server_url + '/startride', data=data)
response = json.loads(r.text)
if response["status"] == 'success':
# TODO: Open the lock
print("Open Lock")
self.set_phase(CycleController.PHASE_GREEN)
self.ride()
else:
print("Failed response")
self.set_phase(CycleController.PHASE_RED)
except:
# TODO: Handle network error
print("Network Error")
self.set_phase(CycleController.PHASE_RED)
else:
# Tell server to reject ride
print("Ride rejected")
data = {
'status': 'Rejected',
'message': 'Invalid Card',
'ride_id': self.ride_id,
'cycle_id': self.id
}
try:
r = requests.post(
url=server_url + '/startride', data=data)
except:
# TODO: Handle network error
print("Network Error")
self.set_phase(CycleController.PHASE_RED)
def ride(self):
# Wait for RFID to be removed
print("Waiting for rfid to be removed")
_rem = raw_input("remove?")
# Tell server to reject ride
data = {
'ride_id': self.ride_id,
'cycle_id': self.id
}
try:
r = requests.post(url= server_url + '/stopride', data=data)
print "Ride stopped."
except:
# TODO: Handle network error
# Store the ride_id and rfid in special file
# and send them later to server.
print("Network Error")
self.set_phase(CycleController.PHASE_ORANGE)
if __name__ == "__main__":
cycle = CycleController(1, "hrishi")
while True:
# Restore state
try:
p = ["Red", "Yellow", "Green", "Orange"]
print("Restoring cycle phase {0}".format(p[cycle.phase]))
if cycle.phase == CycleController.PHASE_GREEN:
print("Ride restored")
cycle.ride()
else:
# Phase Yellow
if cycle.phase == CycleController.PHASE_YELLOW:
cycle.set_phase(CycleController.PHASE_RED)
cycle.listen()
except:
print("Network Error. Retrying after one sec.")
time.sleep(1)