diff --git a/web/.htaccess b/web/.htaccess index 8cf20e6..6a2b195 100644 --- a/web/.htaccess +++ b/web/.htaccess @@ -2,3 +2,4 @@ RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ dashboard.php [QSA,L] + diff --git a/web/api/alarm_event.php b/web/api/alarm_event.php new file mode 100644 index 0000000..a0e84af --- /dev/null +++ b/web/api/alarm_event.php @@ -0,0 +1,30 @@ +Cabinet ID: ".$cabinet_id; + echo "
Door Open: ".(boolval($door_open) ? 'True' : 'False'); + echo "
Defib Removed: ".(boolval($defib_removed) ? 'True' : 'False'); + echo "
Defib Fault: ".(boolval($alarm_status) ? 'True' : 'False'); + echo "
Temp Status: ".$temp_status."°C

"; + + // Declare and set the old status variables + $old_door_open = ""; + $old_defib_removed = ""; + $old_alarm_status = ""; + + $result = mysqli_query($conn,"SELECT * FROM tbl_status WHERE cabinet_id='".$cabinet_id."' + ORDER BY last_update DESC LIMIT 1;"); + while ($row = mysqli_fetch_array($result)) { + $old_door_open = $row['door_open']; + $old_defib_removed = $row['defib_removed']; + $old_alarm_status = $row['alarm_status']; + } + + // Update the status table with the new entries + $sql = "INSERT INTO tbl_status (cabinet_id, door_open, defib_removed, alarm_status, temp_status) + VALUES ('".$cabinet_id."', '".$door_open."', '".$defib_removed."', '".$alarm_status."', '".$temp_status."')"; + if (mysqli_query($conn, $sql)) { + echo "

Status update complete.

"; + } else { + echo "

Error: " . $sql . "
" . mysqli_error($conn) . "

"; + } + + // Run SMS events + include 'door_event.php'; + include 'alarm_event.php'; + include 'defib_event.php'; + + } elseif (!empty($data['cabinet_id'])) { + echo "

Missing some JSON post data. The status update could not be completed.

"; + } else { + echo "

No cabinet JSON data received.

