示例#1
0
class W2Coordinator_Wrapper:

    SERVER_ADDRESS = '/tmp/w2coordinator.sock'

    def create_client(self):
        self.unix_client = UnixClient(self.SERVER_ADDRESS)

    def send_2_coordinator(self, message):
        self.unix_client.send_data(str(message))

    def read_from_robot_coordinator(self):
        self.unix_client.read_data()

    def connect_2_unix_server(self, server_address):
        self.unix_client.connect_client(server_address)
class Robot3Coordinator_Wrapper:

    COMPLETED_MESSAGE = "AT6_FINISHED"
    SERVER_ADDRESS = '/tmp/robot3coordinator.sock'

    def create_client(self):
        self.unix_client = UnixClient(self.SERVER_ADDRESS)

    def send_2_robot_coordinator(self, message):
        self.unix_client.send_data(str(message))

    def read_from_robot_coordinator(self):
        self.unix_client.read_data()

    def connect_2_unix_server(self, server_address):
        self.unix_client.connect_client(server_address)
class RobotCtrl_Wrapper:

    START_MESSAGE_PickAndPlace = '{"PickAndPlace": "STARTED","sender": "/tmp/at4.sock"}'
    COMPLETED_MESSAGE_PickAndPlace = '{"PickAndPlace": "FINISHED","sender": "/tmp/at4.sock"}'
    START_MESSAGE_PickAndInsert = '{"PickAndInsert": "STARTED","sender": "/tmp/at4.sock"}'
    COMPLETED_MESSAGE_PickAndInsert = '{"PickAndInsert": "FINISHED","sender": "/tmp/at4.sock"}'
    START_MESSAGE_ScrewPickAndFasten = '{"ScrewPickAndFasten": "STARTED","sender": "/tmp/at4.sock"}'
    COMPLETED_MESSAGE_ScrewPickAndFasten = '{"ScrewPickAndFasten": "FINISHED","sender": "/tmp/at4.sock"}'
    SERVER_ADDRESS = '/tmp/robot1ctrl.sock'

    def create_client(self):
        self.unix_client = UnixClient(self.SERVER_ADDRESS)

    def send_2_robotctrl(self, message):
        self.unix_client.send_data(message)

    def read_from_robot(self):
        self.unix_client.read_data()

    def connect_2_unix_server(self, server_address):
        self.unix_client.connect_client(server_address)
class RobotController:

    SERVER_ADDRESS = "/tmp/robot2ctrl.sock"

    def __init__(self):
        self.unix_server = UnixServer(self.SERVER_ADDRESS)
        self.unix_server.start()

        print("\n \n Unix Server of Robot Controller has just started \n \n")

#########################################################
#                            CALL MICROSERVICES
#########################################################

    def call_pick_and_place(self, device):
        pickandplace = PickAndPlace()
        return pickandplace.start_working(device)  # pick and place service

    def call_pick_and_insert(self, device):
        pickandinsert = PickAndInsert()
        return pickandinsert.start_working(device)  # pick and insert service

    def call_screw_pick_and_fasten(self, device):
        screwpickandfasten = ScrewPickAndFasten()
        return screwpickandfasten.start_working(
            device)  # screw pick and fasten service

    def call_pick_and_press(self, device):
        return "screw pick and press service "  # pick and press service

    def call_move(self, message, device):
        move = Move()
        return move.start_working(message, device)  # move service

    def call_pick_and_flip_and_press(self, device):
        pickandflipandpress = PickAndFlipAndPress()
        return pickandflipandpress.start_working(
            device)  # pick and flip and press service

    def stop_controller(self):
        return " ad"

    def wrong_message_response(self):
        return "wrong messsage received"

    @staticmethod
    def call_microservice(self, service):
        switcher = {
            "pick_and_place": self.call_pick_and_place(),
            "pick_and_insert": self.call_pick_and_insert(),
            "screw_pick_And_fasten": self.call_screw_pick_and_fasten(),
            "pick_and_press": self.call_pick_and_press(),
            "move": self.call_move(),
            "pick_and_flip_press": self.call_pick_and_flip_press(),
        }
        return switcher.get(service, self.wrong_message_response())

    def start_controller(self, device):
        while True:
            try:
                message_received = self.unix_server.read_data()
                print(message_received)
                sender_address = message_received["sender"]
                print(sender_address)
                if "PickAndPlace" in message_received:
                    response = self.call_pick_and_place(device)
                    if "FINISHED" in response:
                        print("controller free to service another call")
                        self.unix_client = UnixClient(str(sender_address))
                        self.unix_client.connect_client(str(sender_address))
                        self.unix_client.send_data(response)
                        self.unix_client.close_client()
                        print("\n \n \n \n ok")
                if "PickAndInsert" in message_received:
                    response = self.call_pick_and_insert(device)
                    if "FINISHED" in response:
                        print("controller free to service another call")
                        self.unix_client = UnixClient(str(sender_address))
                        self.unix_client.connect_client(str(sender_address))
                        self.unix_client.send_data(response)
                        self.unix_client.close_client()
                if "ScrewPickAndFasten" in message_received:
                    response = self.call_screw_pick_and_fasten(device)
                    if "FINISHED" in response:
                        print("controller free to service another call")
                        self.unix_client = UnixClient(str(sender_address))
                        self.unix_client.connect_client(str(sender_address))
                        self.unix_client.send_data(response)
                        self.unix_client.close_client()
                if "PickAndFlipAndPress" in message_received:
                    response = self.call_pick_and_flip_and_press(device)
                    if "FINISHED" in response:
                        print("controller free to service another call")
                        self.unix_client = UnixClient(str(sender_address))
                        self.unix_client.connect_client(str(sender_address))
                        self.unix_client.send_data(response)
                        self.unix_client.close_client()
                if "Move" in message_received:
                    position = message_received["Move"]
                    response = self.call_move(position, device)
                    print("\n \n \n \n \n " + str(position) +
                          "\n \n \n \n \n ")
                    if "REACHED" in response:
                        print("controller free to service another call")
                        self.unix_client = UnixClient(str(sender_address))
                        self.unix_client.connect_client(str(sender_address))
                        self.unix_client.send_data(response)
                        self.unix_client.close_client()

            except (ConnectionResetError, OSError) as e:
                print(e)