-
Notifications
You must be signed in to change notification settings - Fork 0
/
ETL_to_database.php
209 lines (137 loc) · 6.55 KB
/
ETL_to_database.php
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
<?php
$ftp_server = "###";
$ftp_user_name = "chira";
$ftp_user_pass = "###";
#PDO: glue between database and code
$db = new PDO('pgsql:host=192.168.1.10;dbname=iot;', 'postgres', $ftp_user_pass);
// set up basic connection
$conn_id = ftp_connect($ftp_server, 21, 90);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!\n";
echo "Attempted to connect to $ftp_server for user $ftp_user_name\n";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name\n";
}
//Returns a list of files in the given directory
$gpslist = ftp_nlist($conn_id, "/gps");
$camlist = ftp_nlist($conn_id, "/camera");
$audiolist = ftp_nlist($conn_id, "/audio");
foreach ($gpslist as $gpsfile) {
//Don't want to use an intermediate file?
//Use 'php://output' as the filename and then capture the output using output buffering.
ob_start();
ftp_get ($conn_id, "php://output", $gpsfile, FTP_ASCII);
//Return the contents of the output buffer
$data = ob_get_contents();
/*
ob_end_flush sends the contents of the topmost output buffer (if any) and turn the output buffer off.
ob_end_clean (here) will erase the output buffer and turn off output buffering (doesn't send).
*/
ob_end_clean();
$gps_array= json_decode($data, true); //Takes a JSON encoded string and converts it into a PHP variable.
if (array_key_exists('signal', $gps_array)) {
continue;
}
else {
$latitude = $gps_array ["latitude"];
$longitude = $gps_array ["longitude"];
$elevation = $gps_array ["elevation"];
$age = $gps_array ["age"];
$speed = $gps_array ["speed"];
$filename = substr($gpsfile, 5);
echo $filename;
echo "\n";
if ($gps_array ["course"] == "") {
$course = "null";
}
else {
$course = $gps_array ["course"];
}
$trandate = substr($gpsfile, 14, -4);
$gpssql = "INSERT INTO tbgps (latitude, longitude, filename, trandate, elevation, course, age, speed) VALUES (" . $latitude . "," . $longitude . ",'" . $filename . "', '" . $trandate . "', " . $elevation . "," . $course . "," . $age . "," . $speed . ")";
$gpsrecords = $db->prepare($gpssql);
$gpsrecords->execute();
file_put_contents ("/home/chira/Downloads/$gpsfile", $data);
}
}
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
foreach ($camlist as $camfile) {
//download camfile form server to local
//use base64 encode command to convert camfile string into base64 string
//ftp_get returns TRUE on success hence "1" when "file" is echoed
$localdir = "/home/chira/Downloads/$camfile";
ftp_get ($conn_id, $localdir, $camfile, FTP_BINARY);
//chunk split can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts end every chunklen characters.
$b64 = chunk_split(base64_encode(file_get_contents("/home/chira/Downloads/$camfile")));
//Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
$namesplit = explode ("/", $camfile);
//you cant use echo on an array, hence the error returned when echo $f
$camname = $namesplit[2];
//$fname = end($f);
$b64dest = str_replace (".jpg", ".txt", "/home/chira/Downloads/camera/base64/$camname");
//rename($b64, $b64dest);
file_put_contents ($b64dest, $b64);
$filename = substr($camfile, 8);
$trandate = substr($camfile, 16, -5);
$uuid = gen_uuid();
$picsql = "INSERT INTO tbpictures (recid, filename, trandate) VALUES ('" . $uuid . "','" . $filename . "', '" . $trandate . "')";
echo $picsql;
echo ("\n");
$camrecords = $db->prepare($picsql);
$camrecords->execute();
$filesql = "INSERT INTO tbfiles (recid, filename, base64) VALUES ('" . $uuid . "','" . $filename . "','" . $b64 . "')";
$filerecords = $db->prepare($filesql);
$filerecords->execute();
}
foreach ($audiolist as $audiofile) {
//download audiofile form server to local
//use base64 encode command to convert audiofile string into base64 string
//ftp_get returns TRUE on success hence "1" when "file" is echoed
$localdir = "/home/chira/Downloads/$audiofile";
ftp_get ($conn_id, $localdir, $audiofile, FTP_BINARY);
//chunk split can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts end every chunklen characters.
$b64 = chunk_split(base64_encode(file_get_contents("/home/chira/Downloads/$audiofile")));
//Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
$namesplit = explode ("/", $audiofile);
//you can't use echo on an array, hence the error returned when echo $f
$audioname = $namesplit[2];
//$fname = end($f);
$b64dest = str_replace (".wav", ".txt", "/home/chira/Downloads/audio/base64/$audioname");
//rename($b64, $b64dest);
file_put_contents ($b64dest, $b64);
$filename = substr($audiofile, 7);
$trandate = substr($audiofile, 16, -4);
$uuid = gen_uuid();
$audiosql = "INSERT INTO tbaudio (recid, filename, trandate) VALUES ('" . $uuid . "','" . $filename . "', '" . $trandate . "')";
echo $audiosql;
echo ("\n");
$audiorecords = $db->prepare($audiosql);
$audiorecords->execute();
$filesql = "INSERT INTO tbfiles (recid, filename, base64) VALUES ('" . $uuid . "','" . $filename . "','" . $b64 . "')";
$filerecords = $db->prepare($filesql);
$filerecords->execute();
}
// close the FTP stream
ftp_close($conn_id);
?>