<?php
$gett = $_GET["q"];//获取数据
$selectTimeMin = $_GET['selectTimeMin'];
$selectTimeMax = $_GET['selectTimeMax'];
ini_set("precision", "3");  //浮点数小数位数
   class MyDB extends SQLite3
   {
      function __construct()
      {
			//$this->open('C:/Users/Administrator/Desktop/workFile/ElecWaterPrj/水文协议SL651 配置软件20220120/DB/SL651.sqlite.db3');
			//$this->open('C:/Users/Administrator/Desktop/workFile/HlsPrj/河流_50888/DB/SL651.sqlite.db3');
			//$this->open('SL651.sqlite.db3');
			$this->open('C:/Users/Administrator/Desktop/workFile/HlsPrj/河流_50888/DB/SL651.sqlite.db3');
      }
   }
   $con = new MyDB();
   if(!$con){
      echo $con->lastErrorMsg();
   } 
   
// 选择数据库
$sql="SELECT * FROM waterLayerInfo WHERE dataTime > '$selectTimeMin' and dataTime < '$selectTimeMax' and devID = '".$gett."' ORDER by `dataTime` DESC LIMIT 1;";
$result = $con->query($sql);
//echo $sql;
if (!$result) {
    printf("Error: SELECT DB ERR\n");
	$con->close();
    exit();
}

$Bottle1to32 = "";
$Bottle33to64 = "";
$Bottle65to96 = "";
$Bottle97to128 = "";
$Bottle129to160 = "";
$SysRunState = "";
$SysAlarmState = "";

echo "
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>水文数据查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            background-color: #1E3D58; /* 蓝色底 */
        }
        th, td {
            padding: 10px;
            text-align: center;
            border: 1px solid #ddd;
            color: white; /* 字体白色 */
        }
        th {
            background-color: #0A2A3E; /* 更深的蓝色 */
        }
        tr:nth-child(even) {
            background-color: #2C4C6E; /* 较浅的蓝色 */
        }
        tr:nth-child(odd) {
            background-color: #1E3D58; /* 深蓝色 */
        }
        tr:hover {
            background-color: #4C6E8F; /* 鼠标悬停时的背景色 */
        }
        h1 {
			padding: 10px;
            //text-align: center;
            color: white;
        }
    </style>
</head>

<table border='1'>
<tr>

<th>采集时间</th>

<th>设备ID</th>
<th>电压</th>
<th>电流</th>
<th>系统报警状态</th>
<th>温度</th>
<th>运行时间</th>
<th>传感器位置</th>


<th>横电机告警_H</th>
<th>当前层_H</th>

<th>竖电机告警_V</th>
<th>当前层_V</th>

<th>升降电机告警_L</th>
<th>当前层_L</th>

<th>抽水电机电源_W</th>

<th>瓶状态1to32</th>
<th>瓶状态32To64</th>
<th>瓶状态65To96</th>
<th>瓶状态97To128</th>
<th>瓶状态129To160</th>
<th>系统业务状态</th>

</tr>";

// SELECT 操作
while($row = $result->fetchArray(SQLITE3_ASSOC)) {
    echo "<tr>";

    echo "<td>" . $row['dataTime'] . "</td>";

    echo "<td>" . $row['devID'] . "</td>";
    echo "<td>" . $row['Vol']/1000 . "</td>";
    echo "<td>" . $row['iCur'] . "</td>";
    echo "<td>" . $row['SysAlarmState'] . "</td>";
    echo "<td>" . $row['Temperature'] . "</td>";
    echo "<td>" . $row['Runtime'] . "</td>";
    echo "<td>" . $row['SensorPosition'] . "</td>";
	
    echo "<td>" . $row['DevAlarm_H'] . "</td>";
    echo "<td>" . $row['CurLayer_H'] . "</td>";

    echo "<td>" . $row['DevAlarm_V'] . "</td>";
    echo "<td>" . $row['CurLayer_V'] . "</td>";

    echo "<td>" . $row['DevAlarm_L'] . "</td>";
    echo "<td>" . $row['CurLayer_L'] . "</td>";
	
    echo "<td>" . $row['PowerOn_W'] . "</td>";

    echo "<td>" . $row['Bottle1to32'] . "</td>";
	echo "<td>" . $row['Bottle33to64'] . "</td>";
	echo "<td>" . $row['Bottle65to96'] . "</td>";
	echo "<td>" . $row['Bottle97to128'] . "</td>";
	echo "<td>" . $row['Bottle129to160'] . "</td>";
    echo "<td>" . $row['RES_1'] . "</td>";

	$Bottle1to32 = $row['Bottle1to32'];
	$Bottle33to64 = $row['Bottle33to64'];
	$Bottle65to96 = $row['Bottle65to96'];
	$Bottle97to128 = $row['Bottle97to128'];
	$Bottle129to160 = $row['Bottle129to160'];
	$SysRunState = $row['RES_1'];
	$SysAlarmState = $row['SysAlarmState'];
    echo "</tr>";
}
echo "</table>";


