<?php   // скрипт выполнения асинхронной команды к терминалу
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_services_esmo_terminal extends c_page_TXT
{
   public $rec_terminal ;

   function show(&$options=array())
    {   //return ;
        //
        $get_terminal_options=[]; $send_command_options=[];
        if ($_GET['device']) $get_terminal_options['device']=$_GET['device'];
	    ob_start()  ;
	    $terminal_id=(int)$_GET['terminal_id'] ;
	    $this->rec_terminal=DEVICE()->get_terminal_by_pkey($_GET['terminal_id'],$get_terminal_options) ;
	    if (!$this->rec_terminal['pkey']) $result=array('comment'=>'Терминал с ID='.$terminal_id.' не найден','status'=>'terminal not found') ;
        if (!$this->rec_terminal['enabled']) $result=array('comment'=>'Терминал с ID='.$terminal_id.' заблокирован','status'=>'terminal is block') ;
	    $terminal_pkey=(int)$this->rec_terminal['pkey'] ;
	    if ($terminal_pkey and $this->rec_terminal['enabled'])
	    {   if ($_GET['sleep']) sleep($_GET['sleep']) ;
        $__times['start']=get_formatted_microtime() ;
        switch($_GET['cmd'])
	        {  // кодманды для турникета
          case 'turn_open_in':        $this->turn_open_in()  ;         break ;
          case 'turn_open_out':        $this->turn_open_out()  ;         break ;
	          // команды для терминала транслируем как есть
	          default:
                  if ($_GET['device']) $send_command_options['device']=1;
                  $send_command_options['no_check_cur_status']=1; $send_command_options['async']=0;
	              $result=DEVICE()->send_terminal_command($terminal_pkey,$_GET['cmd'],$send_command_options)  ;  ;
        } ;
        $__times['end']=get_formatted_microtime() ;
        $time_int=$__times['end']-$__times['start'] ;
	    }
	    if ($result['comment']) echo $result['comment'] ;
        $text=ob_get_clean() ;
        $reffers=array() ;
        if ($this->rec_terminal) $reffers[]=$this->rec_terminal['_reffer'] ;
        if ($GLOBALS['LS_term_logging_full']// and $text
        ) LOGS()->reg_log('Отправка команды терминалу (async)',$text,array('data'=>$_GET,'_terminal_id'=>$_GET['terminal_id'],'time_exec'=>$time_int,'reffers'=>$reffers)) ;
        echo 'ok' ;
  }

  //[21.12.15, 12:43:46] Sergey Yakimov:  шлем controller: 2, direction 0 - откроет вход на контроллере с адресом 2
  //[21.12.15, 12:44:00] Sergey Yakimov: direction 1 - откроет выход
  function turn_open_in() {$this->turn_open('in');}
  function turn_open_out() {$this->turn_open('out');}


  function turn_open($mode)
  {  $this->rec_terminal=SKD()->get_device_by_id($_GET['terminal_id']) ;
     $this->rec_terminal['port']='9639' ;
     // комнада терминала
     $var = array();
     $var['date'] = date('Y-m-d H:i:s');
     $var['args']['terminal_type']='mt' ;
     $var['args']['id']['controller']=($mode=='out')? $this->rec_terminal['_controller_out_id']:$this->rec_terminal['_controller_in_id'] ;
     if ($this->rec_terminal['in_out_reverse']) $var['args']['id']['direction']=($mode=='out')? 0:1  ; //($mode='out')? $this->rec_terminal['_controller_out_id']:$this->rec_terminal['_controller_out_id'] ;
     else                                 $var['args']['id']['direction']=($mode=='out')? 1:0  ;


     //$var['args']['id']['access_type']=1 ;
     // комнада терминала
     ENGINE()->terminal_send_data($this->rec_terminal,'open',$var) ;
     //
     LOGS()->reg_log('Открываем турникет:',$_GET['terminal_id'].': '.$mode,array('data'=>$this->rec_terminal,'terminal'=>$this->rec_terminal)) ;
     //$this->add_element('show_notife','Первевол терминала в сон: '.$this->rec_terminal['id'],array('color'=>'yellow')) ;

  }

}
?>