Skip to content
Snippets Groups Projects
importSolarStation.php 11.7 KiB
Newer Older
root's avatar
root committed
#!/usr/bin/php
<?php
namespace SmartData;
require_once( __DIR__ . '/../Backend.php');

use \mysqli as mysqli;

abstract class Importer// extends Backend
{
    // mysql -h 150.162.142.228 -u Pablo -pfotovoltaica stagging_ma -A

    private static $_SERVERNAME = '150.162.142.228';
    private static $_SERVERPORT = 3306;
    private static $_DBNAME     = 'stagging_ma';
    private static $_USERNAME   = 'Pablo';
    private static $_PASSWORD   = 'fotovoltaica';

    private static $BATCH_SIZE  = PHP_INT_MAX;
    private static $DOMAIN      = 'solarstationfotovoltaica';

    // Checks the health of databases
    public static function health_check() : bool {
        $sucess = true;
        try {
            $conn = self::_mysqlConnect();
        } catch (\Exception $e) {
            Logger::exception($e);
            $sucess = false;
        } finally {
            $conn = null;
        }
        return $sucess;
    }

    public static function get_time(){
        list($usec, $sec) = explode(' ', microtime());
        return (float) $sec + (float) $usec;
    }

    public static function insert($backend, $series, $smartdata_batch, $j, $i, $start){
        $insertBatch_start = self::get_time();
        echo "Inserting batch...\n";
        if(!$backend->insertBatch($series, ...$smartdata_batch)){
            die ("Insertion Failed!\n");
        } else {
            echo "Inserted...\n";
        }
        $insertBatch_end = self::get_time();
        $insertBatch_elapsed_time = round($insertBatch_end - $insertBatch_start, 5);
        $total_elapsed_time = round($insertBatch_end - $start, 5);
        echo $j,'/',$i,' datapoints imported in ',$insertBatch_elapsed_time,' secs / ',$total_elapsed_time,' secs', PHP_EOL;
    }

    public static function import($table, $time, $column, $unit, $error, $confidence, $x, $y, $z, $d=0, $callback=NULL) {

        $time_start = self::get_time();
        $batch_size = self::$BATCH_SIZE;
        $count = 0;

        $smartdata = new StaticSmartData (
                $unit,
                0,      // value
                $error,
                $confidence,
                $x,
                $y,
                $z,
                0,      // timestamp
                $d      // d
        );

        $series = new Series (
                    SmartData::STATIC_VERSION,
                    $unit,    // unit
                    $x,       // x
                    $y,       // y
                    $z,       // z
                    0,        // r
                    0, //1449123538000000, // t0 (epoch in microseconds)
                    PHP_INT_MAX,  // t1 (epoch in microseconds)
                    $d      // d
        );

        global $__FORCE_VERSION;
        $__FORCE_VERSION = Config_Common::VERSION_1_1;
        $cred = new Credentials(self::$DOMAIN,
                        Config::config()::CASSANDRA_SUPERUSER,
                        Config::config()::CASSANDRA_SUPERPASS);
        $backend = new Backend_V1_1($cred, true);

        echo "Creating data series...\n";
        if(!$backend->create($series))
            die ("Create error");

        $i = 0;
        echo "Pushing data... $series ({$table}.{$column})\n";

        $servername = Importer::$_SERVERNAME;
        $port       = Importer::$_SERVERPORT;
        $dbname     = Importer::$_DBNAME;
        $username   = Importer::$_USERNAME;
        $password   = Importer::$_PASSWORD;

        $mysqli = new mysqli($servername, $username, $password, $dbname);

        if ($mysqli->connect_errno) {
            echo 'Connect failed: ', $mysqli->connect_error, PHP_EOL;
            exit();
        }

        while(true){
            $query = "SELECT UNIX_TIMESTAMP(t.{$time})*1000000 as timestamp, t.{$column} as value
                      FROM {$table} t
                      WHERE t.{$time} is not null AND t.{$column} is not null
                      LIMIT {$count},{$batch_size};";

            echo 'new query', PHP_EOL;
            $result = $mysqli->query($query, MYSQLI_USE_RESULT);
            if(!$result){
                echo 'breaking', PHP_EOL;
                break;
            }

            if(isset($smartdata_batch))
                array_splice($smartdata_batch, 0);

            $smartdata_batch = array();
            $j = 0;
            while ($row = $result->fetch_assoc()) {
                $timestamp  = $row['timestamp'];
                $value = $row['value'];

                $smartdata->time = $timestamp;

                if (is_callable($callback))
                    $smartdata->value = $callback($value);
                else
                    $smartdata->value = $value;

                $smartdata_temp = clone $smartdata;
                array_push($smartdata_batch, $smartdata_temp);

                $i = $i + 1;
                $j = $j + 1;
                if($j >= 20000){
                    self::insert($backend, $series, $smartdata_batch, $j, $i, $time_start);
                    array_splice($smartdata_batch, 0);
                    $j = 0;
                }
            }
            if($j > 0){
                self::insert($backend, $series, $smartdata_batch, $j, $i, $time_start);
                array_splice($smartdata_batch, 0);
            }


            $count+=$batch_size;

            $result->close();
        }

        $mysqli->close();

        $time_end = self::get_time();
        $elapsed_time = round($time_end - $time_start, 5);

        echo "Import finished: {$table}.{$column}  Total points: {$i}\n";
        echo 'Elapsed time: ', $elapsed_time, ' secs. Memory usage: ', round(((memory_get_peak_usage(true) / 1024) / 1024), 2), 'Mb';
        echo "________________________________________________\n";

    }

