discovery — functions
Finding cameras, naming them, and watching them come and go.
Three things live here and they share one idea: none of them opens a camera. Enumerating, naming and monitoring are all questions about what is attached, and answering them by opening the camera would take it away from whoever is streaming from it.
Enumerating. optris_sdk.enumerate_devices sweeps once and
returns snapshots. EnumerationOptions makes the sweep wider
or narrower, and the one option worth understanding is
include_network: it is off by default because it is a broadcast
whose replies have to be waited for, which is the entire cost of a
sweep. A USB application should not pay three seconds per enumeration
for cameras it does not have.
Naming. A camera reports a model and a serial, and neither is what
an operator calls it. set_device_name records a name on this
machine, keyed by serial, and it outlives the process --
enumeration reads it back into DeviceInfo.name. The serial is the
key because it is the only stable identity a camera has: move the plug
and the USB path changes while the camera does not.
DeviceMetadata is the same store with the lid off. The name
is one key in it; anything else worth recording about a camera --
where it is mounted, which line it watches -- is yours to define and
needs no new API.
Monitoring. hotplug_monitor is a context manager around
the SDK's hotplug thread, so the monitor stops on the way out of the
with even if the body raised. Its handler runs on an SDK thread
and every rule in optris_sdk.callbacks applies to it.
connection_name(connection)
'USB', 'network' or 'simulated'.
Prints the connection rather than its ordinal. "connection: 1" tells a reader nothing.
set_device_name(serial, name)
Name a camera on this machine, or forget the name.
An empty name removes it. The name is stored beside the SDK's
per-camera configuration, keyed by serial, and a later enumeration
reads it back into DeviceInfo.name.
Nothing is opened, so naming a camera does not disturb whatever is streaming from it.
get_device_name(serial)
The name recorded for a camera, or '' if it has none.
Never invents one. An unnamed camera is an empty string, not its model.
hotplug_monitor(handler)
Watch the bus, calling handler(device, added) on a change.
A context manager, because the monitor is process-wide state and leaving it running after the code that wanted it has finished is how a handler ends up firing into a half-torn-down program::
def on_change(device, added):
print('+' if added else '-', device.model, device.serial)
with optris_sdk.hotplug_monitor(on_change):
time.sleep(30)
The handler runs on the SDK's monitor thread. Every rule in
optris_sdk.callbacks applies.
The monitor runs until it is stopped, whether or not the program
keeps the object this returns: letting go of it neither stops the
monitor nor makes the handler unsafe to call, and one still running
when the program ends is stopped at exit. Keep the object (or use
with) to stop it yourself.
Reports arrivals and removals only. For claim transitions, or for
cameras on the network, use device_monitor -- the SDK has
one monitor, so the two replace each other rather than running
side by side.
device_change_name(kind)
A change kind as a word, for printing.
device_monitor(handler, options = None)
Watch devices, calling handler(device, kind) on a change.
The superset of hotplug_monitor: the same arrivals and
removals, plus claim transitions with watch_claims, plus
cameras on the network with watch_network. kind is one of
DEVICE_ARRIVED, DEVICE_REMOVED, DEVICE_CLAIMED or
DEVICE_RELEASED::
opts = optris_sdk.DeviceMonitorOptions(watch_claims=True)
with optris_sdk.device_monitor(on_change, opts):
time.sleep(30)
A context manager for the reason hotplug_monitor is one,
and the reason is sharper here: there is one monitor
process-wide, so this and the hotplug monitor replace each other
rather than running side by side, and either stop ends whichever
is running. Leaving one running is how the next start silently
repoints somebody else's callback.
DEVICE_CLAIMED includes this process's own opens. Read
device.claimed_by_this_process to tell yours from somebody
else's -- the SDK's own documentation for this used to say
"another process" and never was.
The first claim poll establishes what is claimed rather than reporting it, so a camera that was already busy when watching started does not arrive as a fresh claim. A device list would otherwise flash "in use" for cameras whose state never changed.
The handler runs on the SDK's monitor thread. Every rule in
optris_sdk.callbacks applies.
The monitor runs until it is stopped, whether or not the program
keeps the object this returns. A monitor started in a function's
local variables, or started and not kept at all, goes on reporting
after the function has returned, and one still running when the
program ends is stopped at exit. Keep the object (or use with)
to stop it yourself.
probe_network(address, serial = None, discovery_timeout_s = None, port = None)
Ask one address who is there, and return a DeviceInfo or
None.
The unicast counterpart of
optris_sdk.enumerate_devices: the same discovery
exchange, addressed to one host instead of broadcast. A unicast
datagram is routed, so this reaches a camera a broadcast cannot --
behind a router, or on a subnet this host has no interface on.
Those cameras are invisible to a sweep however long it waits, and
typing the address is the only way in.
Opens nothing and claims nothing, so probing a camera another process is streaming from does not disturb it. This is what lets a user interface list a camera, and learn its serial, before deciding to open it.
None rather than an exception when nothing answers: an
operator mistyping an address is an ordinary event, and it costs
discovery_timeout_s rather than an operating system connect
timeout.
- Parameter
address— the host to ask. Required -- an empty address raises, because there is then nobody to ask. - Parameter
serial— if given, filters the answer to that camera. - Parameter
discovery_timeout_s— how long to wait.Noneuses the SDK's default. Carried as whole milliseconds, rounded, and never down to zero from a positive value.
register_simulated_device(config = None)
Put a simulated camera in this process's device list.
A camera made by Camera.simulated() exists only as the handle
that made it: it is not enumerated and no monitor reports it.
Registering one puts it in the list, so it is found, opened and
watched by the same code as a camera on the end of a cable -- and
Camera.open_device() on the resulting entry opens it with the
scene registered here rather than with defaults.
This is how an integration test drives enumeration, hotplug and claim handling with no hardware at all.
Reports an arrival to a running monitor, so it is also how a
device_monitor handler can be exercised deliberately.
Returns the DeviceInfo as it will appear in enumeration.
- Parameter
config— aoptris_sdk.camera.SimulatedCamera, orNonefor the defaults. Passserial=''to have one assigned -- the lowest free serial from 9990001 up.SimulatedCamera()fills the serial with'9990001'rather than leaving it empty, so registering two freshly-defaulted configs is a collision and is refused rather than silently giving you one camera.
unregister_simulated_device(serial)
Remove a simulated camera from the device list.
Returns True if one was registered under that serial and
False if none was -- not an error either way, so a teardown
may run unconditionally.
Removes the list entry, not any camera opened from it: a simulated camera that is open owns its own generator and keeps producing frames until it is closed. What changes at once is that the serial stops being enumerated and can no longer be opened.
Reports a removal to a running monitor.
clear_simulated_devices()
Forget every registered simulated camera, reporting each removal.
The teardown for register_simulated_device. Worth calling
in a test's cleanup rather than unregistering by serial: the
registry is process-wide, so a serial left behind by a failed test
is a collision in the next one.