62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
include '../dbinfo.php';
|
|
include '../dbinfo2.php';
|
|
|
|
$con = mysqli_connect('192.168.0.10', 'corey', '41945549', 'controls');
|
|
|
|
// Second connection for west mill data (dbinfo2.php)
|
|
$con2 = mysqli_connect('192.168.0.9', 'corey', '41945549', 'controls');
|
|
|
|
function fetchLatestAndHourAgo(mysqli $con, string $table, string $column): ?array
|
|
{
|
|
$sql = "
|
|
SELECT $column AS value, timestamp
|
|
FROM $table
|
|
ORDER BY id DESC
|
|
LIMIT 3600
|
|
";
|
|
$result = mysqli_query($con, $sql);
|
|
if (!$result) {
|
|
return null;
|
|
}
|
|
|
|
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
|
if (count($rows) === 0) {
|
|
return null;
|
|
}
|
|
|
|
$latest = $rows[0];
|
|
$hourAgo = $rows[min(count($rows) - 1, 3599)]; // fall back if fewer than 3600 rows
|
|
|
|
return [
|
|
'latest' => (float) $latest['value'],
|
|
'hourAgo' => (float) $hourAgo['value'],
|
|
'elapsedH' => max(
|
|
1,
|
|
(strtotime($latest['timestamp']) - strtotime($hourAgo['timestamp'])) / 3600
|
|
),
|
|
];
|
|
}
|
|
|
|
$east = fetchLatestAndHourAgo($con, 'easttotalground', 'easttotground');
|
|
$west = $con2 ? fetchLatestAndHourAgo($con2, 'westtotalground', 'MODBUSENET_TABLE900_WTOTGROUND_VALUE') : null;
|
|
|
|
$totalToday = (float) mysqli_fetch_column(mysqli_query(
|
|
$con,
|
|
"SELECT MillTotalGround FROM mill_total_ground ORDER BY id DESC LIMIT 1"
|
|
));
|
|
|
|
mysqli_close($con);
|
|
if ($con2) {
|
|
mysqli_close($con2);
|
|
}
|
|
|
|
$hoursSinceFive = (time() < strtotime('5:00'))
|
|
? (time() + 86400 - strtotime('5:00')) / 3600
|
|
: (time() - strtotime('5:00')) / 3600;
|
|
$hoursRemaining = max(0, 24 - $hoursSinceFive);
|
|
|
|
$eastProjected = $east ? ($east['latest'] - $east['hourAgo']) / $east['elapsedH'] * $hoursRemaining : 0;
|
|
$westProjected = $west ? ($west['latest'] - $west['hourAgo']) / $west['elapsedH'] * $hoursRemaining : 0;
|
|
|
|
echo number_format($totalToday + $eastProjected + $westProjected, 0); |