<?php
//
// WorkStatusをSpeedTypeとして使用する
//

//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	自動でクラスを参照する
//	関数名	：	__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/06 KMK-Teraoka
//===============================================
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/recv_edit_param.log";

try
{
	$_log = new Log($LOG_FILE);
	
	//===============================================
	//	POST、GETパラメータの受け取り
	//	備考		：	
	//	更新		：	2012/10/25 KMK-Teraoka
	//===============================================
	if (DEBUG_MODE == 1)
	{
		extract($_POST);
		extract($_GET);
		Debug::writes($_POST);
	}
	$androidId = isset($_POST["android_id"]) ? htmlspecialchars($_POST["android_id"]) : "";
	$constId = isset($_POST["const_id"]) ? htmlspecialchars($_POST["const_id"]) : "";
	$applicationId = isset($_POST["application_id"]) ? htmlspecialchars($_POST["application_id"]) : "";
	$terminalName = isset($_POST["terminal_name"]) ? htmlspecialchars($_POST["terminal_name"]) : "";
	$measureInterval = isset($_POST["measure_interval"]) ? htmlspecialchars($_POST["measure_interval"]) : "";
	$measureResult = -1;
	$locationSendInterval = isset($_POST["location_send_interval"]) ? htmlspecialchars($_POST["location_send_interval"]) : "";
	$accOmit = isset($_POST["acc_omit"]) ? htmlspecialchars($_POST["acc_omit"]) : "";
	$stopDistance = isset($_POST["stop_distance"]) ? htmlspecialchars($_POST["stop_distance"]) : "";
	$areaEventsFname = "";
	$workStatus = -1;
	$speedType = isset($_POST["speed_type"]) ? htmlspecialchars($_POST["speed_type"]) : "";
	
	$_log->write("ANDROID_ID->". $androidId. "\n");
	$_log->write("CONST_ID->". $constId. "\n");
	$_log->write("APPLICATION_ID->". $applicationId. "\n");
	$_log->write("TERMINAL_NAME->". $terminalName. "\n");
	$_log->write("MEASURE_INTERVAL->". $measureInterval. "\n");
	//$_log->write("MEASURE_RESULT->". $measureResult. "\n");
	$_log->write("LOCATION_SEND_INTERVAL->". $locationSendInterval. "\n");
	$_log->write("ACC_OMIT->". $accOmit. "\n");
	$_log->write("STOP_DISTANCE->". $stopDistance. "\n");
	//$_log->write("AREA_EVENTS_FNAME->". $areaEventsFname. "\n");
	//$_log->write("WORK_STATUS->". $workStatus. "\n");
	$_log->write("SPEED_TYPE->". $speedType. "\n");
	
	if (0 == strcmp($androidId, ""))
	{
		throw new Exception("Error!! undefined android_id");
	}
	
	//================================================
	//	データベース接続
	//	備考		：	
	//	更新		：	2012/11/29 KMK-Teraoka
	//================================================
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	if (!$conn)
	{
		throw new Exception("Error!! connected faild postgresql DB");
	}
	
	//================================================
	//	設定データの更新
	//	備考		：	
	//	更新		：	2012/11/29 KMK-Teraoka
	//================================================
	$selectTerminal = "SELECT * FROM qgis.ts_terminal";
	$selectTerminal .= " WHERE android_id='". $androidId. "'";
	$selectTerminal .= " AND const_id='". $constId. "'";
	$selectTerminal .= " AND app_id='". $applicationId. "'";
	$selectTerminal .= ";";
	$_log->write($selectTerminal. "\n");
	if (!($resTerminal = pg_query($conn, $selectTerminal)))
	{
		throw new Exception("Error!! Select Terminal\n". $resTerminal. "\n". pg_last_error(). "");
	}
	
	if (pg_num_rows($resTerminal) == 0)
	{
		$refreshTerminal = "INSERT INTO qgis.ts_terminal";
		$refreshTerminal .= " (android_id, const_id, app_id, terminal_name, measure_interval, measure_result, location_send_interval, acc_omit, stop_distance, area_events_fname, work_status, upddatetime, regdatetime)";
		$refreshTerminal .= " VALUES";
		$refreshTerminal .= " ('". $androidId. "','". $constId. "','". $applicationId. "','". $terminalName. "','". $measureInterval. "','". $measureResult. "','". $locationSendInterval. "','". $accOmit. "','". $stopDistance. "','". $areaEventsFname. "','". $speedType. "',current_timestamp,current_timestamp)";
		$refreshTerminal .= ";";
	}
	else
	{
		$refreshTerminal = "UPDATE qgis.ts_terminal";
		$refreshTerminal .= " SET terminal_name='". $terminalName. "'";
		$refreshTerminal .= ", measure_interval='". $measureInterval. "'";
		$refreshTerminal .= ", measure_result='". $measureResult. "'";
		$refreshTerminal .= ", location_send_interval='". $locationSendInterval. "'";
		$refreshTerminal .= ", acc_omit='". $accOmit. "'";
		$refreshTerminal .= ", stop_distance='". $stopDistance. "'";
		$refreshTerminal .= ", area_events_fname='". $areaEventsFname. "'";
		$refreshTerminal .= ", work_status='". $speedType. "'";
		$refreshTerminal .= ", upddatetime=current_timestamp";
		$refreshTerminal .= " WHERE android_id='". $androidId. "'";
		$refreshTerminal .= " AND const_id='". $constId. "'";
		$refreshTerminal .= " AND app_id='". $applicationId. "'";
		$refreshTerminal .= ";";
	}
	$_log->write($refreshTerminal. "\n");
	if (!($resTerminal = pg_query($conn, $refreshTerminal)))
	{
		throw new Exception("Error!! Refresh Terminal Data\n". $resTerminal. "\n". pg_last_error(). "\n");
	}
}
catch(Exception $exc)
{
	$_log->write($exc->getMessage(). "\n");
}

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