<?php
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	自動でクラスを参照する
//	関数名	：	__autoload()
//	備考	：	参照先は./class/XXX.phpとする
//	更新	：	2012/10/19 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function __autoload($className)
{
	$fPath = "./class/" . str_replace("_", "/", $className) . ".php";
	require_once $fPath;
}

//===============================================
//	DEBUG_MODE
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
define("DEBUG_MODE", "0");
Debug::setMode(DEBUG_MODE);
if (DEBUG_MODE == 1)
{
	Stopwatch::start();
}

//===============================================
//	CommonUtilities
//	備考		：	
//	更新		：	2012/11/29 KMK-Teraoka
//===============================================
$SCRIPT_TITLE = "運行管理システム　那須試験";
$PENTA_OCEAN = "五洋建設株式会社";
$TAIL_PENTA_OCEAN = "penta-ocean";

$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/view.log";

$CONST_ID = 1;
$APPLICATION_ID = 20;
$MAP_NAME = "./map/nishinasu.png";

$VIEW_LIST = array(
	"0"		=>	array("head"	=>	"id",			"object"	=>	"android_id",		"width"		=>	"12%"),
	"1"		=>	array("head"	=>	"端末名称",		"object"	=>	"terminal_name",	"width"		=>	"12%"),
	"2"		=>	array("head"	=>	"緯度",			"object"	=>	"lat",				"width"		=>	"10%"),
	"3"		=>	array("head"	=>	"経度",			"object"	=>	"lon",				"width"		=>	"10%"),
	"4"		=>	array("head"	=>	"精度",			"object"	=>	"acc",				"width"		=>	"8%"),
	"5"		=>	array("head"	=>	"速度",			"object"	=>	"spd",				"width"		=>	"8%"),
	"6"		=>	array("head"	=>	"測位時間",		"object"	=>	"mesdatetime",		"width"		=>	"20%"),
	"7"		=>	array("head"	=>	"更新時間",		"object"	=>	"upddatetime",		"width"		=>	"20%"),
);

//===============================================
//	HTML-HEAD-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<html>";
print "<head>";
print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
//print "<meta http-equiv=\"Refresh\" content=\"10\">";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ansys/config/css/style.css\">";
print "<link rel=\"shortcut icon\" href=\"/ansys/config/favicon.ico\">";
print "<title>". $SCRIPT_TITLE. "</title>";
print "</head>";

//===============================================
//	HTML-BODY部開始-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<body>";