"; + } + + // Close the database connection + $conn->close(); diff --git a/web/events/send_sms.php b/web/api/send_sms.php similarity index 88% rename from web/events/send_sms.php rename to web/api/send_sms.php index e903a8d..d17eacd 100644 --- a/web/events/send_sms.php +++ b/web/api/send_sms.php @@ -11,12 +11,11 @@ function send_sms($body, $phone_numbers) $client = new Services_Twilio($AccountSid, $AuthToken); - foreach ($phone_numbers as $send_number) - { + foreach ($phone_numbers as $send_number) { $message = $client->account->messages->create(array( "From" => "+441671642020", // From a valid Twilio number "To" => $send_number, // Text this number - "Body" => $body, + "Body" => $body, // Send this message )); } diff --git a/web/dashboard.php b/web/dashboard.php index f032496..c64fc38 100644 --- a/web/dashboard.php +++ b/web/dashboard.php @@ -1,53 +1,49 @@ -post('/event', function ($request, $response, $args) { - $data = $request->getParsedBody(); //creates array from data posted by user - return $response->write($data["id"]); + $app->post('/event', function ($request, $response, $args) { + $data = $request->getParsedBody(); //creates array from data posted by user + return $response->write($data["id"]); }); -$config['displayErrorDetails'] = true; -$config['addContentLengthHeader'] = false; + $config['displayErrorDetails'] = true; + $config['addContentLengthHeader'] = false; -$config['db']['host'] = "localhost"; -$config['db']['user'] = "root"; -$config['db']['pass'] = "password"; -$config['db']['dbname'] = "pads_db";//login to database + $config['db']['host'] = "localhost"; + $config['db']['user'] = "root"; + $config['db']['pass'] = "password"; + $config['db']['dbname'] = "pads_db"; //login to database -$app = new \Slim\App(["settings" => $config]); + $app = new \Slim\App(["settings" => $config]); + // Get container + $container = $app->getContainer(); + // Assign variables holding the server details required to connect + $servername = "localhost"; + $username = "root"; + $password = "password"; + $dbName = "pads_db"; + $portNumber = "3306"; -// Get container -$container = $app->getContainer(); + // Create aconnection using these variables + $conn = mysqli_connect($servername, $username, $password, $dbName, $portNumber); -// Assign variables holding the server details required to connect -$servername = "localhost"; -$username = "root"; -$password = "password"; -$dbName = "pads_db"; - -// Create aconnection using these variables -$conn = mysqli_connect($servername, $username, $password, $dbName); - -// Check that the connection was successful -if (!$conn) { - // If the connection was not successful, echo a connection error and stop the PHP scripts - die("Connection failed: " . mysqli_connect_error()); + // Check that the connection was successful + if (!$conn) { + // If the connection was not successful, echo a connection error and stop the PHP scripts + die("Connection failed: " . mysqli_connect_error()); } -$query = mysqli_query($conn, 'SELECT * FROM tbl_cabinet'); - - -//Register component on container -$container['view'] = function ($container) { - $view = new \Slim\Views\Twig('./templates', [ -// 'cache' => 'home/pi/PADs/web/cache' + //Register component on container + $container['view'] = function ($container) { + $view = new \Slim\Views\Twig('./templates', [ + //'cache' => 'home/pi/PADs/web/cache' 'cache' => false ]); @@ -56,41 +52,70 @@ $container['request']->getUri() )); - return $view; -}; + return $view; + }; -// Render Twig template in route -$app->get('/event/{id}', function ($request, $response, $args) { + // Render Twig template in route + $app->get('/event/{id}', function ($request, $response, $args) { - $conn= mysqli_connect("localhost", "root", "password", "pads_db")//creates connection!> + $conn= mysqli_connect("localhost", "root", "password", "pads_db", "3306")//creates connection!> or die ("Sorry - could not connect to MySQL"); - $result = mysqli_query($conn, 'SELECT * FROM tbl_cabinet'); //takes everything from tbl_cabinet, assigns to value $query!> - - $tplArray = array(); + $query = "SELECT cabs.id, cabs.location, cabs.postcode, SUBTIME(CURRENT_TIMESTAMP(),'1:02:00') cut_off_time, + COALESCE( + (SELECT stats.door_open + FROM tbl_status stats + WHERE stats.cabinet_id = cabs.id ORDER BY stats.last_update DESC LIMIT 1) , 'Not available') door_open, + COALESCE( + (SELECT stats.defib_removed + FROM tbl_status stats + WHERE stats.cabinet_id = cabs.id ORDER BY stats.last_update DESC LIMIT 1) , 'Not available') defib_removed, + COALESCE( + (SELECT stats.alarm_status + FROM tbl_status stats + WHERE stats.cabinet_id = cabs.id ORDER BY stats.last_update DESC LIMIT 1) , 'Not available') alarm_status, + COALESCE( + (SELECT stats.temp_status + FROM tbl_status stats + WHERE stats.cabinet_id = cabs.id ORDER BY stats.last_update DESC LIMIT 1) , 'Not available') temp_status, + COALESCE( + (SELECT stats.last_update + FROM tbl_status stats + WHERE stats.cabinet_id = cabs.id ORDER BY stats.last_update DESC LIMIT 1) , 'Not available') last_update + FROM tbl_cabinets cabs + ORDER BY cabs.id ASC"; + + $result = mysqli_query($conn, $query); //takes everything from tbl_cabinet, assigns to value $query!> + + $tplArray = array(); while ( $row = mysqli_fetch_array ( $result ) ) { - $tplArray[] = array ( - 'id' => $row ['id'], - 'name' => $row ['name'] - ); + $door_open = (boolval($row['door_open']) ? 'Open' : 'Closed'); + + $temp_status = ($row['temp_status']."°C"); + + if ($row ['defib_removed'] == 1) { + $defib_status = "Unavailable"; + } elseif ($row ['alarm_status'] == 1) { + $defib_status = "Fault"; + } else { + $defib_status = "Available"; + } + // ------------------------------------------------------------------------ + $tplArray[] = array ( + 'id' => $row ['id'], + 'location' => $row ['location'], + 'postcode'=>$row['postcode'],//gets fields from 'select *' to pass to html to display + gives data names + 'door_status'=>$door_open, + 'defib_status'=>$defib_status, + 'last_update'=>$row['last_update'], + 'cut_off_time'=>$row['cut_off_time'], + 'temp_status'=>$temp_status + ); } - return $this->view->render($response, 'sample.html', //calls sample.html array('cabinets' => $tplArray)); // 'id' => $args['id'] ]); }); $app->run(); - -$app->get('/hello/{name}', function($request, $response, $args) { - return $response->write("Hello ".$args['name']); -}); - -$app->get('/bye/{name}', function($request, $response, $args) { - return $response->write("Bye ".$args['name']); -}); - - - -?> diff --git a/web/database/database_design.txt b/web/database/database_design.txt index 470ba0e..4847b8c 100644 --- a/web/database/database_design.txt +++ b/web/database/database_design.txt @@ -1,15 +1,17 @@ Database Name: pads_db tbl_cabinets: - (PK) id - AUTO_INCREMENT INT - location - STRING * + (PK) id - STRING + location - STRING postcode - STRING tbl_status: (PK) id - AUTO_INCREMENT INT (FK) cabinet_id - STRING - door_status - STRING ("Open" / "Closed") - defib_status - STRING ("Available" / "Not Working" / "Removed") + door_open - TINYINT (0 = Closed / 1 = Open) + defib_removed - TINYINT (0 = Hanger Down / 1 = Hanger Up) + alarm_status - TINYINT (0 = Working / 1 = Fault) + temp_status - INT (Current sensor value) last_update - CURRENT TIMESTAMP tbl_guardians: @@ -17,7 +19,10 @@ tbl_guardians: first_name - STRING last_name - STRING phone_number - STRING - (FK) cabinet_id - INT + (FK) cabinet_id - STRING -* Need to ask on Monday/Tuesday about what location data will need to be - stored in our database + tbl_fleet: + (PK) id - AUTO_INCREMENT INT + first_name - STRING + last_name - STRING + phone_number - STRING diff --git a/web/database/sample_database.sql b/web/database/sample_database.sql new file mode 100644 index 0000000..3594175 --- /dev/null +++ b/web/database/sample_database.sql @@ -0,0 +1,192 @@ +-- phpMyAdmin SQL Dump +-- version 4.5.1 +-- http://www.phpmyadmin.net +-- +-- Host: 127.0.0.1 +-- Generation Time: Jul 22, 2016 at 12:57 PM +-- Server version: 10.1.13-MariaDB +-- PHP Version: 5.6.23 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Database: `pads_db` +-- + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tbl_cabinets` +-- + +CREATE TABLE `tbl_cabinets` ( + `id` varchar(200) NOT NULL, + `location` varchar(200) NOT NULL, + `postcode` varchar(200) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- +-- Dumping data for table `tbl_cabinets` +-- + +INSERT INTO `tbl_cabinets` (`id`, `location`, `postcode`) VALUES +('AA123', 'Gateway Business Center, Redruth', 'TR15 1SS'), +('AB123', 'Somewhere, Bude', 'TC83 KD3'), +('BB123', 'Somewhere, Truro', 'TR11 1UR'); + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tbl_fleet` +-- + +CREATE TABLE `tbl_fleet` ( + `id` int(11) NOT NULL, + `first_name` varchar(200) NOT NULL, + `last_name` varchar(200) NOT NULL, + `phone_number` varchar(200) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- +-- Dumping data for table `tbl_fleet` +-- + +INSERT INTO `tbl_fleet` (`id`, `first_name`, `last_name`, `phone_number`) VALUES +(1, 'Jon', 'Cole', '+447477681144'); + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tbl_guardians` +-- + +CREATE TABLE `tbl_guardians` ( + `id` int(11) NOT NULL, + `cabinet_id` varchar(200) NOT NULL, + `first_name` varchar(200) NOT NULL, + `last_name` varchar(200) NOT NULL, + `phone_number` varchar(200) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- +-- Dumping data for table `tbl_guardians` +-- + +INSERT INTO `tbl_guardians` (`id`, `cabinet_id`, `first_name`, `last_name`, `phone_number`) VALUES +(1, 'AA123', 'Tyler', 'Arnold', '+447597997201'); + +-- -------------------------------------------------------- + +-- +-- Table structure for table `tbl_status` +-- + +CREATE TABLE `tbl_status` ( + `id` int(11) NOT NULL, + `cabinet_id` varchar(200) NOT NULL, + `door_open` tinyint(1) NOT NULL, + `defib_removed` tinyint(1) NOT NULL, + `alarm_status` tinyint(1) NOT NULL, + `temp_status` int(11) NOT NULL, + `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +-- +-- Dumping data for table `tbl_status` +-- + +INSERT INTO `tbl_status` (`id`, `cabinet_id`, `door_open`, `defib_removed`, `alarm_status`, `temp_status`, `last_update`) VALUES +(1, 'AA123', 0, 0, 0, 14, '2016-07-22 08:46:29'), +(2, 'AB123', 0, 0, 0, 14, '2016-07-22 08:46:57'), +(3, 'BB123', 0, 0, 0, 16, '2016-07-22 08:47:44'), +(4, 'AB123', 1, 0, 0, 16, '2016-07-22 08:48:00'), +(5, 'AB123', 1, 1, 0, 16, '2016-07-22 08:48:08'), +(6, 'AA123', 1, 0, 0, 15, '2016-07-22 09:29:16'), +(7, 'AA123', 0, 0, 0, 12, '2016-07-22 10:08:26'), +(8, 'AA123', 1, 1, 0, 15, '2016-07-22 10:11:03'), +(9, 'AA123', 1, 0, 0, 15, '2016-07-22 10:11:17'), +(10, 'AA123', 1, 1, 0, 15, '2016-07-22 10:11:45'), +(11, 'AA123', 1, 0, 0, 15, '2016-07-22 10:13:20'), +(12, 'AA123', 1, 0, 0, 15, '2016-07-22 10:13:50'), +(13, 'AA123', 0, 0, 0, 12, '2016-07-22 10:15:30'), +(14, 'AA123', 1, 0, 0, 15, '2016-07-22 10:15:58'), +(15, 'AA123', 1, 0, 0, 15, '2016-07-22 10:16:28'), +(16, 'AA123', 1, 1, 0, 15, '2016-07-22 10:22:02'), +(17, 'AA123', 0, 0, 0, 12, '2016-07-22 10:27:58'), +(18, 'AA123', 0, 0, 0, 12, '2016-07-22 10:32:57'); + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `tbl_cabinets` +-- +ALTER TABLE `tbl_cabinets` + ADD PRIMARY KEY (`id`); + +-- +-- Indexes for table `tbl_fleet` +-- +ALTER TABLE `tbl_fleet` + ADD PRIMARY KEY (`id`); + +-- +-- Indexes for table `tbl_guardians` +-- +ALTER TABLE `tbl_guardians` + ADD PRIMARY KEY (`id`), + ADD KEY `cabinet_id` (`cabinet_id`); + +-- +-- Indexes for table `tbl_status` +-- +ALTER TABLE `tbl_status` + ADD PRIMARY KEY (`id`), + ADD KEY `cabinet_id` (`cabinet_id`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `tbl_fleet` +-- +ALTER TABLE `tbl_fleet` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +-- +-- AUTO_INCREMENT for table `tbl_guardians` +-- +ALTER TABLE `tbl_guardians` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3; +-- +-- AUTO_INCREMENT for table `tbl_status` +-- +ALTER TABLE `tbl_status` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=19; +-- +-- Constraints for dumped tables +-- + +-- +-- Constraints for table `tbl_guardians` +-- +ALTER TABLE `tbl_guardians` + ADD CONSTRAINT `tbl_guardians_ibfk_1` FOREIGN KEY (`cabinet_id`) REFERENCES `tbl_cabinets` (`id`); + +-- +-- Constraints for table `tbl_status` +-- +ALTER TABLE `tbl_status` + ADD CONSTRAINT `tbl_status_ibfk_1` FOREIGN KEY (`cabinet_id`) REFERENCES `tbl_cabinets` (`id`); + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/web/database/tables_create.php b/web/database/tables_create.php index 13a834f..1cd1e3f 100644 --- a/web/database/tables_create.php +++ b/web/database/tables_create.php @@ -11,7 +11,7 @@ )"; // Create cabinets table - if ($conn->multi_query($sql) === TRUE) { + if ($conn->query($sql) === TRUE) { echo "

