diff --git a/root/push.php b/root/push.php index ecdbb8a..8d2c7e2 100644 --- a/root/push.php +++ b/root/push.php @@ -9,12 +9,42 @@ $data = @json_decode($payload, true); if ($data['_type'] == 'location') { file_put_contents('toto', serialize($data)); - $tst = $data['tst']; - $lat = $data['lat']; - $lon = $data['lon']; - $tid = $data['tid']; - # Convert timestamp to a format suitable for mysql - $dt = date('Y-m-d H:i:s', $tst); + + $db_dir = getenv('DB_DIR'); + # TODO: pour plus tard afin de dynamiser le nom + $db_path = $db_dir.'/db.db'; + $db = new SQLite3($db_path); + + # TODO: à commenter quand ready + # TODO: fixer type (https://owntracks.org/booklet/tech/json/) + $db->exec(' + CREATE TABLE IF NOT EXISTS history( + tst INTEGER PRIMARY KEY, + lat FLOAT, + lon FLOAT, + acc INTEGER + ) + '); + + $stm = $db->prepare(' + INSERT INTO history( + tst, + lat, + lon, + acc + ) + VALUES ( + :tst, + :lat, + :lon, + :acc + ) + '); + $stm->bindValue(':tst', $data['tst'], SQLITE3_INTEGER); + $stm->bindValue(':lat', $data['lat'], SQLITE3_FLOAT); + $stm->bindValue(':lon', $data['lon'], SQLITE3_FLOAT); + $stm->bindValue(':acc', $data['acc'], SQLITE3_INTEGER); + $res = $stm->execute(); } $response = array(); diff --git a/root/test.php b/root/test.php index fd6abf5..71a6024 100644 --- a/root/test.php +++ b/root/test.php @@ -4,3 +4,13 @@ $db_dir = getenv('DB_DIR'); # TODO: pour plus tard afin de dynamiser le nom $db_path = $db_dir.'/db.db'; $db = new SQLite3($db_path); + +# TODO: à commenter quand ready +$db->exec("CREATE TABLE history(tst TEXT PRIMARY KEY, lat TEXT, lon TEXT, tid TEXT)"); + +$stm = $db->prepare("INSERT INTO history(tst, lat, lon, tid) VALUES (:tst, :lat, :lon, :tid)"); +$stm->bindValue(':tst', $tst , SQLITE3_TEXT); +$stm->bindValue(':lat', $lat, SQLITE3_TEXT); +$stm->bindValue(':lon', $lon, SQLITE3_TEXT); +$stm->bindValue(':tid', $tid, SQLITE3_TEXT); +$res = $stm->execute(); \ No newline at end of file