public static function getInfoForHandler($sensor_id)
 {
     $sql = "SELECT `t1`.*, `t2`.`code` AS `metric_code`, `t5`.`code` AS `general_metric_code`\n                FROM `" . StationSensorFeature::model()->tableName() . "` `t1`\n                LEFT JOIN `" . RefbookMetric::model()->tableName() . "`                `t2` ON `t2`.`metric_id` = `t1`.`metric_id`\n                LEFT JOIN `" . RefbookMeasurementType::model()->tableName() . "`       `t3` ON `t3`.`code` = `t1`.`measurement_type_code`\n                LEFT JOIN `" . RefbookMeasurementTypeMetric::model()->tableName() . "` `t4` ON `t4`.`measurement_type_id` = `t3`.`measurement_type_id` AND `t4`.`is_main` = 1\n                LEFT JOIN `" . RefbookMetric::model()->tableName() . "`                `t5` ON `t5`.`metric_id` = `t4`.`metric_id`\n                WHERE `t1`.`sensor_id` = ?";
     $arr = Yii::app()->db->createCommand($sql)->queryAll(true, array($sensor_id));
     foreach ($arr as $val) {
         $arr_key[] = $val['feature_code'];
     }
     return array_combine($arr_key, $arr);
 }
 public static function getMetrics($measurement_type_code)
 {
     $return = array();
     $sql = "SELECT `t1`.`metric_id`, `t3`.`html_code`, `t3`.`full_name`\n                FROM `" . RefbookMeasurementTypeMetric::model()->tableName() . "` `t1`\n                LEFT JOIN `" . RefbookMeasurementType::model()->tableName() . "`  `t2` ON `t2`.`measurement_type_id` = `t1`.`measurement_type_id`\n                LEFT JOIN `" . RefbookMetric::model()->tableName() . "`           `t3` ON `t3`.`metric_id` = `t1`.`metric_id`\n                WHERE `t2`.`code` = '" . $measurement_type_code . "'    \n                ORDER BY `t3`.`full_name`";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         foreach ($res as $key => $value) {
             $return[$value['metric_id']] = $value['html_code'] . ' (' . $value['full_name'] . ')';
         }
     }
     return $return;
 }
