Example #1
0
    def run(self):
        print("Starting " + self.name)
        global running

        while running:
            received = self.sock.recv(1024)
            json_data = ""

            if not received:
                print("Closing connection")
                break

            if received:
                json_data = received.decode()

            if json_data != "":
                # Messages are send in JSON format
                data = json.loads(json_data)
                print("Received: " + str(json_data))

                if data["command"] == "authlogin":
                    if data["auth"] == "True":
                        # When the server accepts the login request, send a new request to the server in order to retrieve the prescriptions of the patient
                        tosend = {}
                        tosend["command"] = "getprescriptions"
                        print("uid: " + str(uid))
                        tosend["uid"] = uid
                        print("Sending: " + str(json.dumps(tosend)))
                        self.sock.send(json.dumps(tosend).encode())
                    elif data["auth"] == "False":
                        # The authentication failed (because of wrong credentials), exit the program
                        # messagebox.showerror("Error", "Authentication failed")
                        print("Authentication failed")
                        running = False
                        s.shutdown(1)
                if data["command"] == "getprescriptions":
                    # The request to retrieve the prescriptions has been answered, convert them to prescription objects and notify the main thread
                    global prescriptions
                    prescriptions = {}
                    prescriptions = Prescription.from_json_list(data["data"])
                    from_dummy_thread(lambda: {unblock_main()})

        print("Exiting " + self.name)
        from_dummy_thread(lambda: unblock_main())