/sensors-api

Primary LanguagePythonMIT LicenseMIT

Sensors API

Public API to receive data from multiple IoT sensors

Table of Contents

About The Project

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

Addicional Notes:

  • 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]

Built With

This project was built with the following technologies:

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

List of things you need to use the software and how to install them.

Env file

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

Installation

Install the project and its dependencies + pre commit hooks running the following:

make install

Run it

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

Run with Docker and Docker Compose

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

API Docs

The API documentation using OpenAPI spec (aka Swagger) is be available in /docs endpoint (http://localhost:8000/docs).

Tests

Just run the command above.

make test

Lint and Formatting

Run the following:

make lint

Roadmap

Some possible improvements for this project:

  • Add deployment configuration
  • Add database migration support
  • Add Authentication and Authorization
  • Add more test cases

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Patrick Rodrigues - @pythrick - contact@patrickrodrigues.me