public function run($args) { if (!Yii::app()->mutex->lock('ScheduleCommand', 3600)) { Yii::app()->end(); } $synchronization = new Synchronization(); if (!$synchronization->isMaster() and $synchronization->isProcessed()) { return; } $generationTime = time(); $proper_periods = ScheduleCommand::getProperPeriods($generationTime); if (count($proper_periods) === 0) { self::$_logger->log(__METHOD__ . ' Exiting. No proper periods found.' . "\n\n"); // Yii::app()->mutex->unlock(); // Yii::app()->end(); } $criteria = new CDbCriteria(); $criteria->select = array('schedule_id', 'report_type', 'station_id', 'report_format', 'period', 'last_scheduled_run_planned', 'last_scheduled_run_fact', '(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleTime', 'UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) AS nextScheduleUnixTime'); $criteria->with = array('station'); $criteria->compare('period', '>0'); $criteria->compare('period', $proper_periods); $criteria->addCondition('(UNIX_TIMESTAMP(`last_scheduled_run_planned` + INTERVAL `period` MINUTE) <= UNIX_TIMESTAMP() OR `last_scheduled_run_planned` = "0000-00-00 00:00:00")'); /** @var array|ScheduleReport[] $scheduledReports */ $scheduledReports = ScheduleReport::model()->findAll($criteria); if (count($scheduledReports) === 0) { self::$_logger->log(__METHOD__ . ' Exiting. No proper reports found.' . "\n\n"); Yii::app()->mutex->unlock(); Yii::app()->end(); } self::$_logger->log(__METHOD__ . ' New scheduled reports', array('report count' => count($scheduledReports))); $reportProcesses = array(); foreach ($scheduledReports as $scheduledReport) { self::$_logger->log("\n"); self::$_logger->log(__METHOD__ . ' Check scheduled report', array('schedule_id' => $scheduledReport->schedule_id)); $check_period = ScheduleCommand::getCheckPeriod($generationTime, $scheduledReport->period); if ($scheduledReport->report_type === 'data_export') { self::$_logger->log(__METHOD__ . ' scheduledReport->report_type = data_export'); // add record about schedule running to process afterwards for ($i = 0; $i < count($scheduledReport->station); $i++) { $schedule_report_process = new ScheduleReportProcessed(); $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id; $schedule_report_process->check_period_start = $check_period[3]; $schedule_report_process->check_period_end = $check_period[4]; $schedule_report_process->save(); $scheduledReport->last_scheduled_run_fact = $check_period[1]; $scheduledReport->last_scheduled_run_planned = $check_period[2]; if ($scheduledReport->validate()) { $scheduledReport->save(false); } else { self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors())); } $reportProcesses[$scheduledReport->station[$i]->id] = array('schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]); } } else { $logRecords = array(); foreach ($scheduledReport->station as $station) { $criteria = new CDbCriteria(); $criteria->compare('station_id', $station->station_id); $criteria->compare('failed', '0'); $criteria->compare('measuring_timestamp', '>' . $check_period[0]); $criteria->compare('measuring_timestamp', '<=' . $check_period[1]); $criteria->order = 'measuring_timestamp desc, log_id desc'; $criteria->limit = '1'; $logRecord = ListenerLog::model()->find($criteria); if (!is_null($logRecord)) { $logRecords[] = $logRecord; } } // add record about schedule running to process afterwards (only in case system received base message) if (count($logRecords) > 0) { for ($i = 0; $i < count($scheduledReport->station); $i++) { $schedule_report_process = new ScheduleReportProcessed(); $continue = false; foreach ($logRecords as $logRecord) { if ($logRecord->station_id == $scheduledReport->station[$i]->station_id) { $schedule_report_process->listener_log_id = $logRecord->log_id; $listener_log_id = $logRecord->log_id; $continue = false; break; } else { $continue = true; } } if ($continue) { continue; } $schedule_report_process->sr_to_s_id = $scheduledReport->station[$i]->id; $schedule_report_process->check_period_start = $check_period[3]; $schedule_report_process->check_period_end = $check_period[4]; $schedule_report_process->save(); $scheduledReport->last_scheduled_run_fact = $check_period[1]; $scheduledReport->last_scheduled_run_planned = $check_period[2]; if ($scheduledReport->validate()) { $scheduledReport->save(false); } else { self::$_logger->log(__METHOD__ . ' Schedule report not saved ', array('schedule_error' => $scheduledReport->getErrors())); } $reportProcesses[$scheduledReport->station[$i]->id] = array('log_id' => $listener_log_id, 'schedule_id' => $scheduledReport->schedule_id, 'schedule_processed_id' => $schedule_report_process->schedule_processed_id, 'schedule_info' => $scheduledReport, 'check_period_start' => $check_period[3], 'check_period_end' => $check_period[4]); } } } } if (count($reportProcesses) > 0) { $total = count($reportProcesses); $i = 1; foreach ($reportProcesses as $reportProcess) { $weatherReport = null; switch (strtolower($reportProcess['schedule_info']->report_type)) { case 'synop': $weatherReport = WeatherReport::create('Synop', self::$_logger); break; case 'bufr': $weatherReport = WeatherReport::create('Bufr', self::$_logger); break; case 'metar': $weatherReport = WeatherReport::create('Metar', self::$_logger); break; case 'odss': $weatherReport = WeatherReport::create('ODSS', self::$_logger); break; default: $weatherReport = WeatherReport::create('Export', self::$_logger); break; } try { $weatherReport->load($reportProcess['schedule_processed_id']); $weatherReport->generate(); $weatherReport->saveProcess(); $weatherReport->deliverReport(); } catch (Exteption $e) { self::$_logger->log(__METHOD__ . ' Error ', array('err' => $e->getMessage())); } self::$_logger->log(__METHOD__ . ' Completed', array('num' => $i++, 'total' => $total)); } } //send report from sttions together $scheduleProcessedIdArray_schedule_id = array(); foreach ($reportProcesses as $reportProcess) { $scheduleProcessedIdArray_schedule_id[$reportProcess['schedule_id']][] = $reportProcess['schedule_processed_id']; } foreach ($scheduleProcessedIdArray_schedule_id as $scheduleProcessedIdArray) { new WeatherReportMailSender($scheduleProcessedIdArray); } self::$_logger->log(__METHOD__ . ' Schedule report completed' . "\n\n\n\n\n\n\n\n\n"); Yii::app()->mutex->unlock(); }
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)); } }