Exemplo n.º 3
0
 /**
  * List sensor feature are grouped
  *
  * @return array|null $this->group_sensor_features
  */
 public function getGroupSensorsFeaturesList()
 {
     if (!$this->group_sensor_features) {
         $handlers = SensorDBHandler::getHandlers('aws');
         $rs_data = array();
         if ($handlers) {
             $measurement_codes = array();
             foreach ($handlers as $handler) {
                 $sensor_features = SensorHandler::create($handler->handler_id_code)->getAwsGraphFeatures();
                 foreach ($sensor_features as $v) {
                     if ($v['measurement_type_code']) {
                         $measurement_codes[] = $v['measurement_type_code'];
                     }
                 }
                 // get stations
                 $stations = array();
                 $station_ids = array();
                 $cssClass = "";
                 if (count($handler->sensors)) {
                     foreach ($handler->sensors as $sensor) {
                         $station = array();
                         $station['station_id_code'] = $sensor->station->station_id_code;
                         $station['station_id'] = $sensor->station->station_id;
                         $station['color'] = $sensor->station->color;
                         $stations[$station['station_id']] = $station;
                         $station_ids[$station['station_id']] = $station['station_id'];
                     }
                     $stations = array_values($stations);
                     $station_ids = array_values($station_ids);
                     $cssClass = implode('-station ', $station_ids);
                     $cssClass .= '-station ';
                     array_multisort($stations, SORT_STRING, $stations);
                 }
                 $this->group_sensor_features[$handler->handler_id_code] = ['name' => $handler->display_name, 'sensor_features' => $sensor_features, 'stations' => $stations, 'class' => $cssClass];
             }
             // Calculates
             $this->group_sensor_features['Temperature']['sensor_features']['calc_1'] = ['feature_name' => 'Dew Point, C degree'];
             $this->group_sensor_features['Pressure']['sensor_features']['calc_2'] = ['feature_name' => 'Pressure MSL, hPa'];
             // Measurement
             $sql = "SELECT `t3`.code,  `t5`.`short_name`,  `t5`.`full_name`\n                    FROM `" . RefbookMeasurementType::model()->tableName() . "`       `t3`\n                    LEFT JOIN `" . RefbookMeasurementTypeMetric::model()->tableName() . "` `t4` ON `t4`.`measurement_type_id` = `t3`.`measurement_type_id` AND `t4`.`is_main` = 1\n                    LEFT JOIN `" . RefbookMetric::model()->tableName() . "`                `t5` ON `t5`.`metric_id` = `t4`.`metric_id`\n                    WHERE `t3`.`code` IN ('" . implode("','", $measurement_codes) . "')";
             $rs = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll();
             if ($rs) {
                 $rs_data = CHtml::listData($rs, 'code', 'short_name');
             }
             $wd_ws_station_params['stations'] = array();
             $wd_ws_station_params['class'] = '';
             foreach ($this->group_sensor_features as $handler_id_code => &$group) {
                 if (in_array($handler_id_code, ['WindDirection', 'WindSpeed']) && in_array('WindSpeed', array_keys($this->group_sensor_features)) && in_array('WindDirection', array_keys($this->group_sensor_features))) {
                     $station_ids_temp = array();
                     foreach ($wd_ws_station_params['stations'] as $wd_ws_station) {
                         $station_ids_temp[] = $wd_ws_station['station_id'];
                     }
                     foreach ($group['stations'] as $station) {
                         if (!in_array($station['station_id'], $station_ids_temp)) {
                             $wd_ws_station_params['stations'][] = $station;
                         }
                     }
                     $station_ids = array();
                     foreach ($wd_ws_station_params['stations'] as $wd_ws_station) {
                         $station_ids[] = $wd_ws_station['station_id'];
                     }
                     $wd_ws_station_params['class'] = implode('-station ', $station_ids) . '-station ';
                 }
                 if (in_array($handler_id_code, ['TemperatureWater', 'TemperatureSoil'])) {
                     foreach ($group['sensor_features'] as $key => $value) {
                         $group['sensor_features'][$key] = $group['name'] . ($rs_data[$value['measurement_type_code']] ? ', ' . $rs_data[$value['measurement_type_code']] : '');
                     }
                 } else {
                     foreach ($group['sensor_features'] as $key => $value) {
                         $group['sensor_features'][$key] = $value['feature_name'] . ($rs_data[$value['measurement_type_code']] ? ', ' . $rs_data[$value['measurement_type_code']] : '');
                     }
                 }
             }
         }
         /**
          * Load custom sensor feature */
         if (count($this->custom_sensor_features)) {
             $this->group_sensor_features['custom'] = ['name' => 'Custom', 'sensor_features' => $this->custom_sensor_features, 'stations' => $wd_ws_station_params['stations'], 'class' => $wd_ws_station_params['class']];
         }
     }
     return (array) $this->group_sensor_features;
 }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 public function prepareFormulaParams()
 {
     $sql = "SELECT `t4`.`sensor_data_id`, `t1`.`sensor_feature_id`, `t4`.`metric_id`, `t4`.`sensor_feature_value`, `t5`.`code` AS `metric_code`, `t4`.`is_m`\n                FROM `" . StationCalculationVariable::model()->tableName() . "` `t1`\n                JOIN `" . StationSensorFeature::model()->tableName() . "`       `t3` ON `t3`.`sensor_feature_id` = `t1`.`sensor_feature_id`\n                JOIN `" . SensorData::model()->tableName() . "`                 `t4` ON (`t4`.`sensor_feature_id` = `t1`.`sensor_feature_id` AND `t4`.`listener_log_id` = ? AND `t4`.`is_m` = '0')\n                JOIN `" . RefbookMetric::model()->tableName() . "`              `t5` ON `t5`.`metric_id` = `t4`.`metric_id`\n                WHERE `t1`.`calculation_id` = ? AND `t1`.`variable_name` = ?";
     $measurements = $this->getMeasurements();
     if ($measurements) {
         foreach ($measurements as $key => $value) {
             $res = Yii::app()->db->createCommand($sql)->queryRow(true, array($this->log_id, $this->calculation_details['calculation_id'], $value['variable_name']));
             if ($res) {
                 $this->formula_params[$value['variable_name']] = It::convertMetric($res['sensor_feature_value'], $res['metric_code'], $value['metric']);
             }
         }
     }
     return true;
 }
Exemplo n.º 6
0
 public function getRainMetric()
 {
     if ($this->station_id) {
         $sql = "SELECT `t3`.`html_code`\n                    FROM `" . StationSensorFeature::model()->tableName() . "` `t1`\n                    LEFT JOIN `" . StationSensor::model()->tableName() . "` `t2` ON `t2`.`station_sensor_id` = `t1`.`sensor_id`\n                    LEFT JOIN `" . RefbookMetric::model()->tableName() . "` `t3` ON `t3`.`metric_id` = `t1`.`metric_id`\n                    WHERE `t2`.`station_id` = '" . $this->station_id . "' AND `t1`.`feature_code` = 'rain'\n                    ORDER BY `t2`.`sensor_id_code`";
         return CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryScalar();
     }
     return 'mm';
 }
