Example #1
0
 def __receive_pickle_update_req(self, conn):
     """ This function is used to receive pickle file
     :param (connection)conn
     """
     response = "recieve_pickle"
     socket_utils.sendJson(conn, {"Response": response})
     socket_utils.sendPickle(conn, self.__pickle_path)
Example #2
0
def socket_client():
    """This function is used for testing purposes"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        # while True:
        # message = input("Enter message (blank input to end): ")
        # if(not message):
        #     socket_utils.sendJson(s, { "end": True })
        #     break

        socket_utils.sendJson(
            s, {
                "user_email": "*****@*****.**",
                "name": "Alexa",
                "status": "logged in"
            })
        object = socket_utils.recvJson(s)

        print("Received:")
        print(object)

        print("Disconnecting from server.")
    print("Done.")
def login(user):
    """
    Method that validates and enables the user to login to the library server.

    Parameters:
        user: Assigns to the user that is logged in at that instance.
    """

    sys.path.append("..")

    DB_NAME = "user.db"

    with open("config.json", "r") as file:
        data = json.load(file)

        HOST = data["masterpi_ip"]  # The server's hostname or IP address.
        PORT = 13942                # The port used by the server.
        ADDRESS = (HOST, PORT)

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        print("Logging in as {}".format(user["username"]))
        socket_utils.sendJson(s, user)

        print("Waiting for Master Pi...")

        while(True):
            object = socket_utils.recvJson(s)
            if("logout" in object):
                print("Master Pi logged out.")
                print()
                break
    def run(self):
        """
        start the socket server 
        """
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind(self.ADDRESS)
            s.listen()

            print("Listening on {}...".format(self.ADDRESS))
            while True:
                print("Waiting for Reception Pi...")
                conn, addr = s.accept()
                with conn:
                    print("Connected to {}".format(addr))
                    print()

                    user = socket_utils.recvJson(conn)
                    print('user: '******'realName'])
                    print("username: "******"logout": True})
Example #5
0
 def __update_report_status(self, reportid, status, conn):
     """ This function is used to update the status of the selected report id
     :param (int)reportid, (str)status, (connection)conn
     """
     response = requests.get("http://localhost:8080/api/reportstatus/" +
                             str(reportid) + "/" + status)
     data = response.json()
     socket_utils.sendJson(conn, {"Response": data['message']})
Example #6
0
 def __get_engineer_details(self, car_id, conn):
     """ This function is used to receive MAC address of the assigned enginner
     :param (str)car_id, (connection)conn
     """
     response = requests.get("http://localhost:8080/api/engineers/" +
                             car_id)
     data = response.json()
     socket_utils.sendJson(conn, {"Response": data})
Example #7
0
 def __receive_booking_details(self, username, booking_id, car_id, conn):
     """ This function is used to receive booking details
     :param (str)username, (str)booking_id, (str)car_id, (connection)conn
     """
     response = requests.get("http://localhost:8080/api/booking/" +
                             booking_id + "/" + username + "/" + car_id)
     data = response.json()
     print(data['isValidBooking'])
     socket_utils.sendJson(conn, {"Response": data['isValidBooking']})
Example #8
0
def login(user):
    """
 - Prompts the user to Login and connects with Master Pi
 - Gives the user 2 options with their car
 - Either to Unlock or Return"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        print("Logging in as {}".format(user["username"]))

        print("Waiting for Master Pi...")
        while(True):
            print()
            choice = input("""
Please choose to either:
 1: Unlock Car
 2: Return Car

 Please enter your choice: """)
            if choice == "1":  
                user["finish"] = 0
                break             
            elif choice == "2":
                
                user["finish"] = 1
                break
                
            else:
                print("\n You must only select either 1 or 2")
                print(" Please try again\n")
                print()

        socket_utils.sendJson(s, user)
        while(True):
            
            object = socket_utils.recvJson(s)
            if("authenticated" in object):
                print("Car Unlocked")
                print()
                break
                
            elif("returned" in object):
                print("Car Returned")
                print()
                break
            elif("nope" in object):
                print("Not Authorized")
                print()
                break
            elif("nobooking" in object):
                print("No Booking Found")
                print()
                break
Example #9
0
    def send_data(self, obj):
        """Function to send json through socket"""
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            print("Connecting to {}...".format(self.ADDRESS))
            s.connect(self.ADDRESS)
            print("Connected.")

            socket_utils.sendJson(s,obj)
            data = s.recv(4096)
            print("Data received")
        return data
