Public API to receive data from multiple IoT sensors
Sensors API is an application which demonstrates the following capabilities:
- Create an API endpoint which can receive data from a streaming device and store it
- Create a simulator which posts data at specific intervals to the above endpoint in the format listed below. There should be at least 10 devices reporting through the simulator
- Create an API endpoint that returns a histogram of status for a given device id
- Every sensor will always send a "deviceId" (string) and a "timestamp" (ISO-formatted timestamp string)
- Status can have only one of the following values [ON, OFF, ACTIVE, INACTIVE]
This project was built with the following technologies:
To get a local copy up and running follow these simple example steps.
List of things you need to use the software and how to install them.
- Python 3.10+
- Poetry
- Docker (optional)
- docker-compose (optional)
Create a copy from template file local.env
:
cp local.env .env
Edit the new .env
file
ENV=local <--------- (string) Name of the running environment (local, staging, production...)
DEBUG= <------------ (boolean) Enables SQL echo and Hot Reload for API server
DATABASE_URL= <----- (connection string) Database connection string, only `asyncpg` and `aiosqlite` are installed:
- postgresql+asyncpg://...:...@db:5432/sensors-api
- sqlite+aiosqlite:///db.sqlite3
- sqlite+aiosqlite://
# Environment Variables for PostgreSQL container in docker-compose.yaml
POSTGRES_PASSWORD= <--- (string) Password to be created in PostgreSQL container
POSTGRES_USER= <------- (string) Username to be created in PostgreSQL container
POSTGRES_DB= <--------- (string) Database to be created in PostgreSQL container
Install the project and its dependencies + pre commit hooks running the following:
make install
Run the API server with:
poetry run api
Run the Simulator with default parameters (10 devices, random streaming interval):
poetry run simulator
Run the Simulator with passing optional parameters:
poetry run simulator --devices=50 --streaming-interval=1 --base-url=http://localhost:8000
There's an option to run using Docker containers. The current docker-compose.yml
has the following available services:
- API Server:
api
- Simulator Scripts:
simulator
- PostgreSQL Database:
db
You can get them up and running altogether running:
docker-compose up
Or run them separately, first the API:
docker-compose up api
And next the Simulator:
docker-compose up simulator
The API documentation using OpenAPI spec (aka Swagger) is be available in /docs
endpoint (http://localhost:8000/docs).
Just run the command above.
make test
Run the following:
make lint
Some possible improvements for this project:
- Add deployment configuration
- Add database migration support
- Add Authentication and Authorization
- Add more test cases
Distributed under the MIT License. See LICENSE
for more information.
Patrick Rodrigues - @pythrick - contact@patrickrodrigues.me