-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsessionManager.py
234 lines (209 loc) · 8.36 KB
/
sessionManager.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from datetime import datetime
from enum import Enum
from filter import Filter, FilterType
from laser import Laser
from mockLaser import MockLaser
from laserSession import LaserSession
import os
import json
import requests
AUTHLIST_FILE = "authorized.json"
# Global Variables ----
class Auth_Result(Enum):
NOT_AUTHENTICATED = 0
AUTHENTICATED = 1
LOGGED_OUT = 2
ANOTHER_USER_LOGGED_IN = 3
ERROR = 4
class SessionManager:
def __init__(self, currentUser=None, currentFilter=None):
# Instance variables
self.currentUser = currentUser
self.currentFilter = currentFilter
# Should we use a mock laser for dev testing?
if 'LASERGUI_MOCK' in os.environ:
self.laserInterface = MockLaser()
else:
self.laserInterface = Laser()
self.laserInterface.disable()
def update_odometer(self):
"""
Fetch the current odometer from the Laser
"""
self.laserInterface.status()
# the laser interface stores the odometer reading as a string
odoInt = int(self.laserInterface.odometer)
if self.currentUser != None:
self.currentUser.end_odo = odoInt
if self.currentFilter != None:
self.currentFilter.endOdometer = odoInt
return odoInt
# Filter Methods
def currentFilterData(self):
"""
Returns filter type and time remaining on filter.
"""
if self.currentFilter != None:
return self.currentFilter.filterSummary()
else:
return 'Unknown', 0
def is_filter_change_needed(self):
"""
Checks if a filter should be retired.
"""
if self.currentFilter == None:
return True
else:
return self.currentFilter.shouldBeRetired()
def create_new_filter(self, filterType):
"""
Calls Filter to create new filter
"""
self.currentFilter = Filter.create_new_filter(filterType, self.update_odometer())
def fetch_existing_filters(self):
"""
Calls class method on Filter to Fetch existing filters from GC
"""
return Filter.fetch_existing_filters()
def switch_to_filter(self, filterObj):
"""
updates the runtime on the current filter before switching to the passed in the filter
"""
odoValue = self.update_odometer()
filterObj.startOdometer = odoValue
filterObj.endOdometer = odoValue
if self.currentFilter != None:
# update current filter usage time
self.currentFilter.updateRuntime()
self.currentFilter = filterObj
# Authentication Methods
def authenticate_credential(self, credential, retries ):
"""
Called when a fob is tagged.
"""
result = Auth_Result.NOT_AUTHENTICATED
if self.currentUser == None:
authSuccess = False
self.currentUser = self.authorized_user_for_credential(credential)
if self.currentUser != None:
result = Auth_Result.AUTHENTICATED
authSuccess = True
self.laserInterface.enable()
self.postActivityListing(credential,authSuccess)
elif self.currentUser.credential == credential:
self.logout()
result = Auth_Result.LOGGED_OUT
else:
result = Auth_Result.ANOTHER_USER_LOGGED_IN
# If we failed to authenticate but have some retries, try again
if result == Auth_Result.NOT_AUTHENTICATED and retries > 0:
print("Auth failed, refetching list and retrying...")
self.fetch_access_list()
return self.authenticate_credential( credential, retries-1 )
return result
def authorized_user_for_credential(self, credential):
"""
Looks for the credential, aka fob, in the access list.
The access list is a json list of laser authorized user disctionaries.
Each user dictionary should contain an RFID key, the credential.
Returns a LaserSession representing the user or none if no match
Throws FileNotFoundError if access list is not found
"""
print('checking for credential: '+ credential +' in access list...')
user = None
accessList = self.load_access_list()
for userDict in accessList:
#print(userDict)
if userDict['RFID'] == credential:
print('found!')
user = LaserSession(userDict, self.update_odometer())
break
return user
def logout(self):
self.laserInterface.disable()
self.currentUser.end_time = datetime.now()
self.update_odometer()
# log laser activity to GC
self.postLaserSession(self.currentUser)
self.currentUser = None
# update current filter usage time
if self.currentFilter != None:
self.currentFilter.updateRuntime()
def fetch_access_list(self):
"""
Pulls certified laser RFIDs from URL defined as an environment variable.
The json list contains only those user allowed to use the laser.
"""
ACCESS_URL = os.environ['ACE_ACCESS_URL']
EXPORT_TOKEN = os.environ['ACE_EXPORT_TOKEN']
success = False
body = {'ace_export_token': EXPORT_TOKEN}
headers = {'User-Agent': 'Wget/1.20.1 (linux-gnu)'}
try:
response = requests.post(ACCESS_URL, body, headers=headers, timeout=5)
userList = response.json()
if "error" in userList:
#{"error":"Token not sent."}
#print(response.content)
print("Access List "+response.text +" from "+ ACCESS_URL)
else:
success = True
print("Length of user list: ",len(userList))
data = response.content
with open(AUTHLIST_FILE, 'wb') as f:
f.write(data)
except requests.exceptions.Timeout:
#TODO Maybe set up for a retry, or continue in a retry loop
print("Timeout connecting to URL")
except requests.exceptions.TooManyRedirects:
#TODO Tell the user their URL was bad and try a different one
print("Invalid URL")
except requests.exceptions.RequestException as ex:
print("Exception thrown in fetch_access_list:", ex)
return success
def load_access_list(self):
"""
Loads the json list of authorized user from file.
Throws FileNotFoundError if AUTHLIST_FILE is not found.
"""
with open(AUTHLIST_FILE, 'r') as json_file :
authorized_rfids = json.load(json_file)
return authorized_rfids
def get_auth_list_time(self):
"""
Throws FileNotFoundError if AUTHLIST_FILE is not found.
"""
filetime = os.path.getmtime(AUTHLIST_FILE)
return datetime.fromtimestamp(filetime )
def postActivityListing(self, credential, authSuccess):
print('posting ActivityListing for '+ credential, authSuccess)
ASSET_ID = os.environ['ACEGC_ASSET_ID']
GC_ASSET_TOKEN = os.environ['ACEGC_ASSET_TOKEN']
reporting_URL = os.environ['ACEGC_BASE_URL'] + "/activitylistings/"
data = {
'access_point': ASSET_ID,
'activity_date': datetime.now(),
'credential': credential,
'success': authSuccess,
}
headers = {'Authorization': "Token {}".format(GC_ASSET_TOKEN)}
#TODO: wrap request in a try
resp = requests.post(reporting_URL, data, headers=headers, timeout=5)
#print(resp.content)
return resp
def postLaserSession(self,currSession):
print('posting LaserSession for '+ currSession.credential)
GC_ASSET_TOKEN = os.environ['ACEGC_ASSET_TOKEN']
reporting_URL = os.environ['ACEGC_BASE_URL'] + "/lasersessions/"
data = {
'credential': currSession.credential,
'member_id': currSession.member_id,
'start_time': currSession.start_time,
'end_time': currSession.end_time,
'start_odo': currSession.start_odo,
'end_odo': currSession.end_odo,
}
headers = {'Authorization': "Token {}".format(GC_ASSET_TOKEN)}
#TODO: wrap request in a try
resp = requests.post(reporting_URL, data, headers=headers, timeout=5)
print(resp.content)