/**
  * @return array
  */
 public function saveSensor()
 {
     $result = array();
     // Save Sensor
     $this->getSensor($this->sensor_id)->display_name = $this->sensor_name;
     if ($this->sensor && $this->sensor->validate()) {
         $this->sensor->save();
         $result = ['status' => 'save', 'sensor_id' => $this->sensor_id, 'sensor_name' => $this->sensor_name];
         // Save constant
         foreach ($this->sensor->ConstantFeature as $feature) {
             /** @var  StationSensorFeature $feature */
             $feature->feature_constant_value = $this->constant[$feature->sensor_feature_id]['value'];
             if ($feature->validate()) {
                 $feature->save();
             }
         }
     }
     return $result;
 }
Example #2
0
 protected function stationSensorAdd($stationId, $sensorParamArray)
 {
     $handler = SensorDBHandler::model()->with('features')->findAllByAttributes(array('handler_id_code' => $sensorParamArray['handler']));
     $handler = $handler[0];
     $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) {
         //error
     }
     $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;
             $validated = $validated & $feature->validate();
         }
         if ($validated) {
             $sensor->save(false);
             if (isset($sensorParamArray['calculation'])) {
                 if ($sensorParamArray['calculation']['DewPoint'] == true) {
                     $this->sensors_for_calcs['DewPoint'][$stationId][] = $sensor;
                 }
                 if ($sensorParamArray['calculation']['PressureSeaLevel'] == true) {
                     $this->sensors_for_calcs['PressureSeaLevel'][$stationId][] = $sensor;
                 }
             }
             if ($sensorFeatures) {
                 foreach ($sensorFeatures as $feature) {
                     $feature->sensor_id = $sensor->station_sensor_id;
                     $feature->save(false);
                 }
             }
             // sensor Created
         }
     }
     // sensor Save Fail
 }
Example #3
0
 public function actionSensor()
 {
     $station = Station::model()->findByPk(isset($_REQUEST['station_id']) ? intval($_REQUEST['station_id']) : null);
     $handler = SensorDBHandler::model()->with('features')->findByPk($_REQUEST['handler_id']);
     if (is_null($station)) {
         It::memStatus('station not found');
         $this->redirect($this->createUrl('admin/stations'));
     }
     if (is_null($handler)) {
         It::memStatus('handler not found');
         $this->redirect($this->createUrl('admin/StationSave', array('station_id' => $station->station_id)));
     }
     $sensor = new StationSensor();
     $sensor->station_id = $station->station_id;
     $sensor->handler_id = $handler->handler_id;
     $sensor->display_name = $handler->handler_default_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($station->station_id, $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) {
         It::memStatus('sensor ' . $handler->display_name . ' is full');
         $this->redirect($this->createUrl('admin/sensors', array('station_id' => $station->station_id)));
     }
     $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;
             }
             $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 and $sensorFeatures) {
         foreach ($sensorFeatures as $feature) {
             $feature->sensor_id = 1;
             $validated = $validated & $feature->validate();
         }
         if ($validated) {
             $sensor->save(false);
             if ($sensorFeatures) {
                 foreach ($sensorFeatures as $feature) {
                     $feature->sensor_id = $sensor->station_sensor_id;
                     $feature->save(false);
                 }
             }
             It::memStatus('sensor created');
             It::setMem('sensor_id', $sensor->station_sensor_id);
             $this->redirect($this->createUrl('admin/sensors', array('station_id' => $station->station_id)));
         }
     }
     It::memStatus('sensor save fail');
     $this->redirect($this->createUrl('admin/sensors', array('station_id' => $station->station_id)));
 }