-
Notifications
You must be signed in to change notification settings - Fork 0
/
u-med.py
444 lines (325 loc) · 12.6 KB
/
u-med.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from main import convertDataToJSON, pinJSONtoIPFS, initContract, w3
import sys
from pprint import pprint
import requests as r
from pathlib import Path
import json
# from mdutils.mdutils import MdUtils
# from mdutils import Html
medicalhistory = initContract()
##################################################
## DOCTOR
def createDoctor():
description = {
"Date & Time :":[input("Date"), input("Time")],
"Doctor Name :":input("Doctor Name"),
"Doctor Gender :": input("Doctor Gender"),
"Hospital Name :": input("Hospital Name")
}
json_data = convertDataToJSON(description)
uri = pinJSONtoIPFS(json_data)
tx_hash=medicalhistory.functions.createDoctor(uri).transact(
{"from":w3.eth.accounts[0]}
)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
return uri, receipt
def getDoctorData(doctor_id):
uri=medicalhistory.functions.getDoctorData(doctor_id).call()
return uri.split('/')[-1]
def get_uri(uri):
return r.get(f"https://ipfs.io/ipfs/{uri}").json()
def get_all_doctor_ids():
event_filter = medicalhistory.events.newDoctor.createFilter(
fromBlock='0x0'
)
events = event_filter.get_all_entries()
return _extract_arg_from_event(events, 'doctor_id')
def _extract_arg_from_event(events, arg):
return [event['args'][arg] for event in events]
##################################################
# HOSPITAL
def createHospital():
description = {
"Date & Time :":[input("Date"), input("Time")],
"Hospital Name :":input("Hospital Name"),
"State :": input("State"),
"City :": input("City")
}
json_data = convertDataToJSON(description)
uri = pinJSONtoIPFS(json_data)
tx_hash=medicalhistory.functions.createHospital(uri).transact(
{"from":w3.eth.accounts[0]}
)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
return uri, receipt
def getHospitalData(hospital_id):
uri=medicalhistory.functions.getHospitalData(hospital_id).call()
return uri.split('/')[-1]
def get_uri(uri):
return r.get(f"https://ipfs.io/ipfs/{uri}").json()
def get_all_hospital_ids():
event_filter = medicalhistory.events.newHospital.createFilter(
fromBlock='0x0'
)
events = event_filter.get_all_entries()
return _extract_arg_from_event(events, 'hospital_id')
def _extract_arg_from_event(events, arg):
return [event['args'][arg] for event in events]
##################################################
#PATIENT
def createPatient():
description = {
"Date & Time :":[input("Date"), input("Time")],
"Patient Name :":input("Patient Name"),
"Patient Gender :": input("Patient Gender"),
"Doctor Name :": input("Doctor Name"),
"Hospital Name :": input("Hospital Name")
}
json_data = convertDataToJSON(description)
uri = pinJSONtoIPFS(json_data)
tx_hash=medicalhistory.functions.createPatient(uri).transact(
{"from":w3.eth.accounts[0]}
)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
return uri, receipt
def getPatientData(patient_id):
uri=medicalhistory.functions.getPatientData(patient_id).call()
return uri.split('/')[-1]
def get_uri(uri):
return r.get(f"https://ipfs.io/ipfs/{uri}").json()
def get_all_patient_ids():
event_filter = medicalhistory.events.newPatient.createFilter(
fromBlock='0x0'
)
events = event_filter.get_all_entries()
return _extract_arg_from_event(events, 'patient_id')
def _extract_arg_from_event(events, arg):
return [event['args'][arg] for event in events]
################################################
## PROCEDURE
def createProcedures():
description = {
"Date & Time :":[input("Date"), input("Time")],
"Procedure Name :":input("Procedure Name"),
"Procedure Description :": input("Procuedure Description")
}
json_data = convertDataToJSON(description)
uri = pinJSONtoIPFS(json_data)
tx_hash=medicalhistory.functions.createProcedures(description["Procedure Name :"],uri).transact(
{"from":w3.eth.accounts[0]}
)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
return uri, receipt
def getProcedures(procedure_id):
uri=medicalhistory.functions.getProcedures(procedure_id).call()
return uri.split('/')[-1]
def get_uri(uri):
return r.get(f"https://ipfs.io/ipfs/{uri}").json()
def get_all_procedure_ids():
event_filter = medicalhistory.events.newProcedure.createFilter(
fromBlock='0x0'
)
events = event_filter.get_all_entries()
return _extract_arg_from_event(events, 'procedure_id')
def _extract_arg_from_event(events, arg):
return [event['args'][arg] for event in events]
###############################################
#DOCTOR VISIT
def newDoctorVisit():
pprint(f"Patient IDs: {get_all_patient_ids()}")
pprint(f"Doctor IDs: {get_all_doctor_ids()}")
pprint(f"Hospital IDs: {get_all_hospital_ids()}")
pprint(f"Procedure IDs: {get_all_procedure_ids()}")
description = {
"Patient ID :": int(input("Patient ID")),
"Doctor ID :": int(input("Doctor ID")),
"Hospital ID :": int(input("Hospital ID")),
"Procedure ID :": int(input("Procedure ID")),
"Date & Time :":[input("Date"), input("Time")],
"Reason for visit :": input("Reason for visit"),
"Assessment :": input("Assessment"),
"Notes :": input("Notes")
}
json_data = convertDataToJSON(description)
uri = pinJSONtoIPFS(json_data)
tx_hash=medicalhistory.functions.createnewDoctorVisit(
int(description['Patient ID :']),
int(description['Doctor ID :']),
int(description['Hospital ID :']),
int(description['Procedure ID :']),
uri).transact(
{"from":w3.eth.accounts[0]}
)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
return uri, receipt
def getDoctorVisitData(doctorVisit_id):
uri=medicalhistory.functions.getnewDoctorVisit(doctorVisit_id).call()
return uri.split('/')[-1]
def get_uri(uri):
return r.get(f"https://ipfs.io/ipfs/{uri}").json()
def get_all_doctorVisit_ids():
event_filter = medicalhistory.events.newDoctorVisit.createFilter(
fromBlock='0x0'
)
events = event_filter.get_all_entries()
return _extract_arg_from_event(events, 'doctorVisit_id')
def _extract_arg_from_event(events, arg):
return [event['args'][arg] for event in events]
###########################################
###########################################
# MAIN CALLOUT
if __name__ == "__main__":
#DOCTOR
if sys.argv[1] == 'reportDoctor':
uri, receipt = createDoctor()
print("Report IPFS Hash", uri)
elif sys.argv[1] == 'getDoctor':
pprint(get_all_doctor_ids())
doctor_id = int(input('doctor_id'))
uri = getDoctorData(doctor_id)
data = get_uri(uri)
print (f"Doctor's name: {data['Doctor Name :']}.")
print (f"Doctor's gender: {data['Doctor Gender :']}.")
print (f"Doctor's Associated Hospital: {data['Hospital Name :']}.")
#HOSPITAL
elif sys.argv[1] == 'reportHospital':
uri, receipt = createHospital()
print("Report IPFS Hash", uri)
elif sys.argv[1] == 'getHospital':
pprint(get_all_hospital_ids())
hospital_id = int(input('hospital_id'))
uri = getHospitalData(hospital_id)
data = get_uri(uri)
print (f"Input date : {data['Date & Time :'][0]}.")
print (f"Input time : {data['Date & Time :'][1]}.")
print (f"Hospital name: {data['Hospital Name :']}.")
print (f"Hospital Location: {data['City :']}, {data['State :']}.")
#PATIENT
elif sys.argv[1] == 'reportPatient':
uri, receipt = createPatient()
# pprint(receipt)
print("Report IPFS Hash", uri)
elif sys.argv[1] == 'getPatient':
pprint(get_all_patient_ids())
patient_id = int(input('patient_id'))
uri = getPatientData(patient_id)
data = get_uri(uri)
print (f"Input date : {data['Date & Time :'][0]}.")
print (f"Input time : {data['Date & Time :'][1]}.")
print (f"Patient's name: {data['Patient Name :']}.")
print (f"Patient's gender: {data['Patient Gender :']}.")
print (f"Doctor's name: {data['Doctor Name :']}.")
print (f"Hospital name: {data['Hospital Name :']}.")
#PROCEDURE
elif sys.argv[1] == 'reportProcedure':
uri, receipt = createProcedures()
print("Report IPFS Hash", uri)
elif sys.argv[1] == 'getProcedure':
pprint(get_all_procedure_ids())
procedure_id = int(input('procedure_id'))
uri = getProcedures(procedure_id)
data = get_uri(uri)
print (f"Input date : {data['Date & Time :'][0]}.")
print (f"Input time : {data['Date & Time :'][1]}.")
print (f"Procedure name: {data['Procedure Name :']}.")
print (f"Procedure description: {data['Procedure Description :']}.")
#DOCTOR VISIT
elif sys.argv[1] == 'reportVisit':
uri, receipt = newDoctorVisit()
print("Report IPFS Hash", uri)
elif sys.argv[1] == 'getVisit':
pprint(get_all_doctorVisit_ids())
doctorVisit_id = int(input('doctorVisit_id'))
uri = getDoctorVisitData(doctorVisit_id)
data = get_uri(uri)
print (f"Visit date : {data['Date & Time :'][0]}.")
print (f"Visit time : {data['Date & Time :'][1]}.")
print (f"Hospital name: {data['Hospital ID']}.")
print (f"Attending Doctor: {data['Doctor ID']}")
print (f"Reason for visit: {data['Reason for visit :']}.")
print (f"Assessment: {data['Assessment :']}.")
print (f"Notes: {data['Notes :']}.")
# PATIENT REPORT
elif sys.argv[1] == 'printReport':
patient_id = int(input('patient_id'))
patientURI = getPatientData(patient_id)
patientDATA = get_uri(patientURI)
L = []
listVisits=get_all_doctorVisit_ids()
for i in listVisits:
visitURI=getDoctorVisitData(i)
visitDATA=get_uri(visitURI)
L.append(
visitDATA
)
d = {}
listDoctors = get_all_doctor_ids()
for i in listDoctors:
doctorURI=getDoctorData(i)
doctorDATA=get_uri(doctorURI)
d[str(i)] = str(doctorDATA['Doctor Name :'])
h = {}
listHospitals=get_all_hospital_ids()
for i in listHospitals:
hospitalURI = getHospitalData(i)
hospitalDATA = get_uri(hospitalURI)
h[str(i)] = str(hospitalDATA['Hospital Name :'])
output_path = Path('PatientReport.txt')
text = [
{"Input date" : patientDATA['Date & Time :'][0],
"Input time" : patientDATA['Date & Time :'][1],
"Patient's name" : patientDATA['Patient Name :'],
"Patient's gender" : patientDATA['Patient Gender :'],
"Doctor's name" : patientDATA['Doctor Name :'],
"Hospital name" : patientDATA['Hospital Name :']
},
"DOCTOR VISITS",
"________________________________________",
L,
"LIST OF DOCTORS",
"________________________________________",
d,
"LIST OF HOSPITALS",
"________________________________________",
h
]
json_dump = json.dumps(text, indent = 4)
with open(output_path,'w') as file:
file.write(str(json_dump))
### LISTS
# GET PATIENT LIST
elif sys.argv[1] == 'listPatients':
listPatients=get_all_patient_ids()
d = {}
for i in listPatients:
uri = getPatientData(i)
data = get_uri(uri)
d[str(i)] = str(data['Patient Name :'])
print (d)
# GET DOCTOR LIST
elif sys.argv[1] == 'listDoctors':
listDoctors=get_all_doctor_ids()
d = {}
for i in listDoctors:
uri = getDoctorData(i)
data = get_uri(uri)
d[str(i)] = str(data['Doctor Name :'])
print (d)
# GET HOSPITAL LIST
elif sys.argv[1] == 'listHospitals':
listHospitals=get_all_hospital_ids()
d = {}
for i in listHospitals:
uri = getHospitalData(i)
data = get_uri(uri)
d[str(i)] = str(data['Hospital Name :'])
print (d)
# GET PROCEDURE LIST
elif sys.argv[1] == 'listProcedures':
listProcedures=get_all_procedure_ids()
d = {}
for i in listProcedures:
uri = getProcedures(i)
data = get_uri(uri)
d[str(i)] = str(data['Procedure Name :'])
print (d)