Compare commits

..

9 commits
main ... main

7 changed files with 110 additions and 30 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
*.pyc
fwledmonitor.service
venv/

16
.vscode/launch.json vendored
View file

@ -1,16 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}

View file

@ -8,7 +8,7 @@ from commands import Commands, send_command
# External Dependencies
import numpy as np
import serial # pyserial
import serial # pip install pyserial, apt install python3-serial
from serial.tools import list_ports

View file

@ -1,6 +1,31 @@
chmod +x run.sh
rm -f fwledmonitor.service || true
cat <<EOF >>./fwledmonitor.service
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_DIR="/opt/fw-led-monitor"
echo "Installing to $INSTALL_DIR..."
sudo mkdir -p "$INSTALL_DIR"
sudo cp "$SCRIPT_DIR/led_system_monitor.py" "$INSTALL_DIR/"
sudo cp "$SCRIPT_DIR/drawing.py" "$INSTALL_DIR/"
sudo cp "$SCRIPT_DIR/monitors.py" "$INSTALL_DIR/"
sudo cp "$SCRIPT_DIR/commands.py" "$INSTALL_DIR/"
sudo cp "$SCRIPT_DIR/requirements.txt" "$INSTALL_DIR/"
sudo cp "$SCRIPT_DIR/run.sh" "$INSTALL_DIR/"
sudo chmod +x "$INSTALL_DIR/run.sh"
sudo chown -R root:root "$INSTALL_DIR"
VENV_DIR="$INSTALL_DIR/venv"
if [ ! -d "$VENV_DIR" ]; then
sudo python3 -m venv "$VENV_DIR"
fi
sudo "$VENV_DIR/bin/pip" install --upgrade pip
sudo "$VENV_DIR/bin/pip" install -r "$INSTALL_DIR/requirements.txt"
rm -f "$SCRIPT_DIR/fwledmonitor.service" || true
cat <<EOF > "$SCRIPT_DIR/fwledmonitor.service"
[Unit]
Description=Framework 16 LED System Monitor
After=network.service
@ -8,14 +33,25 @@ After=network.service
[Service]
Type=simple
Restart=always
WorkingDirectory=$PWD
ExecStart=sh -c "'$PWD/run.sh'"
RestartSec=5
WorkingDirectory=$INSTALL_DIR
ExecStart=/bin/bash $INSTALL_DIR/run.sh
[Install]
WantedBy=default.target
EOF
sudo systemctl stop fwledmonitor
sudo cp fwledmonitor.service /lib/systemd/system
sudo systemctl stop fwledmonitor || true
SERVICE_DIR="/etc/systemd/system"
if ! sudo cp "$SCRIPT_DIR/fwledmonitor.service" "$SERVICE_DIR" 2>/dev/null; then
SERVICE_DIR="/lib/systemd/system"
sudo cp "$SCRIPT_DIR/fwledmonitor.service" "$SERVICE_DIR"
fi
sudo systemctl daemon-reload
sudo systemctl enable fwledmonitor
echo "Installation complete. Service installed to $INSTALL_DIR"
sudo service fwledmonitor start

View file

@ -16,6 +16,14 @@ class DiskMonitor:
self.max_history_size = hysterisis_time
def get(self):
"""
Gets the percentage of the system's disk throughput for reading and writing.
Returns:
A tuple containing the percentages of the system's disk throughput for reading
and writing data as floats between 0.0 and 1.0. This is a ratio based on
the highest disk throughput rate over the history of the monitor.
"""
try:
disk_io = psutil.disk_io_counters()
read_usage = disk_io.read_bytes
@ -52,6 +60,14 @@ class NetworkMonitor:
self.max_history_size = hysterisis_time
def get(self):
"""
Gets the percentage of the system's network usage.
Returns:
A tuple containing the percentages of the system's network usage for sending
and receiving data as floats between 0.0 and 1.0. This is a ratio based on
the highest network usage rate over the history of the monitor.
"""
try:
net_io = psutil.net_io_counters()
sent_usage = net_io.bytes_sent
@ -86,6 +102,12 @@ class CPUMonitor:
self.max_history_size = hysterisis_time
def get(self):
"""
Gets the percentage of the system's CPU usage per core.
Returns:
A list of values between 0.0 and 1.0 which is scaled to the CPU usage per core
"""
try:
cpu_usage = psutil.cpu_percent(percpu=True)
for i in range(self.cpu_count):
@ -108,12 +130,24 @@ class CPUMonitor:
class MemoryMonitor:
@staticmethod
def get():
"""
Gets the percentage of the system's memory usage.
Returns:
A value between 0.0 and 1.0 which is scaled to the memory usage
"""
return psutil.virtual_memory().percent / 100.0
class BatteryMonitor:
@staticmethod
def get():
"""
Gets the percentage of the battery's charge and whether it is plugged in.
Returns:
A tuple containing the battery percentage as a float between 0.0 and 1.0 and whether it is plugged in
"""
battery = psutil.sensors_battery()
if battery is not None:
battery_percentage = battery.percent / 100.0
@ -126,16 +160,25 @@ class BatteryMonitor:
def get_monitor_brightness():
"""
Gets the brightness of the main laptop display so that we can scale the LED Matrix
brightness to match. Handles operating system or configuration specific logic to
retrieve the brightness value.
Returns:
A value between 0.0 and 1.0 which is scaled to the monitor's brightness
"""
try:
if os.name == 'nt':
return wmi.WMI(namespace='wmi').WmiMonitorBrightness()[0].CurrentBrightness / 100.0
else:
try: # First try the dGPU brightness
return int(open('/sys/class/backlight/amdgpu_bl2/brightness', 'r').read()) / 255.0
except: # If that doesn't work, try the iGPU brightness
return int(open('/sys/class/backlight/amdgpu_bl1/brightness', 'r').read()) / 255.0
gpu_designator = os.listdir('/sys/class/backlight')[0] # Get the gpu designator
brightness_max = int(open(f'/sys/class/backlight/{gpu_designator}/max_brightness', 'r').read())
return int(open(f'/sys/class/backlight/{gpu_designator}/brightness', 'r').read()) / brightness_max
except Exception as e:
print(f"Error in get_monitor_brightness(): {e}")
return 1.0
if __name__ == "__main__":
print(get_monitor_brightness())

4
requirements.txt Normal file
View file

@ -0,0 +1,4 @@
pyserial
numpy
psutil
wmi # only for windows

16
run.sh
View file

@ -1,3 +1,15 @@
sudo apt install python3-numpy python3-psutil
#!/bin/bash
set -e
python3 ./led_system_monitor.py
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$SCRIPT_DIR/venv"
if [ ! -d "$VENV_DIR" ]; then
python3 -m venv "$VENV_DIR"
fi
source "$VENV_DIR/bin/activate"
pip install --upgrade pip
pip install -r "$SCRIPT_DIR/requirements.txt"
python "$SCRIPT_DIR/led_system_monitor.py"