iqair-apiserver/static/square.html

241 lines
10 KiB
HTML
Raw Normal View History

2024-01-09 07:13:16 +00:00
<!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;
2024-01-11 10:38:54 +00:00
position: relative;
2024-01-09 07:13:16 +00:00
}
.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;
2024-01-11 10:38:54 +00:00
margin-left: 0px;
2024-01-09 07:13:16 +00:00
padding-top: 0;
margin-top: 0;
margin-bottom: 0;
2024-01-11 10:38:54 +00:00
align-content: center;
justify-content: center;
text-align: center;
2024-01-09 07:13:16 +00:00
}
.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;
}
2024-01-10 08:01:25 +00:00
:root {
--good-color: #ABD162;
--moderate-color: #F7D460;
--unhealthy-sensitive-color: #FD9760;
--unhealthy-color: #F7666E;
--very-unhealthy-color: #A37CBB;
--hazardous-color: #A07685;
--icon-good: /images/aqi-icon-l1.svg;
--icon-moderate: /images/aqi-icon-l2.svg;
--icon-unhealthy-sensitive: /images/aqi-icon-l3.svg;
--icon-unhealthy: /images/aqi-icon-l4.svg;
--icon-very-unhealthy: /images/aqi-icon-l5.svg;
--icon-hazardous: /images/aqi-icon-l6.svg;
}
#health_icon {
transform: scale(1.7);
padding-top: 15px;
padding-bottom: 20px;
}
2024-01-11 10:38:54 +00:00
#offline_icon {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: red;
padding: 5px;
box-sizing: border-box;
}
2024-01-09 07:13:16 +00:00
</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>
2024-01-10 08:01:25 +00:00
<img id="health_icon" src="">
2024-01-09 07:13:16 +00:00
<p class="health_text" , id="health_text">Healthy</p>
</div>
2024-01-11 10:38:54 +00:00
<img id="offline_icon" src="/images/offline-icon.svg" style="display: none; position: absolute; top: 10px; left: 10px;">
2024-01-09 07:13:16 +00:00
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
2024-01-11 10:38:54 +00:00
2024-01-23 14:05:55 +00:00
function dateIsToday(date, server_date) {
return date.getDate() == server_date.getDate() &&
date.getMonth() == server_date.getMonth() &&
date.getFullYear() == server_date.getFullYear();
2024-01-11 10:38:54 +00:00
}
2024-01-09 07:13:16 +00:00
$(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
2024-01-23 14:05:55 +00:00
var server_time = data.server_time
// First we parse the date string into a Date object
2024-01-09 07:13:16 +00:00
// Data is in format YYYY-MM-DD hh:mm:ss
2024-01-23 14:05:55 +00:00
var update_date = new Date(update_time);
// Then we get the server's current date
var server_date = new Date(server_time)
2024-01-11 10:38:54 +00:00
// If the date not today, we want to show show the offline icon
2024-01-23 14:05:55 +00:00
if (!dateIsToday(update_date, server_date)) {
2024-01-11 10:38:54 +00:00
$("#offline_icon").css("display", "block");
} else {
$("#offline_icon").css("display", "none");
}
2024-01-09 07:13:16 +00:00
// Update outdoor AQI and PM2.5 values
$("#outdoor-aqi").text("AQI : "+data.aqi_outdoor);
// Is outdoor AQI healthy?
2024-01-10 08:01:25 +00:00
// If AQI is less than 50, it is good
// If AQI is between 50 and 100, it is moderate
// If AQI is between 100 and 150, it is unhealthy for sensitive groups
// If AQI is between 150 and 200, it is unhealthy
// If AQI is between 200 and 300, it is very unhealthy
// If AQI is more than 300, it is hazardous
let root = document.documentElement;
let root_css = getComputedStyle(root);
2024-01-09 07:13:16 +00:00
if (data.aqi_outdoor < 50) {
2024-01-10 08:01:25 +00:00
let icon_path = root_css.getPropertyValue('--icon-good').trim();
let bg_color = root_css.getPropertyValue('--good-color').trim();
$("#health_icon").attr("src", icon_path);
2024-01-09 07:13:16 +00:00
$("#health_text").text("Healthy");
2024-01-10 08:01:25 +00:00
$("#health_icon_frame").css("background-color", bg_color);
2024-01-09 07:13:16 +00:00
} else if (data.aqi_outdoor >= 50 && data.aqi_outdoor < 100) {
2024-01-10 08:01:25 +00:00
let icon_path = root_css.getPropertyValue('--icon-moderate').trim();
let bg_color = root_css.getPropertyValue('--moderate-color').trim();
$("#health_icon").attr("src", icon_path);
2024-01-09 07:13:16 +00:00
$("#health_text").text("Moderate");
2024-01-10 08:01:25 +00:00
$("#health_icon_frame").css("background-color", bg_color);
} else if (data.aqi_outdoor >= 100 && data.aqi_outdoor < 150) {
let icon_path = root_css.getPropertyValue('--icon-unhealthy-sensitive').trim();
let bg_color = root_css.getPropertyValue('--unhealthy-sensitive-color').trim();
$("#health_icon").attr("src", icon_path);
$("#health_text").text("Unhealthy for Sensitive Groups");
$("#health_icon_frame").css("background-color", bg_color);
} else if (data.aqi_outdoor >= 150 && data.aqi_outdoor < 200) {
let icon_path = root_css.getPropertyValue('--icon-unhealthy').trim();
let bg_color = root_css.getPropertyValue('--unhealthy-color').trim();
$("#health_icon").attr("src", icon_path);
2024-01-09 07:13:16 +00:00
$("#health_text").text("Unhealthy");
2024-01-10 08:01:25 +00:00
$("#health_icon_frame").css("background-color", bg_color);
} else if (data.aqi_outdoor >= 200 && data.aqi_outdoor < 300) {
let icon_path = root_css.getPropertyValue('--icon-very-unhealthy').trim();
let bg_color = root_css.getPropertyValue('--very-unhealthy-color').trim();
$("#health_icon").attr("src", icon_path);
$("#health_text").text("Very Unhealthy");
$("#health_icon_frame").css("background-color", bg_color);
} else if (data.aqi_outdoor >= 300) {
let icon_path = root_css.getPropertyValue('--icon-hazardous').trim();
let bg_color = root_css.getPropertyValue('--hazardous-color').trim();
$("#health_icon").attr("src", icon_path);
$("#health_text").text("Hazardous");
$("#health_icon_frame").css("background-color", bg_color);
2024-01-09 07:13:16 +00:00
}
});
}
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>