-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Programvareverkstedet/doorsensor
Uploaded door sensor
- Loading branch information
Showing
8 changed files
with
169 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
$dbUser = null; | ||
$dbPass = null; | ||
|
||
$doorSensorSecret = "OGJiZTdjZDctMmFkNy00ZjZjLTk3OGItOTA3NzU3ZDM2Yjlm"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
namespace pvv\side; | ||
|
||
use \PDO; | ||
|
||
class Door{ | ||
private $pdo; | ||
|
||
public function __construct(PDO $pdo){ | ||
$this->pdo = $pdo; | ||
} | ||
|
||
public function getAll() { | ||
$query = 'SELECT time, open FROM door ORDER BY time DESC'; | ||
$statement = $this->pdo->prepare($query); | ||
$statement->execute(); | ||
|
||
$doorEvents = []; | ||
foreach($statement->fetchAll() as $row){ | ||
$doorEvents[] = [ | ||
'time' => (int)$row['time'], | ||
'open' => (bool)$row['open'] | ||
]; | ||
} | ||
|
||
return $doorEvents; | ||
} | ||
|
||
public function getEntriesAfter($startTime) { | ||
$query = 'SELECT time, open FROM door WHERE time > :startTime ORDER BY time DESC'; | ||
$statement = $this->pdo->prepare($query); | ||
$statement->bindParam(':startTime', $startTime, PDO::PARAM_STR); | ||
$statement->execute(); | ||
|
||
$doorEvents = []; | ||
foreach($statement->fetchAll() as $row){ | ||
$doorEvents[] = [ | ||
'time' => (int)$row['time'], | ||
'open' => (bool)$row['open'] | ||
]; | ||
} | ||
|
||
return $doorEvents; | ||
} | ||
|
||
public function getCurrent() { | ||
$query = 'SELECT time, open FROM door ORDER BY time DESC LIMIT 1'; | ||
$statement = $this->pdo->prepare($query); | ||
$statement->execute(); | ||
$row = $statement->fetch(); | ||
return [ | ||
'time' => (int)$row['time'], | ||
'open' => (bool)$row['open'] | ||
]; | ||
} | ||
|
||
private function removeOld() { | ||
$firstValidTime = time() - 60*60*24*7; //One week before now | ||
$query = 'DELETE FROM door WHERE time < :firstValid'; | ||
$statement = $this->pdo->prepare($query); | ||
$statement->bindParam(':firstValid', $firstValidTime, PDO::PARAM_STR); | ||
$statement->execute(); | ||
} | ||
|
||
public function createEvent($time, $open) { | ||
$query = 'INSERT INTO door(time, open) VALUES (:time, :open)'; | ||
$statement = $this->pdo->prepare($query); | ||
$statement->bindParam(':time', $time, PDO::PARAM_STR); | ||
$statement->bindParam(':open', $open, PDO::PARAM_STR); | ||
$statement->execute(); | ||
|
||
$this->removeOld(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,73 @@ | ||
<?php | ||
|
||
require_once dirname(dirname(__DIR__)) . implode(DIRECTORY_SEPARATOR, ['', 'inc', 'include.php']); | ||
$doors = new \pvv\side\Doors($pdo); | ||
|
||
$out = null; | ||
header('Content-Type: application/json'); | ||
if ($_SERVER['REQUEST_METHOD'] === 'GET') { | ||
if (isset($_GET["name"])) { | ||
$out = $doors->getByName($_GET["name"]); | ||
if (!$out) { | ||
echo '{"error": true, "reason": "not found"}'; | ||
http_response_code(404); | ||
exit(); | ||
$door = new \pvv\side\Door($pdo); | ||
|
||
if($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
if (isset($_SERVER["HTTP_AUTHORIZATION"])) { | ||
list($type, $data) = explode(" ", $_SERVER["HTTP_AUTHORIZATION"], 2); | ||
if (strcasecmp($type, "Bearer") == 0) { | ||
if (hash_equals($data, $doorSensorSecret)) { | ||
handleSetState(); | ||
} else { | ||
echo '{"status": "error", "message": "Invalid authentication key"}'; | ||
die(); | ||
} | ||
} else { | ||
echo '{"status": "error", "message": "Invalid authentication method"}'; | ||
die(); | ||
} | ||
} else { | ||
echo '{"status": "error", "message": "Missing authentication"}'; | ||
die(); | ||
} | ||
else { | ||
$out = $doors->getAll(); | ||
} | ||
} | ||
elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
if (isset($_POST["name"]) and isset($_POST["open"]) ) { | ||
$out = $doors->setDoorState($_POST["name"], (strtolower($_POST["open"])==="true")?1:0); | ||
|
||
$out = $doors->getByName($_POST["name"]); | ||
if (!$out) { | ||
echo '{"error": true, "reason": "not found"}'; | ||
http_response_code(404); | ||
exit(); | ||
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') { | ||
|
||
if (isset($_GET["period"])) { | ||
$period = (string)htmlspecialchars($_GET["period"]); | ||
if ($period == "day") { | ||
$startTime = time() - (60*60*24); | ||
} else if ($period == "week") { | ||
$startTime = time() - (60*60*24*7); | ||
} else { | ||
echo '{"status": "error", "message": "Invalid period"}'; | ||
die(); | ||
} | ||
} | ||
else { | ||
echo '{"error": true, "reason": "missing either \"name\" or \"open\" argument"}'; | ||
http_response_code(404); | ||
exit(); | ||
|
||
$lines = $door->getEntriesAfter($startTime); | ||
echo json_encode([ | ||
'status' => "OK", | ||
'entries' => $lines | ||
]); | ||
} else { | ||
//Only last entry | ||
$line = (object)$door->getCurrent(); | ||
echo json_encode([ | ||
'status' => "OK", | ||
'time' => $line->time, | ||
'open' => $line->open | ||
]); | ||
} | ||
} | ||
|
||
function utf8ize($d) { | ||
if (is_array($d)) { | ||
foreach ($d as $k => $v) { | ||
$d[$k] = utf8ize($v); | ||
} | ||
} else if (is_string ($d)) { | ||
return utf8_encode($d); | ||
|
||
function handleSetState() { | ||
global $door; | ||
|
||
$jsonobj = file_get_contents('php://input'); | ||
$event = json_decode($jsonobj); | ||
|
||
if ((!isset($event->time)) || (!is_numeric($event->time))) { | ||
echo '{"status": "error", "message": "Invalid timestamp"}'; | ||
die(); | ||
} | ||
if ((!isset($event->isDoorOpen)) || (!is_bool($event->isDoorOpen))) { | ||
echo '{"status": "error", "message": "Invalid door state"}'; | ||
die(); | ||
} | ||
return $d; | ||
} | ||
|
||
echo json_encode(utf8ize($out)); | ||
$door->createEvent((int)($event->time), (bool)($event->isDoorOpen)); | ||
echo '{"status": "OK"}'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters