iqair-apiserver/static/card.html

362 lines
16 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>
: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;
}
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: #767676;
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: #474747;
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;
}
#health_icon {
transform: scale(2);
padding-top: 30px;
padding-bottom: 20px;
}
</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">
<img id="health_icon" src="">
<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>
<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/m&sup3;)</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 () {
function darkenColor(color, percent) {
var num = parseInt(color.replace("#", ""), 16),
amt = Math.round(2.55 * percent),
R = (num >> 16) + amt,
G = (num >> 8 & 0x00FF) + amt,
B = (num & 0x0000FF) + amt;
return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 + (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1);
}
function dateIsToday(date, server_date) {
return date.getDate() == server_date.getDate() &&
date.getMonth() == server_date.getMonth() &&
date.getFullYear() == server_date.getFullYear();
}
// 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
var server_time = data.server_time
// First we parse the date string into a Date object
// Data is in format YYYY-MM-DD hh:mm:ss
var update_date = new Date(update_time);
// Then we get the server's current date
var server_date = new Date(server_time)
console.log (server_date);
console.log(dateIsToday(update_date,server_date))
// If the update time is not today, we will show the date and time
// Otherwise, we will show the time
if (dateIsToday(update_date,server_date)) {
// We only want hh:mm when the update time is today
update_time = update_time.substring(11, 16);
$("#title").text(data.location + " @ " + update_time);
}
else {
// We want dd/mm/yyyy hh:mm when the update time is not today
update_time = update_time.substring(11, 16);
update_date = update_date.toLocaleDateString("th-TH");
$("#title").text(data.location + " @ " + update_date + " " + update_time);
}
// Update outdoor AQI and PM2.5 values
$("#outdoor-aqi").text(data.aqi_outdoor);
$("#outdoor-pm25").text(data.pm25_outdoor.toFixed(1));
// Update outdoor temperature and humidity values
$("#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 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);
let frame_darken_pct = -4;
if (data.aqi_outdoor < 50) {
let icon_path = root_css.getPropertyValue('--icon-good').trim();
let bg_color = root_css.getPropertyValue('--good-color').trim();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Healthy");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_color);
} else if (data.aqi_outdoor >= 50 && data.aqi_outdoor < 100) {
let icon_path = root_css.getPropertyValue('--icon-moderate').trim();
let bg_color = root_css.getPropertyValue('--moderate-color').trim();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Moderate");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_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();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Unhealthy");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_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();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Unhealthy");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_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();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Very Unhealthy");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_color);
} else {
let icon_path = root_css.getPropertyValue('--icon-hazardous').trim();
let bg_color = root_css.getPropertyValue('--hazardous-color').trim();
let frame_color = darkenColor(bg_color, frame_darken_pct);
$("#health_text").text("Hazardous");
$("#health_icon").attr("src", icon_path);
$("#top_element").css("background-color", bg_color);
$("#health_icon_frame").css("background-color", frame_color);
}
});
}
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>