$SEWER_GOOD = 200;
$SEWER_MIN = 0;
$HCHO_MAX = 30;
$HCHO_OK = 20;
$HCHO_GOOD = 10;
$HCHO_MIN = 0;
$param_date_format = 'Y-m-d';
$user_date_format = 'l, F jS Y H:i';
$db_name = 'iaq';
$conn = mysqli_connect("", "", "", $db_name);
if (mysqli_connect_errno()) {
    exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if (isset($_GET["id"])) {
    $id = htmlspecialchars($_GET["id"]);
    $sensor_type_name = get_sensor_type_name($id);
}
if (!isset($_GET["size"])) {
    $size = 1;
} else {
    $size = htmlspecialchars($_GET["size"]);
}
// ------------------------------------------------------------------- FUNCTIONS
function get_sensor_type_name($id)
{
    global $conn;
    global $user_timezone;
    global $group_name;
    global $sensor_temp;
    global $sensor_dust;
    global $sensor_sewer;
echo "</div>";
echo "</td>";
echo "</tr>";
echo "<tr><td></td><td></td><td></td><td></td><td></td><td></td>";
echo "<th style='text-align:center;'>Last Reading</th><th style='text-align:center;'>Group Type</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    $result_geo = mysqli_query($conn, "SELECT name from geographical where group_id=" . $row['id'] . " order by ts desc");
    $row_geo = mysqli_fetch_array($result_geo);
    echo "<tr>\n";
    echo "<td style='text-align:right;'><div style='font-size:20px;font-weight:bold;'>" . $row['name'] . "</div>";
    echo "</td>\n";
    echo "<td><img src='images/calendar.png' onclick='click_button(" . $row['id'] . ");' height=40 width=40 style='cursor:pointer;'></td>";
    echo "<td><img src='images/graph.png' onclick='click_button(" . $row['id'] . ",\"dashboard.php\");' height=40 width=40 style='cursor:pointer;'></td>";
    echo "<td><img src='images/barchart.png' onclick='click_button(" . $row['id'] . ",\"events/event_monthly.php\");' height=40 width=40 style='cursor:pointer;'></td>";
    echo "<td><img src='images/download.png' onclick='click_button(" . $row['id'] . ",\"transfer/download.php\");' height=40 width=40 style='cursor:pointer;'></td>";
    $name = get_sensor_type_name($row['id']);
    echo "<td style='text-align:center'>";
    echo "<img src='images/location.png' onclick='click_button(" . $row['id'] . ",\"add_geographical.php\");' height=30 width=30 style='cursor:pointer;'><br/>";
    if (strlen($row_geo['name']) > 0) {
        echo "<i>(" . $row_geo['name'] . ")</i>";
    } else {
        echo "<i>&lt;Not Set&gt;</i>";
    }
    echo "</td>";
    echo "<td style='text-align:center;'>";
    $result_group = mysqli_query($conn, "SELECT max(ts) as ts from readings where group_id=" . $row['id']);
    $row_group = mysqli_fetch_array($result_group);
    if (!is_null($row_group['ts'])) {
        $curr_date = Carbon::createFromTimeStamp($row_group['ts']);
        $curr_date->setTimezone($user_timezone);
        if ($curr_date->isToday()) {