<?php
/******************************************************************
 *
 *
 *
 *
 *
 *
 *
 * 
 *******************************************************************/


if (isset($_POST['data'])) {	
	senddata($_POST['data']);
}
elseif (isset($_POST['ip'])&&isset($_POST['port'])) {	

	GetGroupsList($_POST['ip'],$_POST['port']);
}
else{
	require './db/json.php';
	require './db/MySql_Connect.php';
	require 'static/firmware_update/firmware_update.html';	
}

// $datalistArr = new array();
function GetGroupsList($_socketIP,$_socketPORT)
{	
	set_time_limit(0);
	$_checkEvents=0;
	$_eventID=0;
	$fp = pfsockopen("tcp://".$_socketIP,intval($_socketPORT),$errno,$errstr);
	//echo var_dump(get_resource_type($fp));
	if (!$fp) {
		echo $errstr($errno);
	}	
	else{
		stream_set_blocking($fp,1);		
		
		// $data = $data.'\r\n';
		$data = "require blueriver_api 2.14.0.0\r\n";
		if(!fwrite($fp,$data,strlen($data))){
			echo "exit";
		}
		$_data=fread($fp,1024);
		
		while (!strstr($_data, "\r\n")) {
			$_data.=fgets($fp);
        }
		$_recv = '';
		//echo $_data;
		do{
			$data = "event 0\r\n";
			if(!fwrite($fp,$data,strlen($data))){
				echo "exit";
			}
			$_data=fread($fp,1024);
			
			while (!strstr($_data, "\r\n")) {
				$_data.=fgets($fp);
	        }
	        
			$_json = json_decode($_data,true);
			$_length = count($_json['result']['events']);
			// $a='';
			// $b,$c
			for ($i=0; $i < $_length; $i++) {
				$data = "request ".$_json['result']['events'][$i]['request_id']."\r\n";
				if(!fwrite($fp,$data,strlen($data))){
					echo "exit";
				}
				$_data=fread($fp,1024);
				
				while (!strstr($_data, "\r\n")) {
					$_data.=fgets($fp);
		        }
		        // echo $_data.'-----';
			}

			$data = "event ".$_json['result']['events'][$_length-1]['event_id']."\r\n";
			while(1){
				fwrite($fp,$data,strlen($data));
				// $_dataChar='';
				$_data=fread($fp,1024);				
				while (!strstr($_data, "\r\n")) {
					$_data.=fgets($fp);
		        }
		        // echo $_data.'-----';
		        $_json2 = json_decode($_data,true);
				$_length2 = count($_json2['result']['events']);
				
				if ($_length2>0) {
					$_checkEvents = 1;
				}else {
					$_checkEvents = 0;
				}
				//if ($_length2>0) {					
					for ($i=0; $i < $_length2; $i++) {
						if ($_json2['result']['events'][$i]['event_type']=='FIRMWARE_UPDATE_COMPLETE') {
							$_recv .= $_json2['result']['events'][$i]['device_id'].',';
							// PositionInfoSetFile($_data);
							$fp1 = fopen('../db/firmwareUpdateTimeComplate_'.$_json2['result']['events'][$i]['device_id'].'.txt', 'rb');
							$_Checkdata='';
							do{
								$checkdata=fread($fp1,100);
								$_Checkdata.=$checkdata;
							}while($checkdata!='');

							if ($_Checkdata=='') {	
								$_TextData = $_json2['result']['events'][$i]['device_id'];
								ComplatePositionInfoSetFile($_TextData,$_json2['result']['events'][$i]['device_id']);
								echo $_TextData."\n";
							}							
							fclose($fp1);
							
							
						}
						else if ($_json2['result']['events'][$i]['event_type']=='FIRMWARE_UPDATE_PROGRESS') {
							$data = "request ".$_json2['result']['events'][$i]['request_id']."\r\n";
							if(!fwrite($fp,$data,strlen($data))){
								echo "exit";
							}
							$_data=fread($fp,1024);
							
							while (!strstr($_data, "\r\n")) {
								$_data.=fgets($fp);
					        }
					        PositionInfoSetFile($_data,$_json2['result']['events'][$i]['device_id']);
						}
						if ($_json2['result']['events'][$i]['event_id']>$_eventID) {
							$_eventID = $_json2['result']['events'][$i]['event_id'];
						}
					}
					//PositionInfoSetFile($_data);
					
					$data = "event ".$_eventID."\r\n";
					// if ($_recv!=''&&$_checkEvents == 0) {
					// 	echo $_recv;	
					// 	fclose($fp);
					// 	return;
					// }
					
					$fp1 = fopen('../db/firmwareUpdateTime.txt', 'rb');
					$_Checkdata='';
					do{
						$checkdata=fread($fp1,100);
						$_Checkdata.=$checkdata;
					}while($checkdata!='');
					//fclose($fp1);
					if ($_Checkdata=='Close'&&$_length2==0) {
						
						echo $_recv;	
						fclose($fp1);
						fclose($fp);
						CheckCloseClean();
						return;
					}
					
					
					sleep(1);
				//}
				
			}
			sleep(1);
		}while(1);
  		
		
	}
	// fclose($fp);
}

function senddata($data)
{	
	$data = $data."\r\n";
	if(!fwrite($fp,$data,strlen($data))){
		echo "exit";
	}
	$out=fread($fp,1024);
	echo $out;
	
}
function CheckCloseClean(){
	$fp = fopen('../db/firmwareUpdateTime.txt', 'w+b');
	$_data='';
	if (!$fp) {
		# code...
		return 'err';
	}
	fwrite($fp,$_data,strlen($_data));
	fclose($fp);
}
function PositionInfoSetFile($_data,$_id){
	$fp = fopen('../db/firmwareUpdateTime_'.$_id.'.txt', 'w+b');
	if (!$fp) {
		# code...
		return 'err';
	}
	fwrite($fp,$_data,strlen($_data));
	fclose($fp);
}
function ComplatePositionInfoSetFile($_data,$_id){
	$fp = fopen('../db/firmwareUpdateTimeComplate_'.$_id.'.txt', 'w+b');
	if (!$fp) {
		# code...
		return 'err';
	}
	fwrite($fp,$_data,strlen($_data));
	fclose($fp);
}
function closeTCPConnect(){
	//fclose($fp);
}
?>