Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding file watcher and working basic comunication #154

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions manager/libs/applications/compatibility/file_watchdog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import threading
import time
import os
from watchdog.events import FileSystemEventHandler
import watchdog.observers

from src.manager.ram_logging.log_manager import LogManager

class Handler(FileSystemEventHandler):

def __init__(self, file, callback):
self.update_callback = callback
self.file = file
self.hash = None

def on_modified(self, event):
if event.event_type == 'modified':
with open(self.file, 'r') as f:
data = f.read()
if self.hash is None or self.hash != hash(data):
self.hash = hash(data)
self.update_callback(data)

class FileWatchdog(threading.Thread):
def __init__(
self,
file,
callback,
):
super().__init__()
# Create blank file
if not os.path.exists(file):
with open(file, 'w') as f:
f.write("")
event_handler = Handler(file, callback)
self.observer = watchdog.observers.Observer()
self.observer.schedule(event_handler, path=file)
self.observer.start()
self._stop = threading.Event()
LogManager.logger.info("Server Launched")

def run(self) -> None:
try:
while not self._stop.isSet():
time.sleep(1/30)
return
except Exception as ex:
LogManager.logger.exception(ex)

def stop(self) -> None:
self._stop.set()
self.observer.stop()
self.observer.join()

def send(self, data):
pass
7 changes: 0 additions & 7 deletions manager/manager/launcher/launcher_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
}
],
"bt_studio": [
{
"type": "module",
"module": "console",
"display": ":1",
"external_port": 1108,
"internal_port": 5901,
},
{
"type": "module",
"width": 1024,
Expand Down
9 changes: 9 additions & 0 deletions manager/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from src.manager.manager.launcher.launcher_visualization import LauncherVisualization
from src.manager.ram_logging.log_manager import LogManager
from src.manager.libs.applications.compatibility.server import Server
from src.manager.libs.applications.compatibility.file_watchdog import FileWatchdog
from src.manager.manager.application.robotics_python_application_interface import (
IRoboticsPythonApplication,
)
Expand Down Expand Up @@ -156,6 +157,11 @@ def update(self, data):
if self.consumer is not None:
self.consumer.send_message({"update": data}, command="update")

def update_bt_studio(self, data):
LogManager.logger.debug(f"Sending update to client")
if self.consumer is not None:
self.consumer.send_message({"update": data}, command="update")

def on_connect(self, event):
"""
This method is triggered when the application transitions to the 'connected' state.
Expand Down Expand Up @@ -251,6 +257,9 @@ def on_prepare_visualization(self, event):
if visualization_type == "gazebo_rae":
self.gui_server = Server(2303, self.update)
self.gui_server.start()
elif visualization_type == "bt_studio":
self.gui_server = FileWatchdog('/tmp/tree_state', self.update_bt_studio) # TODO: change if type bt
self.gui_server.start()

LogManager.logger.info("Visualization transition finished")

Expand Down