public static function updateMetric()
 {
     $criteria = new CDbCriteria();
     $criteria->index = 'code';
     $mainMetrics = RefbookMeasurementType::model()->with('metricMain')->findAll($criteria);
     foreach ($mainMetrics as $code => $mainMetric) {
         StationSensorFeature::model()->updateAll(array('metric_id' => $mainMetric->metricMain->metric_id), 'measurement_type_code = :code', array(':code' => $code));
     }
 }
 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;
 }
 /**
  * @param int $sensor_id
  *
  * @return bool
  */
 public function loadBySensorId($sensor_id)
 {
     if (!is_null($sensor_id) && is_int($sensor_id) && !is_null($this->getSensor($sensor_id))) {
         $this->sensor_id = $this->sensor->station_sensor_id;
         $this->sensor_name = $this->sensor->display_name;
         $this->handler_name = $this->sensor->handler->display_name;
         foreach ($this->sensor->ConstantFeature as $feature) {
             $metric = RefbookMeasurementType::model()->with('metricMain')->findByAttributes(['code' => $feature->measurement_type_code]);
             $this->constant[$feature->sensor_feature_id] = ['name' => $feature->feature_display_name, 'value' => $feature->feature_constant_value, 'metric' => $metric->metricMain->metric->html_code];
         }
         return true;
     } else {
         return false;
     }
 }
示例#4
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;
 }
示例#5
0
 protected function sensorValidate($stationId, $sensorParamArray)
 {
     $handler = SensorDBHandler::model()->with('features')->findAllByAttributes(array('handler_id_code' => $sensorParamArray['handler']));
     $handler = $handler[0];
     if (!is_object($handler)) {
         $this->addError('sensor', 'handler was not found');
     }
     $sensor = new StationSensor();
     $sensor->station_id = $stationId;
     $sensor->handler_id = $handler->handler_id;
     $sensor->display_name = $sensorParamArray['display_name'];
     $sql = "SELECT UPPER(`sensor_id_code`) FROM `" . StationSensor::model()->tableName() . "` WHERE `station_id` = ? AND `sensor_id_code` <> ?";
     $used_code_id = Yii::app()->db->createCommand($sql)->queryColumn(array($stationId, $sensor->sensor_id_code ? $sensor->sensor_id_code : ''));
     for ($i = 1; $i <= 9; $i++) {
         $code = $handler->default_prefix . $i;
         if (!$used_code_id || !in_array($code, $used_code_id)) {
             $sensor->sensor_id_code = $code;
             break;
         }
     }
     if (!$sensor->sensor_id_code) {
         $this->addError('sensor', 'all numbers for sensor are busy');
     }
     $sensorHandler = SensorHandler::create($handler->handler_id_code);
     $sensorFeatures = array();
     $ft_1 = $sensorHandler->getFeatures();
     $ft_2 = $sensorHandler->getExtraFeatures();
     if ($ft_2) {
         foreach ($ft_2 as $key => $value) {
             $ft_2[$key]['is_extra'] = 1;
         }
     }
     $handler_sensor_features = array_merge($ft_1, $ft_2);
     if ($handler_sensor_features) {
         foreach ($handler_sensor_features as $value) {
             $sf = new StationSensorFeature();
             $default = $handler->features[$value['feature_code']];
             $metric = RefbookMeasurementType::model()->with('metricMain')->findByAttributes(array('code' => $value['measurement_type_code']));
             $sf->feature_constant_value = isset($value['default']) ? $value['default'] : null;
             if ($default) {
                 $sf->feature_constant_value = $default->feature_constant_value;
                 $sf->metric_id = $default->metric_id;
                 $sf->filter_max = $default->filter_max;
                 $sf->filter_min = $default->filter_min;
                 $sf->filter_diff = $default->filter_diff;
             }
             foreach ($sensorParamArray['features'] as $sensorParamFeature) {
                 if ($sensorParamFeature['feature_code'] == $value['feature_code']) {
                     $sf->feature_constant_value = $sensorParamFeature['feature_constant_value'];
                 }
             }
             $sf->metric_id = $metric->metricMain->metric_id;
             $sf->feature_code = $value['feature_code'];
             $sf->feature_display_name = $value['feature_name'];
             $sf->is_constant = isset($value['is_extra']) ? 1 : 0;
             $sf->comment = isset($value['comment']) ? $value['comment'] : null;
             $sf->measurement_type_code = $value['measurement_type_code'];
             $sf->is_cumulative = $value['is_cumulative'];
             $sf->is_main = $value['is_main'];
             $sf->has_filter_min = $value['has_filter_min'];
             $sf->has_filter_max = $value['has_filter_max'];
             $sf->has_filter_diff = $value['has_filter_diff'];
             $sensorFeatures[] = $sf;
         }
     }
     $validated = $sensor->validate();
     if ($validated) {
         $this->errors[] = $sensor->getErrors();
     }
     if ($validated and $sensorFeatures) {
         foreach ($sensorFeatures as $feature) {
             $feature->sensor_id = 1;
             if (!$feature->validate()) {
                 $this->errors[] = $feature->getErrors();
             }
         }
     }
     // sensor Save Fail
 }
