diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..85eb8e6c42b4c3a0681ae50bff06c7b96b0b72c9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.9 +COPY requirements.txt /requirements.txt +RUN pip install -r requirements.txt + +COPY main.py utils.py cgv_connector_pb2.py /app/ +COPY config/ /app/config +WORKDIR "/app" +ENTRYPOINT "python" "main.py" diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..505acb9fe3cc54bb21c73b32fab85715dc0c253c --- /dev/null +++ b/config.yaml @@ -0,0 +1,6 @@ +# enter path to ros3rag/ros3rag.placeB/images +# image_directory: null +image_directory: /data/git/jastadd/ros3rag/ros3rag.placeB/images + +# enter IP or hostname of mqtt broker +mqtt_server: "192.168.0.122" diff --git a/main.py b/main.py index acf96c5f7e7adef0ad25143fdfa64d2f8a544c41..adef5ad15e9da4c82a0dfbfaa86c09aa299f59f2 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,8 @@ import threading import time import base64 import flask +import yaml +import sys import dash from dash import dcc @@ -17,15 +19,11 @@ from google.protobuf import json_format import cgv_connector_pb2 import utils +CONFIG_FILENAME = 'config.yaml' +config = {} external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] -fixed_svg_filename = "ros3rag/ros3rag.placeB/images/2022-05-04T10-19-30-9155.svg" -fixed_svg_filename = "2022-05-04T10-19-30-9155.svg" -# fixed_svg_filename = "" -image_directory = '/data/git/jastadd/ros3rag/ros3rag.placeB/images/' static_image_route = '/static/' -MQTT_SERVER = '192.168.0.122' -# MQTT_SERVER = 'localhost' mqttc = mqtt.Client() # mqtt client connected? @@ -300,7 +298,7 @@ app.layout = html.Div([ html.P(id='model-b-svg-name', style={"margin-left": "15px"}), ], className='row', style=dict(display='flex')), html.Div([ - html.Img(id='model-b-svg-img', src="/static/{}".format(fixed_svg_filename)) + html.Img(id='model-b-svg-img', src="/static/{}".format("undefined.svg")) ], style=dict(position='fixed', overflow='scroll', width='100%')), # html.Img(src="data:image/svg;base64,{}".format(base64.b64encode(open(fixed_svg_filename, 'rb').read()).decode())) ], style=tab_style, selected_style=tab_style) # Tab "SVG Model B" @@ -558,6 +556,7 @@ def update_svg_name(name): @app.server.route('{}<image_path>.svg'.format(static_image_route)) def serve_image(image_path): image_name = '{}.svg'.format(image_path) + image_directory = config['image_directory'] print('{}/{}'.format(image_directory, image_name)) # if image_name not in list_of_images: # raise Exception('"{}" is excluded from the allowed static files'.format(image_path)) @@ -566,7 +565,7 @@ def serve_image(image_path): def on_mqtt_connect(_client, _userdata, _flags, _rc, _properties=None): # Callback for mqtt client when connected - print(f'\nConnected to {MQTT_SERVER} at {datetime.datetime.now().isoformat()}') + print(f'\nConnected to {config["mqtt_server"]} at {datetime.datetime.now().isoformat()}') ready_event.set() mqttc.subscribe(topic='#') threading.Thread(target=publish_test_message).start() @@ -610,12 +609,30 @@ def publish_test_message(): if __name__ == '__main__': print('Starting web-ros3rag') + + try: + with open(CONFIG_FILENAME, 'r') as fd: + config = yaml.safe_load(fd) + except FileNotFoundError as e: + print(f'Could not open {CONFIG_FILENAME}. Aborting.') + sys.exit(1) + + if not config['image_directory']: + print('image_directory not specified. Aborting.') + sys.exit(1) + + dockerized = config.get('docker') + mqttc.on_connect = on_mqtt_connect mqttc.on_disconnect = on_mqtt_disconnect mqttc.on_message = on_mqtt_message mqttc.reconnect_delay_set(max_delay=2) - mqttc.connect_async(MQTT_SERVER) + mqttc.connect_async(config['mqtt_server']) mqttc.loop_start() if not ready_event.wait(2.0): # wait 2 seconds - print('Could not connect to mqtt in time!') - app.run_server(debug=True) + print(f'Could not connect to mqtt at {config["mqtt_server"]} in time!') + + if dockerized: + app.run_server(host='0.0.0.0', port=8050) + else: + app.run_server(debug=True, port=8050) diff --git a/requirements.txt b/requirements.txt index 348442afe293ee5f25271292527488f0cd4c2f81..5ff5d4b71aefc32a22419836471c633a5de886b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ paho-mqtt~=1.6.1 dash~=2.3.1 visdcc~=0.0.50 protobuf~=3.20.0 +pyyaml~=6.0