Disable Indoor Unit

This commit is contained in:
root 2024-01-08 13:47:22 +00:00
parent 2d84e9f6d1
commit 45d952368b
7 changed files with 371 additions and 85 deletions

167
app.py
View File

@ -7,16 +7,18 @@ import time
import requests
import json
global last_indoor_data
# global last_indoor_data
global indoor_server_ip
global indoor_server_password
global outdoor_api_url
global location
# Load the config file "config.json"
config = json.loads(open("config.json", "r").read())
indoor_server_ip = config["indoor_server_ip"]
indoor_server_password = config["indoor_server_password"]
outdoor_api_url = config["outdoor_api_url"]
location = config["location"]
# Assume that the indoor unit is offline
# The get_indoor_data() function will update this variable
@ -24,86 +26,86 @@ last_indoor_data = {
"offline": True
}
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 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
# 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"
# # 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"
# Connect to the SMB server
conn = SMBConnection(username, password, "", "")
conn.connect(server_name, 139)
# # 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()
# # 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")
# # 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()
# # 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"
}
# # 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)
# # 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 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]
# # 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]
# # 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")])))
# # 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 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
def get_outdoor_data_current() -> dict:
# Fetch the data from the AirVisual API
@ -163,6 +165,7 @@ def get_outdoor_data_current() -> dict:
data["last_updated"] = int(time.time())
open("outdoor_data_cache.txt", "w").write(json.dumps(data))
# Remove the last_updated key
# TODO store the data in a database
return data
except:
# Oops, we got rate limited
@ -193,11 +196,17 @@ app = Flask(__name__)
# Refresh the indoor data every 30 seconds
def refresh_data():
while True:
print("Fetching indoor data!")
indoor_data = get_indoor_data()
global last_indoor_data
# last_indoor_data the last dictionary in the list
last_indoor_data = indoor_data[-1]
# print("Fetching indoor data!")
# indoor_data = get_indoor_data()
# global last_indoor_data
# # last_indoor_data the last dictionary in the list
# last_indoor_data = indoor_data[-1]
# Fetch the outdoor data
print("Fetching outdoor data!")
outdoor_data = get_outdoor_data_current()
sleep(30)
# Start the thread to refresh the data
@ -206,10 +215,14 @@ Thread(target=refresh_data).start()
# Return All Data in the current month
@app.route("/get_data", methods=["GET"])
def get_data_route():
global last_indoor_data
indoor_data = last_indoor_data
global location
# global last_indoor_data
# indoor_data = last_indoor_data
# Indoor data fetch is disabled
indoor_data = {}
outdoor_data = get_outdoor_data_current()
merged_data = merge_data(indoor_data, outdoor_data)
merged_data["location"] = location
return jsonify(merged_data)
app.run("0.0.0.0", 5000)

1
apply_config.sh Executable file
View File

@ -0,0 +1 @@
curl -X PUT --data-binary @nginx.json --unix-socket /var/run/control.unit.sock http://localhost/config

277
static/card.html Normal file
View File

