Python
Le paquet Python blickfeld_qb2 est disponible précompilé sur PyPI.
Il renvoie les nuages de points sous forme de tableaux NumPy sans copier les données, suffisamment rapides pour la diffusion et le traitement en direct.
Installer la bibliothèque
pip install -U blickfeld_qb2
import blickfeld_qb2
Se connecter au capteur
Le Channel ouvre une connexion gRPC chiffrée vers le nom d’hôte ou l’adresse IP du capteur. La TokenFactory s’authentifie avec une clé d’application.
import blickfeld_qb2
# Create token factory using an application key
token_factory = blickfeld_qb2.TokenFactory(
application_key_secret="application-key-for-qb2-xxxxxxxxx"
)
# Open a secure & authenticated connection to the sensor
with blickfeld_qb2.Channel(
fqdn_or_ip="qb2-xxxxxxxxx",
token=token_factory
) as channel:
# Call methods of services here
# ..
pass
|
Créez d’abord une clé d’application.
Dans l’exemple, remplacez Si la clé d’application n’est pas valide, le premier appel d’API sur le canal échoue avec le code d’état gRPC 5, |
Transmettez aussi le serial_number du capteur pour vérifier que le canal se connecte bien à ce capteur précis. Sans lui, le canal vérifie seulement qu’il se connecte à un appareil Blickfeld.
|
Envoyer une requête
Cet exemple lit la version du micrologiciel avec get_status() du service Firmware de blickfeld_qb2.system.services.
import blickfeld_qb2
# Create token factory using an application key
token_factory = blickfeld_qb2.TokenFactory(
application_key_secret="application-key-for-qb2-xxxxxxxxx"
)
# Open a secure & authenticated connection to the sensor
with blickfeld_qb2.Channel(
fqdn_or_ip="qb2-xxxxxxxxx",
token=token_factory
) as channel:
# Request the firmware status
response = blickfeld_qb2.system.services.Firmware(channel).get_status()
# Extract the firmware version
firmware_version = response.status.installed_firmware_info
# Print the firmware version
print(firmware_version)
Chaque réponse est un objet contenant des objets imbriqués, tels que définis dans la référence de l’API. Un même canal traite un nombre quelconque de requêtes. Sortie, un champ par ligne :
FirmwareInfo(
label='HENNING v3.2.4',
version=Version(major=3, minor=2, patch=4, revision='d1c2d249'),
allowed_downgrade_version=Version(major=3, patch=11)
)
La version d’un firmware publié affiche aussi is_release=True.
Envoyer des requêtes de manière asynchrone
La variante asynchrone de chaque méthode porte le préfixe async_ :
import asyncio
import blickfeld_qb2
async def main():
# Create token factory using an application key
token_factory = blickfeld_qb2.TokenFactory(
application_key_secret="application-key-for-qb2-xxxxxxxxx"
)
# Open a secure, authenticated & asynchronous connection to the sensor
with blickfeld_qb2.Channel(
fqdn_or_ip="qb2-xxxxxxxxx",
token=token_factory
) as channel:
# Request the firmware status asynchronously and await for the response
response = await blickfeld_qb2.system.services.Firmware(channel).async_get_status()
# Get the firmware version from the response
firmware_version = response.status.installed_firmware_info
# Print the firmware version
print(firmware_version)
# run coroutine on the event loop
asyncio.run(main())
Sortie, un champ par ligne :
FirmwareInfo(
label='HENNING v3.2.4',
version=Version(major=3, minor=2, patch=4, revision='d1c2d249'),
allowed_downgrade_version=Version(major=3, patch=11)
)