0
0
mirror of https://github.com/Doodle3D/doodle3d-connect.git synced 2024-12-25 01:53:48 +01:00

REST like interface (json responses), auto refreshing index page

This commit is contained in:
peteruithoven 2013-09-27 12:31:55 +02:00
parent c28818c46f
commit f5a09e7c0f
6 changed files with 108 additions and 42 deletions

View File

@ -6,7 +6,12 @@
$password = "mysql";
$table = "signins";
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$response = array( "status" => "error",
"msg" => $e->getMessage()." (".$e->getCode().")");
exit(json_encode($response));
}
?>

View File

@ -1,15 +1,3 @@
<?php
require 'connect.php';
$hourago = time() - 60*60;
$remoteip = getenv('REMOTE_ADDR');
$statement = $db->prepare("SELECT * FROM $table where date >= FROM_UNIXTIME(:hourago) AND remoteip = :remoteip");
$statement->execute(array( ':hourago' => $hourago,
':remoteip' => $remoteip));
$boxes = $statement->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
@ -24,15 +12,13 @@
<link href="css/normalize.css" rel="stylesheet" media="screen">
<link href="css/main.css" rel="stylesheet" media="screen">
<script src="js/libs/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<p>Hi, we found the following Doodle3D WiFi boxes near you:</p>
<ul>
<?php
foreach($boxes as $box) {
echo "<li><a href='http://".$box['localip']."' >".$box['wifiboxid']."</a></li>";
}
?>
<ul id="list">
</ul>
</body>
</html>

2
js/libs/jquery-1.8.3.min.js vendored Normal file

File diff suppressed because one or more lines are too long

35
js/main.js Normal file
View File

@ -0,0 +1,35 @@
var retrieveListDelay; // retry setTimout instance
var retrieveListInterval = 5000;
var $list;
$(function() {
//console.log("ready");
retrieveList();
$list = $("#list");
})
function retrieveList() {
$.ajax({
url: "/list.php",
dataType: 'json',
success: function(response){
//console.log("retrieveList response: ",response);
if(response.status == "success") {
updateList(response.data);
}
clearTimeout(retrieveListDelay);
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval);
}
}).fail(function() {
//console.log("retrieveList: failed");
clearTimeout(retrieveListDelay);
retrieveListDelay = setTimeout(retrieveList, retrieveListInterval); // retry after delay
});
}
function updateList(boxes) {
//console.log("list: ",boxes);
$list.empty();
jQuery.each(boxes, function (index,box) {
$list.append("<li><a href='/"+box.localip+"'>"+box.wifiboxid+"</a></li>");
});
}

21
list.php Normal file
View File

@ -0,0 +1,21 @@
<?php
require 'connect.php';
$hourago = time() - 60*60;
$remoteip = getenv('REMOTE_ADDR');
try {
$statement = $db->prepare("SELECT * FROM $table where date >= FROM_UNIXTIME(:hourago) AND remoteip = :remoteip");
$statement->execute(array( ':hourago' => $hourago,
':remoteip' => $remoteip));
$boxes = $statement->fetchAll();
} catch (PDOException $e) {
$response = array( "status" => "error",
"msg" => $e->getMessage()." (".$e->getCode().")");
exit(json_encode($response));
}
$response = array( "status" => "success",
"data" => $boxes);
exit(json_encode($response));
?>

View File

@ -1,42 +1,59 @@
<?php
if ($_SERVER['REQUEST_METHOD'] != "POST") return;
if ($_SERVER['REQUEST_METHOD'] != "POST") {
$response = array( "status" => "error",
"msg" => "'signin' can only be accessed with the POST method");
exit(json_encode($response));
}
require 'connect.php';
if(!isset($_POST['localip'])) echo "missing localip </br>";
$localip = $_POST['localip'];
echo "localip: $localip </br>";
if(!isset($_POST['wifiboxid'])) echo "missing wifiboxid </br>";
$wifiboxid = $_POST['wifiboxid'];
echo "wifiboxid: $wifiboxid </br>";
$remoteip = getenv('REMOTE_ADDR');
echo "remoteip: $remoteip </br>";
$timestamp = time();
echo "timestamp: $timestamp </br>";
$id = $remoteip.'/'.$wifiboxid;
echo "id: $id </br>";
$statement = $db->prepare( "REPLACE INTO $table " .
"SET id = :id, " .
" remoteip = :remoteip, " .
" localip = :localip, " .
" wifiboxid = :wifiboxid, " .
" date = FROM_UNIXTIME(:timestamp)");
$statement->execute(array( ':id' => $id,
':remoteip' => $remoteip,
':localip' => $localip,
':wifiboxid' => $wifiboxid,
':timestamp' => $timestamp));
try {
$statement = $db->prepare( "REPLACE INTO $table " .
"SET id = :id, " .
" remoteip = :remoteip, " .
" localip = :localip, " .
" wifiboxid = :wifiboxid, " .
" date = FROM_UNIXTIME(:timestamp)");
$statement->execute(array( ":id" => $id,
":remoteip" => $remoteip,
":localip" => $localip,
":wifiboxid" => $wifiboxid,
":timestamp" => $timestamp));
} catch (PDOException $e) {
$response = array( "status" => "error",
"msg" => $e->getMessage()." (".$e->getCode().")");
exit(json_encode($response));
}
$responseData = array( "remoteip" => $remoteip,
"localip" => $localip,
"wifiboxid" => $wifiboxid,
"timestamp" => $timestamp);
$response = array( "status" => "success",
"data" => $responseData);
exit(json_encode($response));
// Remove old signins
$hourago = time() - 60*60;
echo "hourago: $hourago (time: ".time().")</br>";
$statement = $db->prepare("DELETE FROM $table WHERE date < FROM_UNIXTIME(:hourago)");
$statement->execute(array( ':hourago' => $hourago));
try {
$statement = $db->prepare("DELETE FROM $table WHERE date < FROM_UNIXTIME(:hourago)");
$statement->execute(array( ':hourago' => $hourago));
} catch (PDOException $e) {
$response = array( "status" => "error",
"msg" => $e->getMessage()." (".$e->getCode().")");
exit(json_encode($response));
}
?>