Exemplo n.º 7
0
 public function prepareSensorsInfo($listener_log_id)
 {
     $this->_logger->log(__METHOD__);
     $this->_logger->log(__METHOD__ . " listener_log_id " . $listener_log_id);
     $this->_logger->log(__METHOD__ . " this->schedule_process_info->ScheduleReportToStation->realStation->station_id " . $this->schedule_process_info->ScheduleReportToStation->realStation->station_id);
     $sensors = array();
     if (!is_null($this->schedule_process_info->ScheduleReportToStation->realStation->station_id)) {
         // get sensors
         $sql = "SELECT `t1`.`station_sensor_id`, `t1`.`sensor_id_code`, \r\n                           `t2`.`handler_id_code`, \r\n                           `t3`.`feature_code`, `t3`.`feature_constant_value`,\r\n                           `t3`.`sensor_feature_id`,\r\n                           `t4`.`code` AS `metric_code`, \r\n                           `t5`.`sensor_feature_value`, \r\n                           `t5`.`period` AS `sensor_feature_period`,\r\n                           `t6`.`code` AS `value_metric_code`,\r\n                           `t5`.`is_m`\r\n                    FROM `" . StationSensor::model()->tableName() . "`             `t1`\r\n                    LEFT JOIN `" . SensorDBHandler::model()->tableName() . "`      `t2` ON `t2`.`handler_id` = `t1`.`handler_id`\r\n                    LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON (`t3`.`sensor_id` = `t1`.`station_sensor_id`)\r\n                    LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t4` ON `t4`.`metric_id` = `t3`.`metric_id`\r\n                    LEFT JOIN `" . SensorData::model()->tableName() . "`           `t5` ON (`t5`.`sensor_feature_id` = `t3`.`sensor_feature_id` AND `t5`.`listener_log_id` = '" . $listener_log_id . "')\r\n                    LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t6` ON `t6`.`metric_id` = `t5`.`metric_id`\r\n                    WHERE `t1`.`station_id` = '" . $this->schedule_process_info->ScheduleReportToStation->realStation->station_id . "'\r\n                    ORDER BY `t1`.`sensor_id_code` ASC";
         $sensors_info = Yii::app()->db->createCommand($sql)->queryAll();
         if (!$sensors_info) {
             $this->errors[] = 'Station has no sensors';
             return;
         }
         $sensors = array();
         foreach ($sensors_info as $value) {
             if ($value['feature_code'] === 'height' && $value['handler_id_code'] === 'Pressure') {
                 $sensors['pressure_height']['height'] = $value['feature_constant_value'];
                 $sensors['pressure_height']['height_metric_code'] = $value['metric_code'];
                 continue;
             }
             $value['metric_code'] = isset($value['value_metric_code']) ? $value['value_metric_code'] : $value['metric_code'];
             unset($value['value_metric_code']);
             if (!isset($sensors[$value['feature_code']])) {
                 $sensors[$value['feature_code']] = $value;
             }
         }
     }
     return $sensors;
 }
Exemplo n.º 8
0
 public function actionRgPanel()
 {
     $stations = Station::getList('rain');
     $handlers = array();
     SensorDBHandler::handlerWithFeature($handlers, 'rg');
     $features = array_shift($handlers)->features;
     if ($stations) {
         foreach ($stations as $key => &$station) {
             $station['sensor_details'] = array('last_msg' => '-', 'amount' => '-', 'period' => '-', 'rate' => '-', '1hr_total' => '-', 'batt_volt' => '-', '24hr_total' => '-');
             $period = $station['event_message_period'];
             if ($period == 5) {
                 $use_field = '5min_sum';
             } else {
                 if ($period == 10) {
                     $use_field = '10min_sum';
                 } else {
                     if ($period == 20) {
                         $use_field = '20min_sum';
                     } else {
                         if ($period == 30) {
                             $use_field = '30min_sum';
                         } else {
                             $use_field = '60min_sum';
                         }
                     }
                 }
             }
             $station['filter_limit_max'] = round($features['rain']->filter_max / 60 * $period, 2);
             $station['filter_limit_min'] = round($features['rain']->filter_min / 60 * $period, 2);
             $station['filter_limit_diff'] = round($features['rain']->filter_diff / 60 * $period, 2);
             $sql_groupped_table = "SELECT `station_id`, MAX(`measuring_timestamp`) AS `MaxDateTime`\n                                       FROM `" . SensorDataMinute::model()->tableName() . "`\n                                       WHERE `{$use_field}` > 0 AND `station_id` = '" . $station['station_id'] . "' AND `is_tmp` = 0";
             $sql = "SELECT `tt`.*, `t2`.`html_code` AS `metric_html_code`\n                        FROM `" . SensorDataMinute::model()->tableName() . "` `tt`\n                        INNER JOIN `" . RefbookMetric::model()->tableName() . "` t2 ON t2.metric_id = tt.metric_id\n                        INNER JOIN ( {$sql_groupped_table} ) `groupedtt` ON `tt`.`station_id` = `groupedtt`.`station_id` AND `tt`.`measuring_timestamp` = `groupedtt`.`MaxDateTime`";
             $res = Yii::app()->db->createCommand($sql)->queryRow();
             $last_logs = ListenerLog::getLast2Messages($station['station_id']);
             if ($res) {
                 $station['sensor_details'] = array('sensor_data_id' => $res['sensor_data_id'], 'last_msg' => date('Y-m-d H:i', strtotime($res['measuring_timestamp'])), 'amount' => $res[$use_field] * $res['bucket_size'], 'period' => $period, 'rate' => $res[$use_field] * (60 / $period) * $res['bucket_size'], '1hr_total' => $res['60min_sum'] * $res['bucket_size'], 'batt_volt' => $res['battery_voltage'] / 10, '24hr_total' => $res['1day_sum'] * $res['bucket_size'], 'metric' => $res['metric_html_code']);
                 if (count($last_logs) > 0) {
                     $station['last_tx'] = date('m/d/Y H:i', strtotime($last_logs[0]['measuring_timestamp']));
                     $next_expected = strtotime($last_logs[0]['measuring_timestamp']) + $period + 300;
                     $station['sensor_details']['next_expected'] = date('m/d/Y H:i', $next_expected);
                     if ($next_expected < time()) {
                         $station['sensor_details']['next_lates'] = 1;
                     }
                 }
                 if ($station['filter_limit_max'] > 0) {
                     if ($station['sensor_details']['amount'] >= $station['filter_limit_max']) {
                         $station['filter_errors'][] = "R >= " . $station['filter_limit_max'];
                     }
                 }
                 if ($station['filter_limit_min'] > 0) {
                     if ($station['sensor_details']['amount'] <= $station['filter_limit_min']) {
                         $station['filter_errors'][] = "R <= " . $station['filter_limit_min'];
                     }
                 }
             }
         }
     }
     $template = 'index';
     $render_data = array('stations' => $stations);
     if (Yii::app()->request->isAjaxRequest) {
         $this->renderPartial($template, array('render_data' => $render_data));
     } else {
         $this->render('autorefresh', array('render_data' => $render_data, 'template' => $template));
     }
 }
