public function forwardMessages($timeout = 5)
 {
     $this->_logger->log(__METHOD__);
     foreach ($this->_forwardingClients as $client_id => $client) {
         $criteria = new CDbCriteria();
         $criteria->with = array('messageTemp');
         $criteria->compare('client_id', $client_id);
         $criteria->compare('status', 'new');
         $criteria->order = "message_id asc";
         $criteria->limit = 100;
         $messages = ForwardedMessage::model()->findAll($criteria);
         $this->_logger->log(__METHOD__ . ' Found messages', array('client_id' => $client_id, 'count' => count($messages)));
         //            $this->_logger->log(__METHOD__.print_r($messages,1));
         if (count($messages) > 0) {
             if (!$client->connect($timeout)) {
                 $this->_logger->log(__METHOD__ . ' Can`t connect. Skip client', array('client_id' => $client_id));
                 continue;
             }
             $this->_logger->log(__METHOD__ . print_r($messages, 1));
             foreach ($messages as $message) {
                 if (!$client->sendMessage($message->messageTemp->message . "\r\n", $timeout)) {
                     $this->_logger->log(__METHOD__, array('errors' => $client->errors()));
                     break;
                 } else {
                     $message->status = 'sent';
                     if (!$message->save(false)) {
                         $this->_logger->log(__METHOD__ . ' Message was not saved', array('message' => $message->mesage_id));
                     }
                 }
             }
             $client->disconnect();
         }
     }
 }
 /**
  * Start process
  * @param array $arg
  * @return int|void
  * @this_server Synchronization
  * @messagesToSend ForwardedMessage
  * @TcpSocketClientConnector TcpSocketClientConnector
  */
 public function run($arg)
 {
     $this_server = new Synchronization();
     $messagesToSend = ForwardedMessage::model()->getNewMessages();
     foreach ($messagesToSend as $message) {
         if (!is_object($message)) {
             continue;
         }
         $TcpSocketClientConnector = new TcpSocketClientConnector($this->_logger, 'tcp', $this_server->remote_server_ip, $this_server->remote_server_port);
         $TcpSocketClientConnector->connect($timeout = 5);
         $message = serialize(array('message' => $message->message->message));
         $TcpSocketClientConnector->sendMessage($message, $timeout = 5);
         //            $inputDataFromServer =  $TcpSocketClientConnector->readDataFromServer();
         //            $serverData = unserialize($inputDataFromServer);
         $TcpSocketClientConnector->disconnect();
     }
     //        $this->_logger->log(__METHOD__ .' inputDataFromServer: '. $inputDataFromServer);
     //        $this->_logger->log(__METHOD__ .' serverData: '.print_r( $serverData,1));
 }
 public function actionSetup()
 {
     $criteria = new CDbCriteria();
     $criteria->condition = "ord > 0";
     $criteria->order = "ord ASC";
     $meas_types = RefbookMeasurementType::model()->findAll($criteria);
     if ($meas_types) {
         foreach ($meas_types as $key => $value) {
             $sql = "SELECT `t1`.`metric_id`, CONCAT(`t2`.`html_code`, ' (', `t2`.`full_name`, ')') AS `name`, `t1`.`is_main`, `t1`.`measurement_type_metric_id`\n                        FROM `" . RefbookMeasurementTypeMetric::model()->tableName() . "` `t1`\n                        LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t2` ON `t2`.`metric_id` = `t1`.`metric_id`\n                        WHERE `t1`.`measurement_type_id` = '" . $value->measurement_type_id . "'";
             $meas_types[$key]->metrics_list = Yii::app()->db->createCommand($sql)->queryAll();
         }
     }
     if (Yii::app()->request->isPostRequest && isset($_POST['main_metric'])) {
         foreach ($_POST['main_metric'] as $key => $value) {
             if ($meas_types[$key]->metrics_list) {
                 foreach ($meas_types[$key]->metrics_list as $v1) {
                     $update = array('is_main' => $v1['metric_id'] == $value ? 1 : 0);
                     RefbookMeasurementTypeMetric::model()->updateByPk($v1['measurement_type_metric_id'], $update);
                 }
             }
         }
         StationSensorFeature::updateMetric();
         $DB = array('db' => CStubActiveRecord::getDbConnect(), 'db_long' => CStubActiveRecord::getDbConnect(true));
         foreach ($DB as $db) {
             $db->createCommand("DELETE FROM `" . ScheduleReportProcessed::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . ForwardedMessage::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . StationCalculationData::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SeaLevelTrend::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SensorDataMinute::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . SensorData::model()->tableName() . "`")->query();
             $db->createCommand("DELETE FROM `" . ListenerLog::model()->tableName() . "`")->query();
         }
         It::memStatus('admin_metrics_saved');
         $this->redirect($this->createUrl('admin/setup'));
     }
     $this->render('setup', array('meas_types' => $meas_types));
 }