File "recv.php"
Full path: /www/shinmoji/wg/recv.php
File size: 3.67 KiB (3755 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
$macid = isset($_GET["mid"]) ? htmlspecialchars($_GET["mid"]) : "";
$millivolts = isset($_GET["mv"]) ? htmlspecialchars($_GET["mv"]) : "";
if (strcmp($millivolts, "") == 0 || strcmp($macid, "") == 0)
exit(1);
$nowUtime = time();
$fp = fopen("./log/log.txt", "a");
fwrite($fp, ">". $macid. "=". $millivolts. "\n");
$conn = pg_connect("
host = 192.168.100.153
dbname = cnbs
user = postgres
password= am#71ZJ*
");
if (!$conn)
exit(1);
$mvarray = explode(",", $millivolts);
$cnt = count($mvarray);
$total = 0;
for($i = 0; $i < $cnt; $i++)
{
$insertLog = "INSERT INTO shinmoji.wg_log (macid, millivolt, regdate) VALUES ('". $macid. "', '". $mvarray[$i]. "', '". date("Y-m-d H:i:s", $nowUtime). "');";
$response = pg_query($conn, $insertLog);
$total += $mvarray[$i];
}
// 平均値の計算処理
$avg = array();
$avg["3s"]["total"] = $total;
$avg["3s"]["cnt"] = $cnt;
$avg["3s"]["avg"] = $avg["3s"]["total"] / $avg["3s"]["cnt"];
$avg["30s"]["total"] = $avg["3s"]["avg"];
$avg["30s"]["cnt"] = 1;
$avg["1m"]["total"] = $avg["3s"]["avg"];
$avg["1m"]["cnt"] = 1;
$avg["10m"]["total"] = $avg["3s"]["avg"];
$avg["10m"]["cnt"] = 1;
$avg["1h"]["total"] = $avg["3s"]["avg"];
$avg["1h"]["cnt"] = 1;
$avg["2h"]["total"] = $avg["3s"]["avg"];
$avg["2h"]["cnt"] = 1;
$avg["24h"]["total"] = $avg["3s"]["avg"];
$avg["24h"]["cnt"] = 1;
// 以前のデータを取得
$selectOlder = "SELECT avg_3s, regdate FROM shinmoji.wg_avg WHERE macid='". $macid. "' AND regdate >= '". date("Y-m-d H:i:s", $nowUtime - 86400). "' ORDER BY regdate DESC;";
$response = pg_query($conn, $selectOlder);
$selcnt = pg_num_rows($response);
for($i = 0; $i < $selcnt; $i++)
{
$row = pg_fetch_assoc($response, $i);
$utime = strtotime($row["regdate"]);
$diffTime = $nowUtime - $utime;
$avg["24h"]["total"] += $row["avg_3s"];
$avg["24h"]["cnt"]++;
if ($diffTime <= 7200)
{
$avg["2h"]["total"] += $row["avg_3s"];
$avg["2h"]["cnt"]++;
if ($diffTime <= 3600)
{
$avg["1h"]["total"] += $row["avg_3s"];
$avg["1h"]["cnt"]++;
if ($diffTime <= 600)
{
$avg["10m"]["total"] += $row["avg_3s"];
$avg["10m"]["cnt"]++;
if ($diffTime <= 60)
{
$avg["1m"]["total"] += $row["avg_3s"];
$avg["1m"]["cnt"]++;
if ($diffTime <= 30)
{
$avg["30s"]["total"] += $row["avg_3s"];
$avg["30s"]["cnt"]++;
}
}
}
}
}
}
if ($avg["30s"]["cnt"] > 0)
$avg["30s"]["avg"] = $avg["30s"]["total"] / $avg["30s"]["cnt"];
else
$avg["1m"]["avg"] = 0;
if ($avg["1m"]["cnt"] > 0)
$avg["1m"]["avg"] = $avg["1m"]["total"] / $avg["1m"]["cnt"];
else
$avg["1m"]["avg"] = 0;
if ($avg["10m"]["cnt"] > 0)
$avg["10m"]["avg"] = $avg["10m"]["total"] / $avg["10m"]["cnt"];
else
$avg["10m"]["avg"] = 0;
if ($avg["1h"]["cnt"] > 0)
$avg["1h"]["avg"] = $avg["1h"]["total"] / $avg["1h"]["cnt"];
else
$avg["1h"]["avg"] = 0;
if ($avg["2h"]["cnt"] > 0)
$avg["2h"]["avg"] = $avg["2h"]["total"] / $avg["2h"]["cnt"];
else
$avg["2h"]["avg"] = 0;
if ($avg["24h"]["cnt"] > 0)
$avg["24h"]["avg"] = $avg["24h"]["total"] / $avg["24h"]["cnt"];
else
$avg["24h"]["avg"] = 0;
$insertAvg = "INSERT INTO shinmoji.wg_avg (macid, avg_3s, avg_30s, avg_1m, avg_10m, avg_1h, avg_2h, avg_24h, regdate)";
$insertAvg .= " VALUES (";
$insertAvg .= "'". $macid. "'";
$insertAvg .= ", '". $avg["3s"]["avg"] . "'";
$insertAvg .= ", '". $avg["30s"]["avg"] . "'";
$insertAvg .= ", '". $avg["1m"]["avg"]. "'";
$insertAvg .= ", '". $avg["10m"]["avg"]. "'";
$insertAvg .= ", '". $avg["1h"]["avg"]. "'";
$insertAvg .= ", '". $avg["2h"]["avg"]. "'";
$insertAvg .= ", '". $avg["24h"]["avg"]. "'";
$insertAvg .= ", '". date("Y-m-d H:i:s", $nowUtime). "');";
$response = pg_query($conn, $insertAvg);
fwrite($fp, ">sql". $insertAvg. "\n");
pg_close($conn);
fclose($fp);
?>