    private static function _mysqlConnect() : \PDO {
        $servername = Importer::$_SERVERNAME;
        $port       = Importer::$_SERVERPORT;
        $dbname     = Importer::$_DBNAME;
        $username   = Importer::$_USERNAME;
        $password   = Importer::$_PASSWORD;
        $conn = new \PDO("mysql:host=$servername;port=$port;dbname=$dbname", $username, $password);
        $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $conn->setAttribute(\PDO::ATTR_PERSISTENT, true);
        $conn->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
        return $conn;
    }
}

if (!Importer::health_check())
    die ("Authentication failure\n");

$__FORCE_SCREEN_LOG = true;

//                         si      | mod     | sr            | rad           |  m            |  kg           |  s            |  A            |  K            |  mol          |  cd
const Irradiance         = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 0) << 18 | (4 + 1) << 15 | (4 - 3) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Speed              = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 1) << 18 | (4 + 0) << 15 | (4 - 1) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Temperature        = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 0) << 18 | (4 + 0) << 15 | (4 + 0) << 12 | (4 + 0) << 9  | (4 + 1) << 6  | (4 + 0) << 3  | (4 + 0);
const Length             = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 1) << 18 | (4 + 0) << 15 | (4 + 0) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Pressure           = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 - 1) << 18 | (4 + 1) << 15 | (4 - 2) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Electric_Potential = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 2) << 18 | (4 + 1) << 15 | (4 - 3) << 12 | (4 - 1) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Current            = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 0) << 18 | (4 + 0) << 15 | (4 + 0) << 12 | (4 + 1) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Electric_Power     = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 2) << 18 | (4 + 1) << 15 | (4 - 3) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);
const Electrical_Energy  = 1 << 31 | 0 << 27 | (4 + 0) << 24 | (4 + 0) << 21 | (4 + 2) << 18 | (4 + 1) << 15 | (4 - 2) << 12 | (4 + 0) << 9  | (4 + 0) << 6  | (4 + 0) << 3  | (4 + 0);


//Solar Building North - SSB
$x =  375812600;  
$y = -423903900;  
$z = -292065800;  