Exemplo n.º 9
0
 public function actionMsgGeneration()
 {
     ini_set('memory_limit', '-1');
     $form = new GenerateMessageForm();
     $messages = array();
     $sensors = array();
     if (Yii::app()->request->isPostRequest && (isset($_POST['generate']) || isset($_POST['import']))) {
         $form->attributes = $_POST['GenerateMessageForm'];
         if ($form->validate()) {
             $sensors = array();
             if ($form->sensor_id) {
                 $sql = "SELECT `t1`.`station_sensor_id`, `t1`.`sensor_id_code`, `t2`.`handler_id_code`, `t3`.`feature_code`, `t4`.`code` AS `metric_code` \n                            FROM `" . StationSensor::model()->tableName() . "` `t1`\n                            LEFT JOIN `" . SensorDBHandler::model()->tableName() . "`      `t2` ON `t2`.`handler_id` = `t1`.`handler_id`\n                            LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t3` ON `t3`.`sensor_id`  = `t1`.`station_sensor_id`\n                            LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t4` ON `t4`.`metric_id`  = `t3`.`metric_id`\n                            WHERE `t1`.`station_id` = '" . $form->station_id . "' AND `t1`.`station_sensor_id` IN (" . implode(',', $form->sensor_id) . ")";
                 $res = Yii::app()->db->createCommand($sql)->queryAll();
                 if ($res) {
                     foreach ($res as $key => $value) {
                         if (!isset($sensors[$value['station_sensor_id']])) {
                             $sensors[$value['station_sensor_id']] = array('station_sensor_id' => $value['station_sensor_id'], 'sensor_id_code' => $value['sensor_id_code'], 'handler_id_code' => $value['handler_id_code']);
                         }
                         $sensors[$value['station_sensor_id']]['features'][$value['feature_code']] = $value['metric_code'];
                     }
                 }
             }
             $i = $form->start_timestamp;
             while ($i <= $form->end_timestamp) {
                 $messages[$i]['timestamp'] = $i;
                 $i = $i + $form->interval * 60;
             }
             foreach ($messages as $key => $value) {
                 if ($form->choosed_station['station_type'] === 'rain') {
                     $messages[$key]['parts'][] = 'D';
                     $messages[$key]['parts'][] = $form->choosed_station['station_id_code'];
                     $messages[$key]['parts'][] = date('ymd', $key);
                     $messages[$key]['parts'][] = date('Hi', $key);
                     $messages[$key]['parts'][] = str_pad(rand(100, 135), 3, "0", STR_PAD_LEFT);
                     $messages[$key]['parts'][] = '00';
                 } else {
                     $messages[$key]['parts'][] = 'D';
                     $messages[$key]['parts'][] = $form->choosed_station['station_id_code'];
                     $messages[$key]['parts'][] = date('ymd', $key);
                     $messages[$key]['parts'][] = date('Hi', $key);
                     $messages[$key]['parts'][] = '00';
                 }
                 $sensors_values = array();
                 if ($sensors) {
                     foreach ($sensors as $k1 => $v1) {
                         $handler = SensorHandler::create($v1['handler_id_code']);
                         $random_value = $handler->getRandomValue($v1['features']);
                         $sensors_values[] = $v1['sensor_id_code'] . $random_value;
                     }
                     shuffle($sensors_values);
                     foreach ($sensors_values as $k1 => $v1) {
                         $messages[$key]['parts'][] = $v1;
                     }
                 }
                 $crc = It::prepareCRC(implode('', $messages[$key]['parts']));
                 $messages[$key]['parts'][] = $crc;
                 array_push($messages[$key]['parts'], '$');
                 array_unshift($messages[$key]['parts'], '@');
             }
         }
     }
     $messages_display = array();
     $messages_copy = array();
     foreach ($messages as $key => $value) {
         $messages_display[] = implode(' ', $value['parts']);
         $messages_copy[] = implode('', $value['parts']);
     }
     $station_sensors = StationSensor::getList($form->station_id);
     if ($station_sensors) {
         foreach ($station_sensors as $key => $value) {
             $station_sensors[$key]['checked'] = 1;
         }
     }
     if (isset($_POST['GenerateMessageForm']['sensor_id'])) {
         if ($station_sensors) {
             foreach ($station_sensors as $key => $value) {
                 $station_sensors[$key]['checked'] = in_array($value['station_sensor_id'], $_POST['GenerateMessageForm']['sensor_id']) ? 1 : 0;
             }
         }
     }
     $this->render('msg_generation', array('form' => $form, 'messages_display' => $messages_display, 'messages_copy' => $messages_copy, 'station_sensors' => $station_sensors));
 }
