From 5205bf629165ac443082bd6d2a225aafeda01f5f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 18 Jul 2023 15:19:39 +0300 Subject: [PATCH] misc: Add client workflow. --- .github/workflows/client-test.yml | 50 +++++++++++++++++++ .github/workflows/requirements.txt | 4 ++ tests/ci.py | 79 ++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 .github/workflows/client-test.yml create mode 100644 .github/workflows/requirements.txt create mode 100644 tests/ci.py diff --git a/.github/workflows/client-test.yml b/.github/workflows/client-test.yml new file mode 100644 index 0000000..b877584 --- /dev/null +++ b/.github/workflows/client-test.yml @@ -0,0 +1,50 @@ +name: '๐Ÿงช Test Cloud Client' + +on: + pull_request: + types: + - opened + - edited + - reopened + - synchronize + branches: + - 'main' + paths: + - '*.py' + - '.github/workflows/*.yml' + - '.github/workflows/*.json' + - '!**/README.md' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: 'โณ Checkout repository' + uses: actions/checkout@v3 + + - name: '๐Ÿ Set up Python 3' + uses: actions/setup-python@v3 + with: + cache: 'pip' + python-version: "3.10" + + - name: '๐Ÿ›  Install dependencies' + run: | + pip install -r .github/workflows/requirements.txt + python3 -m build --version + #sudo apt-get install softhsm2 gnutls-bin python3-m2crypto + + - name: '๐Ÿ“ฆ Build package' + run: python3 -m build + + - name: '๐Ÿ›  Install package' + run: | + python3 -m build + pip install dist/arduino_iot_cloud-*.whl + + - name: 'โ˜๏ธ Connect to IoT cloud' + env: + DEVICE_ID: ${{ secrets.DEVICE_ID }} + SECRET_KEY: ${{ secrets.SECRET_KEY }} + run: | + python tests/ci.py --basic-auth diff --git a/.github/workflows/requirements.txt b/.github/workflows/requirements.txt new file mode 100644 index 0000000..df7f663 --- /dev/null +++ b/.github/workflows/requirements.txt @@ -0,0 +1,4 @@ +build==0.10.0 +cbor2==5.4.6 +M2Crypto==0.38.0 +micropython-senml==0.1.0 diff --git a/tests/ci.py b/tests/ci.py new file mode 100644 index 0000000..2c0df7b --- /dev/null +++ b/tests/ci.py @@ -0,0 +1,79 @@ +# This file is part of the Python Arduino IoT Cloud. +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ +import time +import logging +import os +import sys +import asyncio +from arduino_iot_cloud import ArduinoCloudClient +import argparse +import arduino_iot_cloud.ussl as ssl + +KEY_PATH = "pkcs11:token=arduino" +CERT_PATH = "pkcs11:token=arduino" +CA_PATH = "ca-root.pem" + + +def exception_handler(loop, context): + pass + + +def on_value_changed(client, value): + logging.info(f"The answer to life, the universe, and everything is {value}") + loop = asyncio.get_event_loop() + loop.set_exception_handler(exception_handler) + sys.exit(0) + + +if __name__ == "__main__": + # Parse command line args. + parser = argparse.ArgumentParser(description="arduino_iot_cloud.py") + parser.add_argument( + "-d", "--debug", action="store_true", help="Enable debugging messages" + ) + parser.add_argument( + "-b", "--basic-auth", action="store_true", help="Enable basic authentication" + ) + args = parser.parse_args() + + # Configure the logger. + # All message equal or higher to the logger level are printed. + # To see more debugging messages, pass --debug on the command line. + logging.basicConfig( + datefmt="%H:%M:%S", + format="%(asctime)s.%(msecs)03d %(message)s", + level=logging.DEBUG if args.debug else logging.INFO, + ) + + # Create a client object to connect to the Arduino IoT cloud. + # To use a secure element, set the token's "pin" and URI in "keyfile" and "certfile", and + # the CA certificate (if any) in "ssl_params". Alternatively, a username and password can + # be used to authenticate, for example: + # client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY) + if args.basic_auth: + client = ArduinoCloudClient( + device_id=os.environ["DEVICE_ID"], + username=os.environ["DEVICE_ID"], + password=os.environ["SECRET_KEY"], + ) + else: + client = ArduinoCloudClient( + device_id=os.environ["DEVICE_ID"], + ssl_params={ + "pin": "1234", + "keyfile": KEY_PATH, + "certfile": CERT_PATH, + "ca_certs": CA_PATH, + "cert_reqs": ssl.CERT_REQUIRED, + }, + ) + + # Register cloud objects. + # Note: The following objects must be created first in the dashboard and linked to the device. + # This cloud object is initialized with its last known value from the cloud. When this object is updated + # from the dashboard, the on_switch_changed function is called with the client object and the new value. + client.register("answer", value=None, on_write=on_value_changed) + + # Start the Arduino IoT cloud client. + client.start()