Files
smart-city-digital-twin-mar…/populate_influx.py

77 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Populate InfluxDB with Smart City sensor data for Martinique."""
import os
import time
import random
from datetime import datetime, timezone
try:
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
except ImportError:
print("❌ influxdb-client not installed. Run: pip install influxdb-client")
exit(1)
# Config
INFLUX_URL = os.environ.get("INFLUX_URL", "http://localhost:8086")
INFLUX_ORG = os.environ.get("INFLUX_ORG", "digitribe")
INFLUX_BUCKET = os.environ.get("INFLUX_BUCKET", "iot_data")
INFLUX_TOKEN = os.environ.get("INFLUX_TOKEN",
"yA8zFZYsPOLDdDxlviIfHw_5gELH8k439TANamk2JiJIyAbhyNCHDzUeYJkjL-hAy99fs_96j_59WprZy98A==")
# Martinique coordinates (Fort-de-France area)
BASE_LAT = 14.6091
BASE_LON = -61.2155
# Sensor types and their fields
SENSOR_TYPES = {
"traffic": {
"fields": {"vehicle_count": (10, 150), "average_speed_kmh": (10, 80), "congestion_level": (0, 5)},
"locations": ["Carrefour Central", "Avenue des Caraïbes", "Boulevard Pasteur", "Rue des Flamboyants", "Place de la République"]
},
"airquality": {
"fields": {"pm25_ugm3": (5, 80), "pm10_ugm3": (10, 150), "no2_ugm3": (5, 60), "o3_ugm3": (20, 120)},
"locations": ["Quartier Bonde", "Port de Fort-de-France", "Château Denis", "Lamentin Aéroport"]
},
"parking": {
"fields": {"total_spots": (50, 500), "available_spots": (0, 500), "occupancy_percent": (0, 100)},
"locations": ["Parking Rivière-Salée", "Parking Cluny", "Parking Médoc", "Parking Grand-Camp"]
},
}
def main():
print("📈 Connecting to InfluxDB...")
client = influxdb_client.InfluxDBClient(url=INFLUX_URL, token=INFLUX_TOKEN, org=INFLUX_ORG)
write_api = client.write_api(write_options=SYNCHRONOUS)
points = []
now = datetime.now(timezone.utc)
print("📊 Generating sensor data for Martinique...")
for stype, config in SENSOR_TYPES.items():
for i, location in enumerate(config["locations"]):
sid = f"{stype}_{i:03d}"
lat = BASE_LAT + random.uniform(-0.05, 0.05)
lon = BASE_LON + random.uniform(-0.05, 0.05)
for field, (lo, hi) in config["fields"].items():
value = round(random.uniform(lo, hi), 1)
p = influxdb_client.Point(stype)\
.tag("sensor_id", sid)\
.tag("location", location)\
.field(field, float(value))\
.field("lat", lat)\
.field("lon", lon)\
.time(now)
points.append(p)
print(f"📤 Writing {len(points)} points to bucket '{INFLUX_BUCKET}'...")
write_api.write(bucket=INFLUX_BUCKET, record=points)
print(f"✅ Done! {len(points)} points written.")
client.close()
if __name__ == "__main__":
main()