protected function setCalculation()
 {
     $criteria = new CDbCriteria();
     $criteria->index = 'handler_id';
     $this->calculations = CalculationDBHandler::model()->with('metric')->findAll($criteria);
 }
 public function generate()
 {
     $this->_logger->log(__METHOD__);
     if ($this->errors) {
         $this->_logger->log(__METHOD__, array('errors' => $this->errors));
         return false;
     }
     $current_user_timezone = date_default_timezone_get();
     $timezone_id = 'UTC';
     if ($timezone_id != $current_user_timezone) {
         TimezoneWork::set($timezone_id);
     }
     $this->report_parts = array();
     $this->explanations = array();
     // get sensors' values for all messages received in reporting period
     $sql = "SELECT `t5`.`listener_log_id`,\r\n                       `t5`.`measuring_timestamp`,\r\n                       `t1`.`station_sensor_id`, `t1`.`sensor_id_code`, \r\n                       \r\n                       `t3`.`feature_code`, `t3`.`feature_constant_value`,\r\n                       `t4`.`code` AS `metric_code`, \r\n                       `t5`.`sensor_feature_value`, \r\n                       `t5`.`is_m`,\r\n                       `t5`.`period` AS `sensor_feature_period`,\r\n                       `t6`.`code` AS `value_metric_code`,\r\n                       `t7`.`handler_id_code`\r\n\r\n                FROM `" . SensorData::model()->tableName() . "`  `t5`\r\n                LEFT JOIN `" . StationSensor::model()->tableName() . "`        `t1` ON t1.station_sensor_id = t5.sensor_id\r\n                LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON (`t3`.`sensor_feature_id` = `t5`.`sensor_feature_id`)\r\n                LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t4` ON `t4`.`metric_id` = `t3`.`metric_id`\r\n                LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t6` ON `t6`.`metric_id` = `t5`.`metric_id`\r\n                LEFT JOIN `" . SensorDBHandler::model()->tableName() . "`      `t7` ON t7.handler_id = t1.handler_id\r\n                WHERE `t5`.`station_id` = '" . $this->station_info->station_id . "' AND `t5`.`listener_log_id` IN (" . $this->schedule_process_info->listener_log_ids . ")\r\n                ORDER BY `t5`.`measuring_timestamp` DESC, `t1`.`sensor_id_code` ASC, `t3`.`feature_code` ASC";
     $sensor_data = Yii::app()->db->createCommand($sql)->queryAll();
     $data = array();
     if ($sensor_data) {
         // get calculation values for all messages received in reporting period
         $sql = "SELECT `t1`.`listener_log_id`,\r\n                           `t1`.`value`,\r\n                           `t3`.`handler_id_code`\r\n                    FROM `" . StationCalculationData::model()->tableName() . "`    `t1`\r\n                    LEFT JOIN `" . StationCalculation::model()->tableName() . "`   `t2` ON t2.calculation_id = t1.calculation_id\r\n                    LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` `t3` ON `t3`.`handler_id` = `t2`.`handler_id`\r\n                    WHERE `t2`.`station_id` = '" . $this->station_info->station_id . "' AND `t1`.`listener_log_id` IN (" . $this->schedule_process_info->listener_log_ids . ")\r\n                    ORDER BY `t3`.`handler_id_code`";
         $res2 = Yii::app()->db->createCommand($sql)->queryAll();
         if ($res2) {
             foreach ($res2 as $key => $value) {
                 $calculations[$value['listener_log_id']][] = $value;
             }
         }
         foreach ($sensor_data as $key => $value) {
             $data[$value['listener_log_id']][] = $value;
         }
         // prepare $result_item array, where each line represents line in report.
         foreach ($data as $key => $value) {
             $result_item = array('StationId', $this->station_info->station_id_code, 'WMO AWS #', $this->station_info->wmo_block_number . $this->station_info->station_number, 'National AWS #', $this->station_info->national_aws_number, 'Tx DateTime', date('m/d/Y H:i', strtotime($value[0]['measuring_timestamp'])));
             foreach ($value as $key2 => $value2) {
                 $handler_obj = SensorHandler::create($value2['handler_id_code']);
                 if (in_array($value2['handler_id_code'], array('BatteryVoltage', 'Humidity', 'Pressure', 'Temperature'))) {
                     $sensor_id_code = $value2['sensor_id_code'];
                 } else {
                     $sensor_id_code = $value2['sensor_id_code'] . ' (' . $handler_obj->getFeatureName($value2['feature_code']) . ')';
                 }
                 $result_item[] = $sensor_id_code;
                 if ($value2['is_m']) {
                     $result_item[] = '-';
                 } else {
                     $value2['sensor_feature_value'] = $handler_obj->applyOffset($value2['sensor_feature_value'], $this->station_info->magnetic_north_offset);
                     $result_item[] = str_replace(',', ' ', $handler_obj->formatValue($value2['sensor_feature_value'], $value2['feature_code']));
                 }
             }
             if (isset($calculations[$key])) {
                 foreach ($calculations[$key] as $key2 => $value2) {
                     if ($value2['handler_id_code'] === 'DewPoint') {
                         $result_item[] = 'DP';
                     } else {
                         if ($value2['handler_id_code'] === 'PressureSeaLevel') {
                             $result_item[] = 'MSL';
                         } else {
                             $result_item[] = 'Unknown calculation';
                         }
                     }
                     $result_item[] = str_replace(',', ' ', number_format(round($value2['value'], 1), 1));
                 }
             }
             $this->report_parts[] = $result_item;
         }
     }
     if ($timezone_id != $current_user_timezone) {
         TimezoneWork::set($current_user_timezone);
     }
     $this->_logger->log(__METHOD__ . ' Export generation completed.');
     return true;
 }
