Ejemplo 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;
     }
 }
 public function __construct($logger, $protocol, $address, $port)
 {
     parent::__construct($logger);
     $this->_logger->log(__METHOD__);
     $this->_protocol = $protocol;
     $this->_address = $address;
     $this->_port = $port;
 }
Ejemplo n.º 3
0
 protected function grabModemMessages($cycle)
 {
     $this->_logger->log(__CLASS__ . ' ' . __METHOD__, array('cycle' => $cycle));
     $output = null;
     if ($this->_connector->readData($output) === false) {
     }
     $this->returnResult(__CLASS__ . ' ' . __METHOD__, $output);
 }
Ejemplo n.º 4
0
 protected function listen()
 {
     $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' Start listen.', array('source' => $this->source));
     $messages = null;
     $process = $this;
     $logger = $process->_logger;
     $this->_connector->onReceiveMessage = function ($message, $station_id, $source_info) use(&$process, &$logger) {
         $logger->log(__CLASS__ . ' ' . __METHOD__ . ' New message', array('message' => $message, 'listener_id' => $process->listener->listener_id, 'overwrite' => $process->settings->overwrite_data_on_listening));
         $messageId = ListenerLog::addNew($message, $process->listener->listener_id, $process->settings->overwrite_data_on_listening, 'server', 0, $source_info);
         ListenerProcess::addComment($process->listener->listener_id, 'comment', 'got msg #' . $messageId);
     };
     $this->_connector->readData($messages);
     $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' Complete listen.', array('source' => $this->source));
 }
Ejemplo n.º 5
0
 public function readData(&$output)
 {
     parent::readData($output);
     $xml = $this->loadXml();
     if (is_null($xml)) {
         $this->errors[] = 'Can\'t load xml file: ' . $path;
         return false;
     }
     if ($this->validate($xml) === false) {
         $this->errors[] = 'Validation failed.';
         return false;
     }
     $this->process($xml);
     return true;
 }
Ejemplo n.º 6
0
 protected function listenDLTorrent($cycle = 1)
 {
     $this->_logger->log(__METHOD__, array('cycle' => $cycle));
     $this->_connector->setParams(array('timeout' => 60));
     ListenerProcess::addComment($this->listener->listener_id, 'comment', 'Opening COM connection');
     $messages = null;
     $process = $this;
     $logger = $process->_logger;
     $this->_connector->onReceiveMessage = function ($message) use(&$process, &$logger) {
         $logger->log(__METHOD__ . ' New message', array('message' => $message, 'listener_id' => $process->listener->listener_id, 'overwrite' => $process->settings->overwrite_data_on_listening));
         $messageId = ListenerLog::addNew($message, $process->listener->listener_id, $process->settings->overwrite_data_on_listening, 'datalogger');
         ListenerProcess::addComment($process->listener->listener_id, 'comment', 'got msg #' . $messageId);
     };
     $result = $this->_connector->readData($messages);
     $this->_logger->log(__METHOD__ . ' Complete listen datalogger.', array('cycle' => $cycle, 'result' => $result));
 }
