public function prepareFormulaParams() { $this->formula_params['h_station_above_sea'] = $this->station_obj->altitude; // h_barometer_above_station $sensor_id_codes = $this->getUsedSensors($this->station_obj->station_id); $sensor_id_code = array_shift(preg_grep('/^PR.$/', $sensor_id_codes)); $qb = new CDbCriteria(); $qb->with = ['sensor.handler', 'metric']; $qb->addCondition('sensor.sensor_id_code LIKE \'' . $sensor_id_code . '\''); $qb->addCondition('sensor.station_id = ' . $this->station_obj->station_id); $qb->addCondition('handler.handler_id_code LIKE \'Pressure\''); $qb->addCondition('t.feature_code LIKE \'height\''); $res = StationSensorFeature::model()->find($qb); $h_barometer_above_station = 0; if ($res && !is_null($res->feature_constant_value) && !is_null($res->metric->code)) { $h_barometer_above_station = It::convertMetric($res->feature_constant_value, $res->metric->code, 'meter'); } $this->formula_params['h_barometer_above_station'] = $h_barometer_above_station; // coefficient_from_station_gravity $station_gravity = floatval($this->station_obj->station_gravity); $this->formula_params['coefficient_from_station_gravity'] = ($station_gravity > 0 ? $station_gravity : array_shift(array_keys(yii::app()->params['station_gravity']))) / 0.0065 / 287; return parent::prepareFormulaParams(); }
/** * 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); } } }
public function prepareFormulaParams() { return parent::prepareFormulaParams(); }
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 prepareList($station_id = 0) { if ($this->hasErrors()) { return ['prepared_header' => [], 'prepared_data' => []]; } $prepared_header = array(); $prepared_data = array(); $sensor_feature_code = $this->getSensorFeatureCode(); $handler_code = array_keys($this->getSelectedGroupSensorFeatureCode()); $search_features = array(); $search_calcs = array(); foreach ($sensor_feature_code as $key) { if (in_array($key, array_keys($this->calc_handlers))) { $search_calcs[] = $this->calc_handlers[$key]; } else { $search_features[] = $key; } } if (count($this->station_id) > 1) { $sql_part = "`t2`.`station_id` IN (" . implode(',', $this->station_id) . ") "; } else { $sql_part = "`t2`.`station_id` = '" . $this->station_id[0] . "' "; } // 1.a) GET FEATURES if (count($search_features) > 0) { $sql = "SELECT `t1`.`sensor_feature_id`,\n `t1`.`feature_code`,\n `t1`.`sensor_id`,\n `t3`.`station_id_code`,\n `t2`.`sensor_id_code`,\n `t2`.`station_id`,\n `t4`.`handler_id_code`,\n `t3`.`magnetic_north_offset`\n FROM `" . StationSensorFeature::model()->tableName() . "` `t1`\n LEFT JOIN `" . StationSensor::model()->tableName() . "` `t2` ON `t1`.`sensor_id` = `t2`.`station_sensor_id`\n LEFT JOIN `" . SensorDBHandler::model()->tableName() . "` `t4` ON `t4`.`handler_id` = `t2`.`handler_id`\n LEFT JOIN `" . Station::model()->tableName() . "` `t3` ON `t3`.`station_id` = `t2`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`feature_code` IN ('" . implode("','", $search_features) . "') AND `t4`.`handler_id_code` IN ('" . implode("','", $handler_code) . "')\n ORDER BY `t1`.`feature_code`, `t3`.`station_id_code`, `t2`.`sensor_id_code`"; $found_sensors = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_sensors = count($found_sensors); } // 1.b) GET CALCS if (count($search_calcs) > 0) { $sql = "SELECT `t1`.`calculation_id`,\n `t1`.`handler_id`,\n `t2`.`station_id_code`,\n `t2`.`station_id`,\n IF(`t1`.`handler_id` = 1, 'DP', 'MSL') AS `sensor_id_code`,\n IF(`t1`.`handler_id` = 1, 'Dew Point', 'Pressure MSL') AS `feature_code`,\n IF(`t1`.`handler_id` = 1, 'DewPoint', 'PressureSeaLevel') AS `handler_id_code`\n FROM `" . StationCalculation::model()->tableName() . "` `t1`\n LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON `t2`.`station_id` = `t1`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`handler_id` IN (" . implode(',', $search_calcs) . ")\n ORDER BY `t1`.`handler_id`, `t2`.`station_id_code`"; $found_calcs = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_calcs = count($found_calcs); } $start_datetime = strtotime($this->date_from . ' ' . $this->time_from); $end_datetime = strtotime($this->date_to . ' ' . $this->time_to); $features_set = array(); // 2.a) PREPARE HEADER if (is_array($found_sensors) && $total_found_sensors > 0) { $sensor_feature_ids = array(); for ($i = 0; $i < $total_found_sensors; $i++) { $key = $found_sensors[$i]['handler_id_code'] . $found_sensors[$i]['feature_code']; if (!isset($prepared_header[$key])) { $prepared_header[$key] = array('sensor_feature_code' => $found_sensors[$i]['feature_code'], 'handler_id_code' => $found_sensors[$i]['handler_id_code'], 'sensors' => array(), 'station_sensors' => array()); } $sensor_feature_ids[] = $found_sensors[$i]['sensor_feature_id']; $prepared_header[$key]['sensors'][] = array('station_id' => $found_sensors[$i]['station_id'], 'sensor_id_code' => $found_sensors[$i]['sensor_id_code']); if (isset($prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']])) { $prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']]++; } else { $prepared_header[$key]['station_sensors'][$found_sensors[$i]['station_id']] = 1; } $features_set[$found_sensors[$i]['sensor_feature_id']] = array('station_id' => $found_sensors[$i]['station_id'], 'station_id_code' => $found_sensors[$i]['station_id_code'], 'value' => '-', 'sensor_id' => $found_sensors[$i]['sensor_id'], 'sensor_id_code' => $found_sensors[$i]['sensor_id_code'], 'sensor_feature_code' => $found_sensors[$i]['feature_code'], 'handler_id_code' => $found_sensors[$i]['handler_id_code'], 'magnetic_north_offset' => $found_sensors[$i]['magnetic_north_offset']); } } // 2.b) PREPARE HEADER if (count($search_calcs) > 0) { $sql = "SELECT `t1`.`calculation_id`,\n `t1`.`handler_id`,\n `t2`.`station_id_code`,\n `t2`.`station_id`,\n IF(`t1`.`handler_id` = 1, 'DP', 'MSL') AS `sensor_id_code`,\n IF(`t1`.`handler_id` = 1, 'Dew Point', 'Pressure MSL') AS `feature_code`,\n IF(`t1`.`handler_id` = 1, 'DewPoint', 'PressureSeaLevel') AS `handler_id_code`\n FROM `" . StationCalculation::model()->tableName() . "` `t1`\n LEFT JOIN `" . Station::model()->tableName() . "` `t2` ON `t2`.`station_id` = `t1`.`station_id`\n WHERE " . $sql_part . " AND `t1`.`handler_id` IN (" . implode(',', $search_calcs) . ")\n ORDER BY `t1`.`handler_id`, `t2`.`station_id`"; $found_calcs = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); if (is_array($found_calcs) && count($found_calcs) > 0) { $calculation_ids = array(); for ($i = 0; $i < count($found_calcs); $i++) { $key = 'calc_' . $found_calcs[$i]['handler_id']; if (!isset($prepared_header[$key])) { $prepared_header[$key] = array('sensor_feature_code' => $key, 'sensors' => array(), 'station_sensors' => array()); } $calculation_ids[] = $found_calcs[$i]['calculation_id']; $prepared_header[$key]['sensors'][] = array('station_id' => $found_calcs[$i]['station_id'], 'sensor_id_code' => $found_calcs[$i]['sensor_id_code']); if (isset($prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']])) { $prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']]++; } else { $prepared_header[$key]['station_sensors'][$found_calcs[$i]['station_id']] = 1; } $features_set['calc_' . $found_calcs[$i]['calculation_id']] = array('station_id' => $found_calcs[$i]['station_id'], 'station_id_code' => $found_calcs[$i]['station_id_code'], 'value' => '-', 'calculation_id' => $found_calcs[$i]['calculation_id'], 'sensor_id_code' => $found_calcs[$i]['sensor_id_code'], 'sensor_feature_code' => $found_calcs[$i]['feature_code'], 'handler_id_code' => $found_calcs[$i]['handler_id_code']); } } } // 3.a) PREPARE DATA if (is_array($found_sensors) && $total_found_sensors) { $qb = new CDbCriteria(); $qb->select = ['sensor_data_id', 'station_id', 'sensor_id', 'sensor_feature_id', 'sensor_feature_normalized_value', 'is_m', 'measuring_timestamp']; $qb->addInCondition('sensor_feature_id', $sensor_feature_ids); $qb->addBetweenCondition('measuring_timestamp', date('Y-m-d H:i:s', $start_datetime), date('Y-m-d H:i:s', $end_datetime)); $qb->order = 'measuring_timestamp DESC'; $found_values = SensorData::model()->long()->findAll($qb); $total_found_values = count($found_values); if (is_array($found_values) && $total_found_values > 0) { if ($this->accumulation_period == 0) { for ($j = 0; $j < $total_found_values; $j++) { $f_id = $found_values[$j]['sensor_feature_id']; $f_time = $found_values[$j]['measuring_timestamp']; $f_code = $features_set[$f_id]['sensor_feature_code']; $magnetic_north_offset = $features_set[$f_id]['magnetic_north_offset']; $st_id = $found_values[$j]['station_id']; if (!isset($prepared_data[$f_time])) { $prepared_data[$f_time] = array(); $prepared_data[$f_time]['stations'] = array(); } if (!isset($prepared_data[$f_time]['data'])) { $prepared_data[$f_time]['data'] = $features_set; } $handler_obj = SensorHandler::create($features_set[$f_id]['handler_id_code']); if ($found_values[$j]['is_m'] == 1) { $prepared_data[$f_time]['data'][$f_id]['value'] = '-'; } else { $found_values[$j]['sensor_feature_normalized_value'] = $handler_obj->applyOffset($found_values[$j]['sensor_feature_normalized_value'], $magnetic_north_offset); $prepared_data[$f_time]['data'][$f_id]['value'] = $handler_obj->formatValue($found_values[$j]['sensor_feature_normalized_value'], $f_code); } if (!in_array($st_id, $prepared_data[$f_time]['stations'])) { $prepared_data[$f_time]['stations'][] = $st_id; } } } else { for ($j = 0; $j < $total_found_values; $j++) { $f_id = $found_values[$j]['sensor_feature_id']; $f_time = $found_values[$j]['measuring_timestamp']; $f_code = $features_set[$f_id]['sensor_feature_code']; $magnetic_north_offset = $features_set[$f_id]['magnetic_north_offset']; $st_id = $found_values[$j]['station_id']; $period = $start_datetime + (intval((strtotime($f_time) - $start_datetime) / ($this->accumulation_period * 60)) + 1) * $this->accumulation_period * 60; $period = $period > $end_datetime ? $end_datetime : $period; $period = date('Y-m-d H:i:s', $period); if (!isset($prepared_data[$period])) { $prepared_data[$period] = array(); $prepared_data[$period]['stations'] = array(); } if (!isset($prepared_data[$period]['data'])) { $prepared_data[$period]['data'] = $features_set; } $handler_obj = SensorHandler::create($features_set[$f_id]['handler_id_code']); if ($found_values[$j]['is_m'] == 1) { $prepared_data[$period]['data'][$f_id]['value'] = '-'; } else { $found_values[$j]['sensor_feature_normalized_value'] = $handler_obj->applyOffset($found_values[$j]['sensor_feature_normalized_value'], $magnetic_north_offset); $prepared_data[$period]['data'][$f_id]['value'] = ($prepared_data[$period]['data'][$f_id]['value'] ? $prepared_data[$period]['data'][$f_id]['value'] : 0) + $handler_obj->formatValue($found_values[$j]['sensor_feature_normalized_value'], $f_code); } if (!in_array($st_id, $prepared_data[$period]['stations'])) { $prepared_data[$period]['stations'][] = $st_id; } } } } } // 3.b) PREPARE DATA if (is_array($found_calcs) && $total_found_calcs > 0) { $sql = "SELECT `t2`.`station_id`,\n `t1`.`calculation_id`,\n `t1`.`value`,\n `t2`.`measuring_timestamp`\n FROM `" . StationCalculationData::model()->tableName() . "` `t1`\n LEFT JOIN `" . ListenerLog::model()->tableName() . "` `t2` ON `t2`.`log_id` = `t1`.`listener_log_id`\n WHERE `t1`.`calculation_id` IN (" . implode(',', $calculation_ids) . ")\n AND `t2`.`measuring_timestamp` >= '" . date('Y-m-d H:i:s', $start_datetime) . "'\n AND `t2`.`measuring_timestamp` <= '" . date('Y-m-d H:i:s', $end_datetime) . "'\n ORDER BY `t2`.`measuring_timestamp` DESC"; $found_values = CStubActiveRecord::getDbConnect(true)->createCommand($sql)->queryAll(); $total_found_values = count($found_values); if (is_array($found_values) && $total_found_values) { for ($j = 0; $j < $total_found_values; $j++) { $f_id = 'calc_' . $found_values[$j]['calculation_id']; $f_time = $found_values[$j]['measuring_timestamp']; $st_id = $found_values[$j]['station_id']; if (!$prepared_data[$f_time]) { $prepared_data[$f_time] = array(); $prepared_data[$f_time]['stations'] = array(); } if (!$prepared_data[$f_time]['data']) { $prepared_data[$f_time]['data'] = $features_set; } $prepared_data[$f_time]['data'][$f_id]['value'] = CalculationHandler::formatValue($found_values[$j]['value']); $prepared_data[$f_time]['data'][$f_id]['station_id'] = $st_id; if (!in_array($st_id, $prepared_data[$f_time]['stations'])) { $prepared_data[$f_time]['stations'][] = $st_id; } } } } //need sort krsort($prepared_data); //print_r(array( // 'prepared_header' => $prepared_header, // 'prepared_data' => $prepared_data, // ));exit; return array('prepared_header' => $prepared_header, 'prepared_data' => $prepared_data); }
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)); }
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>'); }