public static function lastMsgIds($station_ids, &$stations = null)
 {
     $lastMessages = ListenerLog::getAllLast2Messages($station_ids);
     $lastMessageIds = array();
     foreach ($lastMessages as $key => $stationMessages) {
         //station last message
         if (isset($stations[$key])) {
             $next_expected = strtotime($stationMessages[0]->measuring_timestamp) + $stations[$key]->event_message_period * 60 + 300;
             $stations[$key]->nextMessageIsLates = $next_expected < time() ? 1 : 0;
             $stations[$key]->lastMessage = $stationMessages[0];
         }
         //log id
         foreach ($stationMessages as $message) {
             $lastMessageIds[] = $message->log_id;
         }
     }
     return $lastMessageIds;
 }
 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));
     }
 }