$mbar_to_pascal     = function($milibar){ return ($milibar/1000)*100000; };
$gcel_to_kelv  =      function($celsius){ return $celsius+274.15; };
$mm_to_m            = function($millimeters){ return $millimeters/1000; };
$k_to_none          = function($k){ return $k*1000; };
$kWh_to_Ws          = function($kWh){ return $kWh*3600000; };

// HT_SPP_ESS_MIN
// HT_SPP_ESS_SEC

//static import($table, $time, $column, $unit, $error, $confidence, $x, $y, $z, $d=0, $callback=NULL);

//nohup ./importSolarStation.php 2>&1 >> iss_log &

// OK =============================================================================================
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'AV_M2000002', Temperature,        0,0, $x,$y,$z, 0, $gcel_to_kelv);        // M2000002  MA2 - ESS - TEMPERATURA INTERNA DATALOGGER       (K)
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'MN_M2000003', Electric_Potential, 0,0, $x,$y,$z, 0, null);                 // M2000003  MA2 - ESS - TENSÃO DA BATERIA DATALOGGER         (V) kg.m^2/(s^3.A)
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'AV_M2000004', Temperature,        0,0, $x,$y,$z, 1, $gcel_to_kelv);        // M2000004  MA2 - ESS - TEMPERATURA AMBIENTE                 (K)
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'AV_M2000006', Speed,              0,0, $x,$y,$z, 0, null);                 // M2000006  MA2 - ESS - VELOCIDADE DO VENTO                  (m/s)
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'AV_M2000008', Pressure,           0,0, $x,$y,$z, 0, $mbar_to_pascal);      // M2000008  MA2 - ESS - PRESSÃO BAROMÉTRICA                  (Pa) kg/(m.s^2) [1 bar = 100000 Pa]
//Importer::import('HT_MA2_ESS_MIN', 'TS_SAMPLETM', 'TT_M2000009', Length,             0,0, $x,$y,$z, 0, $mm_to_m);             // M2000009  MA2 - ESS - ÍNDICE PLUVIOMÉTRICO                 (m)

//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000010', Irradiance,         0,0, $x,$y,$z, 0, null);                 // M2000010  MA2 - ESS - IRRADIÂNCIA HORIZONTAL               (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000011', Irradiance,         0,0, $x,$y,$z, 1, null);                 // M2000011  MA2 - ESS - IRRADIÂNCIA INCLINADA                (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000012', Irradiance,         0,0, $x,$y,$z, 2, null);                 // M2000012  MA2 - ESS - IRRADIÂNCIA INCLINADA (CÉLULA LIMPA) (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000013', Irradiance,         0,0, $x,$y,$z, 3, null);                 // M2000013  MA2 - ESS - IRRADIÂNCIA INCLINADA (CÉLULA SUJA)  (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000014', Irradiance,         0,0, $x,$y,$z, 4, null);                 // M2000014  MA2 - ESS - IRRADIÂNCIA GLOBAL                   (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000015', Irradiance,         0,0, $x,$y,$z, 5, null);                 // M2000015  MA2 - ESS - IRRADIÂNCIA DIFUSA                   (W/m2)
//Importer::import('HT_MA2_ESS_SEG', 'TS_SAMPLETM', 'VL_M2000016', Irradiance,         0,0, $x,$y,$z, 6, null);                 // M2000016  MA2 - ESS - IRRADIÂNCIA DIRETA                   (W/m2)

// TO IMPORT ======================================================================================
#DNI_SHP1_Avg IRRADIÂNCIA (W/m2)
//Importer::import('HT_SPP_ESS_MIN', 'TIMESTAMP', 'DNI_SHP1_Avg', Irradiance,        0,0, $x,$y,$z, 0, null);

//`TempAr_Avg`
Importer::import('HT_SPP_ESS_MIN', 'TIMESTAMP', 'TempAr_Avg', Temperature,        0,0, $x,$y,$z, 0, $gcel_to_kelv);

exit;