from robodk.robolink import *  # Import the RoboDK API module
import time
import logging
import signal
import numpy as np

import time

shutdown_requested = False

logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s')

def handler(_signo, _stack_frame):
    logging.info("Shutting down RoboDK viewer")
    global shutdown_requested
    shutdown_requested = True

def main():
    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    # Connect to the running instance of RoboDK (make sure it's already running)
    logging.info("Connecting...")
    RDK = Robolink()
    RDK.Connect()
    logging.info("Connected...")

    for i in range(1,20):
        CAM_NAME = 'StaticCamera'
        static_camera = RDK.Item(CAM_NAME)
        start_time = time.time()
        logging.info("Taking snapshot...")
        bytes_img = RDK.Cam2D_Snapshot("", static_camera, "")
        end_time = time.time()
        logging.info("Snapshot took %.6f seconds", (end_time - start_time))
        # I have commented out the image processing code
        # import cv2 as cv
        # import cv2.typing
        # logging.info(f"image byte array size: %d", len(bytes_img))
        
        # nparr = np.frombuffer(bytes_img, np.uint8)
        # img_socket : cv2.typing.MatLike = cv.imdecode(nparr, cv.IMREAD_COLOR)
        # logging.info(f"imgage size: %d", img_socket.size)

    logging.info("Done!")

    exit(0)

if __name__ == "__main__":
    main()