Beispiel #3
0
 protected function addCalculations($file_config)
 {
     $station_id = $file_config['station']['station_id'];
     $this->stations[$station_id];
     foreach ($file_config['station_calculation'] as $calc) {
         $calc_main_handler_db = CalculationDBHandler::model()->findByAttributes(array('handler_id_code' => $calc['handler']));
         $calculation_db = new StationCalculation();
         $calculation_db->station_id = $station_id;
         $calculation_db->handler_id = $calc_main_handler_db->handler_id;
         $calculation_db->formula = $calc['formula'] ? $calc['formula'] : 'default';
         $calculation_db->save(false);
         foreach ($this->sensors_for_calcs[$calc['handler']][$station_id] as $sensor) {
             $calc_variables = new StationCalculationVariable();
             $station_sensor_features = $sensor->features();
             foreach ($station_sensor_features as $s_feature) {
                 if (is_object($s_feature)) {
                     if ($s_feature->is_main) {
                         $calc_variables->sensor_feature_id = $s_feature->sensor_feature_id;
                         $calc_variables->variable_name = $s_feature->feature_code;
                         $calc_variables->calculation_id = $calculation_db->getPrimaryKey();
                         $calc_variables->save(false);
                     }
                 }
             }
         }
     }
 }
 public function getUsedSensors($station_id)
 {
     $sql = "SELECT t5.sensor_id_code\n                FROM `" . StationCalculationVariable::model()->tableName() . "` `t1`\n                LEFT JOIN `" . StationCalculation::model()->tableName() . "` t2 ON t2.calculation_id = t1.calculation_id\n                LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` t3 ON t3.handler_id = t2.handler_id\n                LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` t4 ON t4.sensor_feature_id = t1.sensor_feature_id\n                LEFT JOIN `" . StationSensor::model()->tableName() . "` t5 ON t5.station_sensor_id = t4.sensor_id\n                WHERE `t3`.handler_id_code = ? AND t2.station_id = ? \n                ORDER BY t5.sensor_id_code";
     $res = Yii::app()->db->createCommand($sql)->queryColumn(array($this->handler_id_code, $station_id));
     return $res;
 }
 public function _prepareCalculationsInfo($listener_log_id)
 {
     $this->_logger->log(__METHOD__);
     $calculations = array();
     if (!is_null($this->station_info)) {
         $sql = "SELECT `t1`.`value`, `t3`.`handler_id_code`\r\n                    FROM `" . StationCalculationData::model()->tableName() . "`    `t1`\r\n                    LEFT JOIN `" . StationCalculation::model()->tableName() . "`   `t2` ON `t2`.`calculation_id` = `t1`.`calculation_id`\r\n                    LEFT JOIN `" . CalculationDBHandler::model()->tableName() . "` `t3` ON `t3`.`handler_id` = `t2`.`handler_id`\r\n                    WHERE `t1`.`listener_log_id` = '" . $listener_log_id . "'";
         $calculations_info = Yii::app()->db->createCommand($sql)->queryAll();
         if ($calculations_info) {
             foreach ($calculations_info as $value) {
                 if (!isset($calculations[$value['handler_id_code']])) {
                     $calculations[$value['handler_id_code']] = $value;
                 }
             }
         }
     }
     return $calculations;
 }
 public static function getHandlers()
 {
     return CalculationDBHandler::model()->findAll(array('order' => 'display_name asc'));
 }
 public function actionAwsPanel()
 {
     $tableCount = isset($_GET['tableCount']) ? $_GET['tableCount'] : 2;
     $tableSize = isset($_GET['tableSize']) ? $_GET['tableSize'] : 10;
     $pages = new CPagination();
     $pages->pageSize = $tableCount * $tableSize;
     //group station
     $stationGroup = StationGroup::getGroupName();
     $selectStationGroupIds = StationGroup::getStationArrFromGroup($_GET['group_id']);
     //data for view
     $stations = $handlers = $handlersCalc = $sensorData = $handlerGroup = $stationGroupView = array();
     $stationsId = Station::stationFromGroup($stations, $selectStationGroupIds, $pages);
     if (count($stations)) {
         $lastLogsId = ListenerLog::lastMsgIds($stationsId, $stations);
         if ($lastLogsId) {
             SensorHandler::getGroupAwsPanel($handlerGroup);
             $handlersId = SensorDBHandler::handlerWithFeature($handlers, 'aws_panel');
             SensorData::addSensorsData($sensorData, $lastLogsId, $handlers);
             $handlersCalcId = CalculationDBHandler::handlerWithFeatureAndMetric($handlersCalc);
             StationCalculationData::addCalculationData($sensorData, $lastLogsId, $handlersCalcId);
             // sensor data
             if (isset($sensorData['handlers'])) {
                 foreach ($sensorData['handlers'] as $handler_id => &$handler) {
                     SensorHandler::setGroupAwsPanel($handlerGroup, $handlers[$handler_id]->handler_id_code, $handler_id, 'handlers');
                     foreach ($handler['code'] as &$code) {
                         SensorHandler::getDataForAwsPanel($code, $handlers[$handler_id], $stations);
                     }
                 }
             }
             //calculationData
             if (isset($sensorData['handlersCalc'])) {
                 foreach ($sensorData['handlersCalc'] as $handler_id => &$handler) {
                     SensorHandler::setGroupAwsPanel($handlerGroup, $handlersCalc[$handler_id]->handler_id_code, $handler_id, 'handlersCalc');
                     foreach ($handler['stations'] as $station_id => &$station) {
                         CalculationHandler::getDataForAwsPanel($station, $stations[$station_id]->lastMessage->log_id, $handlersCalc[$handler_id]->handler_id_code);
                     }
                 }
             }
         }
         //station groups view
         $stationGroupView = array_chunk($stationsId, $tableSize);
     }
     $render_data = array('stations' => $stations, 'handlers' => $handlers, 'handlersCalc' => $handlersCalc, 'sensorData' => $sensorData, 'handlerGroup' => $handlerGroup, 'stationGroup' => $stationGroupView);
     if (Yii::app()->request->isAjaxRequest) {
         $this->renderPartial('aws_panel', array('render_data' => $render_data), false, true);
     } else {
         $this->render('autorefresh_aws_panel', array('render_data' => $render_data, 'template' => 'aws_panel', 'pages' => $pages, 'stationGroup' => $stationGroup));
     }
 }
