adaugat lab4 si lab5
Signed-off-by: hellisabove <robertnedela15@gmail.com>
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
# Imported symbols here please!
|
||||
from flask import Flask, request, render_template, redirect, session
|
||||
from .pages.index import index_pages
|
||||
from .pages.auth import auth_pages
|
||||
from .pages.iot_api import iot_api
|
||||
|
||||
# Initialize the Flask application
|
||||
# Note: static folder means all files in there will be automatically offered over HTTP
|
||||
app = Flask(__name__, static_folder="../public",
|
||||
template_folder="../templates")
|
||||
app.secret_key = "gxnMaYjinQ27DeBwgKsDyuDQO"
|
||||
app.register_blueprint(index_pages)
|
||||
app.register_blueprint(auth_pages)
|
||||
app.register_blueprint(iot_api)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
""" Data model for user accounts """
|
||||
|
||||
ACCOUNTS = {
|
||||
"admin@example.com": "h4ckm3",
|
||||
}
|
||||
|
||||
|
||||
def check_login(email, password):
|
||||
""" Checks whether the specified credentials are valid """
|
||||
if ACCOUNTS.get(email) == password:
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
Data model for IoT sensors.
|
||||
"""
|
||||
import os, json
|
||||
|
||||
iot_data = {
|
||||
"ext_temp": 0,
|
||||
"int_temp": 0,
|
||||
"ext_hum": 0,
|
||||
"int_hum": 0,
|
||||
"rain": 0
|
||||
}
|
||||
DATA_FILE = "/home/nedi/iap1-labs/lab4/iot_data.json"
|
||||
|
||||
def load_data(path):
|
||||
if os.path.exists(path):
|
||||
with open(path, "r") as f:
|
||||
stored = json.load(f)
|
||||
for key in set(iot_data.keys()):
|
||||
if key in stored:
|
||||
iot_data[key] = stored[key]
|
||||
|
||||
load_data("/home/nedi/iap1-labs/lab4/iot_data.json")
|
||||
|
||||
def save_data(path):
|
||||
with open(path, "w") as f:
|
||||
json.dump(iot_data, f, indent=2)
|
||||
|
||||
def fetch_iot_data():
|
||||
return iot_data
|
||||
|
||||
|
||||
def set_iot_data(data):
|
||||
# TODO: update the dictionary with the new data
|
||||
iot_data.update(data)
|
||||
pass
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
from flask import session, Blueprint, request, redirect, render_template
|
||||
from ..model.account import check_login
|
||||
|
||||
|
||||
auth_pages = Blueprint("auth", __name__)
|
||||
|
||||
@auth_pages.route("/login", methods=["GET", "POST"])
|
||||
def login_page():
|
||||
error = ""
|
||||
if "email" in request.form:
|
||||
email = request.form["email"]
|
||||
password = request.form["password"]
|
||||
if check_login(email, password):
|
||||
session["email"] = email
|
||||
session["auth"] = True
|
||||
return redirect("/", code=302)
|
||||
else:
|
||||
error = "Invalid username or password"
|
||||
return render_template("login.html", error=error)
|
||||
|
||||
@auth_pages.route("/logout")
|
||||
def logout():
|
||||
session["auth"] = False
|
||||
return redirect("/")
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
from flask import session, Blueprint, request, redirect, render_template
|
||||
from ..model.sensors import fetch_iot_data
|
||||
|
||||
|
||||
index_pages = Blueprint("index", __name__)
|
||||
|
||||
@index_pages.route("/")
|
||||
def index_page():
|
||||
if not session.get("auth", False):
|
||||
return redirect("/login", code=302)
|
||||
sensors = fetch_iot_data()
|
||||
return render_template("index.html", sensors=sensors)
|
||||
|
||||
@index_pages.route("/contact")
|
||||
def contact_page():
|
||||
return render_template("contact.html")
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from ..model.sensors import iot_data, set_iot_data, save_data
|
||||
import os, json
|
||||
|
||||
# TODO: create a Blueprint + routes for 'api/iot' endpoint
|
||||
iot_api = Blueprint("iot_api", __name__)
|
||||
|
||||
DATA_FILE = "/home/nedi/iap1-labs/lab4/iot_data.json"
|
||||
|
||||
@iot_api.route("/api/iot", methods=["GET"])
|
||||
def get_sensor_data():
|
||||
return jsonify(iot_data)
|
||||
|
||||
@iot_api.route("/api/iot", methods=["POST"])
|
||||
def update_sensor_data():
|
||||
payload = request.get_json(silent=True)
|
||||
|
||||
set_iot_data(payload)
|
||||
|
||||
save_data(DATA_FILE)
|
||||
return jsonify(iot_data)
|
||||
|
||||
@iot_api.route("/api/iot", methods=["DELETE"])
|
||||
def reset_sensor_data():
|
||||
for key in iot_data:
|
||||
iot_data[key] = 0
|
||||
save_data(DATA_FILE)
|
||||
return jsonify(iot_data)
|
||||
Reference in New Issue
Block a user