from datetime import timedelta

import dv_processing as dv

# Discover connected camera to the system
cameras = dv.io.camera.discover()
if len(cameras) < 2:
    raise RuntimeError("Unable to discover two cameras")

# Open the cameras
leftCamera = dv.io.camera.openSync(cameras[0])
rightCamera = dv.io.camera.openSync(cameras[1])

dv.io.camera.synchronizeAnyTwo(leftCamera, rightCamera)

# Initialize a stereo stream slicer
slicer = dv.io.StereoEventStreamSlicer()


# Callback method for time based slicing
def print_time_interval(left_events: dv.EventStore, right_events: dv.EventStore):
    # Print the time duration received by this method
    print(f"* Received events with duration: left[{left_events.duration()}] - right[{right_events.duration()}]")


# Register a job to be performed every 33 milliseconds
slicer.doEveryTimeInterval(timedelta(milliseconds=33), print_time_interval)


# Callback method for number based slicing
def print_event_number(left_events: dv.EventStore, right_events: dv.EventStore):
    # Print the number of events received here
    print(
        f"# Received events in number-based slicing, counts: left[{left_events.size()}] - right[{right_events.size()}]")


# Register this method to be called every 1000 events
slicer.doEveryNumberOfEvents(1000, print_event_number)

# Continue the loop while both cameras are connected
while leftCamera.isRunning() and rightCamera.isRunning():
    # Initialize empty stores
    left = dv.EventStore()
    right = dv.EventStore()

    # Receive packet from left camera
    leftPacket = leftCamera.getNextEventBatch()
    # Assign the packet if some data was received
    if leftPacket is not None:
        left = leftPacket

    # Receive packet from right camera
    rightPacket = rightCamera.getNextEventBatch()
    # Assign the packet if some data was received
    if rightPacket is not None:
        right = rightPacket

    # Pass all events into the slicer
    slicer.accept(left, right)
