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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import csv
|
3 |
+
from datetime import datetime, date, time
|
4 |
+
import firebase_admin
|
5 |
+
from firebase_admin import credentials, firestore
|
6 |
+
|
7 |
+
fileName = str(date.today())
|
8 |
+
fileName = fileName + ".csv"
|
9 |
+
|
10 |
+
cred = credentials.Certificate("certificate.json")
|
11 |
+
app = firebase_admin.initialize_app(cred)
|
12 |
+
|
13 |
+
store = firestore.client()
|
14 |
+
|
15 |
+
collection_name = fileName
|
16 |
+
|
17 |
+
doctorFileName = "DoctorList.csv"
|
18 |
+
|
19 |
+
def batch_data(iterable, n=1):
|
20 |
+
l = len(iterable)
|
21 |
+
for ndx in range(0, l, n):
|
22 |
+
yield iterable[ndx:min(ndx + n, l)]
|
23 |
+
|
24 |
+
def deleteData():
|
25 |
+
docs = store.collection(collection_name).get()
|
26 |
+
for doc in docs:
|
27 |
+
key = doc.id
|
28 |
+
store.collection(collection_name).document(key).delete()
|
29 |
+
|
30 |
+
def uploadData():
|
31 |
+
deleteData()
|
32 |
+
data = []
|
33 |
+
headers = []
|
34 |
+
with open(fileName) as csv_file:
|
35 |
+
csv_reader = csv.reader(csv_file, delimiter=',')
|
36 |
+
line_count = 0
|
37 |
+
for row in csv_reader:
|
38 |
+
if line_count == 0:
|
39 |
+
for header in row:
|
40 |
+
headers.append(header)
|
41 |
+
line_count += 1
|
42 |
+
else:
|
43 |
+
obj = {}
|
44 |
+
for idx, item in enumerate(row):
|
45 |
+
obj[headers[idx]] = item
|
46 |
+
data.append(obj)
|
47 |
+
line_count += 1
|
48 |
+
print(f'Processed {line_count} lines.')
|
49 |
+
|
50 |
+
for batched_data in batch_data(data, 499):
|
51 |
+
batch = store.batch()
|
52 |
+
for data_item in batched_data:
|
53 |
+
doc_ref = store.collection(collection_name).document()
|
54 |
+
batch.set(doc_ref, data_item)
|
55 |
+
batch.commit()
|
56 |
+
|
57 |
+
print('Done')
|
58 |
+
|
59 |
+
def retriveData(type):
|
60 |
+
data = []
|
61 |
+
|
62 |
+
docs = store.collection(collection_name).get()
|
63 |
+
for doc in docs:
|
64 |
+
data.append(doc.to_dict())
|
65 |
+
|
66 |
+
if type == "ID":
|
67 |
+
IDs = []
|
68 |
+
for each in data:
|
69 |
+
IDs.append(each["Queue ID"])
|
70 |
+
return IDs
|
71 |
+
elif type == "All":
|
72 |
+
return data
|
73 |
+
else:
|
74 |
+
for each in data:
|
75 |
+
if (type == each["Queue ID"]):
|
76 |
+
return each["Status"]
|
77 |
+
|
78 |
+
def updateData(QueueID, newStatus):
|
79 |
+
docs = store.collection(collection_name).get()
|
80 |
+
for doc in docs:
|
81 |
+
key = doc.id
|
82 |
+
temp = doc.to_dict()
|
83 |
+
if QueueID == temp["Queue ID"]:
|
84 |
+
store.collection(collection_name).document(key).update({"Status":newStatus})
|
85 |
+
break
|
86 |
+
|
87 |
+
def deleteDoctor():
|
88 |
+
docs = store.collection(doctorFileName).get()
|
89 |
+
for doc in docs:
|
90 |
+
key = doc.id
|
91 |
+
store.collection(doctorFileName).document(key).delete()
|
92 |
+
|
93 |
+
def uploadDoctor():
|
94 |
+
deleteDoctor()
|
95 |
+
data = []
|
96 |
+
headers = []
|
97 |
+
with open(doctorFileName) as csv_file:
|
98 |
+
csv_reader = csv.reader(csv_file, delimiter=',')
|
99 |
+
line_count = 0
|
100 |
+
for row in csv_reader:
|
101 |
+
if line_count == 0:
|
102 |
+
for header in row:
|
103 |
+
headers.append(header)
|
104 |
+
line_count += 1
|
105 |
+
else:
|
106 |
+
obj = {}
|
107 |
+
for idx, item in enumerate(row):
|
108 |
+
obj[headers[idx]] = item
|
109 |
+
data.append(obj)
|
110 |
+
line_count += 1
|
111 |
+
print(f'Processed {line_count} lines.')
|
112 |
+
|
113 |
+
for batched_data in batch_data(data, 499):
|
114 |
+
batch = store.batch()
|
115 |
+
for data_item in batched_data:
|
116 |
+
doc_ref = store.collection(doctorFileName).document()
|
117 |
+
batch.set(doc_ref, data_item)
|
118 |
+
batch.commit()
|
119 |
+
|
120 |
+
print('Done')
|
121 |
+
|
122 |
+
def retriveDoctor(caller):
|
123 |
+
data = []
|
124 |
+
|
125 |
+
docs = store.collection(doctorFileName).get()
|
126 |
+
for doc in docs:
|
127 |
+
data.append(doc.to_dict())
|
128 |
+
|
129 |
+
if caller == "client":
|
130 |
+
doctorNames = []
|
131 |
+
for each in data:
|
132 |
+
doctorNames.append(each["Doctor Name"])
|
133 |
+
return doctorNames
|
134 |
+
elif caller == "doctor":
|
135 |
+
doctorInfo = []
|
136 |
+
for each in data:
|
137 |
+
doctorInfo.append([each["Doctor Name"], each["Password"]])
|
138 |
+
return doctorInfo
|
139 |
+
|
140 |
+
# ---------------------------------------------------------------------------------
|
141 |
+
|
142 |
+
def resetDoctor():
|
143 |
+
writeFile = open("DoctorList.csv", 'w', newline="")
|
144 |
+
writer1 = csv.writer(writeFile)
|
145 |
+
writer1.writerow(["Doctor Name", "Password"])
|
146 |
+
writeFile.close()
|
147 |
+
deleteDoctor()
|
148 |
+
|
149 |
+
def register(name, password):
|
150 |
+
# readFile = open("DoctorList.csv", "r")
|
151 |
+
# reader = csv.reader(readFile)
|
152 |
+
reader = retriveDoctor("client")
|
153 |
+
for row in reader:
|
154 |
+
if row == name:
|
155 |
+
return False
|
156 |
+
# readFile.close()
|
157 |
+
|
158 |
+
appendFile = open("DoctorList.csv", 'a', newline="")
|
159 |
+
writer2 = csv.writer(appendFile)
|
160 |
+
writer2.writerow([name, password])
|
161 |
+
appendFile.close()
|
162 |
+
return True
|
163 |
+
|
164 |
+
def login(name, password):
|
165 |
+
listing = retriveDoctor("doctor")
|
166 |
+
for row in listing:
|
167 |
+
if (row[0] == name) and (row[1] == password):
|
168 |
+
return True
|
169 |
+
return False
|
170 |
+
|
171 |
+
# ----------------------------------------------------------------------------------------------
|
172 |
+
|
173 |
+
def resetQueueV2():
|
174 |
+
file = open(fileName, "w", newline="")
|
175 |
+
writer = csv.writer(file)
|
176 |
+
writer.writerow(["Doctor Name", "Queue ID", "Appointed", "Time", "Status"])
|
177 |
+
file.close()
|
178 |
+
deleteData()
|
179 |
+
|
180 |
+
def addQueueV2(doctor, appointed):
|
181 |
+
retrivingData = retriveData("All")
|
182 |
+
amount = len(retrivingData) + 1
|
183 |
+
name = "Q" + str(amount)
|
184 |
+
rightNow = datetime.now()
|
185 |
+
subQueue = [doctor, name, str(appointed), rightNow.strftime("%H:%M:%S"), "Waiting"]
|
186 |
+
file = open(fileName, 'a', newline="")
|
187 |
+
writer = csv.writer(file)
|
188 |
+
writer.writerow(subQueue)
|
189 |
+
file.close()
|
190 |
+
return name
|
191 |
+
|
192 |
+
def callQueue(queueNumber):
|
193 |
+
data = retriveData("All")
|
194 |
+
for each in data:
|
195 |
+
if queueNumber == each["Queue ID"]:
|
196 |
+
return each
|
197 |
+
|
198 |
+
# ---------------------------------------------------------------------------------------------
|
199 |
+
|
200 |
+
st.title("Hello patient. Please enter this form to line up.")
|
201 |
+
|
202 |
+
insertForm = st.empty()
|
203 |
+
form = insertForm.form(key="TestForm1", clear_on_submit=True)
|
204 |
+
doctor = form.selectbox("Which doctor are you here for?", retriveDoctor("client"))
|
205 |
+
appointed = form.checkbox("Have you made an appointment before-hand?", value=False)
|
206 |
+
|
207 |
+
submit_button = form.form_submit_button(label="Submit")
|
208 |
+
|
209 |
+
if submit_button:
|
210 |
+
output = addQueueV2(doctor, appointed)
|
211 |
+
globalOutput = output
|
212 |
+
insertForm.empty()
|
213 |
+
st.success("Thank you for lining up. Your Queue Number is {}.\nPlease screenshot it until your queue has been called.".format(output))
|
214 |
+
uploadData()
|
215 |
+
initial_Pending = False
|
216 |
+
current_Status = retriveData(output)
|
217 |
+
previous = -1
|
218 |
+
setTime2 = time.time() - 60
|
219 |
+
delay = 60
|
220 |
+
while True:
|
221 |
+
currentTime2 = time.time()
|
222 |
+
if (currentTime2 - setTime2 > delay):
|
223 |
+
setTime2 = time.time()
|
224 |
+
status = retriveData(output)
|
225 |
+
if current_Status != status:
|
226 |
+
current_Status = status
|
227 |
+
if status == "Waiting":
|
228 |
+
st.info("Your queue has not been called yet. Please wait until called.")
|
229 |
+
elif status == "Pending1":
|
230 |
+
previous = int(time.time())
|
231 |
+
initial_Pending = True
|
232 |
+
current_Status = status
|
233 |
+
updateData(output, "Pending2")
|
234 |
+
elif status == "Pending2":
|
235 |
+
current = int(time.time())
|
236 |
+
time_remains = (360 - (current - previous))
|
237 |
+
current_waiting = -1
|
238 |
+
setTime3 = time.time() - 60
|
239 |
+
while (time_remains // 60) > 1 and current_Status == status:
|
240 |
+
currentTime3 = time.time()
|
241 |
+
if (currentTime3 - setTime3) > delay:
|
242 |
+
setTime3 = time.time()
|
243 |
+
current = int(time.time())
|
244 |
+
time_remains = (360 - (current - previous))
|
245 |
+
if ((time_remains // 60 < 6) and (time_remains // 60 > 1)) and (time_remains // 60 != current_waiting):
|
246 |
+
current_waiting = (time_remains // 60)
|
247 |
+
st.warning("You queue has been called. You have {} minutes left.".format(str(time_remains // 60)))
|
248 |
+
status = retriveData(output)
|
249 |
+
current_Status = status
|
250 |
+
updateData(output, "Pending3")
|
251 |
+
elif status == "Pending3":
|
252 |
+
current = int(time.time())
|
253 |
+
time_remains = (360 - (current - previous))
|
254 |
+
st.warning("You queue has been called. You have less than 1 minute left.")
|
255 |
+
setTime4 = time.time() - 60
|
256 |
+
while time_remains > 0 and current_Status == status:
|
257 |
+
currentTime4 = time.time()
|
258 |
+
if (currentTime4 - setTime4) > delay:
|
259 |
+
setTime4 = time.time()
|
260 |
+
current = int(time.time())
|
261 |
+
time_remains = (360 - (current - previous))
|
262 |
+
status = retriveData(output)
|
263 |
+
current_Status = status
|
264 |
+
updateData(output, "Pending4")
|
265 |
+
elif status == "Pending4":
|
266 |
+
st.warning("5 minutes have passed. Please meet the doctor immediately before your queue get skipped.")
|
267 |
+
current_Status = status
|
268 |
+
setTime5 = time.time() - 60
|
269 |
+
while current_Status == status:
|
270 |
+
currentTime5 = time.time()
|
271 |
+
if (currentTime5 - setTime5) > delay:
|
272 |
+
setTime5 = time.time()
|
273 |
+
status = retriveData(output)
|
274 |
+
elif status == "Complete":
|
275 |
+
st.success("Thank you for coming to meet the doctor today. Have a nice day!")
|
276 |
+
break
|
277 |
+
else:
|
278 |
+
st.error("Something is wrong with the system. Please contact the staff for the moment.")
|
279 |
+
break
|
requirement.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
firebase_admin
|
3 |
+
csv
|