Real-time event streaming service that connects Dahua IVSS cameras to TelefΓ³nica's IoT Platform (FIWARE).
- Overview
- Features
- Architecture
- Installation
- Configuration
- Usage
- Monitoring
- Troubleshooting
- API Reference
- Contributing
This system provides real-time ingestion and processing of video analytics events from Dahua IVSS (Intelligent Video Surveillance System) cameras, forwarding them to TelefΓ³nica's IoT Agent JSON platform for integration with FIWARE Context Broker.
-
EVD (Electric Vehicle Detection / Traffic)
TrafficJunction- Vehicle crossing detectionTrafficFlowStat- Traffic flow statistics
-
CROWD (People Counting)
PeopleCounting- People counting eventsCrowd/CrowdDensity- Crowd density monitoringPassengerFlow- Passenger flow statisticsHumanTrait/SmartMotionHuman- Human detectionLineCounting/CrossLine- Line crossing detection
- β Real-time Event Streaming - Continuous IVSS event stream processing
- β Event Deduplication - Configurable TTL-based deduplication
- β Privacy Protection - Automatic redaction of sensitive data (license plates)
- β Device Whitelisting - Allowlist-based device filtering
- β Name Mapping - Friendly names for devices
- β Rate Limiting - Per-device token bucket rate limiter
- β Circuit Breaker - Automatic failure protection with cooldown
- β Automatic Reconnection - Resilient IVSS connection management
- β Hot Reload - Configuration reload without restart (SIGHUP)
- β Graceful Shutdown - Clean service termination
- β Web Dashboard - Modern, responsive real-time dashboard
- β Metrics Export - Comprehensive runtime metrics
- β Event Logging - NDJSON event log with rotation
- β Health Checks - Service and connectivity monitoring
- β Status Files - Runtime state persistence
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Dahua IVSS β Stream β traffic.py β HTTPS β IoT Agent JSON β
β Cameras βββββββββ>β (Event Pipeline)βββββββββ>β (FIWARE) β
β (10.0.0.12) β HTTP β β POST β (TelefΓ³nica) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
β Runtime Files
βΌ
βββββββββββββββββββ
β /opt/traffic/ β
β runtime/ β
β - status.json β
β - events.json β
β - last_*.json β
βββββββββββββββββββ
β
β HTTP API
βΌ
βββββββββββββββββββ
β dashboard.py β
β Web UI :5000 β
βββββββββββββββββββ
- Ingestion - Connect to IVSS event stream (HTTP GET with streaming)
- Parsing - Extract JSON objects from stream
- Deduplication - Check event fingerprint against recent cache
- Privacy - Redact sensitive information (plates, etc.)
- Extraction - Parse event type and extract relevant fields
- Resolution - Map machine name β device serial β friendly name
- Filtering - Apply allowlist and rate limits
- Publishing - POST to IoT Agent JSON with circuit breaker protection
- Logging - Write to NDJSON log and update status files
- Python 3.12 or higher
- Linux system with systemd (recommended)
- Network access to IVSS cameras and IoT Platform
- VPN connection (if required for camera network)
# Clone repository
git clone https://github.com/yourusername/IOT.git
cd IOT
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install --upgrade pip
pip install -r requirements.txt
# Create configuration
cp .env.example .env
nano .env # Edit with your settings
# Create runtime directories
sudo mkdir -p /opt/traffic/runtime
sudo mkdir -p /opt/traffic/config
sudo chown -R $USER:$USER /opt/traffic
# Create configuration files
touch /opt/traffic/allowlist.txt
echo '{}' > /opt/traffic/machine_to_serial.json
echo '{}' > /opt/traffic/camera_name_map.json
# Test traffic service
python traffic.py
# In another terminal, start dashboard
python dashboard.py# Install as systemd services
sudo cp systemd/traffic.service /etc/systemd/system/
sudo cp systemd/dashboard.service /etc/systemd/system/
# Edit service files with your paths
sudo nano /etc/systemd/system/traffic.service
sudo nano /etc/systemd/system/dashboard.service
# Reload systemd and start services
sudo systemctl daemon-reload
sudo systemctl enable traffic dashboard
sudo systemctl start traffic dashboard
# Check status
sudo systemctl status traffic
sudo systemctl status dashboardSee .env.example for all configuration options. Key settings:
TRAFFIC_IVSS_URL=http://10.0.0.12/cgi-bin/eventManager.cgi?...
CAMERA_USER=admin
CAMERA_PASS=your_passwordIOTA_JSON_BASE=https://iota-json.iotplatform.telefonica.com:18185/iot/json
IOTA_APIKEY_EVD=your_evd_apikey
IOTA_APIKEY_CRWD=your_crowd_apikeyIOTA_MAX_PER_MINUTE=60 # Max events per device per minute
IOTA_BURST=10 # Max events per second (burst)IOTA_CB_OPEN_AFTER=20 # Open after N consecutive failures
IOTA_CB_COOLDOWN_SEC=30 # Cooldown period in secondsWhitelist of allowed device serials (one per line):
ABC12345678
XYZ98765432
DEF11223344
Empty file = allow all devices.
Map machine names to device serials:
{
"Camera-Front-Door": "ABC12345678",
"Camera-Back-Gate": "XYZ98765432",
"IPC-HFW5442E": "DEF11223344"
}Map device serials to friendly names:
{
"ABC12345678": "Main Entrance",
"XYZ98765432": "Rear Gate",
"DEF11223344": "Parking Lot"
}Reload configuration files without restarting:
# Send SIGHUP signal to reload maps
sudo systemctl reload traffic
# Or manually
kill -HUP $(pgrep -f traffic.py)# Development mode
python traffic.py # Terminal 1
python dashboard.py # Terminal 2
# Production mode (systemd)
sudo systemctl start traffic
sudo systemctl start dashboard
# View logs
sudo journalctl -u traffic -f
sudo journalctl -u dashboard -fOpen browser: http://localhost:5000
The dashboard provides:
- Real-time event metrics
- Event rate charts (last 10 minutes)
- System health indicators
- Circuit breaker status
- Last events (EVD/CROWD)
- Debug information
# Check service status
sudo systemctl status traffic
# Restart service
sudo systemctl restart traffic
# Reload configuration
sudo systemctl reload traffic
# View recent logs
sudo journalctl -u traffic -n 100
# Follow logs
sudo journalctl -u traffic -f
# Check runtime status
cat /opt/traffic/runtime/status.json | jq .# Dashboard health check
curl http://localhost:5000/healthz
# Get system status
curl http://localhost:5000/api/status | jq .
# Get event rates
curl http://localhost:5000/api/rate | jq .
# Get last events
curl http://localhost:5000/api/last/evd | jq .
curl http://localhost:5000/api/last/crowd | jq .All runtime state is stored in /opt/traffic/runtime/:
- status.json - Current system status
- events.json - NDJSON log of all events (rotated at 10MB)
- last_evd.json - Last vehicle/traffic event
- last_crowd.json - Last people counting event
- last_unknown.json - Last unrecognized event (debugging)
Key metrics tracked:
total_events_received- Events received from IVSStotal_events_published- Events successfully published to IoTAtotal_events_deduplicated- Duplicate events filteredtotal_events_filtered- Events filtered by allowlisttotal_rate_limited- Events delayed by rate limiterconsecutive_failures- Current failure streakcircuit_breaker.trip_count- Times circuit breaker opened
-
Check IVSS connection:
curl -u admin:password "http://10.0.0.12/cgi-bin/eventManager.cgi?action=attach&codes=%5BTrafficJunction%5D" -
Check VPN connectivity:
ip link show ppp0 ping 10.0.0.12
-
Verify credentials in
.env
-
Check circuit breaker status:
curl http://localhost:5000/api/status | jq '.breaker'
-
Check rate limiting:
curl http://localhost:5000/api/status | jq '.metrics'
-
Test IoTA connectivity:
curl -X POST \ "https://iota-json.iotplatform.telefonica.com:18185/iot/json?k=APIKEY&i=DEVICEID" \ -H "Content-Type: application/json" \ -d '{"test": "value"}'
-
Check if events are being received:
tail -f /opt/traffic/runtime/events.json | grep -i people -
Check last unknown event (might be using different field names):
cat /opt/traffic/runtime/last_unknown.json | jq .
-
Verify camera firmware supports people counting events
-
Check IVSS URL includes people counting codes:
codes=%5BPeopleCounting,Crowd,CrowdDensity%5D
This is likely a system-level issue with your VPN client. Check:
-
VPN client logs:
sudo journalctl -u pppd -f
-
Network stability:
ping -c 100 10.0.0.12
-
Consider using a VPN keepalive script or auto-reconnect daemon
# Check cooldown remaining
curl http://localhost:5000/api/status | jq '.breaker.cooldown_remaining'
# Force restart to reset
sudo systemctl restart trafficFor detailed documentation, troubleshooting, and development guides, see:
π docs/README.md - Main documentation hub
Quick links:
- π Debugging: No People Events -
β οΈ Start here if people counting doesn't work - π Event Types Reference - All supported event types
- π Roadmap - Planned improvements
- π§ Diagnostic Script - Auto-diagnose people events
Health check endpoint.
Response: 200 OK
Get complete system status.
Response:
{
"service": "traffic",
"version": "1.0.0",
"server_time_utc": "2024-01-15T10:30:00Z",
"ivss": {
"connected": true,
"last_event_utc": 1705318200
},
"iota": {
"last_code": 200,
"breaker_open": false
},
"families": {
"EVD": {"total": 1234, "last_utc": 1705318200},
"CROWD": {"total": 567, "last_utc": 1705318190}
},
"metrics": {
"received": 2000,
"published": 1801,
"deduplicated": 150,
"filtered": 49,
"rate_limited": 10,
"consec_fail": 0
},
"breaker": {
"breaker_open": false,
"consec_fail": 0,
"trip_count": 2,
"cooldown_remaining": 0
},
"maps": {
"allowlist_count": 5,
"machine_to_serial_count": 8,
"name_map_count": 8
},
"dedup": {
"queue_len": 150,
"ttl_sec": 10
},
"infra": {
"vpn_iface": "ppp0",
"vpn_up": true,
"ivss_host": "10.0.0.12",
"ivss_ping": true
}
}Get event rates by family.
Response:
{
"window_sec": 90,
"EVD": {"rate": 0.1234, "count": 11},
"CROWD": {"rate": 0.0567, "count": 5}
}Get last vehicle/traffic event.
Get last people counting event.
Get last unrecognized event (debugging).
Get last N events from NDJSON log.
Get events within last N seconds.
Get timeline data for charting.
Response:
{
"timestamps": [1705318140, 1705318200, ...],
"evd": [5, 3, 7, ...],
"crowd": [2, 4, 1, ...],
"minutes": 10
}Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 guidelines
- Use type hints (Python 3.12+)
- Add docstrings to functions
- Run
blackfor formatting - Run
flake8for linting - Run
mypyfor type checking
# Format code
black traffic.py dashboard.py
# Lint
flake8 traffic.py dashboard.py
# Type check
mypy traffic.py dashboard.pyThis project is licensed under the MIT License - see the LICENSE file for details.
- Dahua Technology for IVSS camera systems
- TelefΓ³nica for IoT Platform (FIWARE)
- FIWARE Foundation for Context Broker
For issues, questions, or contributions:
- Open an issue: GitHub Issues
- Documentation: Wiki
Built with β€οΈ for smart city traffic monitoring