USER-APPLICATION-HONEYCOMB is a Python-based application designed to fetch, manage, and process IoT device data from ChirpStack. The system dynamically updates the device list and decodes incoming sensor data using predefined codecs.
- Device Management: Fetches and maintains a dictionary (
all_devices) with devices and their codecs. - Event Processing: Listens to MQTT events from ChirpStack and decodes device payloads.
- Automatic Updates: A scheduler ensures newly added devices are fetched periodically.
- Modular Design: Well-structured code for better maintainability.
- Asymmetric Ciphering Support: Supports ECDH ciphering for secure encryption and decryption of device data.
USER-APPLICATION-HONEYCOMB/
🕠 __pycache__/ # Compiled Python files
🕠 .venv/ # Virtual environment (if used)
🕠 old_code/ # Backup of previous versions
🕠 .gitignore # Git ignore rules
🕠 application_fetcher.py # Fetches applications from ChirpStack
🕠 codec_fetcher.py # Retrieves codec information for devices
🕠 codec_struct_dec.py # Decodes structured codec data
🕠 codec.js # JavaScript codec file
🕠 config.py # Stores configuration variables
🕠 device_fetcher.py # Fetches devices from ChirpStack
🕠 device_manager.py # Manages device storage and updates
🕠 downlink.py # Handles downlink messaging
🕠 event_fetcher_parse.py # Listens to MQTT events and decodes data
🕠 http_integration_fetcher.py# Handles HTTP integration with ChirpStack
🕠 key_rotation.py # Manages key rotation for encryption
🕠 main.py # Starts the system (scheduler + MQTT listener)
🕠 README.md # Project documentation
🕠 requirements.txt # Required dependencies
🕠 scheduler.py # Periodically updates device list
🕠 send_http_request.py # Handles sending HTTP requests
🕠 tenant_fetcher.py # Fetches tenants from ChirpStack
Ensure you have:
- Python 3.10.12
- ChirpStack MQTT broker running
pipinstalled
git clone https://github.com/your-repo/user-application-honeycomb.git
cd user-application-honeycombpython -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install -r requirements.txtUpdate config.py with your ChirpStack credentials:
CHIRPSTACK_HOST = "localhost:8080" # Update with your host
AUTH_METADATA = {"Authorization": "Bearer YOUR_TOKEN"}device_manager.pyfetches and stores devices inall_devices(key:device_name, value:{euid, codec}).scheduler.pyrefreshes the device list every 10 minutes.
event_fetcher_parse.pysubscribes to ChirpStack MQTT topics.- When an event arrives:
- It extracts
dev_euiand payload data. - It matches
dev_euiwitheuidinall_devices. - It retrieves the correct codec and decodes the data.
- It extracts
Start the application by running:
python main.pyThis will:
- Start the scheduler (to update the device list every 10 minutes).
- Start the MQTT listener (to process incoming sensor events).
Handles fetching and storing device information:
device_manager.fetch_all_devices() # Fetches all devices
device_manager.show_device_names() # Displays the list of devicesRuns every 10 minutes to update all_devices:
schedule.every(10).minutes.do(scheduled_update)Listens for MQTT events and decodes messages:
client.subscribe("application/+/device/+/event/+")Matches dev_eui with euid:
def get_device_codec(dev_eui):
for device_name, device_info in device_manager.all_devices.items():
if device_info.get("euid") == dev_eui:
return device_info.get("codec")
return NoneCheck device updates:
tail -f scheduler.logCheck incoming MQTT messages:
tail -f event_fetcher.log- Real-time WebSocket integration for device updates.
- Database storage for historical events.
- Custom decoders for different device types.
This project is licensed under the MIT License.
Pull requests are welcome! Open an issue for discussions.
This section explains how to set up and run a database migration using Alembic for the Honeycomb User Application.
Make sure you have a virtual environment activated, then install all required Python packages:
pip install -r requirements.txt
Create a new Alembic migration environment:
alembic init alembic
This will create a folder named alembic/ and a configuration file alembic.ini in your project directory.
Open the alembic.ini file and verify that the sqlalchemy.url line is configured correctly.
It should look like this (update values as per your local setup):
[alembic]
sqlalchemy.url = postgresql://myuser:mypassword@localhost:5434/mydatabase
Open the file alembic/env.py inside the alembic/ directory and make the following changes.
from dotenv import load_dotenv load_dotenv()
from auth.database import Base target_metadata = Base.metadata
This ensures Alembic can detect your SQLAlchemy models and load environment variables from .env.
Now, generate a new migration file to add a new column to the users table:
alembic revision -m "add mfa_secret to users"
This command will create a new file under alembic/versions/.
Open the newly created migration file under alembic/versions/, and replace its content with the following:
def upgrade(): op.add_column('users', sa.Column('mfa_secret', sa.String(length=64), nullable=True))
def downgrade(): op.drop_column('users', 'mfa_secret')
This defines what happens when you apply (upgrade) or revert (downgrade) the migration.
Run the migration to update your database schema:
alembic upgrade head
If successful, the new column mfa_secret will be added to the users table.
You’ve successfully completed the Alembic migration setup and applied your first migration.
---------For second Migration for email alert column----------
Now, generate a new migration file to add a new column to the users table:
alembic revision -m "add loginalert email"
This command will create a new file under alembic/versions/.
Open the newly created migration file under alembic/versions/, and replace its content with the following:
def upgrade(): op.add_column('users', sa.Column('login_alert_email', sa.String(length=100), nullable=True)) pass
def downgrade(): op.drop_column('users', 'login_alert_email') pass
This defines what happens when you apply (upgrade) or revert (downgrade) the migration.
Run the migration to update your database schema:
alembic upgrade head
If successful, the new column login_alert_email will be added to the users table.
You’ve successfully completed the Alembic Second migration.