web-monitor/TableLaptop.py
wildercayden ea72d4d451 Small fix
2025-06-26 16:57:10 -04:00

94 lines
2.4 KiB
Python

from networktables import NetworkTables
import configparser
import tkinter as tk
from tkinter import ttk
import time
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
NetworkTables.initialize()
NetworkTables.startServer()
config = configparser.ConfigParser()
config.read('config.ini')
table_name = config.get('settings', 'tables')
table = NetworkTables.getTable(table_name)
root = tk.Tk()
root.title("TableLaptop")
root.geometry("300x150")
status_label = ttk.Label(root, text="Waiting for data...")
status_label.pack(pady=20)
ttk.Button(root, text="Close", command=root.destroy).pack()
def update_data():
cpu = table.getNumber("CPU Usage", -1)
temp = table.getNumber("Temperature", -1)
mem = table.getNumber("Memory", -1)
status_label.config(text=f"CPU: {cpu}%, Temp: {temp}°C Mem: {mem} MB")
root.after(1000, update_data)
update_data()
# Create second window (graph window)
graph_window = tk.Toplevel(root)
graph_window.title("Live Graphs")
graph_window.geometry("800x900")
# Store time and metric data
x_data = []
cpu_data = []
temp_data = []
mem_data = []
# Create 3 subplots (3 rows, 1 column)
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 9), sharex=True)
fig.tight_layout(pad=3.0)
canvas = FigureCanvasTkAgg(fig, master=graph_window)
canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
def add_data():
timestamp = time.strftime('%H:%M:%S')
x_data.append(timestamp)
cpu = min(max(table.getNumber("CPU Usage", -1), 0), 100)
temp = table.getNumber("Temperature", -1)
mem = table.getNumber("Memory", -1)
cpu_data.append(cpu)
temp_data.append(temp)
mem_data.append(mem)
# Clear each subplot
ax1.clear()
ax2.clear()
ax3.clear()
# Only show last 10 points
x = x_data[-25:]
cpu = cpu_data[-25:]
temp = temp_data[-25:]
mem = mem_data[-25:]
ax1.plot(x, cpu, marker='o', linestyle='-')
ax1.set_title("CPU Usage (%)")
ax1.tick_params(axis='x', rotation=45)
ax2.plot(x, temp, marker='o', linestyle='-', color='orange')
ax2.set_title("Temperature (°C)")
ax2.tick_params(axis='x', rotation=45)
ax3.plot(x, mem, marker='o', linestyle='-', color='green')
ax3.set_title("Memory Usage (MB)")
ax3.set_xlabel("Time")
ax3.tick_params(axis='x', rotation=45)
canvas.draw()
graph_window.after(2000, add_data)
add_data() # Start graph updating loop
root.mainloop()