Cabinets table created successfully.

"; } else { echo "

Cabinets table creation failed.
"; @@ -21,15 +21,19 @@ // Define status table $sql = "CREATE TABLE tbl_status ( - id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, + id INT NOT NULL AUTO_INCREMENT, cabinet_id VARCHAR(200) NOT NULL, - door_status VARCHAR(200) NOT NULL, - defib_status VARCHAR(200) NOT NULL, - last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + door_open TINYINT(1) NOT NULL, + defib_removed TINYINT(1) NOT NULL, + alarm_status TINYINT(1) NOT NULL, + temp_status INT NOT NULL, + last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id), + FOREIGN KEY (cabinet_id) REFERENCES tbl_cabinets(id) )"; // Create status table - if ($conn->multi_query($sql) === TRUE) { + if ($conn->query($sql) === TRUE) { echo "

Status table created successfully.

"; } else { echo "

Status table creation failed.
"; @@ -39,15 +43,17 @@ // Define guardians table $sql = "CREATE TABLE tbl_guardians ( - id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, + id INT NOT NULL AUTO_INCREMENT, + cabinet_id VARCHAR(200) NOT NULL, first_name VARCHAR(200) NOT NULL, last_name VARCHAR(200) NOT NULL, phone_number VARCHAR(200) NOT NULL, - cabinet_id VARCHAR(200) NOT NULL + PRIMARY KEY (id), + FOREIGN KEY (cabinet_id) REFERENCES tbl_cabinets(id) )"; // Create guardians table - if ($conn->multi_query($sql) === TRUE) { + if ($conn->query($sql) === TRUE) { echo "

