Skip to content

Commit

Permalink
fix: move interpreter to a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
nampereira committed Jul 25, 2023
1 parent f1812f0 commit 1066565
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 14 additions & 7 deletions arena/arena_mqtt.py
Expand Up @@ -7,6 +7,7 @@
import ssl
import sys
from datetime import datetime
import threading

import paho.mqtt.client as mqtt

Expand Down Expand Up @@ -149,13 +150,8 @@ def __init__(

self.msg_queue = asyncio.Queue()

# check if we want to start the command interpreter
enable_interp = os.getenv("ENABLE_INTERPRETER", 'False').lower() in ('true', '1', 't')
if enable_interp:
self.cmd_interpreter = ArenaCmdInterpreter(self,
show_attrs=('config_data', 'scene', 'users', 'auth', 'all_objects', 'msg_io'),
get_callables=('persisted_objs', 'persisted_scene_option', 'writable_scenes', 'user_list'))
self.run_async(self.cmd_interpreter.cmd_loop_task)
# setup event to let others wait on connection
self.connected_evt = threading.Event()

# connect to mqtt broker
if "port" in kwargs:
Expand All @@ -173,6 +169,14 @@ def __init__(
print(f'MQTT connect error to {self.mqtt_host}, port={port}: Result Code={err}')
self.mqttc.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)

# check if we want to start the command interpreter
enable_interp = os.getenv("ENABLE_INTERPRETER", 'False').lower() in ('true', '1', 't')
if enable_interp:
self.cmd_interpreter = ArenaCmdInterpreter(self,
show_attrs=('config_data', 'scene', 'users', 'auth', 'all_objects', 'msg_io'),
get_callables=('persisted_objs', 'persisted_scene_option', 'writable_scenes', 'user_list'))
self.cmd_interpreter.start_thread(self.connected_evt)


def parse_cli(self):
"""
Expand Down Expand Up @@ -297,6 +301,9 @@ def on_connect(self, client, userdata, flags, rc):

print("Connected!")
print("=====")

# set event
self.connected_evt.set()
else:
print(f"Connection error! Result code={rc}")

Expand Down
14 changes: 8 additions & 6 deletions arena/utils/cmd_interpreter.py
@@ -1,6 +1,6 @@
# A simple command interpreter

import cmd, os, json, asyncio
import cmd, os, json, asyncio, threading, time
from datetime import date, datetime
class ArenaCmdInterpreter(cmd.Cmd):
intro = 'Type help or ? to list available commands.\n'
Expand All @@ -20,10 +20,14 @@ def __init__(self, scene, show_attrs=('config_data', 'scene', 'users', 'auth', '
self._show_attrs = show_attrs
self._get_callables = get_callables

# cmd loop; must externally setup a task/thread to run this
def cmd_loop_task(self):
def __cmd_loop_thread(self, start_cmd_event):
if start_cmd_event: start_cmd_event.wait(5) # try to start cmd last; wait on event with timeout
self.cmdloop()


def start_thread(self, start_cmd_event=None):
t = threading.Thread(name='interpreter_thread', target=self.__cmd_loop_thread, args=(start_cmd_event,))
t.start()

def do_show(self, arg):
if arg not in self._show_attrs:
self.help_show()
Expand Down Expand Up @@ -58,8 +62,6 @@ def do_exit(self, arg):
answer = input("This will terminate the ARENA program. Are you sure [Y/N]? ").lower()
if answer == "y":
print("Exiting...")
loop = asyncio.get_event_loop()
loop.stop()
os._exit(0)

return True
Expand Down

0 comments on commit 1066565

Please sign in to comment.