forked from iQuHACK/2022_qutech_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QMpatient.py
71 lines (59 loc) · 2.22 KB
/
QMpatient.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
import socket
import pickle
from _thread import *
from threading import Thread
from QMserver import Datapackage
class QMpatient:
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 2004
def __init__(self, ID, host=DEFAULT_HOST, port=DEFAULT_PORT):
self.ID = ID
self.key = str()
self.host = host
self.port = port
# TODO: create function to apply refreshing of keys
def connect(self):
import PySimpleGUI as sg
patient_socket = socket.socket()
patient_socket.connect((self.host, self.port))
self.key = patient_socket.recv(2048).decode('utf-8')
disp_layout = [
[sg.Image(filename="x.png", key=f"-{self.ID}-", size=(209, 209))]
]
UI = sg.Window(f"Patient {self.ID}", [[sg.Column(disp_layout)]], location=(800, 400))
while True:
try:
print('check0!!')
event, _ = UI.read(timeout=5000)
print('check1!!')
# if event == "Exit" or event == sg.WIN_CLOSED:
# break
print('check1!!')
compressed_data = patient_socket.recv(2048)
print('check2!!')
extracted_file_name = self.attempt_open(compressed_data)
if extracted_file_name:
print('Recognized!!')
UI["-" + str(self.ID) + "-"].update(filename="xray.png")
else:
UI["-" + str(self.ID) + "-"].update(filename='x.png')
# TODO: use thread to branch out the show of image
except ConnectionResetError:
print(f'Server disconnected!')
break
patient_socket.close()
def attempt_open(self, compressed_data):
data_package = pickle.loads(compressed_data)
filename = data_package.unlock(self.key)
return filename
def generate_patients(keys_dict):
return list(QMpatient(ID, key) for ID, key in keys_dict.items)
def startPatient(ID,key):
patient = QMpatient(ID, key)
patient.connect()
if __name__ == "__main__":
patient = QMpatient(ID=1)
# thread = Thread(target=startPatient, args=(patient,))
patient.connect()
# thread.start()
# thread.join()