<?php
// アプリケーションの起動時にconst_id、app_id、android_idがPOSTされてくる
// 起動時間テーブルをUPDATE
// アプリのバージョンを取得
// 端末の名称を取得
// 「端末名称,アプリバージョン<\r>」を出力
//print "ミキサー2号車,1.0.0\n";

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

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

//===============================================
//	CommonUtilities
//	備考		：	
//	更新		：	2012/11/29 KMK-Teraoka
//===============================================
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/boot_check.log";

try
{
	$_log = new Log($LOG_FILE);
	
	//===============================================
	//	POST、GETパラメータの受け取り
	//	備考		：	
	//	更新		：	2012/11/29 KMK-Teraoka
	//===============================================
	if (DEBUG_MODE == 1)
	{
		extract($_POST);
		extract($_GET);
		Debug::writes($_POST);
	}
	$constId = isset($_POST["const_id"]) ? htmlspecialchars($_POST["const_id"]) : "";
	$appId = isset($_POST["app_id"]) ? htmlspecialchars($_POST["app_id"]) : "";
	$androidId = isset($_POST["android_id"]) ? htmlspecialchars($_POST["android_id"]) : "";
	
	$_log->write("CONST_ID->". $constId. "\n");
	$_log->write("APP_ID->". $appId. "\n");
	$_log->write("TERMINAL_NAME->". $androidId. "\n");
	Debug::write($constId, $appId, $androidId);
	
	if (0 != strcmp($constId, "") && 0 != strcmp($appId, "") && 0 != strcmp($androidId, ""))
	{
		//================================================
		//	データベース接続
		//	備考		：	
		//	更新		：	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-Teraoka
		//================================================
		$selectTerminalName = "SELECT terminal_name FROM qgis.ts_terminal";
		$selectTerminalName .= " WHERE android_id='". $androidId. "'";
		$selectTerminalName .= " AND const_id='". $constId. "'";
		$selectTerminalName .= " AND app_id='". $appId. "'";
		$selectTerminalName .= ";";
		Debug::write($selectTerminalName);
		if (!($resTerminalName = pg_query($conn, $selectTerminalName)))
		{
			throw new Exception("データ取得に失敗<br />". $resTerminalName. "<br />". pg_last_error(). "<br />");
		}
		$rowTerminalName = pg_fetch_array($resTerminalName, 0, PGSQL_ASSOC);
		$termName = $rowTerminalName["terminal_name"];
		
		//================================================
		//	起動ステータスをON
		//	備考		：	
		//	更新		：	2013/05/08 KMK-Teraoka
		//================================================
		$updRunningStatus = "UPDATE qgis.ts_terminal SET";
		$updRunningStatus .= " running='1'";
		$updRunningStatus .= " WHERE android_id='". $androidId. "'";
		$updRunningStatus .= " AND const_id='". $constId. "'";
		$updRunningStatus .= " AND app_id='". $appId. "'";
		$updRunningStatus .= ";";
		Debug::write($updRunningStatus);
		$resRunnigStatus = pg_query($conn, $updRunningStatus);
		
		//================================================
		//	アプリケーションバージョンの取得
		//	備考		：	
		//	更新		：	2012/11/29 KMK-Teraoka
		//================================================
		$selAppVer = "SELECT app_version FROM apc_common.applications";
		$selAppVer .= " WHERE app_id='". $appId. "'";
		$selAppVer .= ";";
		Debug::write($selAppVer);
		if (!($resAppVer = pg_query($conn, $selAppVer)))
		{
			throw new Exception("データ取得に失敗<br />". $resAppVer. "<br />". pg_last_error(). "<br />");
		}
		$rowAppVer = pg_fetch_array($resAppVer, 0, PGSQL_ASSOC);
		$appVer = $rowAppVer["app_version"];
		
		//================================================
		//	レスポンスを表示
		//	備考		：	
		//	更新		：	2012/11/29 KMK-Teraoka
		//================================================
		print "". $termName. ",". $appVer. "\n";
	}
}
catch(Exception $exc)
{
	$_log->writeLog($exc->getMessage(). "\n");
}

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