<?php
	// 設置資料類型 json,編碼格式 utf-8
	header('Content-Type: application/json; charset=UTF-8');
	
	// 判斷如果是 GET 請求,則進行搜尋;如果是 POST 請求,則進行新建
	// $_SERVER['REQUEST_METHOD'] 返回訪問頁面使用的請求方法
	if ($_SERVER['REQUEST_METHOD'] == "GET") {
		create();
	} else if ($_SERVER['REQUEST_METHOD'] == "POST") {
		create();
	}

	// 新建員工
	function create() {
		//Cmd 1: force charging  2: remote stop
		//1: force charging
		//EnableForceCharging
		//StartForceCharging
		//FTargetVoltage
		//FTargetCurrent
		//2: remote stop
		

		checkValue('Cmd');
		if($_REQUEST['Cmd']==1){
			if(isset($_REQUEST['FTargetVoltage'])){
				isNumeric("FTargetVoltage",65535);
				$json['FTargetVoltage']				= (int)$_REQUEST['FTargetVoltage'];
			}
			else{
				$jsone['result'] = "Fail";
				$jsone['message'] = "You should enter a value of FTargetVoltage";
				echo json_encode($jsone);
				exit;
			}
			if(isset($_REQUEST['FTargetCurrent'])){
				isNumeric("FTargetCurrent",65535);
				$json['FTargetCurrent']				= (int)$_REQUEST['FTargetCurrent'];
			}
			else{
				$jsone['result'] = "Fail";
				$jsone['message'] = "You should enter a value of FTargetCurrent";
				echo json_encode($jsone);
				exit;
			}
		}

//		ob_start();
		
		shell_exec('sync;sync;sync');
		chdir("/root");
		$str_json=json_encode($json);//var_dump($str_json);
		if($_REQUEST['Cmd']=="1"){
			exec("'./WebService' 'PowerCabinet' '1' '".$str_json."'",$output,$return_var);
		}
		if($_REQUEST['Cmd']=="2"){
			exec("'./WebService' 'PowerCabinet' '2' '".$str_json."'",$output,$return_var);
		}
		 if(count($output)!=0){
			 $jsone['result'] = "Success";
			 $jsone['message'] = $json;
			 echo json_encode($jsone);
			 exit;
		 }
		 else{
			 $jsone['result'] = "Error";
			 $jsone['message'] = "Something went wrong on machine";
			 echo json_encode($jsone);
			 return false;
			 exit;
		 }
//		ob_end_clean();	
	}

	function checkValue($id){
		if($_REQUEST[$id] != ""){
			if($_REQUEST[$id]!=1 && $_REQUEST[$id]!=2){
				$jsone['result'] = "Fail";
				$jsone['message'] = "You have entered a wrong value on " . $id . ", it should be 1 or 2";
				echo json_encode($jsone);
				exit;
			}
/*			if(strlen($_REQUEST[$id])!=1){
					$jsone['result'] = "Fail";
					$jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
					echo json_encode($jsone);
					exit;
			}
			if(!is_numeric($_REQUEST[$id])){
					$jsone['result'] = "Fail";
					$jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
					echo json_encode($jsone);
					exit;
			}*/
		}
	}
	function checkLength($id,$value){
		if(strlen($_REQUEST[$id])>$value){
				$jsone['result'] = "Fail";
				$jsone['message'] = "Length of " . $id . " should be less than ".$value+"(Chinese 5 words)";
				echo json_encode($jsone);
				exit;
		}
	}
	function isNumeric($id,$value){
		if($_REQUEST[$id] != ""){
			if(!is_numeric($_REQUEST[$id])){
					$jsone['result'] = "Fail";
					$jsone['message'] = "You have entered a wrong value on " . $id . ", it should be numeric";
					echo json_encode($jsone);
					exit;
			}
			if($_REQUEST[$id]>$value){
					$jsone['result'] = "Fail";
					$jsone['message'] = "Value of " . $id . " should be less than ".$value;
					echo json_encode($jsone);
					exit;
			}
		}
	}
?>