Guardians table created successfully.

"; } else { echo "

Guardians table creation failed.
"; @@ -55,48 +61,66 @@ $errors_occurred = 1; } - // If errors occurred - if ($errors_occurred == 1) { - echo "

One or more of the tables could not be created.
"; - echo "Please ensure that you have created the database first.

"; - } - + // Define FLEET table + $sql = "CREATE TABLE tbl_fleet( + id INT NOT NULL AUTO_INCREMENT, + first_name VARCHAR(200) NOT NULL, + last_name VARCHAR(200) NOT NULL, + phone_number VARCHAR(200) NOT NULL, + PRIMARY KEY (id) + )"; - //Create GetAllCabinetRecords stored procedure - $sql = "CREATE PROCEDURE GetAllCabinetRecords()\n" - ."BEGIN\n" - ."SELECT * FROM tbl_cabinets;\n" - ."END"; - if (mysqli_query($conn, $sql)) { - echo "

Created GetAllCabinetRecords stored procedure successfully.

"; + // Create FLEET table + if ($conn->query($sql) === TRUE) { + echo "

FLEET table created successfully.

"; } else { - echo "

Error creating stored procedure GetAllCabinetRecords: " . mysqli_error($conn) . "

"; + echo "

FLEET table creation failed.
"; + echo "Error: " . $conn->error . "

