File "index.php"
Full path: /www/Lazurite/6/index.php
File size: 4.65 KiB (4760 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
$data_dir = "./data/";
//$fp2 = fopen("test_20170920.txt", "a");
//fwrite($fp2, $_GET[data] ."\n");
//fclose($fp2);
//print $_GET[data] ."<BR>";
$res = data_decode($_GET[data]);
echo $res ? 'OK' : 'ERROR';
exit;
// データのデコード
function data_decode($data) {
// 最初に保存ファイル名を取得しておく(ファイルが存在しなかったらヘッダーを作成)
$dname = $GLOBALS["data_dir"] . date("Ym") . "/";
$fname = $dname . date("Ymd") . ".csv";
if (!file_exists($dname)) {
if (!create_data_directory($dname)) {
return false;
}
}
if (!file_exists($fname)) {
if (!create_csv_header($fname)) {
return false;
}
}
// データを日付とデータ部に分割
$datas = split(',', $data);
if (count($datas) != 2) {
return false;
}
// データ部をBase64URLデコード
$bytes = base64_url_decode($datas[1]);
if (!$bytes) {
return false;
}
// Base64URLデコード文字列をfloat配列に変換
$floats = binary_to_floats($bytes);
if (!$floats) {
$fp = fopen("abc.txt","a");
fwrite($fp,"D" ."\n");
fclose($fp);
return false;
}
// CSVファイルに書き出し
if (!write_csv($fname, date("Y/m/d H:i:s"), $datas[0], $floats)) {
$fp = fopen("abc.txt","a");
fwrite($fp,"CSV" ."\n");
fclose($fp);
return false;
}
return true;
}
// データディレクトリの作成
function create_data_directory($dname) {
if (!mkdir($dname, 0755, true)) {
return false;
}
return true;
}
// CSVファイルとヘッダーの作成
function create_csv_header($fname) {
$fp = fopen($fname, "w");
if (!$fp) {
return false;
}
// fwrite($fp, "[DataTime],date,time,roll,pitch,yaw,ax,ay,az,lat,lng,meters\n");
fwrite($fp, "[DataTime],date,time,wp_volt\n");
fclose($fp);
return true;
}
// Base64URLデコード
function base64_url_decode($val) {
// $str = str_replace(array('_', '-', '.'), array('+', '/', '='), $val);
$str = str_replace(array('_', '-', '.'), array('/', '+', '='), $val);
$amari = strlen($str) % 4;
if ($amari > 0) {
for ($i = 0; $i < $amari; $i++) {
$str .= "=";
}
}
$bytes = base64_decode($str);
return $bytes;
}
// バイナリデータのfloat配列化
function binary_to_floats($bytes) {
// $bytesをfloat単位で分割
$len = strlen($bytes);
if ($len % 4 != 0) {
return false;
}
$floats = array();
for ($i = 0; $i < $len; $i += 4) {
$str = substr($bytes, $i, 4);
$float = unpack('f', $str);
array_push($floats, $float[1]);
}
$fp = fopen("abc.txt","a");
fwrite($fp,count($floats) ."\n");
fclose($fp);
// if (count($floats) % 12 != 0) {
// return false;
// }
return $floats;
}
// floatからdoubleを得る
function float_to_double($float1, $float2) {
$double = unpack('d', pack('f2', $float1, $float2));
return $double[1];
}
// CSVファイル書き出し
function write_csv($fname, $now, $datestr, $floats) {
$DATA_TABLE = "lazurite.data_log_6";
$HOST = "192.168.100.153";
$DB = "arduino";
$USER = "postgres";
$PASS = "postgres";
$CURRENT_PATH = "/www/Lazurite/6/";
$INSERT_DATA_NAME = "insert_temp.csv";
//================================================
// データベース接続
// 備考 :
// 更新 : 2013/03/12 KMK-Yokosuka
//================================================
$conn = pg_connect("
host = ". $HOST. "
dbname = ". $DB. "
user = ". $USER. "
password= ". $PASS. "
");
// $datestrから対象となる基準日時を得る
$t = strtotime(substr($datestr, 0, 4) . '-' . substr($datestr, 4, 2) . '-' . substr($datestr, 6, 2) . ' ' . substr($datestr, 8, 2) . ':' . substr($datestr, 10, 2) . ':' . substr($datestr, 12, 2));
$len = count($floats);
$fp = fopen($fname, "a");
if (!$fp) {
return false;
}
//データベース登録用の一時ファイル作成
$fname = $CURRENT_PATH .$INSERT_DATA_NAME;
$fp2 = fopen($fname, "w");
$decord = "";
$datetime_str = date("Y-m-d H:i:s", $t);
$date_str = date("Y/m/d", $t);
$time_str = date("H:i:s", $t);
$sum_data = 0;
$len = 60;
for ($i = 0; $i < 60; $i ++)
{
$decord .= "," .number_format($floats[$i],'5','.','');
//平均の計算の為に値を全て足す
$sum_data += number_format($floats[$i],'5','.','');
}
// print $decord ."<BR>";
//データの平均を計算
$avg_data = number_format($sum_data / $len,'5','.','');
$text = sprintf("%s,%s,%s", $now, $date_str, $time_str) .$decord ."\n";
$text2 = sprintf("%s,%s", $datetime_str ,$avg_data) .$decord ."," .$now ."\n";
fwrite($fp, $text);
fwrite($fp2, $text2);
// }
fclose($fp);
//■データベース登録処理
$insert = "COPY " .$DATA_TABLE ." FROM '" .$CURRENT_PATH .$INSERT_DATA_NAME ."' WITH CSV;";
$res = pg_query($conn, $insert);
fclose($fp2);
return true;
}
?>