echo "
<h1>系统告警状态</h1>
<table border='1'>
<tr>
    <th>告警</th>
    <th>状态</th>
</tr>
";
// Fetch the alarm status from the database
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    // Assuming $SysAlarmState contains the 16-bit alarm status (the value of u16SysAlarm)
    $SysAlarmState = $row['SysAlarmState'];  // Replace 'SysAlarmState' with the actual column name

    // Check each bit and display the alarm status
    $alarms = [
        '服务器离线' => ($SysAlarmState & (1 << 0)) > 0,
        'U盘空间满' => ($SysAlarmState & (1 << 1)) > 0,
        'SD卡丢失' => ($SysAlarmState & (1 << 2)) > 0,
        '4G模块丢失' => ($SysAlarmState & (1 << 3)) > 0,
        '电压上限告警' => ($SysAlarmState & (1 << 4)) > 0,
        '电压下限告警' => ($SysAlarmState & (1 << 5)) > 0,
        '环境温度上限告警' => ($SysAlarmState & (1 << 6)) > 0,
        '充电电流上限告警' => ($SysAlarmState & (1 << 7)) > 0,
        '放电电流上限告警' => ($SysAlarmState & (1 << 8)) > 0,
        '铁电丢失' => ($SysAlarmState & (1 << 9)) > 0,
        '串口1丢失' => ($SysAlarmState & (1 << 10)) > 0,
        '串口2丢失' => ($SysAlarmState & (1 << 11)) > 0,
        '串口3丢失' => ($SysAlarmState & (1 << 12)) > 0,
        '串口4丢失' => ($SysAlarmState & (1 << 13)) > 0,
        '串口5丢失' => ($SysAlarmState & (1 << 14)) > 0,
    ];

    // Loop through each alarm and print it if the bit is set
    foreach ($alarms as $alarm => $isActive) {
        if ($isActive) {
            echo "<tr><td>{$alarm}</td><td>已激活</td></tr>";
        }
    }
}
echo "</table>";


 
echo "
<h1>系统业务状态</h1>
<table border='1'>
<tr>
    <th>当前状态</th>
</tr>
";
while($row = $result->fetchArray(SQLITE3_ASSOC)) {
    echo "<tr>";
    
    // 根据数值判断任务状态
    switch($SysRunState) {
        case 0:
            $status = "无任务状态";
            break;
        case 1:
            $status = "分层采集开始";
            break;
        case 2:
            $status = "横电机工作";
            break;
        case 3:
            $status = "竖电机工作";
            break;
        case 4:
            $status = "升降电机下降";
            break;
        case 5:
            $status = "升降电机上升";
            break;
        case 6:
            $status = "升降电机停止";
            break;
        case 7:
            $status = "打开水泵排管内水";
            break;
        case 8:
            $status = "打开水泵注水";
            break;
        case 9:
            $status = "关闭水泵";
            break;
        case 10:
            $status = "分层采集完成";
            break;
        case 11:
            $status = "电机回原点开始";
            break;
        case 12:
            $status = "电机回原点完成";
            break;
        case 13:
            $status = "接近开关判断开始";
            break;
        case 14:
            $status = "接近开关判断完成";
            break;
        case 15:
            $status = "升降电机下降测试开始";
            break;
        case 16:
            $status = "升降电机下降测试完成";
            break;
        case 17:
            $status = "升降电机上升测试开始";
            break;
        case 18:
            $status = "升降电机上升测试完成";
            break;
        default:
            $status = "未知状态";
            break;
    }
    
    echo "<td>" . $status . "</td>";
    echo "</tr>";
}
echo "</table>";





