Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/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;