"; + $errors_occurred = 1; } - //Create GetCabinetRecord stored procedure - $sql = "CREATE PROCEDURE GetCabinetRecord(IN cabID VARCHAR(200))\n" - ."BEGIN\n" - ."SELECT * FROM tbl_cabinets WHERE id = cabID;\n" - ."END"; - if (mysqli_query($conn, $sql)) { - echo "

Created GetCabinetRecord stored procedure successfully.

"; - } else { - echo "

Error creating stored procedure GetCabinetRecord: " . mysqli_error($conn) . "

"; - } - //Create UpdateCabinetDetails stored procedures - $sql = "CREATE PROCEDURE `UpdateCabinetDetails`(IN `cabID` VARCHAR(200), IN `NewLocation` VARCHAR(30), IN `NewPostcode` VARCHAR(30)) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER BEGIN \n" - . "UPDATE tbl_cabinets \n" - . "SET tbl_cabinets.location = NewLocation, \n" - . "tbl_cabinets.postcode = NewPostcode \n" - . "WHERE tbl_cabinets.id = cabID; \n" - . "END"; - if (mysqli_query($conn, $sql)) { - echo "

Created UpdateCabinetDetails stored procedure successfully.

"; - } else { - echo "

Error creating stored procedure UpdateCabinetDetails: " . mysqli_error($conn) . "

"; + // If errors occurred + if ($errors_occurred == 1) { + echo "

One or more of the tables could not be created.
"; + echo "Please ensure that you have created the database first.

"; } + // Store procedures - Out of date, should be updated or removed + // //Create GetAllCabinetRecords stored procedure + // $sql = "CREATE PROCEDURE GetAllCabinetRecords()\n" + // ."BEGIN\n" + // ."SELECT * FROM tbl_cabinets;\n" + // ."END"; + // if (mysqli_query($conn, $sql)) { + // echo "

Created GetAllCabinetRecords stored procedure successfully.

"; + // } else { + // echo "

Error creating stored procedure GetAllCabinetRecords: " . mysqli_error($conn) . "

"; + // } + // + // //Create GetCabinetRecord stored procedure + // $sql = "CREATE PROCEDURE GetCabinetRecord(IN cabID VARCHAR(200))\n" + // ."BEGIN\n" + // ."SELECT * FROM tbl_cabinets WHERE id = cabID;\n" + // ."END"; + // if (mysqli_query($conn, $sql)) { + // echo "

Created GetCabinetRecord stored procedure successfully.

"; + // } else { + // echo "

Error creating stored procedure GetCabinetRecord: " . mysqli_error($conn) . "

"; + // } + // + // //Create UpdateCabinetDetails stored procedures + // $sql = "CREATE PROCEDURE `UpdateCabinetDetails`(IN `cabID` VARCHAR(200), IN `NewLocation` VARCHAR(30), IN `NewPostcode` VARCHAR(30)) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER BEGIN \n" + // . "UPDATE tbl_cabinets \n" + // . "SET tbl_cabinets.location = NewLocation, \n" + // . "tbl_cabinets.postcode = NewPostcode \n" + // . "WHERE tbl_cabinets.id = cabID; \n" + // . "END"; + // if (mysqli_query($conn, $sql)) { + // echo "

