Add door sensor REST api

We have yet to add the door states to the frontpage or something
This commit is contained in:
Peder Bergebakken Sundt 2018-08-24 12:28:06 +02:00
parent 69ed1df0d1
commit dd2ca22bba
4 changed files with 141 additions and 0 deletions

11
dist/pvv.sql vendored
View File

@ -38,5 +38,16 @@ CREATE TABLE "motd" (
INSERT INTO motd (title, content)
VALUES ("MOTD ./dev.sh", "du kan endre motd i admin panelet");
CREATE TABLE doors (
"name" TEXT PRIMARY KEY,
"open" BOOLEAN,
"description" BOOLEAN
);
INSERT INTO doors(name, open, description) VALUES
("koserommet", FALSE, "Døra inn til koserommet på stripa"),
("terminalrommet", FALSE, "Døra inn til terminalrommet på stripa");
INSERT INTO users (uname, groups)
VALUES ("min_test_bruker", 1);

13
dist/pvv_mysql.sql vendored
View File

@ -35,3 +35,16 @@ CREATE TABLE motd (
`title` TEXT,
`content` TEXT
);
/*
INSERT INTO motd (title, content)
VALUES ("MOTD ./dev.sh", "du kan endre motd i admin panelet");
*/
CREATE TABLE doors (
`name` VARCHAR(20) PRIMARY KEY,
`open` BOOLEAN,
`description` BOOLEAN,
);
INSERT INTO doors(name, open, description) VALUES
("koserommet", FALSE, "Døra inn til koserommet på stripa"),
("terminalrommet", FALSE, "Døra inn til terminalrommet på stripa");

79
src/pvv/side/doors.php Normal file
View File

@ -0,0 +1,79 @@
<?php
namespace pvv\side;
use \PDO;
class Doors{
private $pdo;
public function __construct(PDO $pdo){
$this->pdo = $pdo;
}
public function getAll() {
$query = 'SELECT name, open, description FROM doors ORDER BY open DESC, name ASC';
$statement = $this->pdo->prepare($query);
$statement->execute();
$doors = [];
foreach($statement->fetchAll() as $row){
$doors[] = [
'name' => $row['name'],
'open' => (int)$row['open'],
'description' => $row['description'],
];
}
return $doors;
}
public function getByName($name){
$query = 'SELECT * FROM doors WHERE name=:name';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->execute();
$row = $statement->fetch();
if (!$row) {
return false;
}
return [
'name' => $row['name'],
'open' => (int)$row['open'],
'description' => $row['description'],
];
}
public function setDoorState($name, bool $open) {
$query = 'UPDATE doors SET open=:open WHERE name=:name';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->bindParam(':open', $open, PDO::PARAM_INT);
$statement->execute();
}
public function createDoor($name, $description) {
$query = 'INSERT INTO doors(name, open, description) VALUES (:name, TRUE, :desc)';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->bindParam(':desc', $description, PDO::PARAM_STR);
$statement->execute();
}
public function updateDoorDescription($name, $description) {
$query = 'UPDATE doors SET descriptin=:desc WHERE name=:name';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->bindParam(':desc', $description, PDO::PARAM_STR);
$statement->execute();
}
public function deleteDoor($name) {
$query = 'DELETE FROM doors WHERE name = :name;';
$statement = $this->pdo->prepare($query);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->execute();
}
}

38
www/door/index.php Normal file
View File

@ -0,0 +1,38 @@
<?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();
}
}
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();
}
}
else {
echo '{"error": true, "reason": "missing either \"name\" or \"open\" argument"}';
http_response_code(404);
exit();
}
}
echo json_encode($out);