From dd2ca22bba88c6939c462b7b708a20e2eb198f73 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 24 Aug 2018 12:28:06 +0200 Subject: [PATCH] Add door sensor REST api We have yet to add the door states to the frontpage or something --- dist/pvv.sql | 11 ++++++ dist/pvv_mysql.sql | 13 +++++++ src/pvv/side/doors.php | 79 ++++++++++++++++++++++++++++++++++++++++++ www/door/index.php | 38 ++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 src/pvv/side/doors.php create mode 100644 www/door/index.php diff --git a/dist/pvv.sql b/dist/pvv.sql index 1df0f67..9ffdf94 100644 --- a/dist/pvv.sql +++ b/dist/pvv.sql @@ -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); diff --git a/dist/pvv_mysql.sql b/dist/pvv_mysql.sql index ae02dc5..d1e2ac7 100644 --- a/dist/pvv_mysql.sql +++ b/dist/pvv_mysql.sql @@ -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"); diff --git a/src/pvv/side/doors.php b/src/pvv/side/doors.php new file mode 100644 index 0000000..c05965c --- /dev/null +++ b/src/pvv/side/doors.php @@ -0,0 +1,79 @@ +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(); + } +} diff --git a/www/door/index.php b/www/door/index.php new file mode 100644 index 0000000..2052a8d --- /dev/null +++ b/www/door/index.php @@ -0,0 +1,38 @@ +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);