Created UpdateCabinetDetails stored procedure successfully.

"; + // } else { + // echo "

Error creating stored procedure UpdateCabinetDetails: " . mysqli_error($conn) . "

"; + // } + // Close the connection $conn->close(); - diff --git a/web/database/tables_fill.php b/web/database/tables_fill.php deleted file mode 100644 index 51f2206..0000000 --- a/web/database/tables_fill.php +++ /dev/null @@ -1,21 +0,0 @@ - Error: " . mysqli_error($conn); - } - - mysqli_close($conn); - diff --git a/web/events/door_open.php b/web/events/door_open.php deleted file mode 100644 index 588ba9a..0000000 --- a/web/events/door_open.php +++ /dev/null @@ -1,23 +0,0 @@ -prepare("INSERT INTO tbl_status (cabinet_id, door_status, defib_status) VALUES (?, ?, ?)"); - $stmt->bind_param("sss", $cabinet_id, $door_status, $defib_status); - $stmt->execute(); - $stmt->close(); - echo "Status update completed."; - - // If the door is open, send the SMS message - if ($door_status == "Open") { - include 'door_open.php'; - } -} else { - echo "No JSON post data."; -} - -// Close the database connection -$conn->close(); diff --git a/web/index.php b/web/index.php index 5763235..00cf03d 100644 --- a/web/index.php +++ b/web/index.php @@ -1,7 +1,7 @@ - +<--should redirect to dashboard.... diff --git a/web/login.php b/web/login.php index 6044440..f25c436 100644 --- a/web/login.php +++ b/web/login.php @@ -3,6 +3,3 @@ use \Psr\Http\Message\ServerRequestInterface as Request;//shortens path to 'request' use \Psr\Http\Message\ResponseInterface as Response;//^^to 'Response' - -?> - diff --git a/web/status_dash.php b/web/status_dash.php new file mode 100644 index 0000000..fece47a --- /dev/null +++ b/web/status_dash.php @@ -0,0 +1,96 @@ +post('/status', function ($request, $response, $args) { + $data = $request->getParsedBody(); //creates array from data posted by user + return $response->write($data["id"]); +}); + +$config['displayErrorDetails'] = true; +$config['addContentLengthHeader'] = false; + +$config['db']['host'] = "localhost"; +$config['db']['user'] = "root"; +$config['db']['pass'] = "password"; +$config['db']['dbname'] = "pads_db";//login to database + +$app = new \Slim\App(["settings" => $config]); + + + +// Get container +$container = $app->getContainer(); + +// Assign variables holding the server details required to connect +$servername = "localhost"; +$username = "root"; +$password = "password"; +$dbName = "pads_db"; + +// Create aconnection using these variables +$conn = mysqli_connect($servername, $username, $password, $dbName); + +// Check that the connection was successful +if (!$conn) { + // If the connection was not successful, echo a connection error and stop the PHP scripts + die("Connection failed: " . mysqli_connect_error()); +} + + +$query = mysqli_query($conn, 'SELECT * FROM tbl_status'); + + +//Register component on container +$container['view'] = function ($container) { + $view = new \Slim\Views\Twig('./templates', [ +// 'cache' => 'home/pi/PADs/web/cache' + 'cache' => false + ]); + + $view->addExtension(new \Slim\Views\TwigExtension( + $container['router'], + $container['request']->getUri() + )); + + return $view; +}; + +// Render Twig template in route +$app->get('/status/{id}', function ($request, $response, $args) { + + $conn= mysqli_connect("localhost", "root", "password", "pads_db")//creates connection!> + or die ("Sorry - could not connect to MySQL"); + + $result = mysqli_query($conn, 'SELECT * FROM tbl_status'); //takes everything from tbl_cabinet, assigns to value $query!> + + $tplArray = array(); + while ( $row = mysqli_fetch_array ( $result ) ) + { + $tplArray[] = array ( + 'cabinet_id' => $row ['cabinet_id'], + 'door_status' => $row ['door_status'], + 'defib_status'=>$row['defib_status'],//gets fields from 'select *' to pass to html to display + gives data names + 'last_update'=>$row['last_update'] + ); + } + + + return $this->view->render($response, '/templates/status.html', //calls sample.html + array('cabinets' => $tplArray)); // 'id' => $args['id'] ]); +}); + +$app->run(); + +$app->get('/hello/{name}', function($request, $response, $args) { + return $response->write("Hello ".$args['name']); +}); + +$app->get('/bye/{name}', function($request, $response, $args) { + return $response->write("Bye ".$args['name']); +}); + + diff --git a/web/templates/action.html b/web/templates/action.html new file mode 100644 index 0000000..523ad7c --- /dev/null +++ b/web/templates/action.html @@ -0,0 +1,25 @@ + + + + Add new defibrilator + + + + +

