==============
hikrobotcamlib
==============

Package HIK Robotics camera drivers and a Pythonic wrappers to the data. You still need to install the SDK to get the
drivers/libraries.

This supports a subset of the platforms HIKRobotics' supports:

  - Linux: glibc (*not* MUSL, ie not Alpine) x86_64, aarch64 and armhf (probably, only tested on aarch64)
  - MacOS (idk if the dynlibs from vendor are both for Apple silicon and x86_64 but at least x86_64 works)

I've tried to make nice wrappers that should mostly handle things cleanly but this uses ctypes and calls external libraries
that are not real Python bindings (via cddl). One wrong move and everything crashes hard.

Usage
-----

The wrappers are documented and type-hinted, enjoy. TLDR:

.. code-block:: python3

    from typing import Optional
    import time

    from hikrobotcamlib Camera, DeviceList, Frame, DeviceTransport

    def frame_callback(frame: Frame, cam: Camera) -> None:
        """Handle frames from camera"""
        # TODO: Do something with the frame

    cam: Optional[Camera] = None
    for devinfo in DeviceList(DeviceTransport.GIGE | DeviceTransport.USB):
        cam = Camera(devinfo)
        break

    if not cam:
        raise RuntimeError("No camera")

    cam.open()
    cam.frame_callback = frame_callback
    cam.trigger_enable(False)
    cam.set_framerate(10.0)
    cam.start()
    time.sleep(2.5)
    cam.stop()
    cam.close()

Docker
------

For more controlled deployments and to get rid of "works on my computer" -syndrome, we always
make sure our software works under docker.

It's also a quick way to get started with a standard development environment.

SSH agent forwarding
^^^^^^^^^^^^^^^^^^^^

We need buildkit_::

    export DOCKER_BUILDKIT=1

.. _buildkit: https://docs.docker.com/develop/develop-images/build_enhancements/

And also the exact way for forwarding agent to running instance is different on OSX::

    export DOCKER_SSHAGENT="-v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"

and Linux::

    export DOCKER_SSHAGENT="-v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK -e SSH_AUTH_SOCK"

Creating a development container
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Build image, create container and start it::

    docker build --ssh default --target devel_shell -t hikrobotcamlib:devel_shell .
    docker create --name hikrobotcamlib_devel -v `pwd`":/app" -it `echo $DOCKER_SSHAGENT` hikrobotcamlib:devel_shell
    docker start -i hikrobotcamlib_devel

pre-commit considerations
^^^^^^^^^^^^^^^^^^^^^^^^^

If working in Docker instead of native env you need to run the pre-commit checks in docker too::

    docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit install"
    docker exec -i hikrobotcamlib_devel /bin/bash -c "pre-commit run --all-files"

You need to have the container running, see above. Or alternatively use the docker run syntax but using
the running container is faster::

    docker run --rm -it -v `pwd`":/app" hikrobotcamlib:devel_shell -c "pre-commit run --all-files"

Test suite
^^^^^^^^^^

You can use the devel shell to run py.test when doing development, for CI use
the "tox" target in the Dockerfile::

    docker build --ssh default --target tox -t hikrobotcamlib:tox .
    docker run --rm -it -v `pwd`":/app" `echo $DOCKER_SSHAGENT` hikrobotcamlib:tox

Production docker
^^^^^^^^^^^^^^^^^

TODO: Remove this section if this is a library and not an application

There's a "production" target as well for running the application, remember to change that
architecture tag to arm64 if building on ARM::

    docker build --ssh default --target production -t hikrobotcamlib:amd64-latest .
    docker run --rm --network host -it --name hikrobotcamlib hikrobotcamlib:amd64-latest

Development
-----------

TODO: Remove the repo init from this document after you have done it.

TLDR:

- Create and activate a Python 3.11 virtualenv (assuming virtualenvwrapper)::

    mkvirtualenv -p `which python3.11` my_virtualenv

- change to a branch::

    git checkout -b my_branch

- install Poetry: https://python-poetry.org/docs/#installation
- Install project deps and pre-commit hooks::

    poetry install
    pre-commit install
    pre-commit run --all-files

- Ready to go.

Remember to activate your virtualenv whenever working on the repo, this is needed
because pylint and mypy pre-commit hooks use the "system" python for now (because reasons).