Ejemplo n.º 7
0
 protected function listenPollingTemp($cycle = 1)
 {
     $this->_logger->log(__CLASS__ . ' ' . __METHOD__, array('cycle' => $cycle));
     $this->_connector->setParams(array('timeout' => 60));
     // 1min
     ListenerProcess::addComment($this->listener->listener_id, 'comment', 'Start polling');
     $messages = null;
     $process = $this;
     $logger = $process->_logger;
     $this->_connector->onReceiveMessage = function ($message, $stationId) use(&$process, &$logger) {
         $logger->log(__CLASS__ . ' ' . __METHOD__ . ' New message', array('message' => $message, 'listener_id' => $process->listener->listener_id, 'overwrite' => $process->settings->overwrite_data_on_listening));
         $messageId = ListenerLogTemp::addNew($message, $process->listener->listener_id, $process->settings->overwrite_data_on_listening, 'poller', $stationId);
         ListenerProcess::addComment($process->listener->listener_id, 'comment', 'got msg #' . $messageId);
     };
     $result = $this->_connector->readData($messages);
     $this->_logger->log(__CLASS__ . ' ' . __METHOD__ . ' Complete listen datalogger.', array('cycle' => $cycle, 'result' => $result));
     return $result;
 }
 public function readData(&$output)
 {
     parent::readData($output);
     $this->_logger->log(__METHOD__);
     $master = array();
     // !!! PROTOCOL (TCP by default) SHOULD BE IN LOWER CASE !!!
     $uri = $this->_protocol . '://' . $this->_address . ':' . $this->_port;
     $this->_logger->log(__METHOD__ . ' Trying to start server', array('uri' => $uri));
     $server = @stream_socket_server($uri, $errno, $errstr);
     //        stream_set_blocking($server,0);
     $this->_logger->log(__METHOD__ . ' Server start result', array('socket' => $server));
     if ($server === false) {
         $this->_logger->log(__METHOD__ . ' Server start error', array('error' => $errstr, 'number' => $errno));
         $this->_errors[] = $errstr . '(' . $errno . ')';
     } else {
         $master[] = $server;
         $idleTime = 0;
         while (true) {
             $read = $master;
             $this->_logger->log(__METHOD__ . ' Waiting for event.');
             $except = null;
             $write = null;
             $startTime = time();
             // If timeout is null then it will wait forever.
             // Use null in case when reset SMS message is not needed.
             $modCount = @stream_select($read, $write, $except, 2000);
             //                $idleTime += time() - $startTime;
             $this->_logger->log(__METHOD__ . ' Waiting result', array('count' => $modCount));
             //
             //
             if ($modCount === false) {
                 $this->_errors[] = 'Error during waiting for event.';
                 continue;
             }
             foreach ($read as $readStream) {
                 if ($readStream === $server) {
                     $this->_logger->log(__METHOD__ . ' Client connected');
                     // New client connected. Add to list.
                     $client = @stream_socket_accept($server);
                     $master[] = $client;
                 } else {
                     // Client did something
                     $socketData = $this->readStringFromSocket($readStream);
                     $this->_logger->log(__METHOD__ . ' Received data', array('data' => $socketData));
                     if ($socketData === false) {
                         // Error
                         $index = array_search($readStream, $master, true);
                         unset($master[$index]);
                     } else {
                         if (strlen($socketData) === 0) {
                             $this->_logger->log(__METHOD__ . ' Client disconnected');
                             // Client disconnected
                             $index = array_search($readStream, $master, true);
                             @fclose($readStream);
                             unset($master[$index]);
                         } else {
                             // Client sent some data
                             $this->_logger->log(__METHOD__ . ' Received message', array('message' => $socketData));
                             $output = preg_split("/\r\n/", $socketData, -1, PREG_SPLIT_NO_EMPTY);
                             foreach ($output as $message) {
                                 $this->_logger->log(__METHOD__ . ' $readStream1 ' . print_r($readStream, 1));
                                 $m = $this->onReceiveDataMessageEvent($message);
                                 //                                    $this->_logger->log(__METHOD__. ' $readStream2 '.print_r($readStream,1));
                                 $this->writeStringToSocket($readStream, $m);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
 public function readData(&$output)
 {
     parent::readData($output);
 }
Ejemplo n.º 10
0
 /**
  * Opens COM port and tries to read messages.
  * @param array $output
  * @return boolean True if there was no errors, false - otherwise.
  */
 public function readData(&$output)
 {
     parent::readData($output);
     $this->_logger->log(__METHOD__);
     set_error_handler(array($this, 'errorHandler'));
     try {
         if ($this->_serial->deviceSet($this->_port) === true) {
             if (!is_null($this->_baudrate)) {
                 $this->_serial->confBaudRate($this->_baudrate);
             }
             if (!is_null($this->_flowControl)) {
                 $this->_serial->confFlowControl($this->_flowControl);
             }
             if (!is_null($this->_parity)) {
                 $this->_serial->confParity($this->_parity);
             }
             if (!is_null($this->_stopBits)) {
                 $this->_serial->confStopBits($this->_stopBits);
             }
             if (!is_null($this->_dataBits)) {
                 $this->_serial->confCharacterLength($this->_dataBits);
             }
             if ($this->_serial->deviceOpen('r+b') === true) {
                 $this->readCustomData($output);
                 $this->_serial->deviceClose();
             } else {
                 $this->_errors[] = 'Can\'t open port "' . $this->_port . '".';
             }
         } else {
             $this->_errors[] = 'Can\'t set port "' . $this->_port . '".';
         }
     } catch (Exception $ex) {
         if ($this->_serial->_dState === PhpSerial::SERIAL_DEVICE_OPENED) {
             $this->_serial->deviceClose();
         }
         $this->_errors[] = $ex->getMessage();
     }
     restore_error_handler();
     $result = count($this->_errors) === 0;
     return $this->returnResult(__METHOD__, $result);
 }
Ejemplo n.º 11
0
 public function readData(&$output)
 {
     parent::readData($output);
     $this->_logger->log(__METHOD__);
     return $this->pollDataFromStation($this->_station, $this->_timeout);
 }