diff --git a/agcs.html b/agcs.html index 28b126d..42c8621 100644 --- a/agcs.html +++ b/agcs.html @@ -71,8 +71,6 @@ line-height:140%; } #windSpeed { - # background-color: #f1f1f1; - # border-radius: 15px; font-family: 'andale mono', monospace; font-size: 48pt; width: 300px; @@ -82,8 +80,6 @@ line-height:140%; } #avgWindSpeed { - # background-color: #f1f1f1; - # border-radius: 15px; font-family: 'andale mono', monospace; font-size: 48pt; width: 300px; @@ -136,8 +132,6 @@ line-height:140%; console.log("AVG Speed " + loopObj.windSpeed_knot); console.log("AVG Dir " + loopObj.windDir); console.log(loopObj); - //var audio = new Audio('BPCBeep.mp3'); - //audio.play(); lastAvgWind = Date.now(); avgWindValid = 1; avgWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0); @@ -153,8 +147,6 @@ line-height:140%; if (loopObj.windSpeed_knot) { lastWind = Date.now(); windValid = 1; - console.log("LOOP Speed " + loopObj.windSpeed_knot); - console.log("LOOP Dir " + loopObj.windDir); instantWindSpeed = Number(loopObj.windSpeed_knot).toFixed(0); roundedWind = (Math.round(loopObj.windDir / 10) * 10).toFixed(0); 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() { if (avgWindValid == 1 && windValid == 1) { document.getElementById("status").style.backgroundColor = '#00AA00'; @@ -262,11 +275,6 @@ line-height:140%; 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() { const response = await fetch('/wlproxy.php?api=current/195562'); @@ -496,11 +504,10 @@ let windDirection = 0; // Initial wind direction in degrees invalidateDisplay(); MQTTconnect(); getPressure(); +getInitialArchive(); updateClock(); -// Initial draw drawCompass(); -// Example: Update wind direction dynamically every 2 seconds diff --git a/archive.php b/archive.php new file mode 100644 index 0000000..9c699e8 --- /dev/null +++ b/archive.php @@ -0,0 +1,42 @@ +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); + +?>