Get initial archive data on connect

This commit is contained in:
2025-02-24 12:07:19 +00:00
parent d49c2f3472
commit b3c49d4116
2 changed files with 64 additions and 15 deletions

View File

@@ -71,8 +71,6 @@ line-height:140%;
} }
#windSpeed { #windSpeed {
# background-color: #f1f1f1;
# border-radius: 15px;
font-family: 'andale mono', monospace; font-family: 'andale mono', monospace;
font-size: 48pt; font-size: 48pt;
width: 300px; width: 300px;
@@ -82,8 +80,6 @@ line-height:140%;
} }
#avgWindSpeed { #avgWindSpeed {
# background-color: #f1f1f1;
# border-radius: 15px;
font-family: 'andale mono', monospace; font-family: 'andale mono', monospace;
font-size: 48pt; font-size: 48pt;
width: 300px; width: 300px;
@@ -136,8 +132,6 @@ line-height:140%;
console.log("AVG Speed " + loopObj.windSpeed_knot); console.log("AVG Speed " + loopObj.windSpeed_knot);
console.log("AVG Dir " + loopObj.windDir); console.log("AVG Dir " + loopObj.windDir);
console.log(loopObj); console.log(loopObj);
//var audio = new Audio('BPCBeep.mp3');
//audio.play();
lastAvgWind = Date.now(); lastAvgWind = Date.now();
avgWindValid = 1; avgWindValid = 1;
avgWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0); avgWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0);
@@ -153,8 +147,6 @@ line-height:140%;
if (loopObj.windSpeed_knot) { if (loopObj.windSpeed_knot) {
lastWind = Date.now(); lastWind = Date.now();
windValid = 1; windValid = 1;
console.log("LOOP Speed " + loopObj.windSpeed_knot);
console.log("LOOP Dir " + loopObj.windDir);
instantWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0); instantWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0);
roundedWind = (Math.round(loopObj.windDir / 10) * 10).toFixed(0); roundedWind = (Math.round(loopObj.windDir / 10) * 10).toFixed(0);
zeroFilledDir = ('000' + roundedWind).substr(-3); zeroFilledDir = ('000' + roundedWind).substr(-3);
@@ -169,6 +161,27 @@ line-height:140%;
} }
function getInitialArchive() {
fetch(`archive.php`)
.then(response => response.json())
.then(archive => {
let now = Math.floor(Date.now() / 1000);
if (now - archive.dateTime > 300) {
console.log("Archive record too old");
} else {
document.getElementById("OAT").innerHTML = Number((archive.outTemp - 32) * 5 / 9).toFixed(0) + " °C";
avgWindSpeed = (archive.windSpeed * 0.868976).toFixed(0);
avgWindGustSpeed = (archive.windGust * 0.868976).toFixed(0);
roundedWind = (Math.round(archive.windDir / 10) * 10).toFixed(0);
avgWindDir = ('000' + roundedWind).substr(-3);
roundedWind = (Math.round(archive.windGustDir / 10) * 10).toFixed(0);
avgWindGustDir = ('000' + roundedWind).substr(-3);
avgWindValid = 1;
updateAvgWind();
}
})
}
function updateStatus() { function updateStatus() {
if (avgWindValid == 1 && windValid == 1) { if (avgWindValid == 1 && windValid == 1) {
document.getElementById("status").style.backgroundColor = '#00AA00'; document.getElementById("status").style.backgroundColor = '#00AA00';
@@ -262,11 +275,6 @@ line-height:140%;
return false; return false;
} }
// TODO: This is all a bit rough and ready. The station ID is hardcoded and so are the sensor IDs
// It should really look these up each time
//
// Also what I thought was QNH is actually QFE
async function getPressure() { async function getPressure() {
const response = await fetch('/wlproxy.php?api=current/195562'); const response = await fetch('/wlproxy.php?api=current/195562');
@@ -496,11 +504,10 @@ let windDirection = 0; // Initial wind direction in degrees
invalidateDisplay(); invalidateDisplay();
MQTTconnect(); MQTTconnect();
getPressure(); getPressure();
getInitialArchive();
updateClock(); updateClock();
// Initial draw
drawCompass(); drawCompass();
// Example: Update wind direction dynamically every 2 seconds
</script> </script>

42
archive.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
//
// Just returns the latest Archive data from WeeWx Database
//
// Database connection details
$host = 'ikarus.egfh.internal';
$username = 'weewx';
$password = 'p7P0DK9tlWAzKIH';
$database = 'weewx';
function connectDb() {
$conn = new mysqli( $GLOBALS['host'], $GLOBALS['username'], $GLOBALS['password'], $GLOBALS['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
$conn = connectDb();
$sql = "SELECT dateTime,windSpeed,windDir,windGust,windGustDir,outTemp FROM archive ORDER BY dateTime DESC LIMIT 1";
$result = $conn->query($sql);
$data = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
$conn->close();
header('Content-Type: application/json');
echo json_encode($data[0], JSON_PRETTY_PRINT);
?>