Exemplo n.º 1
2
 public function init()
 {
     if (preg_match('/COM[0-9]+/', $this->source)) {
         $this->_logger->log(__METHOD__ . ' Check Serial port');
         // else it is COM port connection: it can be DataLogger or GSM modem
         ListenerProcess::addComment($this->listener->listener_id, 'hardware_recognizing', SMSCOMPort::getLinuxComName($this->source) . ' is Serial port.');
         try {
             // try to send AT command
             // if script recieves "OK" - this is GSM modem. Else - it can be only DataLogger
             $serial = new PhpSerial($this->_logger);
             $this->_logger->log(__METHOD__ . $this->source);
             $serial->deviceSet($this->source);
             $serial->confFlowControl(Yii::app()->params['com_connect_params']['hardwareflowcontrol']);
             $serial->confBaudRate(Yii::app()->params['com_connect_params']['baudrate']);
             $serial->confParity(Yii::app()->params['com_connect_params']['parity']);
             $serial->confStopBits(Yii::app()->params['com_connect_params']['stopbits']);
             $serial->confCharacterLength(Yii::app()->params['com_connect_params']['databits']);
             $this->_connector = new GsmModemSerialConnector($this->_logger, $serial);
             $this->_connector->setParams(array('port' => $this->source));
             ListenerProcess::addComment($this->listener->listener_id, 'hardware_recognizing', 'Script started to recognize hardware connected to PC via Serial port');
             if ($this->listener->additional_param == 'SMS') {
                 while (1) {
                     if ($this->_connector->check()) {
                         $this->_logger->log(__METHOD__, array('hardware' => 'modem'));
                         $this->hardware = 'modem';
                         ListenerProcess::addComment($this->listener->listener_id, 'hardware_recognizing', 'Hardware connected to PC via Serial port - is GSM Modem');
                         $this->_logger->log(__METHOD__, array('hardware' => $this->hardware));
                         return true;
                     }
                     sleep(60);
                 }
             }
             $this->_connector = new DataLoggerSerialConnector($this->_logger, $serial);
             $this->_connector->setParams(array('port' => $this->source));
             if ($this->listener->additional_param == 'DIRECT') {
                 if ($this->_connector->check()) {
                     $this->_logger->log(__METHOD__, array('hardware' => 'datalogger'));
                     $this->hardware = 'dl';
                     ListenerProcess::addComment($this->listener->listener_id, 'hardware_recognizing', 'Hardware connected to PC via Serial port - is Datalogger');
                     $this->_logger->log(__METHOD__, array('hardware' => $this->hardware));
                     return true;
                 }
             }
         } catch (Exception $e) {
             $this->_logger->log(__METHOD__, array('ExceptionMessage' => $e->getMessage()));
         }
         $this->_logger->log(__METHOD__, array('hardware' => 'unknown'));
         $this->hardware = 'unknown';
         ListenerProcess::addComment($this->listener->listener_id, 'hardware_recognizing', 'No device found.');
         $this->_logger->log(__METHOD__, array('hardware' => $this->hardware));
         return false;
     }
 }