|
| 1 | +<?php |
| 2 | + |
| 3 | +// Store sensors data from remote stations into a datacenter database. |
| 4 | + |
| 5 | +require_once('db_dsn.php'); |
| 6 | + |
| 7 | +// ISO 8601 UTC timestamp format. See strftime() PHP function. |
| 8 | +define('TIMESTAMP_FORMAT', '%Y-%m-%dT%H:%M:%SZ'); |
| 9 | + |
| 10 | +$login = (isset($_REQUEST['login'])) ? $_REQUEST['login'] : NULL; |
| 11 | +$password = (isset($_REQUEST['password'])) ? $_REQUEST['password'] : NULL; |
| 12 | +if ($login == NULL) exit('Invalid login'); |
| 13 | + |
| 14 | +// Connect to the database, use the MDB2 abstraction. |
| 15 | +$mdb2 = MDB2::connect($dsn); |
| 16 | +if (PEAR::isError($mdb2)) exit($mdb2->getMessage()); |
| 17 | +$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC); |
| 18 | + |
| 19 | +// Authenticate the client. |
| 20 | +$sql = 'SELECT id, password FROM stations WHERE login = ?'; |
| 21 | +$query = $mdb2->prepare($sql, array('text'), array('integer', 'text')); |
| 22 | +$result = $query->execute($login); |
| 23 | +if (PEAR::isError($result)) exit($result->getMessage()); |
| 24 | +$station = $result->fetchRow(); |
| 25 | +$query->free(); |
| 26 | +$result->free(); |
| 27 | +if (! isset($station['password'])) exit('Login failed'); |
| 28 | +if ($station['password'] != md5($password)) exit('Login failed'); |
| 29 | + |
| 30 | +// Received some data, INSERT into the database. |
| 31 | +if (isset($_REQUEST['sensors_data'])) { |
| 32 | + $sensors_data = json_decode($_REQUEST['sensors_data']); |
| 33 | + //error_log("JSON decoded sensors_data = " . print_r($sensors_data, true)); |
| 34 | + // Open a transaction |
| 35 | + $res = $mdb2->beginTransaction(); |
| 36 | + $sql = 'INSERT INTO data (station_id, time_stamp, type, value) VALUES (?, ?, ?, ?)'; |
| 37 | + $query = $mdb2->prepare($sql, array('integer', 'text', 'text', 'float')); |
| 38 | + if (PEAR::isError($query)) exit($query->getMessage()); |
| 39 | + // TODO: Insert the whole array with a single query? |
| 40 | + // TODO: Enclose in BEGIN/COMMIT |
| 41 | + foreach($sensors_data as $row) { |
| 42 | + array_unshift($row, $station['id']); |
| 43 | + $result = $query->execute($row); |
| 44 | + if (PEAR::isError($result)) { |
| 45 | + $mdb2->rollback(); |
| 46 | + exit($result->getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + $mdb2->commit(); |
| 50 | + $query->free(); |
| 51 | +} |
| 52 | + |
| 53 | +// Get the timestamp of last inserted data. |
| 54 | +$sql = 'SELECT max(time_stamp) AS time_stamp FROM data WHERE station_id = ?'; |
| 55 | +$query = $mdb2->prepare($sql, array('integer'), array('timestamp')); |
| 56 | +if (PEAR::isError($query)) exit($query->getMessage()); |
| 57 | +$result = $query->execute($station['id']); |
| 58 | +if (PEAR::isError($result)) exit($result->getMessage()); |
| 59 | +$row = $result->fetchRow(); |
| 60 | +$query->free(); |
| 61 | +$result->free(); |
| 62 | +if ($row['time_stamp'] == NULL) { |
| 63 | + // Unix Epoch (January 1 1970 00:00:00 GMT) |
| 64 | + $utc_timestamp = gmstrftime(TIMESTAMP_FORMAT, 0); |
| 65 | +} else { |
| 66 | + $utc_timestamp = substr($row['time_stamp'], 0, 10) . 'T' . substr($row['time_stamp'], 11, 8) . 'Z'; |
| 67 | + if (!preg_match('/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$/', $utc_timestamp)) { |
| 68 | + exit('Invalid database timestamp format'); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +$mdb2->disconnect(); |
| 73 | + |
| 74 | +header('Content-type: application/json'); |
| 75 | +echo json_encode(array( |
| 76 | + 'name' => $login, |
| 77 | + 'latest_timestamp' => $utc_timestamp |
| 78 | +)); |
| 79 | + |
| 80 | +?> |
0 commit comments