-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiu.py
More file actions
183 lines (137 loc) · 7.41 KB
/
iu.py
File metadata and controls
183 lines (137 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from time import sleep
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import threading
from multiprocessing import Process
from grafica import Grafica
from grafica_fourier import GraficaFourier
import psutil
from comandos_linux import *
class IU:
def __init__(self, medidas, recorrido, fourier, barrera_principal, barrera_principal_fin, recorrido_saltado, barrera_fourier_fin, posicion_cpu, pids, porcen_cpu):
self.barrera_principal = barrera_principal
self.barrera_principal_fin = barrera_principal_fin
self.porcen_cpu = porcen_cpu
self.posicion_cpu = posicion_cpu
self.pids = pids
self.fourier_a_iu = fourier
self.medidas = medidas
self.recorrido = recorrido
self.recorrido_saltado = recorrido_saltado
#-------------------------------------------
self.ventana = tk.Tk()
self.ventana.title("Simulador")
# Creamos los FRAMES principales
frame_izquierdo = tk.Frame(self.ventana, bg="white", width=50, height=50)
frame_derecho = tk.Frame(self.ventana, bg="white", width=50, height=50)
frame_izquierdo.grid(row=0, column=0)
frame_derecho.grid(row=0, column=1)
# Crear sub-frames para los datos de los procesos
self.frame_carrito = tk.Frame(frame_izquierdo, height=50, bg="white")
self.frame_fourier = tk.Frame(frame_izquierdo, height=50, bg="red")
self.frame_iu= tk.Frame(frame_izquierdo, height=50, bg="white")
self.frame_carrito.pack(fill=tk.X)
self.frame_fourier.pack(fill=tk.X)
self.frame_iu.pack(fill=tk.X)
# Agregar etiquetas a cada sub-frame
self.label_carrito = tk.Label(self.frame_carrito, height=2, text="Datos del Carrito", bg="white")
self.label_carrito.pack(fill=tk.X)
self.label_carrito_cpu = tk.Label(self.frame_carrito, height=2, text="Nº CPU:", bg="white")
self.label_carrito_cpu.pack(fill=tk.X)
self.label_carrito_cpu_porcentaje = tk.Label(self.frame_carrito, height=2, text="% CPU:", bg="white")
self.label_carrito_cpu_porcentaje.pack(fill=tk.X)
self.label_carrito_pid = tk.Label(self.frame_carrito, height=2, text="PID:", bg="white")
self.label_carrito_pid.pack(fill=tk.X)
self.label_fourier = tk.Label(self.frame_fourier, height=2, text="Datos de Fourier", bg="orange")
self.label_fourier.pack(fill=tk.X)
self.label_fourier_cpu = tk.Label(self.frame_fourier, height=2, text="Nº CPU:", bg="orange")
self.label_fourier_cpu.pack(fill=tk.X)
self.label_fourier_cpu_porcentaje = tk.Label(self.frame_fourier, height=2, text="% CPU:", bg="orange")
self.label_fourier_cpu_porcentaje.pack(fill=tk.X)
self.label_fourier_pid = tk.Label(self.frame_fourier, height=2, text="PID:", bg="orange")
self.label_fourier_pid.pack(fill=tk.X)
self.label_iu = tk.Label(self.frame_iu, height=2, text="Datos de IU", bg="white")
self.label_iu.pack(fill=tk.X)
self.label_iu_cpu = tk.Label(self.frame_iu, height=2, text="Nº CPU:", bg="white")
self.label_iu_cpu.pack(fill=tk.X)
self.label_iu_cpu_porcentaje = tk.Label(self.frame_iu, height=2, text="% CPU:", bg="white")
self.label_iu_cpu_porcentaje.pack(fill=tk.X)
self.label_iu_pid = tk.Label(self.frame_iu, height=2, text="PID:", bg="white")
self.label_iu_pid.pack(fill=tk.X)
# Crear sub-frames para las gráficas
self.frame_grafica_real = tk.Frame(frame_derecho, bg="white")
self.frame_grafica_medidas = tk.Frame(frame_derecho, bg="white")
self.frame_grafica_fourier = tk.Frame(frame_derecho, bg="white")
self.frame_grafica_real.grid(row=0, column=0, sticky="nsew")
self.frame_grafica_medidas.grid(row=1, column=0, sticky="nsew")
self.frame_grafica_fourier.grid(row=2, column=0, sticky="nsew")
# Crear instancias de las clases Grafica
self.grafica_real = Grafica()
self.grafica_medidas = Grafica()
self.grafica_fourier = GraficaFourier()
# Cambas
self.ncanvas = [None, None, None]
# Añadir la primera gráfica
self.agregar_grafica(self.frame_grafica_real, self.grafica_real, 0)
# Añadir la segunda gráfica
self.agregar_grafica(self.frame_grafica_medidas, self.grafica_medidas, 1)
# Añadir la tercera gráfica
self.agregar_grafica(self.frame_grafica_fourier, self.grafica_fourier, 2)
def cerrar_ventana(self):
self.ventana.destroy()
def iniciar_iu(self):
hilo_iu = threading.Thread(target=self.actualizar)
hilo_iu.start()
self.ventana.protocol("WM_DELETE_WINDOW", self.cerrar_ventana)
self.ventana.mainloop()
hilo_iu.join()
def agregar_grafica(self, frame, grafica, i):
if self.ncanvas[i] is None:
# Si no hay una gráfica en este índice, crea una nueva
self.ncanvas[i] = FigureCanvasTkAgg(grafica.fig, master=frame)
self.ncanvas[i].get_tk_widget().grid(row=0, column=0, sticky="nsew")
else:
# Si ya hay una gráfica, simplemente actualiza el Canvas
self.ncanvas[i].draw()
def update_info_cpu(self):
self.pids[2] = pid()
self.posicion_cpu[2] = cpu(self.pids[2])
self.porcen_cpu[2] = porcentaje_cpu(self.pids[2])
def update_label_cpu(self):
self.label_carrito_cpu.config(text = f"Nº CPU: {self.posicion_cpu[0]}")
self.label_carrito_cpu_porcentaje.config(text = f"% CPU: {self.porcen_cpu[0]}")
self.label_carrito_pid.config(text = f"PID: {self.pids[0]}")
self.label_fourier_cpu.config(text = f"Nº CPU: {self.posicion_cpu[1]}")
self.label_fourier_cpu_porcentaje.config(text = f"% CPU: {self.porcen_cpu[1]}")
self.label_fourier_pid.config(text = f"PID: {self.pids[1]}")
self.label_iu_cpu.config(text = f"Nº CPU: {self.posicion_cpu[2]}")
self.label_iu_cpu_porcentaje.config(text = f"% CPU: {self.porcen_cpu[2]}")
self.label_iu_pid.config(text = f"PID: {self.pids[2]}")
def actualizar(self):
while True:
self.update_info_cpu()
self.update_label_cpu()
self.update_label_cpu()
self.barrera_principal.wait()
ini = self.recorrido_saltado[0]
fin = self.recorrido_saltado[1]
if ini == fin:
real = self.recorrido.get()
self.update_grafica(self.frame_grafica_real, self.grafica_real, real, 0)
else:
for i in range (ini,fin+1):
real = self.recorrido.get()
self.update_grafica(self.frame_grafica_real, self.grafica_real, real, 0)
medida = self.medidas.get()
self.update_grafica2(self.frame_grafica_medidas, self.grafica_medidas, medida, 1)
if not self.fourier_a_iu.empty():
valores_fourier = self.fourier_a_iu.get()
self.update_grafica(self.frame_grafica_fourier, self.grafica_fourier, valores_fourier, 2)
self.barrera_principal_fin.wait()
def update_grafica2(self, frame, grafica, valor, i):
grafica.update_plot2(valor[0], valor[1])
self.agregar_grafica(frame, grafica, i)
def update_grafica(self, frame, grafica, valor, i):
grafica.update_plot(valor)
self.agregar_grafica(frame, grafica, i)