ID

+ +

Location

+ +

Postcode

+ +

Door Status

+ +

Defib Status

+ + + + diff --git a/web/templates/action.php b/web/templates/action.php index 211008e..172bf5a 100644 --- a/web/templates/action.php +++ b/web/templates/action.php @@ -1,16 +1,44 @@ - - -Welcome
-Your email address is: - - - "; -$conn->close(); -?> + include_once '../database/db_connect.php'; + + $sql = ""; + + // Insert new row with fake details + $pad_id = $_POST["pad_id"]; + $pad_l = $_POST["pad_l"]; + $pad_post = $_POST["pad_post"]; + //$cabinet_id = $_POST["pad_id"]; + //$pad_door = $_POST["pad_door"]; + //$pad_defib = $_POST["pad_defib"]; + + $sql = "INSERT INTO tbl_cabinets (id, location, postcode) VALUES ('$pad_id', '$pad_l', '$pad_post');"; + //$sql .= "INSERT INTO tbl_status (cabinet_id, door_status, defib_status) VALUES ('$pad_id', '$pad_door', '$pad_defib');"; + + //add values to tbl_status + // Check if it was successful + if ($res=mysqli_multi_query($conn, $sql)) { + //echo "Successful. "; + } + else { + // If there was an error, display a message at the top of the page + echo "New entries could not be added because of an error " . mysqli_error($conn); + } + + + $conn->close(); +?> + + + + + +

New PAD ID:
+ New PAD Location:
+ New PAD Postcode:
+ + + diff --git a/web/templates/action_stylesheet.css b/web/templates/action_stylesheet.css new file mode 100644 index 0000000..40e4480 --- /dev/null +++ b/web/templates/action_stylesheet.css @@ -0,0 +1,4 @@ +body{ + font-family: "Arial Black", cursive ,sans-serif; + font-size: 30px; + } diff --git a/web/templates/fleet_logo.jpg b/web/templates/fleet_logo.jpg new file mode 100644 index 0000000..0e2a7f6 Binary files /dev/null and b/web/templates/fleet_logo.jpg differ diff --git a/web/templates/sample.html b/web/templates/sample.html index de0c448..5d37813 100644 --- a/web/templates/sample.html +++ b/web/templates/sample.html @@ -1,37 +1,85 @@ - Remote Monitoring - + + + PADs Defibrillators - - -

Fleet PADs

- - - -{% for cab in cabinets %} - -{% else %} - -{% endfor %} - + + + + + +

Fleet PADs

+

{{cab.cut_off_time}}

+
IDName
{{ cab.id }}{{ cab.name }}
No users have been found.
+ + + + + + + + + + + + + + + {% for cab in cabinets %} + + {% if cab.last_update < cab.cut_off_time %} + + + + + + + + + + + {% elseif cab.defib_status == "Unavailable" %} + + + + + + + + + + + {% elseif cab.door_status == "Open" %} + + + + + + + + + + + {% else %} + + + + + + + + + + + {% endif %} + + {% endfor %} + +
IDLocationPostcodeDoor StatusDefib StatusTemp StatusLast Update
{{ cab.id }}{{ cab.location }}{{cab.postcode}}{{cab.door_status}}{{cab.defib_status}}{{cab.temp_status}}{{cab.last_update}}
{{ cab.id }}{{ cab.location }}{{cab.postcode}}{{cab.door_status}}{{cab.defib_status}}{{cab.temp_status}}{{cab.last_update}}
{{ cab.id }}{{ cab.location }}{{cab.postcode}}{{cab.door_status}}{{cab.defib_status}}{{cab.temp_status}}{{cab.last_update}}
{{ cab.id }}{{ cab.location }}{{cab.postcode}}{{cab.door_status}}{{cab.defib_status}}{{cab.temp_status}}{{cab.last_update}}
+ diff --git a/web/templates/sample_form.html b/web/templates/sample_form.html index 015d99b..7d1d820 100644 --- a/web/templates/sample_form.html +++ b/web/templates/sample_form.html @@ -1,17 +1,59 @@ + -
+Create New Defibrillator + -

+ .logo{ + float:right; + width:200px; + height:200px; + } + .form_{ + position: relative; + left: 30px; + float:center; + } -
+ + + + +
+ + + + + +

Add New Defibrillator

+ +
+ +

ID

+ +

Location

+ +

Postcode

