Using Events#
The design of the client
, and the SDK in general, is synchronous.
A synchronous design is generally preferred for automation scripts, but in some other cases, like a user interface, an asynchronous design can be preferable.
The SDK has no integration with any asynchronous library (such as asyncio or third-party libraries), but an optional event queue is offered to allow for custom integration.
Note
The Scrutiny GUI is built using QT. The SDK event queue is read in a thread and used to trigger QT signals, making the bridge between the synchronous and asynchronous worlds.
Example#
from scrutiny import sdk
from scrutiny.sdk.client import ScrutinyClient
client = ScrutinyClient()
client.listen_events(ScrutinyClient.Events.LISTEN_DEVICE_READY | ScrutinyClient.Events.LISTEN_DEVICE_GONE | ScrutinyClient.Events.LISTEN_DATALOGGER_STATE_CHANGED )
with client.connect('localhost', 8765):
while True:
event = client.read_event(timeout=0.5)
if event is not None:
if isinstance(event, ScrutinyClient.Events.DeviceReadyEvent):
print(f"Device connected. Session ID : {event.session_id} ")
elif isinstance(event, ScrutinyClient.Events.DeviceGoneEvent):
print(f"Device has disconnected. Session ID : {event.session_id} ")
elif isinstance(event, ScrutinyClient.Events.DataloggerStateChanged):
if event.details.state == sdk.DataloggerState.DataReady:
print(f"Datalogging acquisition ready!")
Methods#
- ScrutinyClient.listen_events(enabled_events, disabled_events=0)[source]#
Select which events are to be listen for when calling
read_event
.- Parameters:
enabled_events (int) – A flag value contructed by ORing values from
ScrutinyClient.Events
disabled_events (int) –
- Raises:
TypeError – Given parameter not of the expected type
ValueError – If the flag value is negative
- Return type:
None
- ScrutinyClient.read_event(timeout=None)[source]#
Read an event from the event queue using a blocking read operation
- Parameters:
timeout (float | None) – Maximum amount of time to block. Blocks indefinetely if
None
- Returns:
The next event in the queue or
None
if there is no events after timeout is expired- Return type:
ConnectedEvent | DisconnectedEvent | DeviceReadyEvent | DeviceGoneEvent | SFDLoadedEvent | SFDUnLoadedEvent | DataloggerStateChanged | StatusUpdateEvent | None
- class scrutiny.sdk.client.ScrutinyClient.Events#
- class DataloggerStateChanged#
Triggered when the datalogger state changes or when the completion ratio is updated while acquiring
- details: DataloggingInfo#
The state of the datalogger and the completion ratio
- LISTEN_ALL = 4294967295#
Listen to all events
- LISTEN_CONNECTED = 1#
Listen for events of type
ConnectedEvent
- LISTEN_DATALOGGER_STATE_CHANGED = 64#
Listen for events of type
DataloggerStateChanged
- LISTEN_DEVICE_GONE = 8#
Listen for events of type
DeviceGoneEvent
- LISTEN_DEVICE_READY = 4#
Listen for events of type
DeviceReadyEvent
- LISTEN_DISCONNECTED = 2#
Listen for events of type
DisconnectedEvent
- LISTEN_NONE = 0#
Listen to no events
- LISTEN_SFD_LOADED = 16#
Listen for events of type
SFDLoadedEvent
- LISTEN_SFD_UNLOADED = 32#
Listen for events of type
SFDUnLoadedEvent
- LISTEN_STATUS_UPDATE_CHANGED = 128#
Listen for events of type
DataloggerStateChanged
- class StatusUpdateEvent#
Triggered when the a new server status is received
- info: ServerInfo#
The status info received
Events#
- class scrutiny.sdk.client.ScrutinyClient.Events.ConnectedEvent#
Triggered when the client connects to a Scrutiny server
- host: str#
The server hostname
- port: int#
The server port
- class scrutiny.sdk.client.ScrutinyClient.Events.DisconnectedEvent#
Triggered when the client disconnects from a Scrutiny server
- host: str#
The server hostname
- port: int#
The server port
- class scrutiny.sdk.client.ScrutinyClient.Events.DeviceReadyEvent#
Triggered when the server establish a communication with a device and the handshake phase is completed
- session_id: str#
A unique ID assigned to the communication session. This ID will change if the same device disconnects and reconnects.
- class scrutiny.sdk.client.ScrutinyClient.Events.DeviceGoneEvent#
Triggered when the the communication between the server and a device stops
- session_id: str#
The unique ID assigned to the communication session.
- class scrutiny.sdk.client.ScrutinyClient.Events.SFDLoadedEvent#
Triggered when the server loads a Scrutiny Firmware Description file, making Aliases and Variables available through the API
- firmware_id: str#
The firmware ID that matches the SFD
- class scrutiny.sdk.client.ScrutinyClient.Events.SFDUnLoadedEvent#
Triggered when the server unloads a Scrutiny Firmware Description file
- firmware_id: str#
The firmware ID that matches the SFD
- class scrutiny.sdk.client.ScrutinyClient.Events.DataloggerStateChanged#
Triggered when the datalogger state changes or when the completion ratio is updated while acquiring
- details: DataloggingInfo#
The state of the datalogger and the completion ratio