Exemplo n.º 10
0
 public function process($path)
 {
     if (!file_exists($path)) {
         throw new Exception('Can\'t find file ' . $path);
     }
     $pathinfo = pathinfo($path);
     $base_filename = $pathinfo['basename'];
     $xml_content = file_get_contents($path);
     if (!strlen($xml_content)) {
         throw new Exception($base_filename . " is empty");
     }
     libxml_use_internal_errors(true);
     $sxe = simplexml_load_string($xml_content);
     $error_str = "";
     if ($sxe === false) {
         foreach (libxml_get_errors() as $error) {
             $error_str .= "\n" . $error->message;
         }
         throw new Exception($error_str);
     }
     // XML must contain 1 RUNWAY tag
     if (count($sxe->RUNWAY) > 1) {
         throw new Exception('XML ' . $base_filename . ' contains ' . count($sxe->RUNWAY) . ' RUNWAY tags');
     }
     if (count($sxe->RUNWAY) == 0) {
         throw new Exception('XML ' . $base_filename . ' doesn\'t contain RUNWAY tags');
     }
     // RUNWAY's "NAME" attribute must be "08/26"
     if ($sxe->RUNWAY['NAME'] != '08/26') {
         throw new Exception('XML ' . $base_filename . ' RUNWAY name = "' . $sxe->RUNWAY['NAME'] . '", "08/26" was expected');
     }
     // RUNWAY must contain at least 1 ZONE tag
     if (count($sxe->RUNWAY->ZONE) == 0) {
         throw new Exception('XML ' . $base_filename . ' doesn\'t contain ZONE tags');
     }
     // XML must contain "UNITS" tag
     if (!isset($sxe->UNITS)) {
         throw new Exception('XML ' . $base_filename . ' doesn\'t contain UNITS section');
     }
     $str = '';
     $possible_units = array('WIND' => 'kt', 'VISBILITY' => 'meters', 'RVR' => 'meters', 'ALTIMETER' => 'hpa');
     foreach ($possible_units as $key => $value) {
         if (!isset($sxe->UNITS->{$key})) {
             $str .= ($str ? '; ' : '') . ' UNITS[' . $key . '] is missed';
         } else {
             if ($sxe->UNITS->{$key} != $value) {
                 $str .= ($str ? '; ' : '') . ' unknown metric "' . $sxe->UNITS->{$key} . '" in UNITS[' . $key . ']';
             }
         }
     }
     if ($str) {
         throw new Exception($str);
     }
     $result = array();
     $messages = array();
     for ($key = 0; $key < count($sxe->RUNWAY->ZONE); $key++) {
         // Get's Station ID
         if ($sxe->RUNWAY->ZONE[$key]['NAME'] == '08') {
             $messages[$key]['station_id_code'] = 'AWS08';
         } else {
             if ($sxe->RUNWAY->ZONE[$key]['NAME'] == '26') {
                 $messages[$key]['station_id_code'] = 'AWS26';
             } else {
                 continue;
             }
         }
         //Gets sensor's data from tags:
         // WIND SPEED
         if (isset($sxe->RUNWAY->ZONE[$key]->WSPD_5SEC)) {
             $messages[$key]['sensors']['WindSpeed'][0]['wind_speed_1'] = (string) $sxe->RUNWAY->ZONE[$key]->WSPD_5SEC;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->WSPD_2MIN)) {
             $messages[$key]['sensors']['WindSpeed'][0]['wind_speed_2'] = (string) $sxe->RUNWAY->ZONE[$key]->WSPD_2MIN;
         }
         // WIND DIRECTION
         if (isset($sxe->RUNWAY->ZONE[$key]->WDIR_5SEC)) {
             $messages[$key]['sensors']['WindDirection'][0]['wind_direction_1'] = (string) $sxe->RUNWAY->ZONE[$key]->WDIR_5SEC;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->WDIR_2MIN)) {
             $messages[$key]['sensors']['WindDirection'][0]['wind_direction_2'] = (string) $sxe->RUNWAY->ZONE[$key]->WDIR_2MIN;
         }
         // TEMPERATURE
         if (isset($sxe->RUNWAY->ZONE[$key]->TEMP_5MIN)) {
             $messages[$key]['sensors']['Temperature'][0]['temperature'] = (string) $sxe->RUNWAY->ZONE[$key]->TEMP_5MIN;
         }
         // HUMIDITY
         if (isset($sxe->RUNWAY->ZONE[$key]->HUM_5MIN)) {
             $messages[$key]['sensors']['Humidity'][0]['humidity'] = (string) $sxe->RUNWAY->ZONE[$key]->HUM_5MIN;
         }
         // PRESSURE
         if (isset($sxe->RUNWAY->ZONE[$key]->PRESSURE1)) {
             $messages[$key]['sensors']['Pressure'][0]['pressure'] = (string) $sxe->RUNWAY->ZONE[$key]->PRESSURE1;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->PRESSURE2)) {
             $messages[$key]['sensors']['Pressure'][1]['pressure'] = (string) $sxe->RUNWAY->ZONE[$key]->PRESSURE2;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->PRESSURE3)) {
             $messages[$key]['sensors']['Pressure'][2]['pressure'] = (string) $sxe->RUNWAY->ZONE[$key]->PRESSURE3;
         }
         // CLOUD
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDRANGE)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_measuring_range'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDRANGE;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDVV)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_vertical_visibility'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDVV;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDH1)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_height_1'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDH1;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDH2)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_height_2'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDH2;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDH3)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_height_3'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDH3;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDD1)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_depth_1'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDD1;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDD2)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_depth_2'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDD2;
         }
         if (isset($sxe->RUNWAY->ZONE[$key]->CLOUDD3)) {
             $messages[$key]['sensors']['CloudHeightAWS'][0]['cloud_height_depth_3'] = (string) $sxe->RUNWAY->ZONE[$key]->CLOUDD3;
         }
         // VISIBILITY
         $vis = (string) $sxe->RUNWAY->ZONE[$key]->EXC;
         if (isset($sxe->RUNWAY->ZONE[$key]->EXC) && is_numeric($vis) && $vis != 0) {
             //P = (1/σ) x ln (1/0.05)
             //where ln is the log to base e or the natural logarithm. σ  is the extinction cooefficient.
             //This number will be in km, so we will need to multiply by 1000.
             $messages[$key]['sensors']['VisibilityAWS'][0]['visibility_1'] = 1 / $vis * log(20) * 1000;
         }
         // SOLAR
         if (isset($sxe->RUNWAY->ZONE[$key]->SOLAR_1MIN)) {
             $messages[$key]['sensors']['SolarRadiation'][0]['solar_radiation_in_period'] = (string) $sxe->RUNWAY->ZONE[$key]->SOLAR_1MIN;
         }
         // Rain fall
         if (isset($sxe->RUNWAY->ZONE[$key]->PRECIP_ACCUM)) {
             $messages[$key]['sensors']['RainAws'][0]['period'] = 5;
             $messages[$key]['sensors']['RainAws'][0]['rain_in_period'] = (string) $sxe->RUNWAY->ZONE[$key]->PRECIP_ACCUM;
         }
         // Sunshine duration
         if (isset($sxe->RUNWAY->ZONE[$key]->SUN_ACCUM)) {
             $messages[$key]['sensors']['SunshineDuration'][0]['period'] = 5;
             $messages[$key]['sensors']['SunshineDuration'][0]['sun_duration_in_period'] = (string) $sxe->RUNWAY->ZONE[$key]->SUN_ACCUM;
         }
     }
     if (!$messages) {
         $result[] = $base_filename . " : No datasets found";
         return implode("\n", $result);
     }
     $result[] = $base_filename . " : " . count($messages) . ' datasets were found';
     $sql = "SELECT * \n                FROM `" . Station::model()->tableName() . "` `t1`\n                WHERE `t1`.`station_id_code` IN ('AWS08', 'AWS26')";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if (!$res) {
         $result[] = "AWS08 and AWS26 are not exist in database, no sense to convert XML into messages";
         return implode("\n", $result);
     }
     // for each stationID looks for sensors and features of this station
     $stations = array();
     foreach ($res as $key => $value) {
         $stations[$value['station_id_code']] = $value;
         $sql = "SELECT `t1`.`sensor_id_code`,\n                           `t1`.`station_id`,\n                           `t2`.`feature_code`,\n                           `t3`.`code` AS `metric_code`,\n                           `t4`.`handler_id_code`\n                    FROM `" . StationSensor::model()->tableName() . "` `t1`\n                    LEFT JOIN `" . StationSensorFeature::model()->tableName() . "` `t2` ON `t1`.`station_sensor_id` = `t2`.`sensor_id`\n                    LEFT JOIN `" . RefbookMetric::model()->tableName() . "`        `t3` ON `t3`.`metric_id` = `t2`.`metric_id`\n                    LEFT JOIN `" . SensorDBHandler::model()->tableName() . "`      `t4` ON `t4`.`handler_id` = `t1`.`handler_id`\n                    WHERE `t1`.`station_id` = '" . $value['station_id'] . "'\n                    ORDER BY `t4`.`handler_id_code` ASC, `t1`.`sensor_id_code` ASC";
         $res2 = Yii::app()->db->createCommand($sql)->queryAll();
         if ($res2) {
             $tmp = array();
             foreach ($res2 as $value2) {
                 $tmp[$value2['handler_id_code']][$value2['sensor_id_code']][$value2['feature_code']] = $value2['metric_code'];
             }
             foreach ($tmp as $key_handler => $value_sensors) {
                 foreach ($value_sensors as $key_sensor => $value_features) {
                     $stations[$value['station_id_code']]['sensors'][$key_handler][] = array('sensor_id_code' => $key_sensor, 'features' => $value_features);
                 }
             }
         }
     }
     $date_parsed = date_parse_from_format("D, j M Y H:i:s", $sxe->DATE);
     $date_prepared = mktime($date_parsed['hour'], $date_parsed['minute'], $date_parsed['second'], $date_parsed['month'], $date_parsed['day'], $date_parsed['year']);
     // convert parsed XML data into regular message
     // for this kind of messages we have an agreement to put X instead of D at the beginning of message.
     foreach ($messages as $key => $value) {
         if (!$stations[$value['station_id_code']]) {
             $result[] = $value['station_id_code'] . " station is not exists in database, no sense to convert RNWY part into message";
             continue;
         }
         $result_message_body = 'X' . $value['station_id_code'];
         $result_message_body .= date('ymdHi', $date_prepared);
         $result_message_body .= '00';
         // we need last_log for this satation to calculate period of measurement (some sensors' strings should contain this period
         $last_logs = ListenerLog::getLast2Messages($stations[$value['station_id_code']]['station_id']);
         if (isset($value['sensors'])) {
             foreach ($value['sensors'] as $key_handler => $value_sensors) {
                 if ($value_sensors) {
                     foreach ($value_sensors as $key_sensor => $value2) {
                         if (isset($stations[$value['station_id_code']]['sensors'][$key_handler][$key_sensor])) {
                             // create handler for each sensor (we parsed new data for)
                             $handler = SensorHandler::create($key_handler);
                             if ($key_handler == 'SolarRadiation' && $last_logs[0]['log_id']) {
                                 if ($last_logs[0]['log_id']) {
                                     $total_minutes = round(abs($date_prepared - strtotime($last_logs[0]['measuring_timestamp'])) / 60);
                                     $value2['period'] = $total_minutes;
                                 } else {
                                     $value2['period'] = 1;
                                 }
                                 if ($value2['solar_radiation_in_period'][0] != 'M') {
                                     $value2['solar_radiation_in_period'] = $value2['solar_radiation_in_period'] * $value2['period'] * 60;
                                 }
                             }
                             // each handler has it's own implementation of preparing sensors string for message basing on XML data
                             $res = $handler->prepareXMLValue($value2, $stations[$value['station_id_code']]['sensors'][$key_handler][$key_sensor]['features']);
                             $result_message_body .= $stations[$value['station_id_code']]['sensors'][$key_handler][$key_sensor]['sensor_id_code'] . $res;
                         }
                     }
                 }
             }
             $result_message_body .= It::prepareCRC($result_message_body);
             $result_message_body = '@' . $result_message_body . '$';
             // add new message into database. It will be processed later as all newcame messages
             $log_id = ListenerLog::addNew($result_message_body, 0, 1);
             $result[] = $base_filename . " : New message #" . $log_id . " was added";
         }
     }
     // return some comments created during convertation
     return implode("\n", $result);
 }
