@@ -47,7 +47,29 @@ def update(self, element_id, *args):
4747 element .update (* args )
4848 break
4949
50- def update_all_elements (self ):
50+ def generate_element_id_map (self ):
51+ """Generate a list of all element IDs in the monitor manager."""
52+ element_count = 0
53+
54+ def _element_id_generator (elements ):
55+ """Recursively generate element IDs."""
56+ nonlocal element_count
57+ for element in elements :
58+ if isinstance (element , MonitorGroup ):
59+ yield from _element_id_generator (element .elements .values ())
60+ else :
61+ yield element_count , element .element_id
62+ element_count += 1
63+
64+ return {
65+ str (number ): element_id
66+ for number , element_id in _element_id_generator (self .elements )
67+ }
68+
69+
70+ class TerminalManager (MonitorManager ):
71+
72+ def update_screen_buffer (self ):
5173 """Construct the full screen content in a buffer"""
5274 self .buffer = [] # Clear the buffer for the new frame
5375
@@ -73,42 +95,67 @@ async def update_screen_fixed_rate(self, frequency=1):
7395 self .update_screen () # Display the current metrics
7496 await asyncio .sleep (1 / frequency ) # Wait for the next update cycle
7597
76- async def update_all_elements_fixed_rate (self , frequency = 1 ):
98+ async def update_screen_buffer_fixed_rate (self , frequency = 1 ):
7799 """Asynchronously update all elements at a fixed rate."""
78100 assert frequency > 0 , "Frequency must be greater than 0."
79101 while True :
80- self .update_all_elements ()
102+ self .update_screen_buffer ()
81103 await asyncio .sleep (1 / frequency )
82104
83105 async def update_fixed_rate (self , frequency = 30 ):
84106 await asyncio .gather (
85107 self .update_screen_fixed_rate (frequency = frequency ),
86- self .update_all_elements_fixed_rate (frequency = frequency ),
108+ self .update_screen_buffer_fixed_rate (frequency = frequency ),
87109 )
88110
89- def generate_element_id_map (self ):
90- """Generate a list of all element IDs in the monitor manager."""
91- element_count = 0
92111
93- def _element_id_generator (elements ):
94- """Recursively generate element IDs."""
95- nonlocal element_count
96- for element in elements :
97- if isinstance (element , MonitorGroup ):
98- yield from _element_id_generator (element .elements .values ())
99- else :
100- yield element_count , element .element_id
101- element_count += 1
112+ import asyncio
113+ import json
114+ from flask_socketio import SocketIO
102115
103- return {
104- str (number ): element_id
105- for number , element_id in _element_id_generator (self .elements )
106- }
116+
117+ class SocketManager (MonitorManager ):
118+ """Subclass of MonitorManager that adds functionality to push data to a WebSocket."""
119+
120+ def __init__ (self , socketio : SocketIO , frequency = 1 ):
121+ """
122+ Initialize with a SocketIO instance and a push frequency.
123+
124+ :param socketio: SocketIO instance to handle WebSocket communication.
125+ :param frequency: Frequency in Hz for pushing updates to clients.
126+ """
127+ super ().__init__ () # Initialize the parent MonitorManager
128+ self .socketio = socketio
129+ self .frequency = frequency
130+
131+ def to_json (self ):
132+ """Convert all monitor elements to JSON format."""
133+ data = {}
134+ for element in self .elements :
135+ if isinstance (element , MonitorGroup ):
136+ data [element .group_id ] = {
137+ e_id : el .display () for e_id , el in element .elements .items ()
138+ }
139+ else :
140+ data [element .element_id ] = element .display ()
141+ return json .dumps (data )
142+
143+ async def push_data (self ):
144+ """Asynchronously push data to all connected WebSocket clients at the specified frequency."""
145+ while True :
146+ data = self .to_json () # Get data in JSON format
147+ self .socketio .emit ("update" , data ) # Push data to WebSocket clients
148+ await asyncio .sleep (1 / self .frequency ) # Control push frequency
149+
150+ def set_frequency (self , frequency ):
151+ """Set the frequency at which data is pushed to clients."""
152+ assert frequency > 0 , "Frequency must be greater than 0."
153+ self .frequency = frequency
107154
108155
109156async def main ():
110157 # Create a MonitorManager instance
111- manager = MonitorManager ()
158+ manager = TerminalManager ()
112159
113160 # Add a text element and a progress bar
114161 text = TextElement ("Buffers" )
0 commit comments