Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/NERC-CEH/iot-swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-chambers committed May 30, 2024
2 parents c0b7980 + cd2580e commit bf2cf77
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 147 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages

on:
# Runs on pushes targeting the default branch
workflow_run:
workflows: ["Python Tests"]
branches: ["main"]
types:
- completed

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Single deploy job since we're just deploying
deploy:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Set up Python
# This is the version of the action for setting up Python, not the Python version.
uses: actions/setup-python@v5
with:
# Semantic version range syntax or exact version of a Python version
python-version: '3.12'
# Optional - x64 or x86 architecture, defaults to x64
architecture: 'x64'
cache: 'pip'
- name: Install Dependencies
run: |
pip install --upgrade pip
pip install .[docs]
- name: Build Docs
run: |
python -c "import iotdevicesimulator; print(iotdevicesimulator.__version__)"
pushd docs
. ./make.sh apidoc
. ./make.sh build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
# Upload entire repository
path: 'docs/_build/html'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python Tests

on: [push, pull_request]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install .[test]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: pytest -m "not oracle"
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.python/*
.pytest/*
.vscode/*
.*/*
**/__pycache__/*
*.docx
Expand All @@ -12,5 +9,6 @@ config.cfg
aws-auth
*.log
*.certs

docs/_*
!.github/*
docs/source/*
docs/_*
16 changes: 11 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ To create an IoT Swarm you must write a script such as the following:
import asyncio
import config
from pathlib import Path
import logging
async def main(config_path: str):
Expand All @@ -66,6 +67,10 @@ To create an IoT Swarm you must write a script such as the following:
Args:
config_path: A path to the config file for credentials and connection points.
"""
log_config = Path(Path(__file__).parent, "__assets__", "loggers.ini")
logging.config.fileConfig(fname=log_config)
app_config = config.Config(config_path)
iot_config = app_config["iot_core"]
Expand All @@ -74,8 +79,8 @@ To create an IoT Swarm you must write a script such as the following:
mqtt_connection = IotCoreMQTTConnection(
endpoint=iot_config["endpoint"],
cert_path=iot_config["cert_path"],
key_path=iot_config["pri_key_path"],
ca_cert_path=iot_config["aws_ca_cert_path"],
key_path=iot_config["key_path"],
ca_cert_path=iot_config["ca_cert_path"],
client_id="fdri_swarm",
)
swarm = await CosmosSwarm.create(
Expand All @@ -91,10 +96,11 @@ To create an IoT Swarm you must write a script such as the following:
await swarm.run()
if __name__ == "__main__":
if __name__ == "__main__":
config_path = str(Path(Path(__file__).parent, "__assets__", "config.cfg"))
asyncio.run(main(config_path))
config_path = "path/to/config.cfg"
asyncio.run(main(config_path))
This instantiates and runs a swarm of 5 sites from the COSMOS database that
each run for 5 cycles of queries and wait for 30 seconds between queries.
Expand Down
2 changes: 1 addition & 1 deletion docs/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function help() {
function apidoc() {
args=$@
if [ -z "$args" ]; then
args="--module-first --force --private"
args="--module-first --force"
fi

sphinx-apidoc $(echo $args) -o source ../src/iotdevicesimulator
Expand Down
20 changes: 0 additions & 20 deletions docs/source/iotdevicesimulator.mqtt.rst

This file was deleted.

73 changes: 0 additions & 73 deletions docs/source/iotdevicesimulator.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/source/modules.rst

This file was deleted.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ dependencies = [
]
name = "iot-device-simulator"
dynamic = ["version"]
# version = "0.0.0"
authors = [{ name = "Lewis Chambers", email = "[email protected]" }]
description = "Package for simulating a net of IoT devices for stress testing."

[project.optional-dependencies]
test = ["pytest", "pytest-cov", "pytest-asyncio", "parameterized"]
docs = ["sphinx", "sphinx-copybutton"]
docs = ["sphinx", "sphinx-copybutton", "sphinx-rtd-theme"]

[tool.setuptools.dynamic]
version = { attr = "iotdevicesimulator.__version__" }
Expand Down
12 changes: 12 additions & 0 deletions src/iotdevicesimulator/__assets__/configs.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
oracle: {
dsn: "wldbase:1521/wla"
user: "lewcha"
password: "Lucid+Barrel8Valley"
}
iot_core: {
endpoint: "a10mem0twl4qxt-ats.iot.eu-west-2.amazonaws.com"
port: 8883
cert_path: "C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotdevicesimulator/__assets__/.certs/fdri_swarm.cert.pem"
key_path: "C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotdevicesimulator/__assets__/.certs/fdri_swarm.private.key"
ca_cert_path: "C:/Users/lewcha/OneDrive - UKCEH/Documents/FDRI/projects/iot-device-simulator/src/iotdevicesimulator/__assets__/.certs/AmazonRootCA1.pem"
}
11 changes: 9 additions & 2 deletions src/iotdevicesimulator/example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Module for demonstrating invocation of a swarm."""

from iotdevicesimulator.queries import CosmosQuery
from iotdevicesimulator.swarm import CosmosSwarm
from iotdevicesimulator.mqtt.aws import IotCoreMQTTConnection
import asyncio
import config
from pathlib import Path
import logging


async def main(config_path: str):
Expand All @@ -12,6 +15,10 @@ async def main(config_path: str):
Args:
config_path: A path to the config file for credentials and connection points.
"""
log_config = Path(Path(__file__).parent, "__assets__", "loggers.ini")

logging.config.fileConfig(fname=log_config)

app_config = config.Config(config_path)

iot_config = app_config["iot_core"]
Expand All @@ -20,8 +27,8 @@ async def main(config_path: str):
mqtt_connection = IotCoreMQTTConnection(
endpoint=iot_config["endpoint"],
cert_path=iot_config["cert_path"],
key_path=iot_config["pri_key_path"],
ca_cert_path=iot_config["aws_ca_cert_path"],
key_path=iot_config["key_path"],
ca_cert_path=iot_config["ca_cert_path"],
client_id="fdri_swarm",
)
swarm = await CosmosSwarm.create(
Expand Down
2 changes: 1 addition & 1 deletion src/iotdevicesimulator/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self, *args, **kwargs):

logpath = Path(
platformdirs.site_data_dir("iot_device_simulator"),
platformdirs.user_data_dir("iot_device_simulator"),
"log.log",
)

Expand Down
4 changes: 0 additions & 4 deletions src/iotdevicesimulator/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
import uuid
import config

log_config = Path(Path(__file__).parent, "__assets__", "loggers.ini")

logging.config.fileConfig(fname=log_config)

logger = logging.getLogger(__name__)


Expand Down
7 changes: 1 addition & 6 deletions src/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
import pytest
import config
import pathlib
import threading
from typing import Awaitable
import asyncio
from iotdevicesimulator import db
from iotdevicesimulator.queries import CosmosQuery
import oracledb

CONFIG_PATH = pathlib.Path(
pathlib.Path(__file__).parents[1], "iotdevicesimulator", "__assets__", "config.cfg"
)


config_exists = pytest.mark.skipif(
not CONFIG_PATH.exists(),
reason="Config file `config.cfg` not found in root directory.",
Expand Down Expand Up @@ -55,6 +49,7 @@ async def test_query(self):
self.assertEqual(row["SITE_ID"], site_id)

@pytest.mark.asyncio
@pytest.mark.oracle
@config_exists
async def test_bad_query_type(self):

Expand Down
Loading

0 comments on commit bf2cf77

Please sign in to comment.