88import mpPy6
99import pandas as pd
1010from PySide6 .QtCore import QThreadPool , Signal
11+ from PySide6 .QtWidgets import QMessageBox
1112from numpy import ndarray
1213
1314from ADScopeControl .controller .mp_AD2Capture .MPCaptDevice import MPCaptDevice
@@ -55,7 +56,6 @@ def __init__(self, ad2capt_model: AD2ScopeModel, start_capture_flag: Value):
5556 self .stream_data_queue = Queue ()
5657 self .capture_data_queue = Queue ()
5758
58-
5959 if start_capture_flag is None :
6060 self .start_capture_flag = Value ('i' , 0 , lock = self .lock )
6161 else :
@@ -65,7 +65,6 @@ def __init__(self, ad2capt_model: AD2ScopeModel, start_capture_flag: Value):
6565 # Number of sa
6666 self .streaming_dqueue : deque = None # a dqueue, initialize later
6767
68-
6968 self .register_child_process (
7069 MPCaptDevice ,
7170 self .stream_data_queue ,
@@ -83,7 +82,16 @@ def __init__(self, ad2capt_model: AD2ScopeModel, start_capture_flag: Value):
8382
8483 self .selected_ain_channel = self .model .analog_in .selected_ain_channel
8584
85+ # Some Signals and slots to connect
86+ self .model .supervisor_information .signals .supervised_changed .connect (self ._on_supervised_changed )
87+ self .model .supervisor_information .signals .supervisor_name_changed .connect (self ._on_supervisor_name_changed )
8688
89+ # Supervision slots
90+ def _on_supervised_changed (self ):
91+ self .logger .info (f"Device is now supervised." )
92+
93+ def _on_supervisor_name_changed (self , name : str ):
94+ self .logger .info (f"Device is supervised by { name } " )
8795
8896 def connect_signals (self ):
8997 self .dwf_version_changed .connect (self ._on_dwf_version_changed )
@@ -115,9 +123,7 @@ def connect_signals(self):
115123
116124 self .device_state_changed .connect (
117125 lambda x : type (self .model .device_information ).device_state .fset (self .model .device_information , x ))
118- self .capture_process_state_changed .connect (
119- lambda x : type (self .model .capturing_information ).device_capturing_state .fset (
120- self .model .capturing_information , x ))
126+ self .capture_process_state_changed .connect (self ._on_capture_process_state_changed )
121127 self .ready_for_recording_changed .connect (
122128 lambda x : type (self .model .capturing_information ).ready_for_recording .fset (
123129 self .model .capturing_information , x ))
@@ -164,8 +170,10 @@ def on_open_device_finished(self, device_handle: int):
164170 self .logger .info (f"Opening device finished with handle { device_handle } " )
165171 self .start_capturing_process ()
166172
173+ @mpPy6 .CProcessControl .register_function (close_device_finished )
167174 def close_device (self ):
168- pass
175+ self .kill_capture_flag .value = int (True )
176+ print ("Closing device" )
169177 # self.close_device()
170178
171179 @mpPy6 .CProcessControl .register_function (capture_process_state_changed )
@@ -204,7 +212,6 @@ def discover_connected_devices(self):
204212 :return:
205213 """
206214
207-
208215 def on_discovered_devices_changed (self , devices : list ):
209216 self .logger .info (f"Discovered devices: { len (devices )} " )
210217 self .logger .debug (f"Discovered devices: { devices } " )
@@ -221,6 +228,19 @@ def update_device_information(self):
221228 def _capture (self ):
222229 raise NotImplementedError
223230
231+ def _on_capture_process_state_changed (self , state ):
232+ self .model .capturing_information .device_capturing_state = state
233+
234+ def read_supervisor_state (self ):
235+ if self .model .supervisor_information .supervised and self .model .supervisor_information .supervisor_model is not None :
236+ self .model .supervisor_information .sweep_start_wavelength = (
237+ self .model .supervisor_information .supervisor_model .laser_config .wl_sweep_start .get ())
238+ self .model .supervisor_information .sweep_stop_wavelength = (
239+ self .model .supervisor_information .supervisor_model .laser_config .wl_sweep_stop .get ())
240+ self .model .supervisor_information .velocity = self .model .supervisor_information .supervisor_model .laser_config .velocity .get ()
241+ self .model .supervisor_information .acceleration = self .model .supervisor_information .supervisor_model .laser_config .acceleration .get ()
242+ self .model .supervisor_information .deceleration = self .model .supervisor_information .supervisor_model .laser_config .deceleration .get ()
243+
224244 def set_ad2_acq_status (self , record ):
225245 if record :
226246 self .model .start_recording = True
@@ -257,7 +277,7 @@ def clear_data(self):
257277 self .model .recorded_samples = []
258278 self .model .recorded_sample_stream = []
259279
260- def set_recorded_data_time_axis (self , func = None ):
280+ def set_recorded_data_time_axis (self , func = None ):
261281
262282 # Create a new column same as the index
263283 self .model .capturing_information .recorded_samples_df ['time (s)' ] = (
@@ -268,7 +288,7 @@ def set_recorded_data_time_axis(self, func = None):
268288
269289 self .model .capturing_information .recorded_samples_df ['time (ms)' ] = (
270290 self .model .capturing_information .recorded_samples_df .index .to_series ().apply (
271- lambda x : (x / self .model .capturing_information .sample_rate )* 1000
291+ lambda x : (x / self .model .capturing_information .sample_rate ) * 1000
272292 )
273293 )
274294
@@ -283,13 +303,19 @@ def set_recorded_data_time_axis(self, func = None):
283303 # self.model.device_capturing_state = AD2Constants.CapturingState.RUNNING()
284304
285305 def create_dataframe (self ):
306+
307+ # self.model.supervisor_information.sweep_start_wavelength
308+ # self.model.supervisor_information.sweep_stop_wavelength
309+ # self.model.supervisor_information.velocity
310+ # self.model.supervisor_information.acceleration
311+ # self.model.supervisor_information.deceleration
312+
286313 self .model .capturing_information .recorded_samples_df = (
287314 pd .DataFrame (self .model .capturing_information .recorded_samples ,
288315 columns = ['Amplitude' ]))
289316
290317 self .set_recorded_data_time_axis ()
291318
292-
293319 def stop_capture (self ):
294320 self .start_capture_flag .value = 0
295321
@@ -326,10 +352,11 @@ def start_device_process(self):
326352
327353 def qt_consume_data (self ):
328354 itoogle = 0
329- while True :
355+ while not self . kill_thread :
330356 t = time .time ()
331357 try :
332- capture_data = self .capture_data_queue .get (block = True )
358+ capture_data = self .capture_data_queue .get (block = True , timeout = 1 )
359+
333360 if isinstance (capture_data , ndarray ):
334361 # print(f"Stream data queue size {len(stream_data)}")
335362 for d in capture_data :
@@ -344,29 +371,32 @@ def qt_consume_data(self):
344371 t_end = time .time ()
345372 # print(f"Time to get data {t_end-t}")
346373 except Exception as e :
347- self .logger .info (f"Timeout reached. No data in queue { self .stream_data_queue .qsize ()} or"
348- f"{ e } " )
374+ pass
375+ #self.logger.info(f"Timeout reached. No data in queue {self.stream_data_queue.qsize()} or"
376+ # f"{e}")
349377 self .logger .info ("Streaming data consume thread ended" )
350378
351379 def qt_stream_data (self ):
352380 itoogle = 0
353- while True :
381+
382+ while not self .kill_thread :
354383 t = time .time ()
355384 try :
356- stream_data = self .stream_data_queue .get (block = True )
385+ stream_data = self .stream_data_queue .get (block = True , timeout = 1 )
357386 if isinstance (stream_data , ndarray ):
358387 # print(f"Stream data queue size {len(stream_data)}")
359388 for d in stream_data :
360- if itoogle == math .ceil (self .model .capturing_information .sample_rate / 1000 ):
389+ if itoogle == math .ceil (self .model .capturing_information .sample_rate / 1000 ):
361390 self .streaming_dqueue .append (d )
362391 itoogle = 0
363392 else :
364393 itoogle = itoogle + 1
365394 t_end = time .time ()
366395 # print(f"Time to get data {t_end-t}")
367396 except Exception as e :
368- self .logger .info (f"Timeout reached. No data in queue { self .stream_data_queue .qsize ()} or"
369- f"{ e } " )
397+ pass
398+ # self.logger.info(f"Timeout reached. No data in queue {self.stream_data_queue.qsize()} or"
399+ # f"{e}")
370400 self .logger .info ("Streaming data consume thread ended" )
371401
372402 def qt_get_state (self ):
@@ -376,9 +406,10 @@ def qt_get_state(self):
376406 # time.sleep(0.1)
377407 self .logger .info ("Status data consume thread ended" )
378408
379-
380409 # ==================================================================================================================
381410 # Destructor
382411 # ==================================================================================================================
383412 def exit (self ):
413+ for c in self .thread_manager .children ():
414+ c .exit ()
384415 self .safe_exit ()
0 commit comments