2023-11-04 19:05:45 +03:00
|
|
|
import cmd
|
|
|
|
import threading
|
2023-12-14 01:07:05 +03:00
|
|
|
from threading import Thread
|
2023-11-04 19:05:45 +03:00
|
|
|
|
2023-11-17 01:16:33 +03:00
|
|
|
from queue import Empty, Queue
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
from equipment.turtle_device import TurtleDevice
|
|
|
|
|
|
|
|
|
|
|
|
class TurtleDeviceThread(threading.Thread):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.device = TurtleDevice()
|
|
|
|
self.queue = Queue()
|
2023-11-17 01:16:33 +03:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
item = self.queue.get()
|
|
|
|
except self.queue.Empty:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if (item == 'exit'):
|
|
|
|
break
|
2023-12-14 01:07:05 +03:00
|
|
|
self.device.execute(*item)
|
2023-11-17 01:16:33 +03:00
|
|
|
self.queue.task_done()
|
|
|
|
|
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-12-14 01:07:05 +03:00
|
|
|
super(NoBlockingTurtleShell, self).__init__()
|
2023-11-17 01:16:33 +03:00
|
|
|
self.turtle_thread = TurtleDeviceThread()
|
2023-12-14 01:07:05 +03:00
|
|
|
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
def do_execute(self, arg):
|
2023-11-17 01:16:33 +03:00
|
|
|
self.turtle_thread.queue.put(arg)
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
def do_exit(self, arg):
|
2023-11-17 01:16:33 +03:00
|
|
|
self.turtle_thread.queue.put('exit')
|
2023-11-04 19:05:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
turtle_thread = TurtleDeviceThread()
|
2023-11-17 01:16:33 +03:00
|
|
|
turtle_thread.start()
|
2023-11-04 19:05:45 +03:00
|
|
|
NoBlockingTurtleShell(turtle_thread).cmdloop()
|