Example #10
0
    def mainRun(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            print("Connecting to {}...".format(self.ADDRESS))
            s.connect(self.ADDRESS)
            print("Connected.")

            print("Logging in as {}".format(self.jsonData))
            socket_utils.sendJson(s, self.jsonData)

            print("Waiting for Master Pi...")
            while (True):
                object = socket_utils.recvJson(s)
                if ("logout" in object):
                    print("Master Pi logged out.")
                    print()
                    break
def login(user):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        print("Logging in as {}".format(user["username"]))
        socket_utils.sendJson(s, user)

        print("Waiting for Master Pi...")
        while(True):
            object = socket_utils.recvJson(s)
            if("logout" in object):
                print("Master Pi logged out.")
                print()
                break
Example #12
0
 def __receive_credentials(self, enc_password, username, conn):
     """ This function is used to receive credentials
     :param (str)enc_password, (str)username, (connection)conn
     """
     response = requests.post("http://localhost:8080/api/login", {
         "email": username,
         "password": enc_password
     })
     print(response)
     data = response.json()
     # data = json.loads(response.text)
     isValidUser = False
     if data and data['email'] == username:
         isValidUser = True
     print(str(isValidUser) + "-------------is valid user---")
     socket_utils.sendJson(conn, {"Response": isValidUser})
Example #13
0
    def run(self):
        '''Start running the daemon'''

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind(self._addr)
            s.listen()

            print("Listening on {}...".format(self._addr))
            while True:
                print("Waiting for Agent...")
                conn, addr = s.accept()
                with conn:
                    print("Accepted connection from {}...".format(addr))

                    data = socket_utils.recvJson(conn)
                    resp = self._updateDetails(data)
                    socket_utils.sendJson(conn, resp)
def main():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        s.listen()

        print("Listening on {}...".format(ADDRESS))
        while True:
            print("Waiting for Reception Pi...")
            conn, addr = s.accept()
            with conn:
                print("Connected to {}".format(addr))
                print()

                user = socket_utils.recvJson(conn)
                menu(user)

                socket_utils.sendJson(conn, {"logout": True})
Example #15
0
    def _execute(self, payload):
        '''Execute command via TCP connection to Master Pi

        Args:
            payload (dict): data being transfer to Master Pi

        Returns:
            (dict) result including rc and an account dict
        '''

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect(
                (self._conf["master"]["host"], self._conf["master"]["port"]))
            socket_utils.sendJson(s, payload)
            resp = socket_utils.recvJson(s)

        return resp
Example #16
0
    def __send_json(self, json):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            print("Connecting to {}...".format(self.__ADDRESS))
            s.connect(self.__ADDRESS)
            print("Connected.")

            socket_utils.sendJson(s, json)

            print("Waiting for Master Pi...")
            while(True):
                object = socket_utils.recvJson(s)
                if object:
                    #print("object is", object)
                    if(object['Response']=="recieve_pickle"):
                        pickle_data = socket_utils.recvPickle(s, self.__pickle_path)
                        if (pickle_data):
                            print("pickle updated successfully")
                    break
Example #17
0
def login(user):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        print("Logging in as {}".format(user))
        #Sending information to the master pi
        socket_utils.sendJson(s, user)

        print("Waiting for Master Pi...")
        #Loop to wait for the master pi to send logout = True
        while(True):
            object = socket_utils.recvJson(s)
            if("logout" in object):
                print("Master Pi logged out.")
                print()
                break
Example #18
0
def queryConnect():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        s.listen()

        print("Listening on {}...".format(ADDRESS))
        while True:
            print("Waiting for authentication...")
            conn, addr = s.accept()
            with conn:
                print("Connected to {}".format(addr))
                print()

                user = socket_utils.recvJson(conn)
                username = user[0]
                email = user[2]
                Menu().userMenu(username, email)
                socket_utils.sendJson(conn, {"logout": True})
Example #19
0
def socket_client(user_email):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        print("Connecting to {}...".format(ADDRESS))
        s.connect(ADDRESS)
        print("Connected.")

        user = get_user(user_email)
        print("Logging in as {}".format(user_email))
        socket_utils.sendJson(s, user)
        while True:
            obj = socket_utils.recvJson(s)
            if obj["status"] == "logged out":
                print("Logging out ...")
                curs.execute(
                    "UPDATE library_users SET status=%s WHERE email=%s",
                    ("logged out", user_email))
                conn.commit()
                print("{} has logged out".format(user_email))
                break
Example #20
0
def main():
    #setting up the socket and listening on the Address
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        s.listen()

        print("Listening on {}...".format(ADDRESS))
        while True:
            print("Waiting for Reception Pi...")
            conn, addr = s.accept()
            with conn:
                print("Connected to {}".format(addr))
                print()

                #recieving information from the reception pi
                user = socket_utils.recvJson(conn)
                #HERE IS WHERE OUR MENU WILL BE PLACED
                menu(user)

                #closing the connection by sending logout = True
                socket_utils.sendJson(conn, {"logout": True})
def main():
    """
    A main method that calls connects the user to the library server.
    """
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        s.listen()

        print("Listening on {}...".format(ADDRESS))
        while True:
            print("Waiting for Reception Pi...")
            conn, addr = s.accept()
            with conn:
                print("Connected to {}".format(addr))
                print()

                user = socket_utils.recvJson(conn)
                LMSUserDatabaseUtils().addUser(user)
                LMSUserDatabaseUtils().getUser(user)
                menu(user)

                socket_utils.sendJson(conn, {"logout": True})
def main():
    with DatabaseUtils() as db:
        db.createCustomerTable()
        db.createCarTable()
        db.createBookHistoryTable()
        print(db.getCustomer())

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        s.listen()

        print("Listening on {}...".format(ADDRESS))
        while True:
            print("Waiting for Reception Pi...")
            conn, addr = s.accept()
            with conn:
                print("Connected to {}".format(addr))
                print()

                user = socket_utils.recvJson(conn)
                menu(user)

                socket_utils.sendJson(conn, {"logout": True})
Example #23
0
 def __receive_credentials(self, enc_password, username, conn):
     """ This function is used to receive credentials
     :param (str)enc_password, (str)username, (connection)conn
     """
     response = "receive cred : " + enc_password + username
     socket_utils.sendJson(conn, {"Response": response})
Example #24
0
 def __receive_lock_details(self, lock_time, car_id, duration, conn):
     response = "lock_details: " + lock_time + car_id + duration
     socket_utils.sendJson(conn, {"Response": response})
Example #25
0
                else:
                    print('Invalid Input!')
                    time.sleep(2)
            except Exception as e:
                print(e)
                time.sleep(2)


if __name__ == '__main__':
    # Opening a socket server and wait for Reception's Pi to respond
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(ADDRESS)
        # Infinite loop to prevent program frond exiting
        while True:
            print("Waiting for Reception Pi...")
            s.listen()
            conn, addr = s.accept()
            print("Connected")
            object = socket_utils.recvJson(conn)
            if ("end" in object):
                break
            # Notify users that their information has been received
            print("Received")
            menu = Menu.get_instance()
            # Invoke main menu
            menu.display_menu(object['name'], object['user_email'])
            # Return the user JSON with a changed status to let Reception Pi knows the user wants to log out
            object['status'] = "logged out"
            # print("Sending data back.")
            socket_utils.sendJson(conn, object)
Example #26
0
 def __receive_pickle_update_req(self, conn):
     response = "recieve_pickle"
     socket_utils.sendJson(conn, {"Response": response})
     socket_utils.sendPickle(conn, self.__pickle_path)
import pprint
import socket
import socket_utils

# HOST = input("Enter IP address of server: ")

HOST = "10.247.202.192"  # The server's hostname or IP address.
PORT = 64000  # The port used by the server.
ADDRESS = (HOST, PORT)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    print("Connecting to {}...".format(ADDRESS))
    s.connect(ADDRESS)
    print("Connected.")

    while True:
        message = input("Enter message (blank input to end): ")
        if not message:
            socket_utils.sendJson(s, {"end": True})
            break

        socket_utils.sendJson(s, {"message": message})
        object = socket_utils.recvJson(s)

        print("Received:")
        pprint.pprint(object)
        print(object["message"])

    print("Disconnecting from server.")
print("Done.")
Example #28
0
 def __receive_booking_details(self, username, booking_id, car_id, conn):
     """ This function is used to receive booking details
     :param (str)username, (str)booking_id, (str)car_id, (connection)conn
     """
     response = "booking  verification : " + username + booking_id + car_id
     socket_utils.sendJson(conn, {"Response": response})
Example #29
0
 def __receive_lock_details(self, lock_time, car_id, duration, conn):
     """ This function is used to receive lock details
     :param (str)lock_time, (str)car_id, (str) duration, (connection)conn
     """
     response = "lock_details: " + lock_time + car_id + duration
     socket_utils.sendJson(conn, {"Response": response})
Example #30
0
 def __receive_unlock_details(self, unlock_time, car_id, conn):
     """ This function is used to receive unlock details
     :param (str)unlock_time, (str)car_id, (connection)conn
     """
     response = "unlock _details: " + unlock_time + car_id
     socket_utils.sendJson(conn, {"Response": response})