/**
  * 
  */
 private function _setLastDataRetrievedTime(IMETOS $BD)
 {
     $query = "\n            SELECT\n                MAX(`last_read_time`)  as 'last_read_time'\n            FROM\n                `seedclima_station_data_retrieve_info`\n            WHERE\n                `f_station_code` = {$this->getStationCode()}";
     if ($BD->sql_select($query, $results)) {
         if ($BD->getRowCount() > 0) {
             $row = $results->fetch(PDO::FETCH_ASSOC);
             $this->_lastDataRetrievedTime = $row['last_read_time'];
         } else {
             $this->_lastDataRetrievedTime = false;
         }
     }
 }
 /**
  *
  * @param Integer $filterByStationCode
  * @param Integer $filterBySensorCode
  * @param Integer $filterBySensorChannel
  * @param Integer $filterByStatus 0|1
  * @return Array
  */
 public static function getAll(IMETOS $BD, $filterByStationCode = null, $filterBySensorCode = null, $filterBySensorChannel = null, $filterByStatus = null, array $filterById = null)
 {
     settype($response, 'array');
     settype($filters, 'array');
     if (isset($filterById)) {
         $filterById = implode(',', $filterById);
         $filters[] = " `row_id` IN ({$filterById})";
     }
     if (isset($filterByStationCode)) {
         $filters[] = " `f_station_code` = '{$filterByStationCode}'";
     }
     if (isset($filterBySensorCode)) {
         $filters[] = " `f_sensor_code` = '{$filterBySensorCode}'";
     }
     if (isset($filterBySensorChannel)) {
         $filters[] = " `f_sensor_ch` = '{$filterBySensorChannel}'";
     }
     if (isset($filterByStatus)) {
         $filters[] = " `enable_sensor` = '{$filterByStatus}'";
     }
     $whereCondition = count($filters) > 0 ? 'WHERE ' . implode(' AND ', $filters) : '';
     $query = "\n            SELECT  *\n            FROM    `seedclima_sensor_info`\n                {$whereCondition} \n            ORDER BY `priority`,`custom_name`,`f_sensor_user_name`";
     //echo "pase por aca 2<br>";
     //$BD=new IMETOS();
     if ($BD->sql_select($query, $result)) {
         if ($BD->getRowCount() > 0) {
             while ($sensorInfo = $result->fetch(PDO::FETCH_ASSOC)) {
                 $sensor = Sensor::load($BD, null, $sensorInfo);
                 $response[$sensor->getSensorCode() . '_' . $sensor->getSensorCh()] = $sensor;
             }
         }
         return $response;
     }
     return false;
 }
 /**
  * 
  * @param IMETOS $BD
  * @param type $f_station_code is string
  */
 public function runQuery(IMETOS $BD, Station $station)
 {
     $f_station_code = $station->getStationCode();
     // periodo a descargar
     // valores: periodo, mes_actual, todos, fijo
     switch ($this->getPeriodo()) {
         case 'periodo':
             // fecha inicial
             $fecha_inicial = $this->getPeriodoMkFechaInicial();
             $fecha_final = $this->getPeriodoMkFechaFinal();
             $date_start = new DateTime(date('Y-m-d', $fecha_inicial));
             $date_final = new DateTime(date('Y-m-d', $fecha_final));
             break;
             //
         //
         case 'mes_actual':
             // mes actual
             $mes_actual = date('n');
             $anio_actual = date('Y');
             $days_number = cal_days_in_month(CAL_GREGORIAN, $mes_actual, $anio_actual);
             $fecha_inicial = mktime(0, 0, 0, $mes_actual, 1, $anio_actual);
             $date_start = new DateTime(date('Y-m-d', $fecha_inicial));
             $fecha_final = mktime(0, 0, 0, $mes_actual, $days_number, $anio_actual);
             $date_final = new DateTime(date('Y-m-d', $fecha_final));
             break;
             //
         //
         case 'todos':
             // todos los datos
             $query = "SELECT MIN(`f_read_time`) as min,\n                                 MAX(`f_read_time`) as max  \n                          FROM `seedclima_sensor_data_retrieve_stats_day`\n                          WHERE `f_station_code`={$this->f_station_code}";
             if (!$BD->sql_select($query, $results)) {
                 error_log(date('Y-m-d H:i:s') . chr(9) . "ERROR. No se puede determinar minimo y maximo en las fechas", 3, ERROR_LOG);
             }
             if ($min_max = $results->fetch(PDO::FETCH_ASSOC)) {
                 $date_start = new DateTime(date('Y-m-d', $min_max['min']));
                 $date_final = new DateTime(date('Y-m-d', $min_max['max']));
             } else {
                 $date_start = new DateTime(date('Y-m-d'));
                 $date_final = new DateTime(date('Y-m-d'));
             }
             break;
             //
         //
         case 'fijo':
             // periodo fijo en dias
             $periodo_dias = $this->getPeriodoDias();
             $date_final = new DateTime(date('Y-m-d'));
             $date_start = new DateTime(date('Y-m-d'));
             $date_start->sub(new DateInterval('P' . $periodo_dias . 'D'));
             break;
     }
     $date_start2 = $date_start->format('Y-m-d');
     $date_final2 = $date_final->format('Y-m-d');
     //
     $date_start3 = $date_start->getTimestamp();
     $date_final3 = $date_final->getTimestamp();
     //
     // sensores
     $query = array();
     $enca1 = "{$this->getSeparador2()}";
     $enca2 = "fecha" . chr($this->getSeparador2());
     foreach ($this->sensores as $key_sensor => $sensor) {
         $select = '`f_read_time`,';
         $where = "`f_station_code`={$f_station_code} AND (`f_read_time`>={$date_start3} AND `f_read_time`<={$date_final3}) AND";
         // sensor contiene code_ch
         $partes = explode("_", $sensor);
         if (count($partes) == 2) {
             $f_sensor_code = $partes[0];
             $f_sensor_ch = $partes[1];
             $qsensor = $station->getSensor($f_sensor_code, $f_sensor_ch, 1);
             $enca1 .= "{$qsensor->getSensorUserName()}{$this->getSeparador2()}";
             if ($qsensor->getValMin()) {
                 $select .= '`min`,';
                 $enca2 .= "min" . chr($this->getSeparador2());
             }
             if ($qsensor->getValMax()) {
                 $select .= '`max`,';
                 $enca2 .= "max" . chr($this->getSeparador2());
             }
             if ($qsensor->getValSum()) {
                 $select .= '`sum`,';
                 $enca2 .= "sum" . chr($this->getSeparador2());
             }
             if ($qsensor->getValAver()) {
                 $select .= '`aver`,';
                 $enca2 .= "aver" . chr($this->getSeparador2());
             }
             if ($qsensor->getValLast()) {
                 $select .= '`last`,';
                 $enca2 .= "last" . chr($this->getSeparador2());
             }
             $where .= " `f_sensor_code`={$f_sensor_code} AND `f_sensor_ch`={$f_sensor_ch}";
         }
         if (substr($select, -1 == ',')) {
             $select = substr($select, 0, -1);
         }
         if (substr($where, -3) == 'AND') {
             // saco el AND
             $where = substr($where, 0, -3);
         }
         // hago la consulta
         $query[$sensor] = "\n                    SELECT {$select}\n                    FROM `seedclima_sensor_data_retrieve_stats_day` \n                    WHERE {$where}\n                    ORDER BY `f_read_time` ASC";
     }
     if (substr($enca1, -strlen($this->getSeparador2()) == $this->getSeparador2())) {
         $enca1 = substr($enca1, 0, -strlen(chr($this->getSeparador2())));
     }
     if (substr($enca2, -strlen(chr($this->getSeparador2())) == chr($this->getSeparador2()))) {
         $enca2 = substr($enca2, 0, -strlen(chr($this->getSeparador2())));
     }
     return array($query, $enca1, $enca2);
 }