Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (92 loc) · 3.61 KB
/
Copy pathmain.py
File metadata and controls
122 lines (92 loc) · 3.61 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from flask import Flask, render_template, jsonify, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, timedelta
import random
WATER_FREQUENCY = 0
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///leakage_data.db'
db = SQLAlchemy(app)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.png', mimetype='image/vnd.microsoft.icon')
class FloodData(db.Model):
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
location = db.Column(db.String(100))
flood_detected = db.Column(db.Boolean)
sensor_value = db.Column(db.Integer) # <-- Add this line
def generate_dummy_data():
if FloodData.query.count() == 0:
now = datetime.now()
timestamps = [now - timedelta(minutes=i*5) for i in range(100)]
locations = ['Basement', 'Street', 'Garage', 'Backyard']
WATER_THRESHOLD = 350
for i in range(100):
flood_detected = random.choice([True, False, False, False])
sensor_value = random.randint(WATER_THRESHOLD + 1, 700) if flood_detected else random.randint(100, WATER_THRESHOLD - 1)
data = FloodData(
location=random.choice(locations),
flood_detected=flood_detected,
sensor_value=sensor_value,
timestamp=timestamps[i]
)
db.session.add(data)
db.session.commit()
print("✅ Dummy flood detection data inserted")
@app.route('/')
def index():
data = FloodData.query.order_by(FloodData.timestamp.desc()).all()
return render_template("index.html", data=data)
@app.route('/dashboard')
def dashboard():
data = FloodData.query.order_by(FloodData.timestamp.desc()).all()
return render_template("dashboard.html", data=data)
@app.route('/api/all')
def all_data():
data = FloodData.query.order_by(FloodData.id.desc()).all()
return jsonify([
{
'id': d.id,
'location': d.location,
'flood_detected': d.flood_detected,
'timestamp': d.timestamp.isoformat()
} for d in data
])
@app.route('/api/latest')
def get_latest_data():
latest_data = FloodData.query.order_by(FloodData.timestamp.asc()).first()
if latest_data:
return jsonify({
'id': latest_data.id,
'timestamp': latest_data.timestamp.isoformat(),
'location': latest_data.location,
'flood_detected': latest_data.flood_detected,
'sensor_value': latest_data.sensor_value
})
else:
return jsonify({'error': 'No data found'}), 404
@app.route('/api/live')
def live_data():
if WATER_FREQUENCY == 0:
random_data = random.randint(100, 500) # Generate random frequency
data = {
'frequency': random_data, # Random water frequency
'location': random.choice(['Basement', 'Street', 'Garage', 'Backyard']),
'flood_detected': True if random_data > 350 else False, # Randomly choose flood detected
}
else:
data = {
'frequency': WATER_FREQUENCY,
'location': 'City',
'flood_detected': WATER_FREQUENCY > 350, # Flood is detected if the frequency is greater than 350
}
return jsonify(data)
@app.route('/live')
def live():
return render_template("live.html")
# Function to start the Arduino data reading process in a separate thread
def start_arduino_thread():
pass
if __name__ == '__main__':
app.run(debug=True)