<?php

Class Signals extends CI_Controller
{	

	function __construct() 
	{
		parent::__construct();
		$this->load->model('users');
		$this->load->library('jwt');
		$this->load->library('jres');
		$this->load->library('sms');
		$this->load->model('signal');
	}


	function index()
	{
		//echo "<pre>"; print_r( $this->input->get()  );
		$DateTime = $this->input->get_post('datetime', true);
		$IMEI = $this->input->get_post('imei', true);
		$number = $this->input->get_post('number', true);
		$messageType = $this->input->get_post('messageType', true);
		$lat = $this->input->get_post('lat', true);
		$long = $this->input->get_post('long', true);
		$posdatatime = $this->input->get_post('posdatatime', true);
		$postype = $this->input->get_post('postype', true);
		$status = $this->input->get_post('status', true);
		$interval = $this->input->get_post('interval', true);
		$speed = $this->input->get_post('speed', true);

		$this->signal->number = $number;
		$get_device_type = $this->signal->get_device_type();
		//debug($get_device_type['device_type']);

		if($get_device_type['device_type'] == 'android' || $get_device_type['device_type'] == 'Android'){

			//echo "heoo";
			if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
				{
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    //die("Couldn't create socket: [$errorcode] $errormsg \n");
				    $this->jres->failure("Couldn't create socket: [$errorcode] $errormsg \n");
				    exit;
				}

				if(!socket_connect($sock , '46.255.118.104' , 5055))
				{
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    //die("Could not connect: [$errorcode] $errormsg \n");
				    $this->jres->failure("Could not connect: [$errorcode] $errormsg \n");
				    exit;
				}
				$signals = "PRT,".$DateTime.",".$IMEI.",".$number.",".$messageType.",".$lat.",".$long.",".$posdatatime.",".$postype.",".$status.",".$interval.",".$speed;

				$checksum = md5($signals);
				
				$message = "PRT,".$DateTime.",".$IMEI.",".$number.",".$messageType.",".$lat.",".$long.",".$posdatatime.",".$postype.",".$status.",".$interval.",".$speed.",".$checksum;
				
				
				/* sending values in database */
				/*$this->signal->DateTime = $DateTime;
				$this->signal->IMEI = $IMEI;
				$this->signal->number = $number;
				$this->signal->messageType = $messageType;
				$this->signal->lat = $lat;
				$this->signal->long = $long;
				$this->signal->posdatatime = $posdatatime;
				$this->signal->postype = $postype;
				$this->signal->status = $status;
				$this->signal->interval = $interval;
				$this->signal->speed = $speed;
				$this->signal->checksum = $checksum;

				$this->signal->send_signals();	*/	

				//Send the message to the server
				if( ! socket_send ( $sock , $message , strlen($message) , 0))
				 {
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    //die("Could not send data: [$errorcode] $errormsg \n");
				    $this->jres->failure("Could not send data: [$errorcode] $errormsg \n");
				    exit;
				}
			socket_close($sock);
			}

			$this->jres->success_message('Signals send successfully');
	}

	function check_date()
	{
		echo date('Y-m-d H:i:s');
	}

	function receive_signal($string)
	{
		if( base64_decode($string) === "orbisarcsignalfromserver" )
		{
			
			if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
				{
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    //die("Couldn't create socket: [$errorcode] $errormsg \n");
				    $this->jres->failure("Couldn't create socket: [$errorcode] $errormsg \n");
				    exit;
				}

				if(!socket_connect($sock , '46.255.118.104' , 5055))
				{
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    //die("Could not connect: [$errorcode] $errormsg \n");
				    $this->jres->failure("Could not connect: [$errorcode] $errormsg \n");
				    exit;
				}
				/*$signals = "PRT,".$DateTime.",".$IMEI.",".$number.",".$messageType.",".$lat.",".$long.",".$posdatatime.",".$postype.",".$status.",".$interval.",".$speed;

				$checksum = md5($signals);
				
				$message = "PRT,".$DateTime.",".$IMEI.",".$number.",".$messageType.",".$lat.",".$long.",".$posdatatime.",".$postype.",".$status.",".$interval.",".$speed.",".$checksum;*/
				
				//Now receive reply from server
				if(socket_recv ( $sock , $buf , 2045 , MSG_WAITALL ) === FALSE)
				{
				    $errorcode = socket_last_error();
				    $errormsg = socket_strerror($errorcode);
				     
				    die("Could not receive data: [$errorcode] $errormsg \n");
				}
				 
				//print the received message
				$this->jres->success_message($buf);
				socket_close($sock);
			}
			else
			{
				$this->jres->failure("Wrong KEy");
		    	exit;
			}
	}
}