Upload project.
This commit is contained in:
parent
b28b77a1ed
commit
b873632828
183
.gitignore
vendored
Normal file
183
.gitignore
vendored
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
|
||||||
|
# Created by https://www.gitignore.io/api/flask,python,visualstudiocode
|
||||||
|
# Edit at https://www.gitignore.io/?templates=flask,python,visualstudiocode
|
||||||
|
|
||||||
|
### Flask ###
|
||||||
|
instance/*
|
||||||
|
!instance/.gitignore
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
### Flask.Python Stack ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
pip-wheel-metadata/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
target/
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
.python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# celery beat schedule file
|
||||||
|
celerybeat-schedule
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# Mr Developer
|
||||||
|
.mr.developer.cfg
|
||||||
|
.project
|
||||||
|
.pydevproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
### Python ###
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
|
||||||
|
# celery beat schedule file
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
|
||||||
|
# Mr Developer
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
|
||||||
|
### VisualStudioCode ###
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
||||||
|
|
||||||
|
### VisualStudioCode Patch ###
|
||||||
|
# Ignore all local history of files
|
||||||
|
.history
|
||||||
|
|
||||||
|
# End of https://www.gitignore.io/api/flask,python,visualstudiocode
|
||||||
|
|
||||||
|
src/iotvenv/
|
||||||
|
|
||||||
|
src/.vscode/
|
||||||
|
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
iotvenv/
|
||||||
|
|
||||||
|
*.db
|
7
config.py
Normal file
7
config.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import os
|
||||||
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
class Config(object):
|
||||||
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
|
||||||
|
'sqlite:///' + os.path.join(basedir, 'app.db')
|
||||||
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
3
flaskapp.py
Normal file
3
flaskapp.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from flaskapp import app
|
||||||
|
|
||||||
|
app.run(host='0.0.0.0', port='80')
|
14
flaskapp/__init__.py
Normal file
14
flaskapp/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from flask import Flask
|
||||||
|
from config import Config
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from flask_migrate import Migrate
|
||||||
|
from flask_bootstrap import Bootstrap
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_object(Config)
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
|
from flaskapp import routes, models, errors
|
||||||
|
|
||||||
|
bootstrap = Bootstrap(app)
|
13
flaskapp/dangercalc.py
Normal file
13
flaskapp/dangercalc.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from flaskapp.models import DangerLevel
|
||||||
|
|
||||||
|
Critical_Temperature = 105
|
||||||
|
Critical_Light = 40
|
||||||
|
Critical_Humididty = 80
|
||||||
|
|
||||||
|
def danger_calculation(baro_pressure,light,humidity,avg_temp):
|
||||||
|
if avg_temp >= Critical_Temperature:
|
||||||
|
return DangerLevel.Critical
|
||||||
|
if light >= Critical_Light:
|
||||||
|
return DangerLevel.Critical
|
||||||
|
if humidity >= Critical_Humididty:
|
||||||
|
return DangerLevel.Critical
|
11
flaskapp/errors.py
Normal file
11
flaskapp/errors.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from flask import render_template
|
||||||
|
from flaskapp import app, db
|
||||||
|
|
||||||
|
@app.errorhandler(404)
|
||||||
|
def not_found_error(error):
|
||||||
|
return render_template('404.html'), 404
|
||||||
|
|
||||||
|
@app.errorhandler(500)
|
||||||
|
def internal_error(error):
|
||||||
|
db.session.rollback()
|
||||||
|
return render_template('500.html'), 500
|
27
flaskapp/models.py
Normal file
27
flaskapp/models.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from flaskapp import db
|
||||||
|
|
||||||
|
class SensorData(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
humidity = db.Column(db.Float)
|
||||||
|
avg_temp = db.Column(db.Float)
|
||||||
|
baro_pressure = db.Column(db.Float)
|
||||||
|
light = db.Column(db.Float)
|
||||||
|
timestamp = db.Column(db.DateTime)
|
||||||
|
danger_level = db.Column(db.Integer)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<SensorData {}>'.format(self.id)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getAll():
|
||||||
|
return SensorData.query.all()
|
||||||
|
|
||||||
|
class DangerLevel():
|
||||||
|
Critical = 1 #The server is in a dangerous state, it should be automatically shutdown to prevent damage to components and data loss.
|
||||||
|
High = 2 #The server is outside of standard operating conditions, and its state should be manually reviewed to prevent data loss.
|
||||||
|
Medium = 3 #The server getting close to the limit of standard operating conditions and should be kept an eye on, but could also be due to sustained load.
|
||||||
|
Low = 4 #The server is a little above standard temperatures, however is standard for burst computing.
|
||||||
|
Nil = 5 #There is no danger level; data is normal.
|
||||||
|
Unknown = 6 #Some data may be incorrect, and the sensor should be checked.
|
||||||
|
Tampered = 7 #The server may have been tampered with, such as getting moved or physically accessed.
|
||||||
|
Other = 8
|
39
flaskapp/routes.py
Normal file
39
flaskapp/routes.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from flaskapp import app, db
|
||||||
|
from flaskapp.models import SensorData, DangerLevel
|
||||||
|
from flask import render_template, request, jsonify
|
||||||
|
import datetime
|
||||||
|
from flaskapp.dangercalc import danger_calculation
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
@app.route("/index")
|
||||||
|
def index():
|
||||||
|
data = SensorData.query.order_by(SensorData.timestamp.desc())
|
||||||
|
return render_template('index.html', title='Home', data=data)
|
||||||
|
|
||||||
|
@app.route('/', methods=["POST"])
|
||||||
|
def newdata():
|
||||||
|
baro_temp = round(request.json['baro_temp'], 2)
|
||||||
|
baro_pressure = round(request.json['baro_pressure'], 2)
|
||||||
|
light = round(request.json['light'], 2)
|
||||||
|
humidity_temp = round(request.json['humidity_temp'], 2)
|
||||||
|
humidity = round(request.json['humidity'], 2)
|
||||||
|
avg_temp = round((baro_temp + humidity_temp)/2, 2)
|
||||||
|
timestamp = datetime.datetime.now()
|
||||||
|
danger_level = danger_calculation(baro_pressure, light, humidity, avg_temp)
|
||||||
|
|
||||||
|
sensordata = SensorData(timestamp=timestamp, baro_pressure=baro_pressure,light=light,humidity=humidity, avg_temp=avg_temp, danger_level=danger_level)
|
||||||
|
|
||||||
|
db.session.add(sensordata)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return jsonify(), 201
|
||||||
|
|
||||||
|
@app.route("/graph")
|
||||||
|
def graph():
|
||||||
|
line_labels = ['']
|
||||||
|
|
||||||
|
line_values = SensorData.getAll()
|
||||||
|
line_values.sort(key=lambda x: x.timestamp)
|
||||||
|
line_values = line_values[-12:]
|
||||||
|
|
||||||
|
return render_template('graph.html', title='Average Temperature Graph', max=40, labels=line_labels, values=line_values)
|
6
flaskapp/templates/404.html
Normal file
6
flaskapp/templates/404.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block app_content %}
|
||||||
|
<h1>Not Found</h1>
|
||||||
|
<p><a href="{{url_for('index') }}">Back</a></p>
|
||||||
|
{% endblock %}
|
6
flaskapp/templates/500.html
Normal file
6
flaskapp/templates/500.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block app_content %}
|
||||||
|
<h1>500 Server Error</h1>
|
||||||
|
<p><a href="{{url_for('index') }}">Back</a></p>
|
||||||
|
{% endblock %}
|
34
flaskapp/templates/base.html
Normal file
34
flaskapp/templates/base.html
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{% extends 'bootstrap/base.html' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
{% if title %}{{ title }} - IoT Flask App{% else %}IoT Flask App{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block navbar %}
|
||||||
|
<nav class="navbar navbar-default">
|
||||||
|
<div class="container">
|
||||||
|
<div class="navbar-header">
|
||||||
|
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
|
||||||
|
<span class="sr-only">Toggle navigation</span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
</button>
|
||||||
|
<a class="navbar-brand" href="{{ url_for('index') }}">IoT Flask App</a>
|
||||||
|
</div>
|
||||||
|
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
||||||
|
<ul class="nav navbar-nav">
|
||||||
|
<li><a href="{{ url_for('index') }}">Home</a></li>
|
||||||
|
</ul>
|
||||||
|
<ul class="nav navbar-nav">
|
||||||
|
<li><a href="{{ url_for('graph') }}">Temp Graph</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
{% block content %}
|
||||||
|
{% endblock %}
|
||||||
|
</div>
|
71
flaskapp/templates/graph.html
Normal file
71
flaskapp/templates/graph.html
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>{{ title }}</title>
|
||||||
|
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js'></script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<center>
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
|
||||||
|
<canvas id="chart" width="600" height="400"></canvas>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
// bar chart data
|
||||||
|
var barData = {
|
||||||
|
labels : [
|
||||||
|
{% for item in values %}
|
||||||
|
"{{ item.timestamp }}",
|
||||||
|
{% endfor %}
|
||||||
|
],
|
||||||
|
|
||||||
|
datasets : [{
|
||||||
|
fillColor: "rgba(151,187,205,0.2)",
|
||||||
|
strokeColor: "rgba(151,187,205,1)",
|
||||||
|
pointColor: "rgba(151,187,205,1)",
|
||||||
|
pointStrokeColor: "#fff",
|
||||||
|
pointHighlightFill: "#fff",
|
||||||
|
pointHighlightStroke: "rgba(151,187,205,1)",
|
||||||
|
bezierCurve : false,
|
||||||
|
data : [
|
||||||
|
{% for item in values %}
|
||||||
|
{{ item.avg_temp }},
|
||||||
|
{% endfor %}]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
Chart.defaults.global.animationSteps = 50;
|
||||||
|
Chart.defaults.global.tooltipYPadding = 16;
|
||||||
|
Chart.defaults.global.tooltipCornerRadius = 0;
|
||||||
|
Chart.defaults.global.tooltipTitleFontStyle = "normal";
|
||||||
|
Chart.defaults.global.tooltipFillColor = "rgba(0,0,0,0.8)";
|
||||||
|
Chart.defaults.global.animationEasing = "easeOutBounce";
|
||||||
|
Chart.defaults.global.responsive = false;
|
||||||
|
Chart.defaults.global.scaleLineColor = "black";
|
||||||
|
Chart.defaults.global.scaleFontSize = 16;
|
||||||
|
|
||||||
|
// get bar chart canvas
|
||||||
|
var mychart = document.getElementById("chart").getContext("2d");
|
||||||
|
|
||||||
|
steps = 10
|
||||||
|
max = {{ max }}
|
||||||
|
// draw bar chart
|
||||||
|
var LineChartDemo = new Chart(mychart).Line(barData, {
|
||||||
|
scaleOverride: true,
|
||||||
|
scaleSteps: steps,
|
||||||
|
scaleStepWidth: Math.ceil(max / steps),
|
||||||
|
scaleStartValue: 0,
|
||||||
|
scaleShowVerticalLines: true,
|
||||||
|
scaleShowGridLines : true,
|
||||||
|
barShowStroke : true,
|
||||||
|
scaleShowLabels: true,
|
||||||
|
bezierCurve: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</center>
|
||||||
|
</body>
|
||||||
|
</html>
|
32
flaskapp/templates/index.html
Normal file
32
flaskapp/templates/index.html
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<h1>Sensor Data</h1>
|
||||||
|
|
||||||
|
<table class="table table-striped table-bordered table-sm">
|
||||||
|
<th>
|
||||||
|
<tr>
|
||||||
|
<td><b>Humidity</b></td>
|
||||||
|
<td><b>Average Temp</b></td>
|
||||||
|
<td><b>Barometer Pressure</b></td>
|
||||||
|
<td><b>Light</b></td>
|
||||||
|
<td><b>Timestamp</b></td>
|
||||||
|
</tr>
|
||||||
|
</th>
|
||||||
|
{% for item in data %}
|
||||||
|
{% if item.danger_level == 1 %}
|
||||||
|
<tr style="color: red;">
|
||||||
|
{% else %}
|
||||||
|
<tr>
|
||||||
|
{% endif %}
|
||||||
|
<td>{{ item.humidity }}%</td>
|
||||||
|
<td>{{ item.avg_temp }}°C</td>
|
||||||
|
<td>{{ item.baro_pressure }} hPa</td>
|
||||||
|
<td>{{ item.light }} lx</td>
|
||||||
|
<td>{{ item.timestamp }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
|
||||||
|
{% endblock %}
|
1
migrations/README
Normal file
1
migrations/README
Normal file
@ -0,0 +1 @@
|
|||||||
|
Generic single-database configuration.
|
45
migrations/alembic.ini
Normal file
45
migrations/alembic.ini
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# A generic, single database configuration.
|
||||||
|
|
||||||
|
[alembic]
|
||||||
|
# template used to generate migration files
|
||||||
|
# file_template = %%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
# set to 'true' to run the environment during
|
||||||
|
# the 'revision' command, regardless of autogenerate
|
||||||
|
# revision_environment = false
|
||||||
|
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
96
migrations/env.py
Normal file
96
migrations/env.py
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
|
# this is the Alembic Config object, which provides
|
||||||
|
# access to the values within the .ini file in use.
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
# Interpret the config file for Python logging.
|
||||||
|
# This line sets up loggers basically.
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
logger = logging.getLogger('alembic.env')
|
||||||
|
|
||||||
|
# add your model's MetaData object here
|
||||||
|
# for 'autogenerate' support
|
||||||
|
# from myapp import mymodel
|
||||||
|
# target_metadata = mymodel.Base.metadata
|
||||||
|
from flask import current_app
|
||||||
|
config.set_main_option(
|
||||||
|
'sqlalchemy.url', current_app.config.get(
|
||||||
|
'SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
|
||||||
|
target_metadata = current_app.extensions['migrate'].db.metadata
|
||||||
|
|
||||||
|
# other values from the config, defined by the needs of env.py,
|
||||||
|
# can be acquired:
|
||||||
|
# my_important_option = config.get_main_option("my_important_option")
|
||||||
|
# ... etc.
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline():
|
||||||
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
|
This configures the context with just a URL
|
||||||
|
and not an Engine, though an Engine is acceptable
|
||||||
|
here as well. By skipping the Engine creation
|
||||||
|
we don't even need a DBAPI to be available.
|
||||||
|
|
||||||
|
Calls to context.execute() here emit the given string to the
|
||||||
|
script output.
|
||||||
|
|
||||||
|
"""
|
||||||
|
url = config.get_main_option("sqlalchemy.url")
|
||||||
|
context.configure(
|
||||||
|
url=url, target_metadata=target_metadata, literal_binds=True
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online():
|
||||||
|
"""Run migrations in 'online' mode.
|
||||||
|
|
||||||
|
In this scenario we need to create an Engine
|
||||||
|
and associate a connection with the context.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# this callback is used to prevent an auto-migration from being generated
|
||||||
|
# when there are no changes to the schema
|
||||||
|
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||||
|
def process_revision_directives(context, revision, directives):
|
||||||
|
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||||
|
script = directives[0]
|
||||||
|
if script.upgrade_ops.is_empty():
|
||||||
|
directives[:] = []
|
||||||
|
logger.info('No changes in schema detected.')
|
||||||
|
|
||||||
|
connectable = engine_from_config(
|
||||||
|
config.get_section(config.config_ini_section),
|
||||||
|
prefix='sqlalchemy.',
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
process_revision_directives=process_revision_directives,
|
||||||
|
**current_app.extensions['migrate'].configure_args
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
24
migrations/script.py.mako
Normal file
24
migrations/script.py.mako
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = ${repr(up_revision)}
|
||||||
|
down_revision = ${repr(down_revision)}
|
||||||
|
branch_labels = ${repr(branch_labels)}
|
||||||
|
depends_on = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
${downgrades if downgrades else "pass"}
|
36
migrations/versions/40562f975781_updated_db.py
Normal file
36
migrations/versions/40562f975781_updated_db.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""Updated DB
|
||||||
|
|
||||||
|
Revision ID: 40562f975781
|
||||||
|
Revises:
|
||||||
|
Create Date: 2020-05-18 18:52:41.817526
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '40562f975781'
|
||||||
|
down_revision = None
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('sensor_data',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('humidity', sa.Float(), nullable=True),
|
||||||
|
sa.Column('avg_temp', sa.Float(), nullable=True),
|
||||||
|
sa.Column('baro_pressure', sa.Float(), nullable=True),
|
||||||
|
sa.Column('light', sa.Float(), nullable=True),
|
||||||
|
sa.Column('timestamp', sa.DateTime(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_table('sensor_data')
|
||||||
|
# ### end Alembic commands ###
|
28
migrations/versions/b9900a0f475d_tweaks.py
Normal file
28
migrations/versions/b9900a0f475d_tweaks.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Tweaks
|
||||||
|
|
||||||
|
Revision ID: b9900a0f475d
|
||||||
|
Revises: 40562f975781
|
||||||
|
Create Date: 2020-05-22 20:02:25.542693
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'b9900a0f475d'
|
||||||
|
down_revision = '40562f975781'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('sensor_data', sa.Column('danger_level', sa.Integer(), nullable=True))
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_column('sensor_data', 'danger_level')
|
||||||
|
# ### end Alembic commands ###
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
Flask_Migrate==2.5.2
|
||||||
|
Flask_SQLAlchemy==2.4.1
|
||||||
|
SQLAlchemy==1.3.13
|
||||||
|
Flask==1.1.1
|
||||||
|
requests==2.22.0
|
||||||
|
alembic==1.3.3
|
||||||
|
bluepy==1.3.0
|
58
sensor/thermometer.py
Normal file
58
sensor/thermometer.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
from bluepy.btle import BTLEException
|
||||||
|
from bluepy.sensortag import SensorTag
|
||||||
|
import time
|
||||||
|
import requests
|
||||||
|
|
||||||
|
SENSOR_ADDRESS = '54:6C:0E:53:12:D5'
|
||||||
|
API = "http://192.168.0.21:5000"
|
||||||
|
|
||||||
|
INTERVAL = 5
|
||||||
|
|
||||||
|
tag = SensorTag(SENSOR_ADDRESS)
|
||||||
|
tag.connect(tag.deviceAddr, tag.addrType)
|
||||||
|
|
||||||
|
print("Connected to sensor")
|
||||||
|
|
||||||
|
def enable_sensors(tag):
|
||||||
|
|
||||||
|
tag.barometer.enable()
|
||||||
|
tag.IRtemperature.enable()
|
||||||
|
tag.humidity.enable()
|
||||||
|
tag.lightmeter.enable()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
def disable_sensors(tag):
|
||||||
|
tag.barometer.disable()
|
||||||
|
tag.IRtemperature.disable()
|
||||||
|
tag.humidity.disable()
|
||||||
|
tag.lightmeter.disable()
|
||||||
|
|
||||||
|
def get_readings(tag):
|
||||||
|
try:
|
||||||
|
enable_sensors(tag)
|
||||||
|
readings = {}
|
||||||
|
readings["baro_temp"],readings["pressure"]=tag.barometer.read()
|
||||||
|
readings["light"]=tag.lightmeter.read()
|
||||||
|
readings["humidity_temp"],readings["humidity"] = tag.humidity.read()
|
||||||
|
disable_sensors(tag)
|
||||||
|
return readings
|
||||||
|
except BTLEException as e:
|
||||||
|
print(e)
|
||||||
|
return {}
|
||||||
|
|
||||||
|
while(True):
|
||||||
|
time.sleep(INTERVAL)
|
||||||
|
readings = get_readings(tag)
|
||||||
|
|
||||||
|
# POST
|
||||||
|
data = {
|
||||||
|
'baro_temp':readings["baro_temp"],
|
||||||
|
'baro_pressure':readings["pressure"],
|
||||||
|
'light':readings["light"],
|
||||||
|
'humidity_temp':readings['humidity_temp'],
|
||||||
|
'humidity':readings['humidity']
|
||||||
|
}
|
||||||
|
|
||||||
|
r = requests.post(url=API, json=data)
|
||||||
|
|
||||||
|
print(r.status_code)
|
Loading…
Reference in New Issue
Block a user