add square html
This commit is contained in:
parent
45d952368b
commit
76194119f4
138
app.py
138
app.py
|
@ -6,12 +6,17 @@ from time import sleep
|
|||
import time
|
||||
import requests
|
||||
import json
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# global last_indoor_data
|
||||
global indoor_server_ip
|
||||
global indoor_server_password
|
||||
global outdoor_api_url
|
||||
global outdoor_data
|
||||
global location
|
||||
global mqtt_enabled
|
||||
global mqtt_client
|
||||
global base_topic
|
||||
|
||||
# Load the config file "config.json"
|
||||
config = json.loads(open("config.json", "r").read())
|
||||
|
@ -19,93 +24,44 @@ indoor_server_ip = config["indoor_server_ip"]
|
|||
indoor_server_password = config["indoor_server_password"]
|
||||
outdoor_api_url = config["outdoor_api_url"]
|
||||
location = config["location"]
|
||||
mqtt_enabled = config["mqtt_enabled"]
|
||||
|
||||
# Assume that the indoor unit is offline
|
||||
# The get_indoor_data() function will update this variable
|
||||
last_indoor_data = {
|
||||
"offline": True
|
||||
}
|
||||
# Initialize Variables
|
||||
outdoor_data = {}
|
||||
|
||||
# def get_indoor_data() -> list:
|
||||
# global indoor_server_ip
|
||||
# global indoor_server_password
|
||||
# # SMB server details
|
||||
# server_name = indoor_server_ip
|
||||
# share_name = "airvisual"
|
||||
# username = "airvisual"
|
||||
# password = indoor_server_password
|
||||
def mqtt_reconnect():
|
||||
global mqtt_client
|
||||
global mqtt_server_ip
|
||||
global mqtt_server_port
|
||||
global mqtt_username
|
||||
global mqtt_password
|
||||
global mqtt_client_id
|
||||
global base_topic
|
||||
if not mqtt_client.is_connected():
|
||||
# Connect to the MQTT server
|
||||
mqtt_client.connect(mqtt_server_ip, mqtt_server_port)
|
||||
# Start the MQTT client
|
||||
mqtt_client.loop_start()
|
||||
if mqtt_client.is_connected():
|
||||
print("Connected to MQTT server!")
|
||||
else:
|
||||
print("Failed to connect to MQTT server!")
|
||||
return False
|
||||
return True
|
||||
|
||||
# # File details, The file is a text file with name:
|
||||
# # <year><month>_AirVisual_values.txt
|
||||
# # Get the prefix of the file name
|
||||
# prefix = time.strftime("%Y%m", time.localtime())
|
||||
# file_path = prefix + "_AirVisual_values.txt"
|
||||
if mqtt_enabled:
|
||||
mqtt_server_ip = config["mqtt_server_ip"]
|
||||
mqtt_server_port = config["mqtt_server_port"]
|
||||
mqtt_username = config["mqtt_username"]
|
||||
mqtt_password = config["mqtt_password"]
|
||||
mqtt_client_id = config["mqtt_client_id"]
|
||||
base_topic = config["base_topic"]
|
||||
|
||||
# # Connect to the SMB server
|
||||
# conn = SMBConnection(username, password, "", "")
|
||||
# conn.connect(server_name, 139)
|
||||
|
||||
# # Read the file contents
|
||||
# file_obj = open(file_path, "wb")
|
||||
# conn.retrieveFile(share_name, file_path, file_obj)
|
||||
# conn.close()
|
||||
|
||||
# # Open the local cached file
|
||||
# file_obj = open(file_path, "r")
|
||||
|
||||
# # The first line of the file contains the header
|
||||
# # The header contains the column names separated by a semicolon (;)
|
||||
# # The rest of the file contains the data separated by a semicolon (;)
|
||||
# # Extract the column names and the data from the file
|
||||
# file_obj.seek(0)
|
||||
# header = file_obj.readline().strip().split(";")
|
||||
# data = file_obj.readlines()
|
||||
# # Split all the data into a list of lists
|
||||
# data = [row.strip().split(";") for row in data]
|
||||
# file_obj.close()
|
||||
|
||||
|
||||
# # Remap the header names
|
||||
# headers_map = {
|
||||
# "PM2_5(ug/m3)": "pm25",
|
||||
# "PM10(ug/m3)": "pm10",
|
||||
# "PM1(ug/m3)": "pm1",
|
||||
# "CO2(ppm)": "co2",
|
||||
# "AQI(US)": "aqi",
|
||||
# "Temperature(C)": "temperature",
|
||||
# "Humidity(%RH)": "humidity",
|
||||
# "Timestamp": "time"
|
||||
# }
|
||||
|
||||
# # Remove rows with header names that are not in the header map
|
||||
# # First, get the indices of the header names that are in the header map
|
||||
# headers_indices = []
|
||||
# for index, name in enumerate(header):
|
||||
# if name in headers_map:
|
||||
# headers_indices.append(index)
|
||||
|
||||
# # Construct the new header with the header names that are in the header map
|
||||
# header = [header[index] for index in headers_indices]
|
||||
|
||||
# # Construct the new data with only the columns indicated by the header indices
|
||||
# data = [[row[index] for index in headers_indices] for row in data]
|
||||
|
||||
# # Remap the header names
|
||||
# headers = [headers_map[name] for name in header]
|
||||
|
||||
# # Convert unix timestamp to human readable time
|
||||
# for row in data:
|
||||
# row[headers.index("time")] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(row[headers.index("time")])))
|
||||
|
||||
# # Create a list of dictionaries representing the data
|
||||
# # Each dictionary represents a row of data
|
||||
# data_list = []
|
||||
# for row in data:
|
||||
# data_dict = {}
|
||||
# for header in headers:
|
||||
# data_dict[header] = row[headers.index(header)]
|
||||
# data_list.append(data_dict)
|
||||
# return data_list
|
||||
# Create an MQTT client
|
||||
mqtt_client = mqtt.Client(client_id=mqtt_client_id)
|
||||
# Set the username and password
|
||||
mqtt_client.username_pw_set(mqtt_username, mqtt_password)
|
||||
mqtt_reconnect()
|
||||
|
||||
def get_outdoor_data_current() -> dict:
|
||||
# Fetch the data from the AirVisual API
|
||||
|
@ -142,6 +98,7 @@ def get_outdoor_data_current() -> dict:
|
|||
response = requests.get(url)
|
||||
try:
|
||||
print("Fetching data from API!" )
|
||||
print(response)
|
||||
data = response.json()
|
||||
# Create a dictionary of the data
|
||||
data = {
|
||||
|
@ -167,7 +124,8 @@ def get_outdoor_data_current() -> dict:
|
|||
# Remove the last_updated key
|
||||
# TODO store the data in a database
|
||||
return data
|
||||
except:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
# Oops, we got rate limited
|
||||
# Return the cached data
|
||||
print("Rate limited!")
|
||||
|
@ -196,7 +154,7 @@ app = Flask(__name__)
|
|||
# Refresh the indoor data every 30 seconds
|
||||
def refresh_data():
|
||||
while True:
|
||||
|
||||
global outdoor_data
|
||||
# print("Fetching indoor data!")
|
||||
# indoor_data = get_indoor_data()
|
||||
# global last_indoor_data
|
||||
|
@ -206,7 +164,15 @@ def refresh_data():
|
|||
# Fetch the outdoor data
|
||||
print("Fetching outdoor data!")
|
||||
outdoor_data = get_outdoor_data_current()
|
||||
|
||||
# Reconnect the MQTT client
|
||||
mqtt_reconnect()
|
||||
mqtt_client.publish(base_topic+"/temperature", outdoor_data["temperature"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/humidity", outdoor_data["humidity"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/pressure", outdoor_data["pressure"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/pm25", outdoor_data["pm25"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/pm10", outdoor_data["pm10"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/pm1", outdoor_data["pm1"], retain=True)
|
||||
mqtt_client.publish(base_topic+"/aqi", outdoor_data["aqi"], retain=True)
|
||||
sleep(30)
|
||||
|
||||
# Start the thread to refresh the data
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"listeners": {
|
||||
"*:80": {
|
||||
"*:8080": {
|
||||
"pass": "routes"
|
||||
}
|
||||
},
|
||||
|
@ -40,7 +40,7 @@
|
|||
],
|
||||
"applications": {
|
||||
"iqair-apiserver-satitm": {
|
||||
"type": "python 3.10",
|
||||
"type": "python 3.8",
|
||||
"path": "/app/iqair-apiserver",
|
||||
"home": "/app/venv",
|
||||
"module": "app",
|
||||
|
@ -48,7 +48,7 @@
|
|||
"working_directory": "/app/env-satitm"
|
||||
},
|
||||
"iqair-apiserver-satite": {
|
||||
"type": "python 3.10",
|
||||
"type": "python 3.8",
|
||||
"path": "/app/iqair-apiserver",
|
||||
"home": "/app/venv",
|
||||
"module": "app",
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
.top_element {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 2fr 2fr;
|
||||
background-color: #97FF55;
|
||||
background-color: #767676;
|
||||
height: 190px;
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@
|
|||
justify-content: center;
|
||||
justify-items: center;
|
||||
align-items: center;
|
||||
background-color: #7bdb3f;
|
||||
background-color: #474747;
|
||||
|
||||
font-size: 100px;
|
||||
color: #fff;
|
||||
|
@ -161,8 +161,8 @@
|
|||
<p class="title_text" id="title">Loading . . .</p>
|
||||
<div class="top_element" id="top_element">
|
||||
<div class="health_icon_frame" id="health_icon_frame">
|
||||
<span class="iconify" data-icon="mdi-heart" id="health_icon"></span>
|
||||
<p class="health_text" , id="health_text">Healthy</p>
|
||||
<span class="iconify" data-icon="mdi-close" id="health_icon"></span>
|
||||
<p class="health_text" , id="health_text">Loading</p>
|
||||
</div>
|
||||
<div class="top_frame_label_block">
|
||||
<p class="top_frame_value_text" , id="outdoor-aqi">N/A</p>
|
||||
|
@ -173,7 +173,7 @@
|
|||
<div class="top_frame_label_block">
|
||||
<p class="top_frame_value_text" , id="outdoor-pm25">N/A</p>
|
||||
<div class="top_frame_element_frame">
|
||||
<p class="top_frame_label_text">PM2.5 (μg/m3)</p>
|
||||
<p class="top_frame_label_text">PM2.5 (μg/m³)</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -216,15 +216,21 @@
|
|||
console.log(data);
|
||||
|
||||
// Set Title Location String
|
||||
$("#title").text(data.location);
|
||||
// The location is the location key in the JSON response
|
||||
// It must also be appended with the timestamp
|
||||
var update_time = data.time_outdoor
|
||||
// Data is in format YYYY-MM-DD hh:mm:ss
|
||||
// We only want hh:mm
|
||||
var update_time = update_time.substring(11, 16);
|
||||
$("#title").text(data.location+" @ "+update_time);
|
||||
|
||||
// Update outdoor AQI and PM2.5 values
|
||||
$("#outdoor-aqi").text(data.aqi_outdoor);
|
||||
$("#outdoor-pm25").text(data.pm25_outdoor);
|
||||
$("#outdoor-pm25").text(data.pm25_outdoor.toFixed(1));
|
||||
|
||||
// Update outdoor temperature and humidity values
|
||||
$("#outdoor-temp").text("Temperature: " + data.temperature_outdoor + "°C");
|
||||
$("#outdoor-humid").text("Humidity: " + data.humidity_outdoor + "%");
|
||||
$("#outdoor-temp").text("Temperature: " + data.temperature_outdoor.toFixed(1) + "°C");
|
||||
$("#outdoor-humid").text("Humidity: " + data.humidity_outdoor.toFixed(1) + "%");
|
||||
|
||||
// Is outdoor AQI healthy?
|
||||
// If AQI is less than 50, it is healthy
|
||||
|
|
|
@ -53,8 +53,12 @@
|
|||
<h1>Air Quality Monitoring System</h1>
|
||||
<h2>Chulalongkorn University Demonstation School</h2>
|
||||
<p>Where are you?</p>
|
||||
<a class="button" href="/card.html?location=satite">Primary School</a>
|
||||
<a class="button" href="/card.html?location=satitm">Secondary School</a>
|
||||
<h3>Primary School</h3>
|
||||
<a class="button" href="/card.html?location=satite">Card</a>
|
||||
<a class="button" href="/square.html?location=satite">Compact</a>
|
||||
<h3>Secondary School</h3>
|
||||
<a class="button" href="/card.html?location=satitm">Card</a>
|
||||
<a class="button" href="/square.html?location=satitm">Compact</a>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
|
|
|
@ -0,0 +1,176 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Air Quality Dashboard</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
|
||||
<script src="https://kit.fontawesome.com/34a5e0cdf4.js" crossorigin="anonymous"></script>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
|
||||
<script src="//code.iconify.design/1/1.0.6/iconify.min.js"></script>
|
||||
<style>
|
||||
body {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
font-family: "Roboto", sans-serif;
|
||||
}
|
||||
|
||||
.card_frame {
|
||||
display: grid;
|
||||
width: 175px;
|
||||
height: 175px;
|
||||
border-radius: 30px;
|
||||
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.health_icon_frame {
|
||||
padding-top: 3px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
justify-items: center;
|
||||
align-items: center;
|
||||
background-color: #474747;
|
||||
font-size: 70px;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.health_text {
|
||||
font-size: 20px;
|
||||
font-weight: 500;
|
||||
color: #ffffff;
|
||||
margin-left: 10px;
|
||||
padding-top: 0;
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.aqi_block {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
justify-items: center;
|
||||
align-items: center;
|
||||
background-color: #ffffff;
|
||||
font-size: 70px;
|
||||
color: #000000;
|
||||
height: 30px;
|
||||
width: 130px;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 10px;
|
||||
|
||||
}
|
||||
.aqi_text {
|
||||
font-size: 20px;
|
||||
font-weight: 500;
|
||||
color: #000000;
|
||||
margin-left: 10px;
|
||||
padding-top: 0;
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<div class="card_frame">
|
||||
<div class="health_icon_frame" id="health_icon_frame">
|
||||
<div class="aqi_block" id="top_element">
|
||||
<p class="aqi_text" id="outdoor-aqi">0</p>
|
||||
</div>
|
||||
<span class="iconify" data-icon="mdi-heart" id="health_icon"></span>
|
||||
<p class="health_text" , id="health_text">Healthy</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
// Fetch data from API
|
||||
function fetchData() {
|
||||
var current_path = window.location.pathname;
|
||||
// If current path end with index.html, remove it
|
||||
|
||||
// The API endpoint will have parameters like this:
|
||||
// /card.html?location="satitm/satite"
|
||||
// We will use that location parameter to fetch data from the correct API endpoint
|
||||
api_prefix = "";
|
||||
// Get the location parameter from the URL
|
||||
var urlParams = new URLSearchParams(window.location.search);
|
||||
var location = urlParams.get('location');
|
||||
if (location != null) {
|
||||
api_prefix = location + "/";
|
||||
// Get the root domain of the current URL
|
||||
var root_domain = window.location.protocol + "//" + window.location.host + "/";
|
||||
// append the API prefix to the root domain
|
||||
var api_url = root_domain + api_prefix + "get_data";
|
||||
|
||||
$.get(api_url, function (data) {
|
||||
console.log(data);
|
||||
|
||||
// Set Title Location String
|
||||
// The location is the location key in the JSON response
|
||||
// It must also be appended with the timestamp
|
||||
var update_time = data.time_outdoor
|
||||
// Data is in format YYYY-MM-DD hh:mm:ss
|
||||
// We only want hh:mm
|
||||
var update_time = update_time.substring(11, 16);
|
||||
$("#title").text(data.location+" @ "+update_time);
|
||||
|
||||
// Update outdoor AQI and PM2.5 values
|
||||
$("#outdoor-aqi").text("AQI : "+data.aqi_outdoor);
|
||||
|
||||
// Is outdoor AQI healthy?
|
||||
// If AQI is less than 50, it is healthy
|
||||
// If AQI is between 50 and 100, it is unhealthy
|
||||
// If AQI is more than 100, it is very unhealthy
|
||||
var oldIcon = document.getElementById("health_icon");
|
||||
oldIcon.parentNode.removeChild(oldIcon);
|
||||
var newIcon = document.createElement("span");
|
||||
newIcon.setAttribute("class", "iconify");
|
||||
|
||||
newIcon.setAttribute("data-inline", "false");
|
||||
newIcon.id = "health_icon";
|
||||
if (data.aqi_outdoor < 50) {
|
||||
newIcon.setAttribute("data-icon", "mdi-heart");
|
||||
Iconify.scanDOM();
|
||||
$("#health_text").text("Healthy");
|
||||
$("#health_icon_frame").css("background-color", "#7bdb3f");
|
||||
} else if (data.aqi_outdoor >= 50 && data.aqi_outdoor < 100) {
|
||||
newIcon.setAttribute("data-icon", "mdi-smog");
|
||||
Iconify.scanDOM();
|
||||
$("#health_text").text("Moderate");
|
||||
$("#health_icon_frame").css("background-color", "#f9c300");
|
||||
} else {
|
||||
newIcon.setAttribute("data-icon", "mdi-skull-crossbones");
|
||||
Iconify.scanDOM();
|
||||
$("#health_text").text("Unhealthy");
|
||||
$("#health_icon_frame").css("background-color", "#A54444");
|
||||
}
|
||||
|
||||
document.getElementById("health_icon_frame").appendChild(newIcon);
|
||||
Iconify.scanDOM();
|
||||
});
|
||||
}
|
||||
else {
|
||||
$("#title").text("Please specify location in URL (?location=...)");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fetchData(); // Fetch data immediately when the page loads
|
||||
setInterval(fetchData, 10000); // Fetch data every 10 seconds
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue