Skip to content

Commit f40e98c

Browse files
corrected text formatting and html monitor template
1 parent 2015f2c commit f40e98c

File tree

5 files changed

+264
-66
lines changed

5 files changed

+264
-66
lines changed

html_monitor_example.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,36 @@
1515
# Initialize SocketManager with Flask-SocketIO instance
1616
manager = SocketManager(socketio=socketio, frequency=20) # 20Hz update rate
1717

18-
text_format = TextFormat(width=6, precision=3, force_sign=True)
18+
text_format = TextFormat(width=9, precision=3, force_sign=True)
19+
20+
data_formats = {
21+
"position_x": text_format,
22+
"position_y": text_format,
23+
"position_z": text_format,
24+
"motor1_speed": None,
25+
"motor1_torque": None,
26+
"motor1_status": None,
27+
"motor2_speed": None,
28+
"motor2_torque": None,
29+
"motor2_status": None,
30+
"motor3_speed": None,
31+
"motor3_torque": None,
32+
"motor3_status": None,
33+
"motor4_speed": None,
34+
"motor4_torque": None,
35+
"motor4_status": None,
36+
}
1937
# Define and add elements to the manager
20-
text_element1 = TextElement(element_id="element1", text_format=text_format)
21-
text_element2 = TextElement(element_id="element2", text_format=text_format)
22-
text_element3 = TextElement(element_id="element3", text_format=text_format)
23-
manager.add_element(text_element1)
24-
manager.add_element(text_element2)
25-
manager.add_element(text_element3)
38+
for name, format in data_formats.items():
39+
text_element = TextElement(element_id=name, text_format=format)
40+
manager.add_element(text_element)
2641

2742
# Set up the Serial Update Server
2843
server = SerialUpdateServer(
2944
manager,
30-
port="/dev/tty.usbserial-1460",
45+
detect_devices=True,
3146
baudrate=115200,
32-
decoder=OrderedDecoder(keys=["element1", "element2", "element3"]),
47+
decoder=OrderedDecoder(keys=data_formats.keys()),
3348
)
3449

3550

src/app_monitor/elements_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ def display(self):
133133
self.text_format.format_text(full_text) if self.text_format else full_text
134134
)
135135

136-
# Format the text with padding
137-
padded_text = full_text.ljust(self.width)
136+
return full_text
138137

139-
return self.add_border(padded_text)
138+
# # Format the text with padding
139+
# padded_text = full_text.ljust(self.width)
140+
141+
# return self.add_border(padded_text)
140142

141143
def get_height(self):
142144
"""Calculate the number of lines the text element occupies."""

src/app_monitor/server.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class SerialUpdateServer(UpdateServer):
5959
def __init__(
6060
self,
6161
monitor_manager,
62+
detect_devices=False,
6263
serial_instance=None,
6364
port="/dev/ttyUSB0",
6465
baudrate=9600,
@@ -67,13 +68,41 @@ def __init__(
6768
super().__init__(monitor_manager)
6869
if serial_instance:
6970
self.serial_connection = serial_instance
70-
self.port = serial_instance.port
71-
self.baudrate = serial_instance.baudrate
71+
elif detect_devices:
72+
self.serial_connection = self.detect_devices(baudrate=baudrate)
7273
else:
7374
self.serial_connection = serial.Serial(port=port, baudrate=baudrate)
74-
self.port = port
75-
self.baudrate = baudrate
76-
self.decoder = decoder
75+
76+
self.port = self.serial_connection.port
77+
self.baudrate = self.serial_connection.baudrate
78+
self.decoder = decoder
79+
80+
def detect_devices(
81+
self,
82+
search: str = r"usb",
83+
pattern: str = "/dev/tty*",
84+
baudrate=9600,
85+
):
86+
87+
def find_devices(search, pattern):
88+
import glob
89+
import re
90+
91+
# List all files matching the pattern /dev/tty*
92+
files = glob.glob(pattern)
93+
94+
# Filter files that contain 'usb'
95+
devices = [f for f in files if re.search(search, f)]
96+
97+
return devices
98+
99+
if devices := find_devices(search, pattern):
100+
for device in devices:
101+
try:
102+
serial_device = serial.Serial(port=device, baudrate=baudrate)
103+
return serial_device
104+
except Exception as e:
105+
logger.error(f"Error connecting to {device}: {e}")
77106

78107
async def start(self, frequency: float = 30):
79108
"""Start the serial reader asynchronously by polling the serial port."""

src/app_monitor/text_formatter.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,21 @@ def __init__(self, text, width=None, precision=None, force_sign=False):
4242
self.force_sign = force_sign
4343

4444
def format_text(self):
45-
# Format the number if width, precision, or force_sign is specified
45+
# Try to convert text to a float if it's a string
4646
if isinstance(self.text, str):
4747
try:
4848
self.text = float(self.text)
4949
except ValueError:
50-
return self.text
50+
return self.text # Return original text if it can't be converted
51+
52+
# If the text is a number, format it accordingly
5153
if isinstance(self.text, (int, float)):
52-
# Build the format specification string
54+
# Determine the sign based on force_sign
5355
sign = "+" if self.force_sign else ""
54-
format_spec = (
55-
f"{sign}{self.width}.{self.precision}f"
56-
if self.precision is not None
57-
else f"{self.width}f"
58-
)
59-
# Format the text according to the specification
60-
return f"{float(self.text):{format_spec}}"
56+
# Build the format specification string
57+
format_spec = f"{sign}0{self.width}.{self.precision}f"
58+
# Format the number according to the specification
59+
return f"{self.text:{format_spec}}"
6160
else:
6261
# If it's not a number, return as-is
6362
return str(self.text)

0 commit comments

Comments
 (0)