A Python version of helloworld-binding example, demonstrating how https://github.com/redpesk-common/afb-libpython can be used to implement an AFB binding in Python.
The "helloworld" binding exposes an API called helloworld
with the following verbs:
ping
: takes no argument and returns a "pong" messagetestargs
: takes one JSON argument and tests its internal structure
Since this helloworld binding is not connected to real hardware, it can be run and tested on a Linux workstation.
In order to do so, you will need to install afb-libpython
. It is available in redpesk SDK.
Please follow first the SDK installation documentation if you have not done so before, and if your platform is supported.
Install then Python bindings on your platform thanks to your distribution's tools: e.g. sudo apt install libafb-python
.
You should then be able to run helloworld-binding
with:
python3 helloworld_python_binding/helloworld-binding.py
The aim of this part is to create a package which contains the helloworld-python-binding application.
In order to correctly build an RPM package in the redpesk context, your project should contain a specfile. The helloworld-python-binding specfile can be found at the following path: conf.d/packaging/helloworld-python-binding.spec. This specfile will be used by redpesk to describe how to build your helloworld binding application. The resulting build produces a helloworld widget that is embedded in a RPM. Please read the documentation to correctly create and set your redpesk project and application set up.
Now that a helloworld-python-binding package has been generated thanks to redpesk, let's see how to install it on a board. Since redpesk supports cross-compilation, you can use either an x86_64 or an aarch64 board to get your package installed in. During this part, one case will be studied:
- Emulated x86_64 board: qemu-x86_64
If you want to use the helloworld-python-binding in an embedded aarch64 context, but do not have a real board, another tutorial is present at the following path.
Please refer to the official documentation to download a redpesk image and run it through QEMU.
In order to use redpesk factory, you will need an account on our free instance: https://community-app.redpesk.bzh
You can then follow the guide from the documentation that explains how to add an application.
Use the provided specfile and it should be able to build an RPM package of helloworld-binding ready to be installed on your board.
Refer to the official documentation that explains how to add a new RPM repository that contains your package to the board.
Once the repository has been added, the package can be installed with sudo dnf install helloworld-python-binding
$ python3.6 /usr/lib/python3.6/site-packages/helloworld_python_binding/helloworld-binding.py
Entering Python module initialization function PyInit_libafb
NOTICE: Entering binder mainloop
NOTICE: loop_binder_cb=helloworld-python
In another terminal, via ssh, you can access to its verb ping which belong to its api helloworld thanks to a HTTP or websocket request sent to the port 1234
such as follow
tip: for a better display of the request response, install jq (e.g. with:
dnf install jq
) and use it with| jq
like in the following command
$ curl -s http://localhost:1234/api/helloworld/ping | jq
You should get the following answer from the request
{
"jtype": "afb-reply",
"request": {
"status": "success",
"code": 0
},
"response": {
"pong": 1
}
}
$ curl -s -H "Content-Type: application/json" http://localhost:1234/api/helloworld/testargs -d '{"cezam" : "open"}' | jq
{
"jtype": "afb-reply",
"request": {
"status": "success",
"code": 0
},
"response": {
"code": 123456789
}
}
In another terminal, set the port forwarding with ssh
ssh -L 1234:localhost:1234 root@localhost
Then with your favorite web browser, access the Web UI with this url :
localhost:1234/devtools