def dashboard(): global GLOBAL_CARD_PATH global PHYSICIAN_READ_MSGS global PHYSICIAN_TREATMENT_MSGS global phys if request.method == 'GET': return render_template('dashboard.html', entity_type='physician', name=phys.name) elif request.method == 'POST': if 'physician_id' in request.form: response = phys.physician_id elif 'hospitals' in request.form: response = parser.get_hosp_names_string() elif 'register' in request.form: hospital_name = request.form.get('register') if register(hospital_name): response = "Registration successful" else: response = "Error registering" elif 'affiliated_hospitals' in request.form: response = phys.get_hospitals() elif 'read' in request.form: response = PHYSICIAN_READ_MSGS # TODO: Finish physician APIs by hooking into the handle_message def elif 'seek_treatment' in request.form: if GLOBAL_CARD_PATH: response = None else: # card path set above via handle_message() GLOBAL_CARD_PATH = request.form['seek_treatment'] card = card_helper.get_card_object(GLOBAL_CARD_PATH) if card: response = "-------> Request for treatment from %s" % card.patient_name else: response = None elif 'submit_treatment' in request.form: if GLOBAL_CARD_PATH: card = card_helper.get_card_object(GLOBAL_CARD_PATH) hosp_contact_info = parser.get_hosp_contact_info(card.hospital_name) notes = request.form['submit_treatment'] if phys.seek_treatment(GLOBAL_CARD_PATH, notes, hosp_contact_info[ADDRESS], hosp_contact_info[PORT]): patient_msg.seek_treatment_msg_response(True) GLOBAL_CARD_PATH = None response = "Successfully wrote to hospital db for %s" % card.patient_name else: patient_msg.seek_treatment_msg_response(False) GLOBAL_CARD_PATH = None response = "Unsuccessful update." else: response = "Error: missing patient consent for treatment" elif 'update_phys_treatment_msgs' in request.form: response = PHYSICIAN_TREATMENT_MSGS else: response = ["Error: Flask request not recognized"] return jsonify(response)
def handle_message(message): type = message.get(TYPE) if type == patient_msg.SEEK_TREATMENT: global PHYSICIAN_TREATMENT_MSGS card_path = message.get(patient_msg.CARD_PATH) phys_port = PHYSICIAN_PORT[phys.name] resp = post('http://127.0.0.1:%s/dashboard/physician/%s' % (phys_port, phys.name), data={'seek_treatment': card_path} ) if resp.text == 'None': return patient_msg.seek_treatment_msg_response(False) else: PHYSICIAN_TREATMENT_MSGS.append(resp.text) return patient_msg.seek_treatment_msg_response(True) elif type == patient_msg.PHYS_READ: global PHYSICIAN_READ_MSGS card_path = message.get(patient_msg.CARD_PATH) card = card_helper.get_card_object(card_path) print("-------> Request to read medical records for %s" % card.patient_name) PHYSICIAN_READ_MSGS.append("-------> Request to read medical records for %s" % card.patient_name) # API Call # hosp_contact_info = parser.get_hosp_contact_info(card.hospital_name) success, records = phys.read_patient_record(card_path, hosp_contact_info[ADDRESS], hosp_contact_info[PORT]) if success: resp = patient_msg.phys_read_response_msg(True) for r in records: PHYSICIAN_READ_MSGS.append(r) return resp PHYSICIAN_READ_MSGS.append("-------> Could not read medical records for %s" % card.patient_name) resp = patient_msg.phys_read_response_msg(False) return resp elif type == patient_msg.PHYS_TRANSFER: card_path = message.get(patient_msg.CARD_PATH) card = card_helper.get_card_object(card_path) src_hospital = message.get(patient_msg.SRC_HOSPITAL) print("-------> Request to transfer medical records for %s" %(card.patient_name)) # TODO: Don't hardcode this. dest_hospital = "hospital_3" # Validate params before making API call. if not parser.valid_hosp(dest_hospital): print("ERROR: invalid hospital - %s" %(parser.get_hosp_names_string())) return patient_msg.phys_transfer_response_msg(False) src_hosp_contact_info = parser.get_hosp_contact_info(src_hospital) # API Call # if phys.transfer(card_path, src_hospital, src_hosp_contact_info[ADDRESS], src_hosp_contact_info[PORT], dest_hospital): return patient_msg.phys_transfer_response_msg(True) return patient_msg.phys_transfer_response_msg(False) else: print("ERROR: unknown type %s" %(type))
def register(self, hospital_address, hospital_port): """ Function to register with a hospital. :param hospital_address: Hospital address :param hospital_port: Hospital port :return: boolean """ response = self.send_msg( patient_msg.register_msg(self.name, self.patient_id), hospital_address, hospital_port) if isinstance(response, int): print("ERROR: Hospital server error") return False # Check if appropriate response is received. if response.get(constants.TYPE) != patient_msg.REGISTER_RESPONSE: print( "ERROR: incorrect response type from hospital, should have received %s" % (patient_msg.REGISTER_RESPONSE)) return False # Hospital returns a boolean. if response.get(patient_msg.RESPONSE): print("Obtained card from hospital.") self.card = card_helper.get_card_object(self.card_path) return True else: print("Unable to register with hospital.") return False
def phys_transfer(self, src_hospital, phys_address, phys_port): """ Function for patient to give permission to physician to transfer medical records. :param src_hospital: Current hospital :param phys_address: Physician address :param phys_port: Physician port :return: boolean """ if self.card: response = self.send_msg( patient_msg.phys_transfer_msg(self.card_path, src_hospital), phys_address, phys_port) if isinstance(response, int): print("ERROR: Hospital server error") return False # Physician will return a boolean. if response.get(patient_msg.RESPONSE): self.card = card_helper.get_card_object(self.card_path) print( "Records have been transferred and card has been updated") return True else: print("Unsuccessful transfer") return False print("ERROR: Must register with a hospital first") return False
def transfer(self, src_hospital_address, src_hospital_port, dst_hospital): """ Function to transfer data to another hospital :param src_hospital_address: Current hospital address :param src_hospital_port: Current hospital port :param dst_hospital: New hospital :return: boolean """ if self.card: response = self.send_msg( patient_msg.transfer_msg(self.card_path, dst_hospital), src_hospital_address, src_hospital_port) if isinstance(response, int): print("ERROR: Hospital server error") return False # Hospital returns a boolean. if response.get(patient_msg.RESPONSE): self.card = card_helper.get_card_object(self.card_path) print( "Records have been transferred and card has been updated") return True else: print("Unsuccessful transfer") return False print("ERROR: Must register with a hospital first") return False
def read_patient_record(self, card_path, hospital_address, hospital_port): """ Function to handle patient issuing request for physician to read their medical data. :param card_path: Patient card :param hospital_address: Hospital address :param hospital_port: Hospital port :return: boolean, decrypted medical records """ records = [] card = card_helper.get_card_object(card_path) if card == None: return False, records if not self.hospital_affiliation_valid(card.hospital_name): return False, records # Copied patient code. socket = self.send_msg_get_socket(patient_msg.read_msg(card.uid), hospital_address, hospital_port) if isinstance(socket, int): return False, records data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: if isinstance(response, int): socket.close() return False, records if response.get(patient_msg.RESPONSE): r = crypto.decrypt(response.get(patient_msg.BLOCK), card.priv_key) print(r) records.append(r) num_blocks = response.get(patient_msg.NUM_BLOCKS) # Special casing the first response before reading the rest. for i in range(1, num_blocks): data = socket.recv(MESSAGE_SIZE) if data: responses = constants.deserialize(data) for response in responses: r = crypto.decrypt( response.get(patient_msg.BLOCK), card.priv_key) print(r) records.append(r) # print(crypto.decrypt(response.get(patient_msg.BLOCK), card.priv_key)) else: print("No records were retrieved.") socket.close() return False, records socket.close() return True, records
def seek_treatment(self, card_path, notes, hospital_address, hospital_port): """ Function to handle patient request and to write to hospital db k,v store. :param card_path: Patient card :param notes: Physician notes :param hospital_address: Hospital address :param hospital_port: Hospital port :return: boolean """ card = card_helper.get_card_object(card_path) medical_record = MedicalRecord(self.name, card) # Obtain max note length. max_note_length = phys_msg.get_note_msg_length(card_path, str(medical_record), self.physician_id) #print("max note length %d" %(max_note_length)) notes = (notes[:max_note_length] + '..') if len(notes) > max_note_length else notes medical_record.notes = notes response = self.send_msg( phys_msg.write_msg(card_path, str(medical_record), self.physician_id), hospital_address, hospital_port) if isinstance(response, int): print("ERROR: Hospital server error") return if response.get(constants.TYPE) != phys_msg.WRITE_RESPONSE: print( "ERROR: incorrect response type from hospital, should have received %s" % (phys_msg.WRITE_RESPONSE)) return # Hospital returns a boolean. if response.get(phys_msg.RESPONSE): print("Successfully wrote to hospital db") return True else: print("Write was not successful") return False
def handle_message(message): type = message.get(TYPE) if type == patient_msg.REGISTER: patient_name = message.get(patient_msg.PATIENT_NAME) patient_id = message.get(patient_msg.PATIENT_ID) print("-------> Register Patient %s" %(patient_name)) HOSPITAL_MSGS.append("-------> Register patient request %s" %(patient_name)) # API Call # card = h.register_patient(patient_name, patient_id) if card == None: # Unsuccessful registration. HOSPITAL_MSGS.append("-------> Unsuccessful patient %s registration" %(patient_name)) return patient_msg.register_response_msg(False) else: # Successful registration. card_path = parser.get_patient_card(patient_name) priv_key_path = parser.get_patient_priv_key_path(patient_name) # Store the private key. crypto.store_private_key(priv_key_path, card.priv_key) # Update card to store the location of where the private key is stored. card.priv_key = priv_key_path # Store contents of card in file. f = open(card_path, "w+") f.write(str(card)) f.close() HOSPITAL_MSGS.append("-------> Successful patient %s registration" %(patient_name)) return patient_msg.register_response_msg(True) elif type == phys_msg.REGISTER: physician_name = message.get(phys_msg.PHYSICIAN_NAME) physician_id = message.get(phys_msg.PHYSICIAN_ID) print("-------> Register Physician %s" %(physician_name)) HOSPITAL_MSGS.append("-------> Register physician request %s" %(physician_name)) # API Call # response = h.register_physician(physician_name, physician_id) if response: HOSPITAL_MSGS.append("-------> Successful physician %s registration" %(physician_name)) return phys_msg.register_response_msg(True) else: HOSPITAL_MSGS.append("-------> Unsuccessful physician %s registration" %(physician_name)) return phys_msg.register_response_msg(False) elif type == phys_msg.WRITE: physician_id = message.get(phys_msg.PHYSICIAN_ID) med_rec_string = message.get(phys_msg.MEDICAL_RECORD) card_path = message.get(phys_msg.CARD_PATH) # Convert params into objects. card = card_helper.get_card_object(card_path) med_rec = medical_record.get_medical_record(med_rec_string) print("-------> Write Request") HOSPITAL_MSGS.append("-------> Write Request") response = h.write(card, med_rec, physician_id) if response: HOSPITAL_MSGS.append("-------> Successful write request") return phys_msg.write_response_msg(True) else: HOSPITAL_MSGS.append("-------> Unsuccessful write request") return phys_msg.write_response_msg(False) elif type == patient_msg.READ: card_uid = message.get(patient_msg.CARD_UID) print("-------> Read Request") HOSPITAL_MSGS.append("-------> Read Request") blocks = h.read(card_uid) return blocks elif type == patient_msg.REMOVE: card_path = message.get(patient_msg.CARD_PATH) # Convert params into objects. card = card_helper.get_card_object(card_path) print("-------> Remove Request from %s" %(card.patient_name)) HOSPITAL_MSGS.append("-------> Remove Request from %s" %(card.patient_name)) response = h.remove(card) if response: return patient_msg.remove_response_msg(True) else: return patient_msg.remove_response_msg(False) elif type == patient_msg.TRANSFER: card_path = message.get(patient_msg.CARD_PATH) dest_hosp_name = message.get(patient_msg.DEST_HOSPITAL) # Convert params into objects. card = card_helper.get_card_object(card_path) dest_hosp_contact_info = parser.get_hosp_contact_info(dest_hosp_name) print("-------> Transfer Request from %s" %(card.patient_name)) HOSPITAL_MSGS.append("-------> Transfer Request from %s" %(card.patient_name)) response = h.transfer(card, card_path, dest_hosp_name, dest_hosp_contact_info[ADDRESS], dest_hosp_contact_info[PORT]) if response: HOSPITAL_MSGS.append("-------> Successful transfer request for %s" %(card.patient_name)) return patient_msg.transfer_response_msg(True) else: HOSPITAL_MSGS.append("-------> Unsuccessful transfer request for %s" %(card.patient_name)) return patient_msg.transfer_response_msg(False) elif type == hospital_msg.TRANSFER_WRITE: db_key = message.get(hospital_msg.DB_KEY) block = message.get(hospital_msg.BLOCK) print("---------> Transfer Write") HOSPITAL_MSGS.append("---------> Transfer Write") response = h.transfer_write(db_key, block) if response: HOSPITAL_MSGS.append("---------> Successful transfer write") return hospital_msg.transfer_write_response_msg(True) else: HOSPITAL_MSGS.append("---------> Unsuccessful transfer write") return hospital_msg.transfer_write_response_msg(False) else: print("ERROR: unknown type %s" %(type))