-
Notifications
You must be signed in to change notification settings - Fork 3
/
juicebox.py
executable file
·150 lines (127 loc) · 4.43 KB
/
juicebox.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
#!/usr/bin/python3
"""This program consisted of the functions which are used in main.py program to check the authority of the operator and the staff members
"""
from __future__ import print_function
import http.client
import json
import logging
import signal
import sys
import time
import mfrc522
import requests
device_id = "DEV_ID"
serverURL = "FLUD_BASE/juicebox.php"
headers = {'authorization': "FLUD_KEY"}
class Juicebox:
"""This class represents as the parent class for the JuiceBox
Attributes
----------
rid_1: str
RFID number of the operator
rid_2: str
RFID number of the staff member
temp_rid: str
temporary variable for storing the RFID number
role_1: int
assigned the default value of role_1 as 0
role_2: int
assigned the default value of role_2 as 0
trans_id: str
transaction id used for ending the transaction
go: bool
assigned the default value as False
"""
def __init__(self):
"""the constructor for the initialization with default values"""
self.rid_1 = ""
self.rid_2 = ""
self.temp_rid = ""
self.role_1 = 0
self.role_2 = 0
self.trans_id = ""
self.go = False
def check_status_operator(self, type, id):
"""This function is used for checking the status of operator with its type and student/operator id number
Parameters
----------
type: str
gets the type of rfid status of the operator
id: int
gets the student id number of the operator
Raises
------
ConnectionError
If the device is not connected to the internet
"""
try:
payload = {"type": type, "number": id}
r = requests.request(
"POST", serverURL, json=payload, headers=headers)
response = r.json()
r.raise_for_status()
print(response, file=sys.stderr)
except ConnectionError as e:
"""if the information doesnot match throws an Exception
"""
response = "Check Status: Unable to connect. Verify connection."
print(e, response)
return response
def check_status_staff(self, type, id):
"""This function is used for checking the status of staff member with its type and student/staff id number
Parameters
----------
type: str
gets the type of rfid status of the staff member
id: int
gets the student id number of the staff member
Raises
------
ConnectionError
If the device is not connected to the internet
"""
try:
payload = {"type": type, "number": id}
r = requests.request(
"POST", serverURL, json=payload, headers=headers)
response = r.json()
r.raise_for_status()
print(response, file=sys.stderr)
except ConnectionError as e:
"""if the information doesnot match throws an Exception
"""
response = "Check Status: Unable to connect. Verify connection."
print(e, response)
return response
def authorization_double(self, id_type, id_number, id_number_2, device_id):
""" This function checks whether the id type, id number and device id is correct
Parameters
----------
id_type: str
gets the id type for verification
id_number: int
validates the id number for the operator
id_number_2: int
validates the id number for the staff
device_id: int
validates the device id of the device
Returns
-------
str: json file data is returned
Raises
------
ValueError
If the function failed to request authorization of different ID's from server
"""
try:
payload = {"type": id_type, "number": id_number,
"number_employee": id_number_2, "device": device_id}
r = requests.request(
"POST", serverURL, json=payload, headers=headers)
response = r.json()
print(response)
except ValueError as e:
response = "failed to request authorization of 2 different IDs from server.... check connection and url"
print(e, response)
return response
# end of Juicebox class