Diffuser les événements et les alarmes

Cette page a été traduite automatiquement et n'a pas encore été relue. En cas de divergence, l'original anglais prévaut. Lire l'original anglais

Le service Event de blickfeld_qb2.percept_processing.services interroge et diffuse les événements des zones. Un événement d’intrusion d’une zone de sécurité contient l’identifiant de la zone, les horodatages du début et de la fin de l’intrusion ainsi que les propriétés des objets intrus.

Les exemples utilisent la bibliothèque Python et se connectent avec une clé d’application. Remplacez qb2-xxxxxxxxx par le nom d’hôte ou l’adresse IP de votre capteur et application-key-for-qb2-xxxxxxxxx par le secret de votre clé.

Diffuser les alarmes de toutes les zones de sécurité

Diffusez les alarmes de toutes les zones de sécurité avec le client Python. Installez la bibliothèque blickfeld_qb2, enregistrez le script ci-dessous sous stream_alarms.py et exécutez-le avec le nom d’hôte du capteur et le secret d’une clé d’application créée dans la WebGUI :

pip install -U blickfeld_qb2
python3 stream_alarms.py qb2-<serial number> <application key>
stream_alarms.py
import sys

import blickfeld_qb2
from blickfeld_qb2.betterproto import which_one_of


def describe(kind, event):
    if kind == "security_zone_intrusion_started":
        return f"Alarm in {event.state.generator_name}"
    if kind == "security_zone_intrusion_ended":
        return f"Alarm ended after {event.duration:.1f} s"
    return None


def stream_alarms(host, key=None):
    token = blickfeld_qb2.TokenFactory(application_key_secret=key) if key else None

    with blickfeld_qb2.Channel(host, token=token) as channel:
        events = blickfeld_qb2.percept_processing.services.Event(channel)

        for response in events.stream():
            line = describe(*which_one_of(response.event, "type"))
            if line:
                print(line)


if __name__ == "__main__":
    stream_alarms(*sys.argv[1:3])

Sortie lorsqu’une personne traverse une zone (exemple) :

Alarm in Pre-Alarm
Alarm ended after 13.0 s

Les exemples ci-dessous présentent le service Event étape par étape.

Diffuser les événements

Cet exemple affiche chaque événement.

import blickfeld_qb2


def stream_security_events() -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        # Use Event service
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Iterate over events stream and print them
        for response in service.stream():
            print("Event:", response.event)


stream_security_events()

Sortie lorsqu’une personne entre dans une zone, les deux premiers événements, un champ par ligne et les longues listes coupées par …​ :

Event: Event(
    timestamp=1794557816515962216,
    security_intrusion_started=EventSecurityIntrusionStarted()
)
Event: Event(
    timestamp=1794557816515962216,
    security_zone_intrusion_started=EventSecurityZoneIntrusionStarted(
        zone_uuid='6dde4732-d2d2-4981-bee6-6864d7669a75',
        state=State(
            generator_name='Pre-Alarm',
            object_based_security=StateObjectBasedSecurity(
                alarm=True,
                objects={
                    '59': DetectedObject(
                        pose=Pose(
                            position=Vector3(x=-1.2360198497772217, y=7.185153961181641, z=1.0974624156951904)
                        ),
                        shape=Shape(
                            pose=Pose(
                                position=Vector3(x=-1.2197115421295166, y=7.104532241821289, z=1.0974624156951904)
                            ),
                            box=ShapeBox(
                                dimensions=Vector3(x=0.32164937257766724, y=0.3377991318702698, z=1.2932387590408325)
                            )
                        ),
                        properties=DetectedObjectProperties(size=2, num_points=220, average_intensity=3884, bounding_box_2_d_surface=0.43685492873191833, track_length=5.475871562957764, median_reflectivity=4146),
                        point_cloud=Frame(
                            binary=FrameBinary(
                                length=220,
                                cartesian=array(
                                    [
                                        [-1.1444434 ,  7.0532727 ,  1.2344986 ],
                                        [-1.1692843 ,  7.128196  ,  1.5870416 ],
                                        [-1.3347117 ,  7.079878  ,  1.000879  ],
                                        ...
                                    ],
                                    dtype=float32
                                ),
                                photon_count=array(
                                    [5381, 5142, 4758, ...],
                                    dtype=uint16
                                ),
                                direction_id=array(
                                    [393, 50, 2640, ...],
                                    dtype=uint32
                                )
                            )
                        ),
                        timestamp=1794557808515962216,
                        path=[
                            Pose(
                                position=Vector3(x=-4.121706008911133, y=1.7800681591033936, z=0.4839831292629242)
                            ),
                            Pose(
                                position=Vector3(x=-3.488027572631836, y=3.0714902877807617, z=1.079064130783081)
                            ),
                            Pose(
                                position=Vector3(x=-2.86340594291687, y=4.3938727378845215, z=1.0780154466629028)
                            ),
                            ...
                        ],
                        visible_to_sensors=['ABC123XYZ'],
                        intruding=Flag(state=True, since_timestamp=1794557808515962216),
                        intruder=Flag(state=True, since_timestamp=1794557808515962216),
                        lifetime=8000000000,
                        velocity=Vector3(x=0.3014988899230957, y=0.27406570315361023),
                        in_zone_uuids=['6dde4732-d2d2-4981-bee6-6864d7669a75']
                    )
                },
                intrusion=Flag(state=True, since_timestamp=1794557816515962216),
                intrusion_confidence=1.0,
                tampering=Flag(since_timestamp=1794557807015962216, since_duration=9500000000)
            ),
            coverage=1.0,
            coverage_observed_area=24.43000030517578
        ),
        disqualification=Disqualification()
    )
)

Traiter les détails d’intrusion dans une zone

Cet exemple affiche l’UUID de la zone et la position de chaque objet lorsqu’une intrusion commence :

import blickfeld_qb2


def stream_zone_intrusion_details() -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(
      application_key_secret="application-key-for-qb2-xxxxxxxxx"
    )
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        # Use Event service
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Iterate over events stream
        for response in service.stream():
            event = response.event
            # Condition for checking start of the intrusion of any zone
            if event.security_zone_intrusion_started:
                zone_event = event.security_zone_intrusion_started
                print(f"Zone: {zone_event.zone_uuid}")
                print("Objects involved:")
                # Iterate over objects in intrusion zone and print their position
                for obj_id, obj in zone_event.state.object_based_security.objects.items():
                    pos = obj.pose.position
                    print(f" - ID {obj_id}: (x={pos.x:.2f}, y={pos.y:.2f}, z={pos.z:.2f})")


stream_zone_intrusion_details()

Sortie :

Zone: 6dde4732-d2d2-4981-bee6-6864d7669a75
Objects involved:
 - ID 60: (x=-1.24, y=7.19, z=1.10)

Enregistrer les événements dans un fichier

Cet exemple écrit chaque événement sous forme d’une ligne JSON dans le fichier events.log, dès son arrivée, jusqu’à ce que vous appuyiez sur Ctrl+C.

import blickfeld_qb2


def log_events_to_file(filepath="events.log") -> None:
    # Create token factory using an application key
    token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")
    # Open a secure connection to the sensor
    with blickfeld_qb2.Channel("qb2-xxxxxxxxx", token=token_factory) as channel:
        service = blickfeld_qb2.percept_processing.services.Event(channel)
        # Line buffered: each event is in the file as soon as it is written
        with open(filepath, "w", buffering=1) as f:
            for response in service.stream():
                # Write each event as one line of JSON
                f.write(f"{response.event.to_json()}\n")


log_events_to_file()