示例#6
0
        <tr>
            <th>Name</th>
            <th>Unit</th>
            <th>Min</th>
            <th>Max</th>
            <th>Diff</th>
        </tr>

        <?php 
    foreach ($station->sensors as $key => $sensor) {
        ?>
            <?php 
        $main_feature = $sensor->main_feature;
        ?>
            <?php 
        $metric = RefbookMeasurementType::model()->with('metricMain')->findByAttributes(['code' => $main_feature->measurement_type_code]);
        ?>
            <tr class="<?php 
        echo $key % 2 == 0 ? 'c' : '';
        ?>
" id="station_sensor_<?php 
        echo $sensor->station_sensor_id;
        ?>
">
                <td><?php 
        echo $key + 1;
        ?>
.</td>
                <td><?php 
        echo $sensor->sensor_id_code;
        ?>
示例#7
0
 public function actionSetupSensor()
 {
     $handler_db = SensorDBHandler::model()->findByPk(isset($_REQUEST['handler_id']) ? intval($_REQUEST['handler_id']) : null);
     if (is_null($handler_db)) {
         $this->redirect($this->createUrl('admin/setupsensors'));
     }
     $handler = SensorHandler::create($handler_db->handler_id_code);
     $features = $handler->getFeatures();
     $extraFeatures = $handler->getExtraFeatures();
     if (is_array($extraFeatures)) {
         foreach ($extraFeatures as &$extraFeature) {
             $extraFeature['is_extra'] = 1;
         }
     }
     $handlerSensorFeatures = array_merge($features, $extraFeatures);
     $sensorFeatures = array();
     if ($handlerSensorFeatures) {
         foreach ($handlerSensorFeatures as $key => $handlerSensorFeature) {
             $sensorFeature = SensorDBHandlerDefaultFeature::model()->find('handler_id = :handler_id AND feature_code = :feature_code', array(':handler_id' => $handler_db->handler_id, ':feature_code' => $handlerSensorFeature['feature_code']));
             $metric = RefbookMeasurementType::model()->with('metricMain')->findByAttributes(array('code' => $handlerSensorFeature['measurement_type_code']));
             if (!$sensorFeature) {
                 $sensorFeature = new SensorDBHandlerDefaultFeature();
                 $sensorFeature->feature_constant_value = isset($handlerSensorFeature['default']) ? $handlerSensorFeature['default'] : null;
             }
             $sensorFeature->handler_id = $handler_db->handler_id;
             $sensorFeature->feature_code = $handlerSensorFeature['feature_code'];
             $sensorFeature->feature_display_name = $handlerSensorFeature['feature_name'];
             $sensorFeature->is_constant = isset($handlerSensorFeature['is_extra']) ? 1 : 0;
             $sensorFeature->comment = isset($handlerSensorFeature['comment']) ? $handlerSensorFeature['comment'] : null;
             $sensorFeature->measurement_type_code = $handlerSensorFeature['measurement_type_code'];
             $sensorFeature->is_cumulative = $handlerSensorFeature['is_cumulative'];
             $sensorFeature->has_filter_min = $handlerSensorFeature['has_filter_min'];
             $sensorFeature->has_filter_max = $handlerSensorFeature['has_filter_max'];
             $sensorFeature->has_filter_diff = $handlerSensorFeature['has_filter_diff'];
             $sensorFeature->metrics_list = $metric->metricMain->metric->html_code;
             $sensorFeature->metric_id = $metric->metricMain->metric->metric_id;
             $sensorFeature->aws_panel_show = isset($handlerSensorFeature['aws_panel_show']) ? 1 : 0;
             $sensorFeatures[] = $sensorFeature;
         }
     }
     $validated = true;
     if (Yii::app()->request->isPostRequest) {
         if (isset($_POST['SensorDBHandler'])) {
             $handler_db->setAttributes($_POST['SensorDBHandler']);
             $validated = $validated & $handler_db->save();
         }
         foreach ($sensorFeatures as $key => $value) {
             $sensorFeatures[$key]->attributes = $_POST['SensorDBHandlerDefaultFeature'][$key];
             $validated = $validated & $sensorFeatures[$key]->validate();
         }
         if ($validated) {
             foreach ($sensorFeatures as $key => $value) {
                 $sensorFeatures[$key]->save(false);
                 StationSensorFeature::updateByDefault($handler_db, $sensorFeatures[$key]);
             }
             It::memStatus('admin_default_sensor_saved');
             $this->redirect($this->createUrl('admin/setupsensors'));
         }
     }
     $arrh = array();
     if (SensorDBHandler::checkHandlersFor24h($handler_db->default_prefix)) {
         $arrh[-1] = 'now';
         for ($i = 0; $i < 24; $i++) {
             $arrh[$i] = ($i < 10 ? '0' . $i : $i) . ':00';
         }
     }
     $this->render('setup_default_sensor', array('handler_db' => $handler_db, 'validated' => $validated, 'sensor_features' => $sensorFeatures, 'handler_description' => $handler->getSensorDescription(), 'arrh' => $arrh));
 }
 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));
 }