forked from AsmiBhaskar/Voltonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
108 lines (89 loc) · 3.48 KB
/
Copy pathrun.py
File metadata and controls
108 lines (89 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from app import create_app
from app.models import db, Room, EnergyLog
from app.utils.seed_data import seed_campus
from app.simulation.engine import IoTSimulator
from app.prediction.predictor import EnergyPredictor
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, timedelta
import atexit
# Create Flask app
app = create_app()
# Global predictor instance
predictor = EnergyPredictor()
def initialize_database():
"""Check if database needs seeding"""
with app.app_context():
room_count = Room.query.count()
if room_count == 0:
print("\n📦 Database is empty. Starting seed process...\n")
seed_campus()
print("\n✅ Database seeded successfully!\n")
else:
print(f"\n✅ Database already contains {room_count} rooms\n")
def run_simulation_job():
"""Scheduled job to simulate IoT data every 60 seconds"""
with app.app_context():
try:
IoTSimulator.simulate_all_rooms()
except Exception as e:
print(f"❌ Simulation error: {e}")
def train_ml_model_job():
"""Scheduled job to retrain ML model daily"""
with app.app_context():
try:
print("\n🤖 Starting scheduled model retraining...")
predictor.train_model(hours_back=168)
except Exception as e:
print(f"❌ Model training error: {e}")
def check_and_train_initial_model():
"""Train model if enough data exists"""
with app.app_context():
# Check if we have enough data (at least 2 hours of logs)
cutoff = datetime.now() - timedelta(hours=2)
log_count = EnergyLog.query.filter(EnergyLog.timestamp >= cutoff).count()
if log_count >= 100:
print("\n🤖 Sufficient data found. Training initial ML model...")
success, result = predictor.train_model(hours_back=24)
if success:
print(f"✅ Initial model trained! MAE: {result['mae']} kW\n")
else:
print(f"\n⏳ Not enough data yet ({log_count}/100 records). Model will train once sufficient data is available.\n")
def start_simulation_scheduler():
"""Start background scheduler for IoT simulation"""
scheduler = BackgroundScheduler()
# Run simulation every 60 seconds
scheduler.add_job(
func=run_simulation_job,
trigger="interval",
seconds=60,
id="iot_simulation",
name="IoT Data Simulation",
replace_existing=True
)
# Retrain ML model every 24 hours
scheduler.add_job(
func=train_ml_model_job,
trigger="interval",
hours=24,
id="ml_training",
name="ML Model Retraining",
replace_existing=True
)
scheduler.start()
print("🔄 IoT Simulation Scheduler started (60-second interval)")
print("🤖 ML Model Retraining scheduled (24-hour interval)")
# Run first simulation immediately
run_simulation_job()
# Check if we can train initial model
check_and_train_initial_model()
# Shutdown scheduler on exit
atexit.register(lambda: scheduler.shutdown())
if __name__ == "__main__":
print("⚡ VOLTONIC Backend Starting...\n")
# Initialize database
initialize_database()
# Start IoT simulation
start_simulation_scheduler()
# Run Flask app
print("\n🚀 Starting Flask server on http://127.0.0.1:5000\n")
app.run(debug=True, host='0.0.0.0', port=5000, use_reloader=False)