iqair-apiserver/static/square.html

176 lines
6.8 KiB
HTML

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