Beispiel #8
0
 public function actionCalculationSave()
 {
     $station_id = intval($_REQUEST['station_id']);
     if (!$station_id) {
         $this->redirect($this->createUrl('admin/Stations'));
     }
     $handler_id = intval($_REQUEST['handler_id']);
     if (!$handler_id) {
         $this->redirect($this->createUrl('admin/Sensors', array('station_id' => $station_id)));
     }
     $station = Station::model()->findByPk($station_id);
     $handler_db = CalculationDBHandler::model()->findByPk($handler_id);
     if (!$station || !$handler_db) {
         $this->redirect($this->createUrl('admin/Sensors', array('station_id' => $station_id)));
     }
     $handler = CalculationHandler::create($handler_db->handler_id_code);
     $measurements = $handler->getMeasurements();
     $formulas = $handler->getFormulas();
     $calculation_db = StationCalculation::model()->find('handler_id = :handler_id AND station_id = :station_id', array(':handler_id' => $handler_db->handler_id, ':station_id' => $station->station_id));
     if (!$calculation_db) {
         $calculation_db = new StationCalculation();
         $calculation_db->station_id = $station->station_id;
         $calculation_db->handler_id = $handler_db->handler_id;
         $calculation_db->formula = $formulas ? $formulas[0] : 'default';
     }
     if ($calculation_db->calculation_id) {
         foreach ($measurements as $key => $value) {
             $measurements[$key]['object'] = StationCalculationVariable::model()->find('calculation_id = :calculation_id AND variable_name = :variable_name', array(':calculation_id' => $calculation_db->calculation_id, ':variable_name' => $value['variable_name']));
         }
     }
     foreach ($measurements as $key => $value) {
         if (!$measurements[$key]['object']->calculation_variable_id) {
             $measurements[$key]['object'] = new StationCalculationVariable();
             $measurements[$key]['object']->variable_name = $value['variable_name'];
         }
     }
     if ($measurements) {
         $sql = "SELECT t2.sensor_id_code, t1.sensor_feature_id\n                    FROM `" . StationSensorFeature::model()->tableName() . "` t1\n                    LEFT JOIN `" . StationSensor::model()->tableName() . "` t2 ON t2.station_sensor_id = t1.sensor_id\n                    WHERE t2.station_id = ? AND  t1.feature_code = ?\n                    ORDER BY t2.sensor_id_code";
         foreach ($measurements as $key => $value) {
             $measurements[$key]['sensors'] = Yii::app()->db->createCommand($sql)->queryAll(true, array($station->station_id, $value['variable_name']));
         }
     }
     $formulas_act = array();
     if ($formulas) {
         foreach ($formulas as $key => $value) {
             $formulas_act[$value] = $value;
         }
     }
     $validated = true;
     if (Yii::app()->request->isPostRequest) {
         $calculation_db->attributes = $_POST['StationCalculation'];
         foreach ($measurements as $key => $value) {
             $measurements[$key]['object']->sensor_feature_id = $_POST['StationCalculationVariable'][$key]['sensor_feature_id'];
             if ($measurements[$key]['required'] == 1 && !$measurements[$key]['object']->sensor_feature_id) {
                 $measurements[$key]['object']->addError('sensor_feature_id', $measurements[$key]['display_name'] . ' is required.');
                 $validated = false;
             }
             if ($measurements[$key]['required'] == 0 && $_POST['StationCalculationVariable'][$key]['sensor_feature_id'] == 0) {
                 if (is_object($measurements[$key]['object']) && !$measurements[$key]['object']->isNewRecord) {
                     $measurements[$key]['object']->delete();
                 }
                 unset($measurements[$key]);
             }
         }
         $validated = $validated & $calculation_db->validate();
         if ($validated) {
             $calculation_db->save();
             foreach ($measurements as $key => $value) {
                 $measurements[$key]['object']->calculation_id = $calculation_db->calculation_id;
                 $measurements[$key]['object']->save();
             }
             It::memStatus('admin_station_calculation_saved');
             $this->redirect($this->createUrl('admin/sensors', array('station_id' => $station->station_id)));
         }
     }
     $this->render('station_calculation', array('station' => $station, 'handler_db' => $handler_db, 'measurements' => $measurements, 'formulas' => $formulas_act, 'calculation_db' => $calculation_db));
 }