Upload project.

This commit is contained in:
StevenJW
2020-06-09 21:34:42 +01:00
parent b28b77a1ed
commit b873632828
22 changed files with 743 additions and 0 deletions

14
flaskapp/__init__.py Normal file
View 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
View 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
View 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
View 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
View 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)

View 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 %}

View 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 %}

View 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>

View 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>

View 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 %}