Connect to a Device¶
One of the first steps you need to carry to leverage the API is to connect to one device. This library offers you ways to find one or multiple devices connected to your local network. See below the different ways to connect to a device:
Using the discover_devices
:
discover_devices.py | |
---|---|
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 |
|
from pupil_labs.realtime_api.device import Device
# This address is just an example. Find out the actual IP address of your device!
ip = "192.168.1.169"
device = Device(address=ip, port="8080")
Make sure the Companion App is running! If no device can be found, please have a look at the troubleshooting section.
Below you can find a link to the full code example and the API referece for the returned Device object.
Device Information & Automatic Status Updates¶
Once connected, the Device
object, alows you to retrieve Status
updates by calling get_status
.
This Status
represents the full Companion's Device state, including sub-classes representing:
async with Device.from_discovered_device(dev_info) as device:
status = await device.get_status()
print(f"Device IP address: {status.phone.ip}")
print(f"Battery level: {status.phone.battery_level} %")
print(f"Connected glasses: SN {status.hardware.glasses_serial}")
print(f"Connected scene camera: SN {status.hardware.world_camera_serial}")
world = status.direct_world_sensor()
print(f"World sensor: connected={world.connected} url={world.url}")
gaze = status.direct_gaze_sensor()
print(f"Gaze sensor: connected={gaze.connected} url={gaze.url}")
Device IP address: 192.168.1.60
Battery level: 78 %
Connected glasses: SN -1
Connected scene camera: SN -1
World sensor: connected=True url=rtsp://192.168.1.60:8086/?camera=world&audioenable=on
Gaze sensor: connected=True url=rtsp://192.168.1.60:8086/?camera=gaze&audioenable=on
async with Device.from_discovered_device(dev_info) as device:
duration = 20
print(f"Starting auto-update for {duration} seconds")
# callbacks can be awaitable, too
notifier = StatusUpdateNotifier(device, callbacks=[print_component])
await notifier.receive_updates_start()
await asyncio.sleep(duration)
print("Stopping auto-update")
await notifier.receive_updates_stop()
Starting auto-update for 20 seconds
Phone(battery_level=77, battery_state='OK', device_id='d55a33b5ab845785', device_name='Neon Companion', ip='192.168.1.60', memory=99877777408, memory_state='OK', time_echo_port=12321)
Hardware(version='2.0', glasses_serial='-1', world_camera_serial='-1', module_serial='841684')
Sensor(sensor='imu', conn_type='WEBSOCKET', connected=True, ip='192.168.1.60', params='camera=imu&audioenable=off', port=8686, protocol='rtsp', stream_error=False)
Sensor(sensor='imu', conn_type='DIRECT', connected=True, ip='192.168.1.60', params='camera=imu&audioenable=on', port=8086, protocol='rtsp', stream_error=False)
Sensor(sensor='world', conn_type='WEBSOCKET', connected=True, ip='192.168.1.60', params='camera=world&audioenable=off', port=8686, protocol='rtsp', stream_error=False)
Refer to the Device API documentation for more details.
Device
Device
¶
Bases: DeviceBase
Class representing a Pupil Labs device.
This class provides methods to interact with the device, such as starting and stopping recordings, sending events, and fetching device status. It also provides a context manager for automatically closing the device session.
Methods:
-
api_url
–Construct a full API URL for the given path.
-
close
–Close the connection to the device.
-
convert_from
–Convert another device instance to this type.
-
from_discovered_device
–Create a device instance from discovery information.
-
get_calibration
–Get the current cameras calibration data.
-
get_status
–Get the current status of the device.
-
get_template
–Get the template currently selected on device.
-
get_template_data
–Get the template data entered on device.
-
post_template_data
–Set the data for the currently selected template.
-
recording_cancel
–Cancel the current recording without saving it.
-
recording_start
–Start a recording on the device.
-
recording_stop_and_save
–Stop and save the current recording.
-
send_event
–Send an event to the device.
-
status_updates
–Stream status updates from the device.
Attributes:
-
active_session
(ClientSession
) –Returns the active session, raising an error if it's None.
-
session
(ClientSession | None
) –The HTTP session used for making requests.
-
template_definition
(Template | None
) –The template definition currently selected on the device.
Source code in src/pupil_labs/realtime_api/device.py
74 75 76 77 |
|
active_session
property
¶
active_session: ClientSession
Returns the active session, raising an error if it's None.
session
instance-attribute
¶
session: ClientSession | None
The HTTP session used for making requests.
template_definition
class-attribute
instance-attribute
¶
template_definition: Template | None = None
The template definition currently selected on the device.
api_url
¶
Construct a full API URL for the given path.
Parameters:
-
path
(APIPath
) –API path to access.
-
protocol
(str
, default:'http'
) –Protocol to use (http).
-
prefix
(str
, default:'/api'
) –API URL prefix.
Returns:
-
str
–Complete URL for the API endpoint.
Source code in src/pupil_labs/realtime_api/base.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
close
async
¶
close() -> None
Close the connection to the device.
Source code in src/pupil_labs/realtime_api/device.py
336 337 338 339 |
|
convert_from
classmethod
¶
convert_from(other: T) -> DeviceType
Convert another device instance to this type.
Parameters:
-
other
(T
) –Device instance to convert.
Returns:
-
DeviceType
–Converted device instance.
Source code in src/pupil_labs/realtime_api/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
from_discovered_device
classmethod
¶
from_discovered_device(device: DiscoveredDeviceInfo) -> DeviceType
Create a device instance from discovery information.
Parameters:
-
device
(DiscoveredDeviceInfo
) –Discovered device information.
Returns:
-
DeviceType
–Device instance
Source code in src/pupil_labs/realtime_api/base.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
get_calibration
async
¶
get_calibration() -> Calibration
Get the current cameras calibration data.
Note that Pupil Invisible and Neon are calibration free systems, this refers to the intrinsincs and extrinsics of the cameras and is only available for Neon.
Returns:
-
Calibration
–pupil_labs.neon_recording.calib.Calibration: The calibration data.
Raises:
-
DeviceError
–If the request fails.
Source code in src/pupil_labs/realtime_api/device.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
get_status
async
¶
get_status() -> Status
Get the current status of the device.
Returns:
-
Status
(Status
) –The current device status.
Raises:
-
DeviceError
–If the request fails.
Source code in src/pupil_labs/realtime_api/device.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
get_template
async
¶
get_template() -> Template
Get the template currently selected on device.
Returns:
-
Template
(Template
) –The currently selected template.
Raises:
-
DeviceError
–If the template can't be fetched.
Source code in src/pupil_labs/realtime_api/device.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
get_template_data
async
¶
get_template_data(template_format: TemplateDataFormat = 'simple') -> Any
Get the template data entered on device.
Parameters:
-
template_format
(TemplateDataFormat
, default:'simple'
) –Format of the returned data. - "api" returns the data as is from the api e.g., {"item_uuid": ["42"]} - "simple" returns the data parsed e.g., {"item_uuid": 42}
Returns:
-
Any
–The template data in the requested format.
Raises:
-
DeviceError
–If the template's data could not be fetched.
-
AssertionError
–If an invalid format is provided.
Source code in src/pupil_labs/realtime_api/device.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
post_template_data
async
¶
post_template_data(template_answers: dict[str, list[str]], template_format: TemplateDataFormat = 'simple') -> Any
Set the data for the currently selected template.
Parameters:
-
template_answers
(dict[str, list[str]]
) –The template data to send.
-
template_format
(TemplateDataFormat
, default:'simple'
) –Format of the input data. - "api" accepts the data as in realtime api format e.g., {"item_uuid": ["42"]} - "simple" accepts the data in parsed format e.g., {"item_uuid": 42}
Returns:
-
Any
–The result of the operation.
Raises:
-
DeviceError
–If the data can not be sent.
-
ValueError
–If invalid data type.
-
AssertionError
–If an invalid format is provided.
Source code in src/pupil_labs/realtime_api/device.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
recording_cancel
async
¶
recording_cancel() -> None
Cancel the current recording without saving it.
Raises:
-
DeviceError
–If the recording could not be cancelled. Possible reasons include: - Recording not running
Source code in src/pupil_labs/realtime_api/device.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
recording_start
async
¶
recording_start() -> str
Start a recording on the device.
Returns:
-
str
(str
) –ID of the started recording.
Raises:
-
DeviceError
–If recording could not be started. Possible reasons include: - Recording already running - Template has required fields - Low battery - Low storage - No wearer selected - No workspace selected - Setup bottom sheets not completed
Source code in src/pupil_labs/realtime_api/device.py
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 |
|
recording_stop_and_save
async
¶
recording_stop_and_save() -> None
Stop and save the current recording.
Raises:
-
DeviceError
–If recording could not be stopped. Possible reasons include: - Recording not running - Template has required fields
Source code in src/pupil_labs/realtime_api/device.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
send_event
async
¶
Send an event to the device.
Parameters:
-
event_name
(str
) –Name of the event.
-
event_timestamp_unix_ns
(int | None
, default:None
) –Optional timestamp in unix nanoseconds. If None, the current time will be used.
Returns:
-
Event
(Event
) –The created event.
Raises:
-
DeviceError
–If sending the event fails.
Source code in src/pupil_labs/realtime_api/device.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|
status_updates
async
¶
status_updates() -> AsyncIterator[Component]
Stream status updates from the device.
Yields:
-
Component
(AsyncIterator[Component]
) –Status update components as they arrive.
Auto-reconnect, see: https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.asyncio.client.connect
Source code in src/pupil_labs/realtime_api/device.py
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 |
|
DeviceBase
DeviceBase
¶
DeviceBase(address: str, port: int, full_name: str | None = None, dns_name: str | None = None, suppress_decoding_warnings: bool = True)
Bases: ABC
Abstract base class representing Realtime API host devices.
This class provides the foundation for device implementations that connect to the Realtime API.
Attributes:
-
address
(str
) –REST API server address.
-
port
(int
) –REST API server port.
-
full_name
(str | None
) –Full service discovery name.
-
dns_name
(str | None
) –REST API server DNS name, e.g.
neon.local / pi.local.
.
Parameters:
-
address
(str
) –REST API server address.
-
port
(int
) –REST API server port.
-
full_name
(str | None
, default:None
) –Full service discovery name.
-
dns_name
(str | None
, default:None
) –REST API server DNS name, e.g.
neon.local / pi.local.
. -
suppress_decoding_warnings
(bool
, default:True
) –Whether to suppress libav decoding warnings.
Methods:
-
api_url
–Construct a full API URL for the given path.
-
convert_from
–Convert another device instance to this type.
-
from_discovered_device
–Create a device instance from discovery information.
Source code in src/pupil_labs/realtime_api/base.py
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 |
|
api_url
¶
Construct a full API URL for the given path.
Parameters:
-
path
(APIPath
) –API path to access.
-
protocol
(str
, default:'http'
) –Protocol to use (http).
-
prefix
(str
, default:'/api'
) –API URL prefix.
Returns:
-
str
–Complete URL for the API endpoint.
Source code in src/pupil_labs/realtime_api/base.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
convert_from
classmethod
¶
convert_from(other: T) -> DeviceType
Convert another device instance to this type.
Parameters:
-
other
(T
) –Device instance to convert.
Returns:
-
DeviceType
–Converted device instance.
Source code in src/pupil_labs/realtime_api/base.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
from_discovered_device
classmethod
¶
from_discovered_device(device: DiscoveredDeviceInfo) -> DeviceType
Create a device instance from discovery information.
Parameters:
-
device
(DiscoveredDeviceInfo
) –Discovered device information.
Returns:
-
DeviceType
–Device instance
Source code in src/pupil_labs/realtime_api/base.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
Status
Status
dataclass
¶
Represents the Companion's Device full status
Methods:
-
direct_eye_events_sensor
–Get blinks, fixations sensor.
-
direct_eyes_sensor
–Get the eye camera sensor with direct connection. Only available on Neon.
-
direct_gaze_sensor
–Get the gaze sensor with direct connection.
-
direct_imu_sensor
–Get the IMU sensor with direct connection.
-
direct_world_sensor
–Get the scene camera sensor with direct connection.
-
from_dict
–Create a Status from a list of raw components.
-
matching_sensors
–Find sensors matching specified criteria.
-
update
–Update Component.
Attributes:
-
hardware
(Hardware
) –Information about glasses connected, won't be present if not connected
-
phone
(Phone
) –Information about the connected phone. Always present.
-
recording
(Recording | None
) –Current recording, if any.
-
sensors
(list[Sensor]
) –"List of sensor information.
hardware
instance-attribute
¶
hardware: Hardware
Information about glasses connected, won't be present if not connected
direct_eye_events_sensor
¶
direct_eye_events_sensor() -> Sensor | None
Get blinks, fixations sensor.
Only available on Neon with Companion App version 2.9 or newer.
Source code in src/pupil_labs/realtime_api/models.py
460 461 462 463 464 465 466 467 468 469 470 471 |
|
direct_eyes_sensor
¶
direct_eyes_sensor() -> Sensor | None
Get the eye camera sensor with direct connection. Only available on Neon.
Source code in src/pupil_labs/realtime_api/models.py
453 454 455 456 457 458 |
|
direct_gaze_sensor
¶
direct_gaze_sensor() -> Sensor | None
Get the gaze sensor with direct connection.
Source code in src/pupil_labs/realtime_api/models.py
439 440 441 442 443 444 |
|
direct_imu_sensor
¶
direct_imu_sensor() -> Sensor | None
Get the IMU sensor with direct connection.
Source code in src/pupil_labs/realtime_api/models.py
446 447 448 449 450 451 |
|
direct_world_sensor
¶
direct_world_sensor() -> Sensor | None
Get the scene camera sensor with direct connection.
Note
Pupil Invisible devices, the world camera can be detached
Source code in src/pupil_labs/realtime_api/models.py
425 426 427 428 429 430 431 432 433 434 435 436 437 |
|
from_dict
classmethod
¶
from_dict(status_json_result: list[ComponentRaw]) -> Status
Create a Status from a list of raw components.
Parameters:
-
status_json_result
(list[ComponentRaw]
) –List of raw component dictionaries.
Returns:
-
Status
(Status
) –New Status instance.
Source code in src/pupil_labs/realtime_api/models.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
matching_sensors
¶
matching_sensors(name: SensorName, connection: ConnectionType) -> Iterator[Sensor]
Find sensors matching specified criteria.
Parameters:
-
name
(SensorName
) –Sensor name to match, or ANY to match any name.
-
connection
(ConnectionType
) –Connection type to match, or ANY to match any type.
Yields:
-
Sensor
(Sensor
) –Sensors matching the criteria.
Source code in src/pupil_labs/realtime_api/models.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
update
¶
update(component: Component) -> None
Update Component.
Parameters:
-
component
(Component
) –Component to update.
Source code in src/pupil_labs/realtime_api/models.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
|
Full Code Examples¶
Check the whole example code here
discover_devices.py | |
---|---|
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 |
|
device_status_get_current.py | |
---|---|
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 |
|
device_status_update_via_callback.py | |
---|---|
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 |
|