Skip to content

Commit

Permalink
fix: add default web_host = arenaxr.org
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Aug 1, 2023
1 parent 2b660c0 commit aa7ab26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
30 changes: 16 additions & 14 deletions arena/arena_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ssl
import sys
from datetime import datetime
import threading
import threading

import paho.mqtt.client as mqtt

Expand All @@ -26,6 +26,7 @@ class ArenaMQTT(object):

def __init__(
self,
web_host = "arenaxr.org",
realm = "realm",
network_latency_interval = 10000, # run network latency update every 10s
on_msg_callback = None,
Expand All @@ -41,7 +42,8 @@ def __init__(
self.web_host = kwargs["host"]
print(f"Using Host from 'host' input parameter: {self.web_host}")
else:
sys.exit("ARENA webserver host argument (host) is unspecified or None, aborting...")
# Use default "web_host", helps avoid and web vs mqtt host and other user setup confusion
self.web_host = web_host

if os.environ.get("REALM"):
self.realm = os.environ["REALM"]
Expand Down Expand Up @@ -139,11 +141,11 @@ def __init__(
self.mqttc.on_connect = self.on_connect
self.mqttc.on_disconnect = self.on_disconnect
self.mqttc.on_publish = self.on_publish

# setup msg counters
self.msg_io = { 'last_rcv_time': None, 'last_pub_time': None, 'rcv_msgs': 0, 'pub_msgs': 0, 'rcv_msgs_per_sec': 0.0, 'pub_msgs_per_sec': 0.0}
self.msg_rate_time_start = datetime.now()

# add main message processing + callbacks loop to tasks
self.run_async(self.process_message)

Expand All @@ -154,12 +156,12 @@ def __init__(
# update message rate every second
self.run_forever(self.msg_rate_update,
interval_ms=1000)

self.msg_queue = asyncio.Queue()

# setup event to let others wait on connection
# setup event to let others wait on connection
self.connected_evt = threading.Event()

# connect to mqtt broker
if "port" in kwargs:
port = kwargs["port"]
Expand All @@ -178,9 +180,9 @@ def __init__(

# 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', 'all_objects', 'msg_io'),
if enable_interp:
self.cmd_interpreter = ArenaCmdInterpreter(self,
show_attrs=('config_data', 'scene', 'users', 'all_objects', 'msg_io'),
get_callables=('persisted_objs', 'persisted_scene_option', 'writable_scenes', 'user_list'))
self.cmd_interpreter.start_thread(self.connected_evt)

Expand Down Expand Up @@ -235,7 +237,7 @@ def msg_rate_update(self):
if elapsed.seconds > 0:
self.msg_io['rcv_msgs_per_sec'] = round(self.msg_io['rcv_msgs'] / elapsed.seconds, 2)
self.msg_io['pub_msgs_per_sec'] = round(self.msg_io['pub_msgs'] / elapsed.seconds, 2)

def run_once(self, func=None, **kwargs):
"""Runs a user-defined function on startup"""
if func is not None:
Expand Down Expand Up @@ -311,16 +313,16 @@ def on_connect(self, client, userdata, flags, rc):
# listen to all messages in scene
client.subscribe(self.subscribe_topic)
client.message_callback_add(self.subscribe_topic, self.on_message)

# set event
self.connected_evt.set()

# reset msg rate time
self.msg_rate_time_start = datetime.now()

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

else:
print(f"Connection error! Result code={rc}")

Expand Down
2 changes: 2 additions & 0 deletions arena/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Device(ArenaMQTT):

def __init__(
self,
host = "arenaxr.org",
realm = "realm",
network_latency_interval = 10000, # run network latency update every 10s
on_msg_callback = None,
Expand Down Expand Up @@ -50,6 +51,7 @@ def __init__(
sys.exit("Device argument (device) is unspecified or None, aborting...")

super().__init__(
host,
realm,
network_latency_interval,
on_msg_callback,
Expand Down
6 changes: 4 additions & 2 deletions arena/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Scene(ArenaMQTT):

def __init__(
self,
host = "arenaxr.org",
realm = "realm",
network_latency_interval = 10000, # run network latency update every 10s
on_msg_callback = None,
Expand Down Expand Up @@ -60,6 +61,7 @@ def __init__(
sys.exit("Scene argument (scene) is unspecified or None, aborting...")

super().__init__(
host,
realm,
network_latency_interval,
on_msg_callback,
Expand Down Expand Up @@ -101,8 +103,8 @@ async def process_message(self):
msg = await self.msg_queue.get()
except RuntimeError as e:
print(f"Ignoring error: {e}")
return
return

# extract payload
try:
payload_str = msg.payload.decode("utf-8", "ignore")
Expand Down

0 comments on commit aa7ab26

Please sign in to comment.