-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (37 loc) · 1.3 KB
/
main.py
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
import os
from flask_migrate import Migrate
from app import create_app, db
from app.models import Building, SensorReading
from config import DEFAULT_CONFIG
app = create_app(os.getenv("TARGET_ENVIRONMENT") or DEFAULT_CONFIG)
migrate = Migrate(app=app, db=db)
@app.shell_context_processor
def make_shell_context():
return dict(app=app, db=db, Building=Building, SensorReading=SensorReading)
@app.cli.command()
def seed_db():
import random
building_names = ["Pegasus", "Elixir", "Moon Tiger Enterprise"]
sensor_names = ["temperature_sensor", "humidity_sensor", "light_sensor"]
for name in building_names:
buildig = Building(
building_name=name,
geo_longitude=random.random(),
geo_latitude=random.random(),
)
db.session.add(buildig)
db.session.commit()
buildings = Building.query.all()
for building in buildings:
for _ in range(25):
reading = SensorReading(
building_id=building.id,
sensor_name=sensor_names[random.randint(0,2)],
sensor_value = random.random() * 10
)
db.session.add(reading)
db.session.commit()
@app.cli.command()
def clear_db():
db.session.query(SensorReading).delete()
db.session.query(Building).delete()