How elmadkouryassine/sms_gateway works
Project type and purpose
• This is a full-stack, Flask-based SMS gateway application with both a web UI and a REST API.
• It handles sending/receiving SMS and USSD via Arduino-connected SIM800L modules, persists data in MySQL, and runs a background scheduler daemon.
File structure
• Top level
• app.py – Flask app creation, blueprint registration, secret key, app.run()
• config.py – DB URI and other config
• run_scheduler.py – standalone scheduler loop
• database/
• models.py – SQLAlchemy ORM classes for users, roles, messages, tasks, SIM ports, etc.
• routes/ (Flask Blueprints)
• auth, admin, dashboard, inbox, reports, settings, sim_status, sms, sms_campaign, tasks, ussd, at (AT commands)
• auth_decorator for role-based access
• services/ (business logic)
• scheduler.py – poll pending tasks, update status
• serial_handler.py – low-level AT & USSD comms over /dev/ttyUSB*
• sim_monitor.py – fetch and log SIM health metrics
• sms_parser.py – parse CSV for bulk campaigns
• scripts/
• seed_*.py – DB seeding for roles, admin, demo, settings
• test_db.py, test_sim_updates.py
• static/ & templates/
• JS/CSS assets driving client behavior
• Jinja2 templates structured by feature and partials
Insights from the README
• Describes features: user/role management, single & bulk SMS, inbox, reports, USSD, AT console, real-time SIM status, REST endpoint.
• Emphasizes scheduler as a background daemon.
• Technology stack: Python 3.6+, Flask, Flask-SQLAlchemy, MySQL/MariaDB, Jinja2, serial comms.
a. Main components
1. Client Tier
– Web Browser (UI assets served by Flask)
– External systems (POST to /sms/api/send_single)
2. Web Application (Flask)
– Blueprints (auth, admin, sms, inbox, ussd, at, sim_status, dashboard, reports, settings)
– Static file server & template renderer
– REST API endpoint
3. Services Layer
– Scheduler Service (polls DB for pending SMS/USSD tasks)
– Serial Handler (AT & USSD over serial ports)
– SIM Monitor (periodically reads signal strength, operator)
– SMS Parser (bulk CSV import)
4. Database
– MySQL/MariaDB via SQLAlchemy models
5. Hardware
– Arduino Mega with multiple SIM800L modules on /dev/ttyUSB*
b. Relationships & data flow
• User/browser → HTTP → Flask routes → services & DB → HTML/JSON responses
• External alert system → REST API → Flask → services enqueues task → DB
• Scheduler → DB (get pending tasks) → Serial Handler → SIM modules → status back to DB
• SIM Monitor → Serial Handler → SIM modules → DB updates
• SMS Parser → reads CSV → DB inserts campaign tasks
c. Architectural patterns & principles
• Layered architecture: Presentation (templates/static), Application (Flask routes), Business Logic (services), Data Access (SQLAlchemy models).
• Background worker pattern for scheduler.
• Blueprint modularization for route separation.
• Dependency injection via config and app context.
d. Key technologies & libraries
• Flask + Flask-SQLAlchemy + Jinja2
• Python services using PySerial (serial_handler), scheduled via custom loop (run_scheduler.py)
• MySQL/MariaDB with pymysql driver
• Front-end: vanilla JS modules, CSS
Details
• Interaction with scripts only if they run in production (seed scripts are out-of-band).
• How configuration (config.py) feeds into all Python components.
• Serial port mapping (e.g., /dev/ttyUSB1 → SIM Port 2).