示例#1
0
 /**
  * AWS and AWOS stations can have calculations (dewPoint, MSL)
  * Run calculations basing on already parsed data
  * 
  * @return type 
  */
 public function runCalculations()
 {
     $this->_logger->log(__METHOD__);
     if (!in_array($this->_station->station_type, array('aws', 'awos'))) {
         return false;
     }
     // get calculations for station ID defined from arrived message
     $records = StationCalculation::getStationCalculationHandlers(array($this->_station->station_id));
     $handlers = array_key_exists($this->_station->station_id, $records) ? $records[$this->_station->station_id] : null;
     if (is_array($handlers)) {
         foreach ($handlers as $value) {
             // create object of calculation handler
             $handler = CalculationHandler::create($value->handler->handler_id_code);
             // run calculation for station basing on message's ID
             $handler->calculate($this->_station, $this->message_obj->log_id);
         }
     }
 }
示例#2
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));
 }
示例#3
0
 public function actionAwsSingle()
 {
     $criteria = new CDbCriteria();
     $criteria->compare('station_type', array('aws', 'awos'));
     $criteria->order = 'station_id_code asc';
     $stations = Station::model()->findAll($criteria);
     if (count($stations) > 0) {
         $session = Yii::app()->session;
         $id = isset($_REQUEST['station_id']) ? intval($_REQUEST['station_id']) : (isset($session['single_aws']['station_id']) ? $session['single_aws']['station_id'] : '');
         $station = null;
         if ($id) {
             foreach ($stations as $st) {
                 if ($id == $st->station_id) {
                     $station = $st;
                     break;
                 }
             }
         }
         $station = is_null($station) ? $stations[0] : $station;
         $log_id = isset($_REQUEST['log_id']) ? intval($_REQUEST['log_id']) : null;
         $last2Messages = ListenerLog::getAllLast2Messages(array($station->station_id), $log_id);
         if (array_key_exists($station->station_id, $last2Messages) && count($last2Messages[$station->station_id]) > 0) {
             $station->lastMessage = date('m/d/Y H:i', strtotime($last2Messages[$station->station_id][0]->measuring_timestamp));
             $next_expected = strtotime($last2Messages[$station->station_id][0]->measuring_timestamp) + $station->event_message_period * 60 + 300;
             $station->nextMessageExpected = date('m/d/Y H:i', $next_expected);
             if ($next_expected < time()) {
                 $station->nextMessageIsLates = 1;
             }
         } else {
             $station->lastMessage = 'Unknown';
         }
         if (count($last2Messages[$station->station_id]) > 1) {
             $station->previousMessage = date('m/d/Y H:i', strtotime($last2Messages[$station->station_id][1]->measuring_timestamp));
         } else {
             $station->previousMessage = 'Unknown';
         }
         $list = StationSensor::getSensorsForAWSDisplay(array($station->station_id), 'aws_single');
         $sensors = isset($list[$station->station_id]) ? $list[$station->station_id] : null;
         $handler_sensor = array();
         if (!is_null($sensors)) {
             $sensorPairs = array();
             foreach ($sensors as $sensor) {
                 $sensorPairs[$sensor->handler->handler_id_code][] = array('station_id' => $station->station_id, 'sensor_id' => $sensor->station_sensor_id, 'last_logs' => isset($last2Messages[$station->station_id]) ? $last2Messages[$station->station_id] : array(), 'aws_single_group' => $sensor->handler->aws_single_group);
             }
             $handlersDefault = array();
             SensorDBHandler::handlerWithFeature($handlersDefault, 'single');
             $sensorList = SensorHandler::getFullSensorList(array($station->station_id), $handlersDefault);
             $sensorData = SensorData::getSensorData($last2Messages, $sensorList);
             foreach ($sensorPairs as $handler_id_code => $data) {
                 $handler_obj = SensorHandler::create($handler_id_code);
                 $res = $handler_obj->getInfoForAwsPanel($data, $sensorList, $sensorData, 'single');
                 if ($res) {
                     foreach ($res as $value_sensors) {
                         foreach ($value_sensors as $value_sensor_data) {
                             if ($handler_id_code === 'WindDirection') {
                                 if ($session['single_aws']['chosen_wind_direction'] == 2) {
                                     $chosen_direction = '10minute_average';
                                 } else {
                                     if ($session['single_aws']['chosen_wind_direction'] == 1) {
                                         $chosen_direction = '2minute_average';
                                     } else {
                                         $chosen_direction = 'last';
                                     }
                                 }
                                 $value_sensor_data['chosen_wind_direction'] = $chosen_direction;
                                 $radians = ($value_sensor_data[$chosen_direction] - 90) / 180 * M_PI;
                                 //$value_sensor_data['chosen_wind_coordinates']['x'] = round(86.5+ cos($radians)*70);
                                 //$value_sensor_data['chosen_wind_coordinates']['y'] = round(86.5+ sin($radians)*70);
                                 $value_sensor_data['chosen_wind_coordinates']['x'] = round(83 + cos($radians) * 70);
                                 $value_sensor_data['chosen_wind_coordinates']['y'] = round(83 + sin($radians) * 70);
                             }
                             $value_sensor_data['handler_id_code'] = $handler_id_code;
                             $handler_sensor[$data[0]['aws_single_group']][$handler_id_code][] = $value_sensor_data;
                         }
                     }
                 }
             }
         }
         $handlers = StationCalculation::getStationCalculationHandlers(array($station->station_id));
         $calculation = array();
         if (array_key_exists($station->station_id, $handlers)) {
             foreach ($handlers[$station->station_id] as $value) {
                 $handler = CalculationHandler::create($value->handler->handler_id_code);
                 $used_sensors = $handler->getUsedSensors($station->station_id);
                 if ($used_sensors) {
                     foreach ($handler_sensor as $group => &$v2) {
                         foreach ($v2 as $handler_id_code => &$value_sensor_data) {
                             if ($value->handler->handler_id_code == 'DewPoint' && in_array($handler_id_code, array('Temperature', 'TemperatureSoil', 'TemperatureWater')) || $value->handler->handler_id_code == 'PressureSeaLevel' && in_array($handler_id_code, array('Pressure'))) {
                                 foreach ($value_sensor_data as $k4 => &$v4) {
                                     if (in_array($v4['sensor_id_code'], $used_sensors)) {
                                         $calculation[$group][$value->handler->handler_id_code] = 1;
                                         $v4[$value->handler->handler_id_code] = $handler->getCalculatedValue($station->station_id, $last2Messages[$station->station_id]);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $session['single_aws'] = array('station_id' => $station->station_id, 'chosen_wind_direction' => $session['single_aws']['chosen_wind_direction']);
     }
     $speciReport = null;
     if (count($last2Messages[$station->station_id]) > 0) {
         $criteria = new CDbCriteria();
         $criteria->with = array('ScheduleReportToStation.schedule_report');
         $criteria->compare('report_type', 'speci');
         $criteria->compare('ScheduleReportToStation.station_id', $station->station_id);
         //			$criteria->compare('is_last', 1);
         $criteria->compare('listener_log_id', $last2Messages[$station->station_id][0]->log_id);
         //			$criteria->order = 't.updated desc';
         $criteria->order = 't.created desc';
         $criteria->limit = 1;
         $speciReport = ScheduleReportProcessed::model()->find($criteria);
         if (is_null($speciReport)) {
             $reportRecord = ScheduleReport::model()->findByAttributes(array('report_type' => 'speci', 'station_id' => $station->station_id));
             if (!is_null($reportRecord)) {
                 $speciReport = new ScheduleReportProcessed();
                 $speciReport->schedule_id = $reportRecord->schedule_id;
                 $speciReport->listener_log_id = $last2Messages[$station->station_id][0]->log_id;
                 $speciReport->is_processed = 0;
                 $speciReport->check_period_start = $last2Messages[$station->station_id][0]->measuring_timestamp;
                 $speciReport->check_period_end = $last2Messages[$station->station_id][0]->measuring_timestamp;
                 $speciReport->save();
             }
         }
     }
     //        echo "<pre>";
     //        print_r($handler_sensor);
     //        echo "</pre>";exit;
     $template = 'aws_single';
     $render_data = array('stations' => $stations, 'station' => $station, 'last_logs' => $last2Messages[$station->station_id], 'handler_sensor' => $handler_sensor, 'calculation' => $calculation, 'speciReport' => $speciReport);
     if (Yii::app()->request->isAjaxRequest) {
         $this->renderPartial($template, array('render_data' => $render_data));
     } else {
         $this->render('autorefresh_aws_single', array('render_data' => $render_data, 'template' => $template));
     }
 }
 public function m_0_4_15()
 {
     @apache_setenv('no-gzip', 1);
     @ini_set('zlib.output_compression', 0);
     @ini_set('implicit_flush', 1);
     ini_set('memory_limit', '-1');
     $this->flushNotification("\n...Please wait... Updating is going on... DO NOT LEAVE THIS PAGE!");
     $this->flushNotification("<br/><br/>...Update fields types!");
     $sql = "ALTER TABLE `station_calculation_data` CHANGE `value` `value` DECIMAL( 15, 4 ) NOT NULL";
     Yii::app()->db->createCommand($sql)->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Done!");
     $this->flushNotification("<br/><br/>...Update fields types!");
     $sql = "ALTER TABLE `sensor_data` CHANGE `sensor_feature_value` `sensor_feature_value` DECIMAL( 15, 4 ) NOT NULL";
     Yii::app()->db->createCommand($sql)->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Done!");
     $this->flushNotification("<br/><br/>...Update fields types!");
     $sql = "ALTER TABLE `sensor_data` CHANGE `sensor_feature_normalized_value` `sensor_feature_normalized_value` DECIMAL( 15, 4 ) NOT NULL";
     Yii::app()->db->createCommand($sql)->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Done!");
     $sql = "SELECT t2.sensor_data_id, `t1`.`calculation_data_id`, `t1`.`listener_log_id`, `t1`.`value`\n                FROM `" . StationCalculationData::model()->tableName() . "` `t1`\n                LEFT JOIN `" . SensorData::model()->tableName() . "` `t2` ON `t2`.`listener_log_id` = `t1`.`listener_log_id`\n                where    t2.sensor_data_id is null \n                ";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         $total = count($res);
         for ($i = 0; $i < $total; $i++) {
             if (!$res[$i]['sensor_data_id']) {
                 StationCalculationData::model()->deleteByPk($res[$i]['calculation_data_id']);
             }
         }
     }
     $sql = "SELECT `t1`.`station_id`, t1.handler_id\n                FROM `" . StationCalculation::model()->tableName() . "` `t1`\n                LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON t2.station_id = t1.station_id";
     $res_stations = Yii::app()->db->createCommand($sql)->queryAll();
     $count = count($res_stations);
     $stations = array();
     if (is_array($res_stations) && $count) {
         $station_ids = array();
         for ($i = 0; $i < $count; $i++) {
             if (!$stations[$res_stations[$i]['station_id']]) {
                 $stations[$res_stations[$i]['station_id']]['station_object'] = Station::model()->findByPk($res_stations[$i]['station_id']);
                 $stations[$res_stations[$i]['station_id']]['last_logs'] = ListenerLog::getLastMessages($res_stations[$i]['station_id']);
             }
             $handler_id = $res_stations[$i]['handler_id'] == 1 ? 'DewPoint' : 'PressureSeaLevel';
             $handler = CalculationHandler::create($handler_id);
             if ($stations[$res_stations[$i]['station_id']]['last_logs'][0]['log_id']) {
                 $handler->calculate($stations[$res_stations[$i]['station_id']]['station_object'], $stations[$res_stations[$i]['station_id']]['last_logs'][0]['log_id']);
             }
             if ($stations[$res_stations[$i]['station_id']]['last_logs'][1]['log_id']) {
                 $handler->calculate($stations[$res_stations[$i]['station_id']]['station_object'], $stations[$res_stations[$i]['station_id']]['last_logs'][1]['log_id']);
             }
         }
     }
     It::memStatus('update__success');
     $this->flushNotification('<script type="text/javascript"> setTimeout(function(){document.location.href="' . Yii::app()->controller->createUrl('update/index') . '"}, 5000)</script>');
 }