A simple REST API built with FastAPI to manage sensor values of IOT devices and provide data for applications.
- The API allows to create multiple sensors and users.
- Users can be normal users or superusers with elevated permissions.
- A user (sensor) can create sensor data. The sensor data shall contain temperature, humidity, pressure and voltage.
- The read and write access to sensors and their data is protected. With SensorPermissions you can allow a user to read and / or write data for a given sensor.
- This allows permission control, so that devices providing data can only write data to specific sensors and applications or users can only read data from specific sensors.
- The API has endpoints to request a forecast for a given location.
- The API offers endpoints to request data from the Prepaid-Hoster REST API, to give information about the status of the server.
- You must have Python3.11 installed (not tested with other versions).
- You will need a running instance of PostgreSQL. The API has been tested on version 16.4.
- Create and activate a venv:
- Install all the needed requirements from the
requirements.txtfile.- In case you want to develop you will also need the
dev-requirements.txtinstalled.- This includes test dependencies as well as black and isort.
- In case you want to develop you will also need the
- Copy the file
SECRETS.example.pyand name itSECRETS.py - Set up a user and a database for the api and update the
PSQL_URLstring accordingly in the SECRETS file. - For the alembic migrations to work, the
example.alembic.inifile need to be copied and namedalembic.ini- In this file
sqlalchemy.urlneeds to be adapted accordingly
- In this file
- For requesting forecast data the api also accesses the Openweathermap One Call API 3.0.
- Create a token and update the
OPENWEATHERMAP_KEYstring in the SECRETS file.
- Create a token and update the
- For requesting stats about the server from my personal hoster (prepaid-hoster.de) I access data from their API.
- For that you also need to add a valid API token and update the
SERVERSTATS_SETTINGSdict accordingly.
- For that you also need to add a valid API token and update the
- To create the database schema run
alembic upgrade head - To create the initial user in the database without manually writing to the database, there is a filed called
cli_create_user.py.- With
python cli_create_user.py --username <username> --password <password> [--superuser]you can create a new user with the given name and password.
- With
- To run the api use the following command:
uvicorn api.main:app --reload - on
localhost:8000/docsyou will get detailed documentation over all endpoints.
- Code should always be formatted with
black .andisort . - Tests can be run with coverage with the following command:
pytest tests --cov=api --cov-report=html- A html report will be generated under
htmlcov/index.html
- A html report will be generated under