echo "
<h1>瓶状态表</h1>
<table border='1'>
";
while($row = $result->fetchArray(SQLITE3_ASSOC)) {
    echo "<tr>";
    
    // 获取Bottle1to32的二进制表示
    $binaryValue = str_pad(decbin($row['Bottle1to32']), 32, "0", STR_PAD_LEFT);
    
    // 遍历每一位，并设置对应的单元格颜色
    for ($i = 0; $i < 32; $i++) {
        $color = ($binaryValue[31-$i] == '1') ? 'green' : 'gray';  // 判断该位是1还是0
		 $index = $i+1 ;  // 位的标号，从1开始
		echo "<td style='background-color: $color; width: 30px; height: 30px; text-align: center; vertical-align: middle; color: white;'>
                $index
              </td>";
    }
    echo "</tr>";
}
echo "</table>";

if($Bottle33to64)
{
	echo "
	<table border='1'>";
	while($row = $result->fetchArray(SQLITE3_ASSOC)) {
		echo "<tr>";
		
		// 获取Bottle1to32的二进制表示
		$binaryValue = str_pad(decbin($row['Bottle33to64']), 32, "0", STR_PAD_LEFT);
		
		// 遍历每一位，并设置对应的单元格颜色
		for ($i = 0; $i < 32; $i++) {
			$color = ($binaryValue[31-$i] == '1') ? 'green' : 'gray';  // 判断该位是1还是0
			 $index = $i+33 ;  // 位的标号，从1开始
			echo "<td style='background-color: $color; width: 30px; height: 30px; text-align: center; vertical-align: middle; color: white;'>
					$index
				  </td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}


if($Bottle65to96)
{
	echo "<table border='1'>";
	while($row = $result->fetchArray(SQLITE3_ASSOC)) {
		echo "<tr>";
		
		// 获取Bottle1to32的二进制表示
		$binaryValue = str_pad(decbin($row['Bottle65to96']), 32, "0", STR_PAD_LEFT);
		
		for ($i = 0; $i < 32; $i++) {
			$color = ($binaryValue[31-$i] == '1') ? 'green' : 'gray';  // 判断该位是1还是0
			 $index = $i+65 ;  // 位的标号，从1开始
			echo "<td style='background-color: $color; width: 30px; height: 30px; text-align: center; vertical-align: middle; color: white;'>
					$index
				  </td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}

if($Bottle97to128)
{
	echo "<table border='1'>";
	while($row = $result->fetchArray(SQLITE3_ASSOC)) {
		echo "<tr>";
		
		// 获取Bottle1to32的二进制表示
		$binaryValue = str_pad(decbin($row['Bottle97to128']), 32, "0", STR_PAD_LEFT);
		
		for ($i = 0; $i < 32; $i++) {
			$color = ($binaryValue[31-$i] == '1') ? 'green' : 'gray';  // 判断该位是1还是0
			 $index = $i+97 ;  // 位的标号，从1开始
			echo "<td style='background-color: $color; width: 30px; height: 30px; text-align: center; vertical-align: middle; color: white;'>
					$index
				  </td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}

if($Bottle129to160)
{
	echo "<table border='1'>";
	while($row = $result->fetchArray(SQLITE3_ASSOC)) {
		echo "<tr>";
		
		// 获取Bottle1to32的二进制表示
		$binaryValue = str_pad(decbin($row['Bottle129to160']), 32, "0", STR_PAD_LEFT);
		
		for ($i = 0; $i < 32; $i++) {
			$color = ($binaryValue[31-$i] == '1') ? 'green' : 'gray';  // 判断该位是1还是0
			 $index = $i+129 ;  // 位的标号，从1开始
			echo "<td style='background-color: $color; width: 30px; height: 30px; text-align: center; vertical-align: middle; color: white;'>
					$index
				  </td>";
		}
		echo "</tr>";
	}
	echo "</table>";
}



 


$con->close();
 ?>