+ + + +
+

+
+ + + +
diff --git a/web/templates/status.html b/web/templates/status.html new file mode 100644 index 0000000..68de332 --- /dev/null +++ b/web/templates/status.html @@ -0,0 +1,41 @@ + + + + + Defib Status' + + + + + + + + +

Fleet PADs

+ + + + {% for cab in cabinets %} + + {% else %} + + {% endfor %} + +
Cabinet IDDoor StatusDefib StatusLast Update
{{ cab.cabinet_id }}{{ cab.door_status }}{{cab.defib_status}}{{cab.last_update}}
No users have been found.
+ + diff --git a/web/templates/status_dash.php b/web/templates/status_dash.php new file mode 100644 index 0000000..266d218 --- /dev/null +++ b/web/templates/status_dash.php @@ -0,0 +1,109 @@ +post('/status', function ($request, $response, $args) { + $data = $request->getParsedBody(); //creates array from data posted by user + return $response->write($data["id"]); +}); + +$config['displayErrorDetails'] = true; +$config['addContentLengthHeader'] = false; + +$config['db']['host'] = "localhost"; +$config['db']['user'] = "root"; +$config['db']['pass'] = "password"; +$config['db']['dbname'] = "pads_db";//login to database + +$app = new \Slim\App(["settings" => $config]); + + + +// Get container +$container = $app->getContainer(); + +// Assign variables holding the server details required to connect +$servername = "localhost"; +$username = "root"; +$password = "password"; +$dbName = "pads_db"; + +// Create aconnection using these variables +$conn = mysqli_connect($servername, $username, $password, $dbName); + +// Check that the connection was successful +if (!$conn) { + // If the connection was not successful, echo a connection error and stop the PHP scripts + die("Connection failed: " . mysqli_connect_error()); +} + + +$query = mysqli_query($conn, 'SELECT * FROM tbl_status'); + + +//Register component on container +$container['view'] = function ($container) { + $view = new \Slim\Views\Twig('./templates', [ +// 'cache' => 'home/pi/PADs/web/cache' + 'cache' => false + ]); + + $view->addExtension(new \Slim\Views\TwigExtension( + $container['router'], + $container['request']->getUri() + )); + + return $view; +}; + +// Render Twig template in route +$app->get('/status/{id}', function ($request, $response, $args) { + + $conn= mysqli_connect("localhost", "root", "password", "pads_db")//creates connection!> + or die ("Sorry - could not connect to MySQL"); + + $result = mysqli_query($conn, 'SELECT * FROM tbl_status'); //takes everything from tbl_cabinet, assigns to value $query!> + + $tplArray = array(); + while ( $row = mysqli_fetch_array ( $result ) ) + { + $cabinet_id = $row['cabinet_id']; + $door_open = (boolval($row['door_status']) ? 'True' : 'False'); + $temp_status = $row['temp_status']; + $last_update = $row['last_update']; + + if ($row ['defib_removed'] == 1) { + $defib_status = "Unavailable"; + } elseif ($row ['alarm_status'] == 1) { + $defib_status = "Fault" + } else { + $defib_status = "Available" + } + + $tplArray[] = array ( + 'cabinet_id' => $cabinet_id, + 'door_status' => $door_open, + 'defib_status' => $defib_status,//gets fields from 'select *' to pass to html to display + gives data names + 'temp_status' => $temp_status, + 'last_update' => $last_update + ); + } + + return $this->view->render($response, '/templates/status.html', //calls sample.html + array('cabinets' => $tplArray)); // 'id' => $args['id'] ]); +}); + +$app->run(); + +$app->get('/hello/{name}', function($request, $response, $args) { + return $response->write("Hello ".$args['name']); +}); + +$app->get('/bye/{name}', function($request, $response, $args) { + return $response->write("Bye ".$args['name']); +}); + +?> diff --git a/web/templates/stylesheet.css b/web/templates/stylesheet.css new file mode 100755 index 0000000..f112bf1 --- /dev/null +++ b/web/templates/stylesheet.css @@ -0,0 +1,42 @@ + h1{ + font-size: 60px; + } + + + body { + font-family: "Arial Black", cursive ,sans-serif; + } + + .logo { + display: block; + width: 150px; + height: 150px; + float: right; + } + .table { + width: 100%; + color: black; + background-color: white; + margin: 0 auto; + padding: 10px; + } + td{ + padding:5px; + + } + thead { + color: white; + background-color: darkblue; + } + .status_unavailable { + background-color: red; + } + .status_warning { + background-color: orange; + } + .door_error { + background-color: red; + } + .door_warning { + background-color: orange; + }