2023-11-04 19:05:45 +03:00
|
|
|
import cmd
|
|
|
|
import threading
|
2023-11-17 18:11:30 +03:00
|
|
|
from queue import Empty, Queue
|
2023-11-04 19:05:45 +03:00
|
|
|
from equipment.turtle_device import TurtleDevice
|
2023-12-19 15:41:52 +03:00
|
|
|
import argparse
|
|
|
|
import time
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TurtleDeviceThread(threading.Thread):
|
|
|
|
# TODO(Homework 4)
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.device = TurtleDevice()
|
|
|
|
self.queue = Queue()
|
2023-12-19 15:41:52 +03:00
|
|
|
self.event = Event()
|
2023-11-04 19:05:45 +03:00
|
|
|
|
2023-11-17 17:12:10 +03:00
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
item = self.queue.get()
|
|
|
|
except self.queue.Empty:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if (item == 'exit'):
|
|
|
|
break
|
|
|
|
self.device.execute(item[0], item[1:])
|
2023-12-19 15:41:52 +03:00
|
|
|
self.queue.task_done()
|
|
|
|
def __message_processing__(self):
|
|
|
|
while True:
|
|
|
|
new_message = self.queue.get()
|
|
|
|
#print(type(new_message))
|
|
|
|
#print(new_message)
|
|
|
|
#print("Name and args of action are {}".format(new_message))
|
|
|
|
self.device.execute(*new_message)
|
|
|
|
#time.sleep(30)
|
|
|
|
self.queue.task_done()
|
|
|
|
if self.event.is_set():
|
|
|
|
break
|
|
|
|
def __message_thread__(self):
|
|
|
|
thread = Thread(target = self.__message_processing__, daemon=True)
|
|
|
|
thread.start()
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
class NoBlockingTurtleShell(cmd.Cmd):
|
|
|
|
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
|
|
|
|
prompt = '(turtle) '
|
|
|
|
file = None
|
|
|
|
|
|
|
|
def __init__(self, turtle_thread: TurtleDeviceThread):
|
2023-11-17 18:11:30 +03:00
|
|
|
super(NoBlockingTurtleShell, self).__init__()
|
2023-12-19 15:41:52 +03:00
|
|
|
turtle_thread.__message_thread__()
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
def do_execute(self, arg):
|
2023-12-19 15:41:52 +03:00
|
|
|
turtle_thread.queue.put(parse(arg))
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
def do_exit(self, arg):
|
2023-12-19 15:41:52 +03:00
|
|
|
turtle_thread.event.set()
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
turtle_thread = TurtleDeviceThread()
|
2023-11-17 17:12:10 +03:00
|
|
|
turtle_thread.start()
|
2023-11-04 19:05:45 +03:00
|
|
|
NoBlockingTurtleShell(turtle_thread).cmdloop()
|