include_once "accessDatabase.php";
$query = "SELECT * FROM `Addresses` WHERE 1";
//  echo "\$query = $query <br />";
if ($result = mysqli_query($link, $query)) {
    $numAddresses = mysqli_num_rows($result);
    //    printf("Select returned %d rows.\n", mysqli_num_rows($result));
}
$dataTimer = date("s");
$setDebug = 0;
for ($x = 0; $x < $numAddresses; $x++) {
    $ipAddressObj = mysqli_fetch_object($result);
    $ip_Address = $ipAddressObj->ipAddress;
    $udp_Port = $ipAddressObj->udpPort;
    //    echo "\$ip_Address = ".$ipAddressObj->ipAddress.", \$udp_Port = ".$ipAddressObj->udpPort."<br />";
    $in = $getStatus . "\n";
    $udpStatus = udpRequest($ip_Address, $udp_Port, $in);
    //    echo "\$udpStatus = ".$udpStatus."<br />";
    $udpType = explode(",", $udpStatus);
    $tempStr = "<div id=\"temp\"; position: relative; width: 100%>\n       <td align=\"center\" width=\"20%\">\n         <form method=\"post\" action=\"modifySettings.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"udp_port\" value=\"" . $udp_Port . "\">\n           <input type=\"hidden\" name=\"device_name\" value=\"" . $udpType[4] . "\">\n           <input type=\"hidden\" name=\"temptype\" value=\"tempc\">\n           <input type=\"submit\" value=\"MODIFY\">\n         </form>\n         <font size=\"10\"><strong>" . $udpType[4] . "</strong></font><br />\n         <form method=\"post\" action=\"SensorStatus.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"udp_port\" value=\"" . $udp_Port . "\">\n           <input type=\"submit\" name=\"remove\" value=\"REMOVE\">\n         </form>\n        </td>\n       <td align=\"center\" width=\"20%\">\n         Temperature\n         <br />\n         <form method=\"post\" action=\"plotData.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"udp_port\" value=\"" . $udp_Port . "\">\n           <input type=\"hidden\" name=\"device_name\" value=\"" . $udpType[4] . "\">\n           <input type=\"hidden\" name=\"temptype\" value=\"temp0\">\n           <input type=\"submit\" value=\"GRAPH\">\n         </form>\n         <font size=\"10\"><strong>" . $udpType[0] . "&deg; C</strong></font>\n         <br /><br />\n       </td>\n       <td align=\"center\" width=\"20%\">\n         Temperature\n         <br />\n         <form method=\"post\" action=\"plotData.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"device_name\" value=\"" . $udpType[4] . "\">\n           <input type=\"hidden\" name=\"temptype\" value=\"temp1\">\n           <input type=\"submit\" value=\"GRAPH\">\n         </form>\n         <font size=\"10\"><strong>" . $udpType[1] . "&deg; F</strong></font>\n         <br /><br />\n       </td>\n       <td align=\"center\" width=\"20%\">\n         Temperature\n         <br />\n         <form method=\"post\" action=\"plotData.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"device_name\" value=\"" . $udpType[4] . "\">\n           <input type=\"hidden\" name=\"temptype\" value=\"temp2\">\n           <input type=\"submit\" value=\"GRAPH\">\n         </form>\n         <font size=\"10\"><strong>" . $udpType[2] . "&deg; F</strong></font>\n         <br /><br />\n       </td>\n       <td align=\"center\" width=\"20%\">\n         Temperature\n         <br />\n         <form method=\"post\" action=\"plotData.php\">\n           <input type=\"hidden\" name=\"ip_address\" value=\"" . $ip_Address . "\">\n           <input type=\"hidden\" name=\"device_name\" value=\"" . $udpType[4] . "\">\n           <input type=\"hidden\" name=\"temptype\" value=\"temp3\">\n           <input type=\"submit\" value=\"GRAPH\">\n         </form>\n         <font size=\"10\"><strong>" . $udpType[3] . "&deg; F</strong></font>\n         <br /><br />\n       </td>";
    echo "<table width=\"100%\" align=\"center\" border=\"2\">\n<tr>\n{$tempStr}</tr>\n</table>";
    if ($setDebug > 0) {
        echo "\$dataTimer = {$dataTimer}\n";
    }
    if ($dataTimer == "00" || $dataTimer == "01") {
        $time = time("U");
        $switch1 = trim($udpType[2]);
        $switch2 = trim($udpType[3]);
        $insertQuery = "INSERT INTO device SET `ipaddress`=\"{$ip_Address}\",`port`={$udp_Port},`time`={$time},`temp0`={$udpType['0']},`temp1`={$udpType['1']},`temp2`={$udpType['2']},`temp3`={$udpType['3']}";
        if ($setDebug != 0) {
            echo $insertQuery . "\n";
        }
$deviceQuery = "SELECT * FROM `Addresses` WHERE 1";
fwrite($fr, $deviceQuery . "\n");
//  echo $deviceQuery."\n";
$devResult = mysqli_query($link, $deviceQuery);
if ($devResult === FALSE) {
    fwrite($fr, "device query Failed\n");
    die("device query Failed\n");
}
$devCnt = mysqli_num_rows($devResult);
fwrite($fr, $devCnt . " device rows retrieved\n");
//  echo $devCnt." device rows retrieved\n";
while ($devObj = mysqli_fetch_object($devResult)) {
    $ipAddress = $devObj->ipAddress;
    $udpPort = $devObj->udpPort;
    $in = $getStatus . "\n";
    $out = udpRequest($ipAddress, $udpPort, $in);
    $tOut = trim($out);
    if ($tOut === "Invalid Command") {
        continue;
    }
    //    echo "\$in = $in";
    //    echo "\$out = $tOut \n";
    $tempArray = explode(",", $tOut);
    $temp0 = trim($tempArray[$arrayTemp0]);
    $temp1 = trim($tempArray[$arrayTemp1]);
    $temp1 = trim($tempArray[$arrayTemp2]);
    $temp1 = trim($tempArray[$arrayTemp3]);
    $tempInsertQuery = "INSERT INTO device SET `ipaddress`=\"{$ipAddress}\",`port`={$udpPort},`time`={$tempTime},`temp0`={$temp0},`temp1`={$temp1},`temp2`={$temp2},`temp2`={$temp2}";
    fwrite($fr, $tempInsertQuery . "\n");
    //    echo $tempInsertQuery."\n";
    $tempInsertResult = mysqli_query($link, $tempInsertQuery);