Exemplo n.º 11
0
 public function actionMetrics()
 {
     $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('superadmin/metrics'));
     }
     $this->render('metrics', array('meas_types' => $meas_types));
 }
Exemplo n.º 12
0
 public function createExport()
 {
     $return_string = "";
     $sql = "SELECT\n                       `t1`.`measuring_timestamp` AS `TxDateTime`,\n                       `t4`.`station_id_code` AS `StationId`, \n                       `t4`.`display_name` AS `StationDisplayName`,\n                       `t3`.`sensor_id_code` AS `SensorId`, \n                       `t3`.`display_name` AS `SensorDisplayName`, \n                       \n                       `t1`.`period` AS `MeasurementPeriod`,\n                       `t1`.`sensor_feature_value` AS `Value`,\n                       `t5`.`short_name` AS `Metric`,\n                       `t8`.`value` AS `DewPoint`,\n                       `t11`.`value` AS `PressureMSL`,\n                       \n                       `t12`.`handler_id_code`,\n                       `t4`.`magnetic_north_offset`\n\n                 FROM `" . SensorData::model()->tableName() . "` t1\n                 LEFT JOIN `" . StationSensorFeature::model()->tableName() . "`          `t2`    ON `t2`.`sensor_feature_id` = `t1`.`sensor_feature_id`\n                 LEFT JOIN `" . StationSensor::model()->tableName() . "`                 `t3`    ON `t3`.`station_sensor_id` = `t2`.`sensor_id`\n                 LEFT JOIN `" . Station::model()->tableName() . "`                       `t4`    ON `t4`.`station_id` = `t1`.`station_id`\n                 LEFT JOIN `" . RefbookMetric::model()->tableName() . "`                 `t5`    ON `t5`.`metric_id` = `t2`.`metric_id`\n\n                 LEFT JOIN `" . StationCalculation::model()->tableName() . "`            `t6`    ON (`t6`.`station_id` = `t1`.`station_id` AND `t6`.`handler_id` = 1)\n                 LEFT JOIN `" . StationCalculationVariable::model()->tableName() . "`    `t7`    ON (`t7`.`sensor_feature_id` = `t2`.`sensor_feature_id` AND `t7`.`calculation_id` = `t6`.`calculation_id`)\n                 LEFT JOIN `" . StationCalculationData::model()->tableName() . "`        `t8`    ON (`t8`.`calculation_id` = `t7`.`calculation_id` AND `t8`.`listener_log_id` = `t1`.`listener_log_id`)\n\n                 LEFT JOIN `" . StationCalculation::model()->tableName() . "`            `t9`    ON (`t9`.`station_id` = `t1`.`station_id` AND `t9`.`handler_id` = 2)\n                 LEFT JOIN `" . StationCalculationVariable::model()->tableName() . "`    `t10`   ON (`t10`.`sensor_feature_id` = `t2`.`sensor_feature_id` AND `t10`.`calculation_id` = `t9`.`calculation_id`)\n                 LEFT JOIN `" . StationCalculationData::model()->tableName() . "`        `t11`   ON (`t11`.`calculation_id` = `t10`.`calculation_id` AND `t11`.`listener_log_id` = `t1`.`listener_log_id`)\n\n                 LEFT JOIN `" . SensorDBHandler::model()->tableName() . "`               `t12`   ON `t12`.`handler_id` = `t3`.`handler_id`\n\n                 WHERE `t1`.`station_id` IN (" . implode(',', $this->station_id) . ")\n                   AND `t1`.`measuring_timestamp` >= FROM_UNIXTIME(" . $this->start_timestamp . ") \n                   AND `t1`.`measuring_timestamp` <= FROM_UNIXTIME(" . $this->end_timestamp . ")\n                   AND `t2`.`is_main` = 1\n                   AND `t1`.`is_m` = '0'\n                 ORDER BY `t1`.`measuring_timestamp` DESC, `t4`.`station_id_code`, `t3`.`sensor_id_code`";
     $res = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll();
     if ($res) {
         foreach ($res as $key => $value) {
             $handler_obj = SensorHandler::create($value['handler_id_code']);
             $res[$key]['Value'] = $handler_obj->applyOffset($res[$key]['Value'], $res[$key]['magnetic_north_offset']);
             $res[$key]['Value'] = $handler_obj->formatValue($res[$key]['Value'], $res[$key]['feature_code']);
             unset($res[$key]['magnetic_north_offset']);
             unset($value['handler_id_code']);
             unset($res[$key]['feature_code']);
         }
         $return_string .= "\"AWS Stations:\"\n" . It::prepareStringCSV($res);
     }
     $sql = "SELECT\n                        `t1`.`measuring_timestamp` AS `TxDateTime`,\n                        `t3`.`station_id_code` AS `StationId`, \n                        `t3`.`display_name` AS `StationDisplayName`,\n                        `t2`.`sensor_id_code` AS `SensorId`, \n                        `t2`.`display_name` AS `SensorDisplayName`,  \n                        (`t1`.`sensor_value` * `t1`.`bucket_size`) AS `Value`,\n                        `t4`.`short_name` AS `Metric`                        \n                 FROM `" . SensorDataMinute::model()->tableName() . "` t1\n                 LEFT JOIN `" . StationSensor::model()->tableName() . "` t2 ON t2.station_sensor_id = t1.sensor_id\n                 LEFT JOIN `" . Station::model()->tableName() . "` t3 ON t3.station_id = t1.station_id\n                 LEFT JOIN `" . RefbookMetric::model()->tableName() . "` t4 ON t4.metric_id = t1.metric_id\n                     \n                 WHERE `t1`.`station_id` IN (" . implode(',', $this->station_id) . ")\n                   AND `t1`.`measuring_timestamp` >= FROM_UNIXTIME(" . $this->start_timestamp . ") \n                   AND `t1`.`measuring_timestamp` <= FROM_UNIXTIME(" . $this->end_timestamp . ")\n                   AND `t1`.`is_tmp` = 0\n                 ORDER BY t1.measuring_timestamp DESC, t3.station_id_code, t2.sensor_id_code\n                ";
     $res = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll();
     if ($res) {
         $return_string .= "\n\n\"RG Stations:\"\n" . It::prepareStringCSV($res);
     }
     It::downloadFile($return_string, 'export__' . date('Y-m-d_Hi') . '.csv', 'text/csv');
 }