@ -0,0 +1,277 @@
<!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: 750px;
height: 260px;
border-radius: 30px;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1);
}
.title_frame {
width: 750px;
height: 50px;
background-color: #ffffff;
border-top-right-radius: 30px;
border-top-left-radius: 30px;
}
.title_text {
font-size: 20px;
padding-top: 5px;
padding-bottom: 5px;
text-align: center;
color: rgb(57, 57, 57);
font-family: "Roboto", sans-serif;
align-self: center;
margin: 0;
}
.top_element {
display: grid;
grid-template-columns: 1fr 2fr 2fr;
background-color: #97FF55;
height: 190px;
}
.bottom_element {
display: grid;
grid-template-columns: 1fr 1fr;
background-color: #ffffff;
border-bottom-right-radius: 30px;
border-bottom-left-radius: 30px;
height: 40px;
}
.buttom_left_frame {
display: flex;
grid-template-columns: 1fr 1fr;
justify-content: center;
justify-items: center;
align-items: center;
background-color: #ffffff;
border-bottom-left-radius: 30px;
height: 40px;
border-right: 1px solid #828282;
}
.buttom_right_frame {
display: flex;
grid-template-columns: 1fr 1fr;
justify-content: center;
justify-items: center;
align-items: center;
background-color: #ffffff;
border-bottom-right-radius: 30px;
height: 40px;
border-left: 1px solid #828282;
}
.buttom_text {
font-size: 20px;
font-weight: 500;
color: #000000;
margin-left: 10px;
font-family: "Roboto", sans-serif;
}
.health_icon_frame {
padding-top: 10px;
display: flex;
flex-direction: column;
justify-content: center;
justify-items: center;
align-items: center;
background-color: #7bdb3f;
font-size: 100px;
color: #fff;
}
.health_text {
font-size: 20px;
font-weight: 500;
color: #ffffff;
margin-left: 10px;
}
.top_frame_element_frame {
display: flex;
;
flex-direction: column;
justify-content: center;
justify-items: center;
align-items: center;
background-color: #ffffff;
border-radius: 30px;
font-size: 100px;
margin-bottom: 7px;
margin-left: 7px;
width: 270px;
color: #939393;
align-self: flex-end;
}
.top_frame_value_text {
font-weight: 400;
color: #7a7a7a;
margin-left: 0px;
text-align: center;
font-size: 110px;
margin-top: 7px;
margin-bottom: 7px;
}
.top_frame_label_text {
font-size: 20px;
font-weight: 400;
color: #000000;
margin-left: 10px;
margin-top: 7px;
margin-bottom: 7px;
}
</style>
</head>
<div class="card_frame">
<div class="title_frame">
<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>
</div>
<div class="top_frame_label_block">
<p class="top_frame_value_text" , id="outdoor-aqi">N/A</p>
<div class="top_frame_element_frame">
<p class="top_frame_label_text">Air Quality Index (US)</p>
</div>
</div>
<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>
</div>
</div>
</div>
<div class="bottom_element">
<div class="buttom_left_frame">
<i class="fa fa-thermometer-half" aria-hidden="true"></i>
<p class="buttom_text" id="outdoor-temp">Temperature: N/A</p>
</div>
<div class="buttom_right_frame">
<i class="fa fa-tint" aria-hidden="true"></i>
<p class="buttom_text" id="outdoor-humid">Humidity: N/A</p>
</div>
</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
$("#title").text(data.location);
// Update outdoor AQI and PM2.5 values
$("#outdoor-aqi").text(data.aqi_outdoor);
$("#outdoor-pm25").text(data.pm25_outdoor);
// Update outdoor temperature and humidity values
$("#outdoor-temp").text("Temperature: " + data.temperature_outdoor + "°C");
$("#outdoor-humid").text("Humidity: " + data.humidity_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");
$("#top_element").css("background-color", "#97FF55");
$("#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");
$("#top_element").css("background-color", "#FFD300");
$("#health_icon_frame").css("background-color", "#f9c300");
} else {
newIcon.setAttribute("data-icon", "mdi-skull-crossbones");
Iconify.scanDOM();
$("#health_text").text("Unhealthy");
$("#top_element").css("background-color", "#FF6969");
$("#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>

View File

@ -53,8 +53,8 @@
<h1>Air Quality Monitoring System</h1>
<h2>Chulalongkorn University Demonstation School</h2>
<p>Where are you?</p>
<a class="button" href="/satite/index.html">Primary School</a>
<a class="button" href="/satitm/index.html">Secondary School</a>
<a class="button" href="/card.html?location=satite">Primary School</a>
<a class="button" href="/card.html?location=satitm">Secondary School</a>
</div>
</body>

View File

@ -19,12 +19,9 @@
align-items: center;
height: 100vh;
margin: 0;
background-color: #828282;
font-family: "Roboto", sans-serif;
}
.card_frame {
margin-top: 10px;
margin-left: 10px;
display: grid;
width: 750px;
height: 230px;

View File

@ -1 +0,0 @@
/app/iqair-apiserver/static/index_global.html

View File

@ -1 +0,0 @@
/app/iqair-apiserver/static/index_global.html