try
{
	$_log = new Log($LOG_FILE);
	
	// ===============================================
	// 画像情報の取得
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
	$imgSize = getimagesize($MAP_NAME);
	$imgFileName = "./tmp/". $CONST_ID. "_". $APPLICATION_ID. "_". md5(uniqid(rand(), true)) .image_type_to_extension($imgSize[2]);
	
	// ===============================================
	// 基準点情報
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
	$ltLon = 139.935257;						//経度(左上)
	$ltLat =  36.907378;						//緯度(左上)
	$rbLon = 140.009055;						//経度(右下)
	$rbLat =  36.867132;						//緯度(右下)
	
	$ltX = 146;								// 左上X座標
	$ltY = 132;								// 左上Y座標
	$rbX =1002;								// 右下X座標
	$rbY = 599;								// 右下Y座標
	
	$diffX = $rbX - $ltX;					// X座標ピクセルの差を求める
	$diffY = $rbY - $ltY;					// Y座標ピクセルの差を求める
	
	$spX = number_format(($rbLon - $ltLon) / $diffX, 10);		// X座標の倍率を求める
	$spY = number_format(($ltLat - $rbLat) / $diffY, 10);		// Y座標の倍率を求める
	
	// ===============================================
	// GD処理準備
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	$src = ImageCreateFromPNG($MAP_NAME);						//パレットへ貼り付ける画像指定(地図画像)
	$img = imagecreatetruecolor($imgSize[0], $imgSize[1]);			//パレットの作成
	imagecopy($img, $src, 0, 0, 0, 0, $imgSize[0], $imgSize[1]);	//画像をパレットへ貼り付け
	
	$font1 = "/usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf";
	imagesetthickness($img, $THIC);
	
	// 使用色の準備
	$Black = imagecolorallocate($img, 0,0,0);
	$White = imagecolorallocate($img, 255, 255, 255);
	
	// 更新時間をマップに表示する
	$up_check = date("H:i:s" ,time());
	imagefilledpolygon($img, array(10, 10, 10, 20, 55, 20, 55, 10), 4, $White);
	ImageTTFText ($img, 8, 0, 10, 20, $Black, $font1, $up_check);
	
	//================================================
	//	データベース接続
	//	備考		：	
	//	更新		：	2012/11/29 KMK-Teraoka
	//================================================
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	if (!$conn)
	{
		throw new Exception("データベースへの接続に失敗しました");
	}
	
	// ===============================================
	// 位置を取得する
	// 備考		：	
	// 更新		：	2012/11/29 KMK T.Teraoka
	// ===============================================
	$selTerm = "SELECT * FROM qgis.ts_terminal";
	$selTerm .= " WHERE const_id='". $CONST_ID. "'";
	$selTerm .= " AND app_id='". $APPLICATION_ID. "'";
	$selTerm .= " ORDER BY android_id";
	$selTerm .= ";";
	$_log->write($selTerm. "\n");
	if (!($resTerm = pg_query($conn, $selTerm)))
	{
		throw new Exception("データ取得に失敗<br />". $resTerm. "<br />". pg_last_error(). "<br />");
	}
	
	for($iCount = 0; $iCount < pg_num_rows($resTerm); $iCount++)
	{
		$rowTerm = pg_fetch_array($resTerm, $iCount, PGSQL_ASSOC);
		
		if (0 != strcmp($rowTerm["lat"], "") && 0 != strcmp($rowTerm["lon"], ""))
		{
			// 表示テスト
			$posX = ($rowTerm["lon"] - $ltLon) / $spX + $ltX;
			$posY = ($ltLat - $rowTerm["lat"]) / $spY + $ltY;
			
			$_log->write("$posX,$posY". "\n");
			
			// 現在位置の描画
			switch($iCount % 4)
			{
			case 0:
				$col = imagecolorallocate($img, 255, 0, 0);
				break;
			case 1:
				$col = imagecolorallocate($img, 250, 100, 0);
				break;
			case 2:
				$col = imagecolorallocate($img, 0, 100, 255);
				break;
			case 3:
				$col = imagecolorallocate($img, 0, 255, 0);
				break;
			}
			
			$ellipSize = 8;
			//print $posX. ",". $posY;
			Imagefilledellipse($img, $posX, $posY, $ellipSize, $ellipSize, $col);
		}
	}
	
	// ===============================================
	// 描画した画像を保存 & パレットを削除
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	imagepng($img, $imgFileName);
	
	imagedestroy($img);
	imagedestroy($src);
	
	//
	//===============================================
	//	一覧の取得
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	$selList = "SELECT * FROM qgis.ts_terminal";
	$selList .= " WHERE const_id='". $CONST_ID. "'";
	$selList .= " AND app_id='". $APPLICATION_ID. "'";
	$selList .= " ORDER BY android_id";
	$selList .= ";";
	Debug::write($selList);
	if (!($resList = pg_query($conn, $selList)))
	{
		throw new Exception("一覧の取得に失敗<br />". $selList. "<br />". pg_last_error(). "<br />");
	}
	
	//===============================================
	//	一覧の表示
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	print "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\">";
	print "<tr>";
	print "<td>";
	print "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">";
	print "<tr bgcolor=\"#99CCFF\" align=\"center\">";
	for($iCount = 0; $iCount < count($VIEW_LIST); $iCount++)
	{
		print "<th style=\"width:". $VIEW_LIST[$iCount]["width"]. ";height:25px;\">";
		print $VIEW_LIST[$iCount]["head"];
		print "</th>";
	}
	print "</tr>";
	
	for($iCount = 0; $iCount < pg_num_rows($resList); $iCount++)
	{
		$rowList = pg_fetch_array($resList, $iCount, PGSQL_ASSOC);
		print "<tr bgcolor=\"#FFFFFF\" align=\"center\">";
		for($jCount = 0; $jCount < count($VIEW_LIST); $jCount++)
		{
			print "<td style=\"height:25px;\">";
			if (strcmp($rowList[$VIEW_LIST[$jCount]["object"]], "") != 0)
			{
				print $rowList[$VIEW_LIST[$jCount]["object"]];
			}
			else
			{
				print "&nbsp;";
			}
			print "</td>";
		}
		print "</tr>";
	}
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
	
	// ===============================================
	// 上で保存した画像を表示
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	print "<img src=\"". $imgFileName. "?t=$now_t\" galleryimg='no'>";
}
catch(Exception $exc)
{
	$_log->write($exc->getMessage(). "\n");
}

// 戻る
print "<br />";
print "<a href=\"../index.php\">戻る</a>";

//===============================================
//	Postgresql終了処理
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
pg_close($conn);

//===============================================
//	HTML終了処理
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "</body>";
print "</html>";

?>
