almost working database, selects and updates correctly

This commit is contained in:
Joel Garcia 2021-10-27 18:32:59 -05:00
parent 30dbde6fe8
commit 67c3a97ef4
15 changed files with 500 additions and 25 deletions

41
auth/index.php Normal file
View File

@ -0,0 +1,41 @@
<?php
session_start();
$login = $_POST['loginname'];
$userpassword = $_POST['userpassword'];
$servername = "localhost";
$username = "pi2";
$password = "emb20";
$database = "parking_lot";
$conn = mysqli_connect($servername,$username,$password,$database);
if(!$conn){
echo "Connection failed";
die("Conection failed: ". mysqli_connect_error());
}
$results = $conn->query("select * from users where username ="."'$login'");
$results -> data_seek(0);
$fila = $results->fetch_assoc();
if($login == $fila['username'] && $userpassword == $fila['password']){
$_SESSION['user_id'] = $fila['id'];
header("Location: /web/vip/");
}
else{
header("Location: /web/login/");
}
mysqli_close($conn);
?>
<!doctype html>
<html>
<head>
<title>Login attempted</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<h3>Login check</h3>
</body>
</html>

View File

@ -12,32 +12,35 @@ def output_query(sql):
values = cur.fetchall()
if sql == available:
if len(values) != 0:
print("These are the available spaces")
print("Estos son los espacios disponibles")
else:
print("There are no spaces available at the moment")
print("Estos son los espacios ocupados")
if sql == all_values:
print("There are {0} spaces in use".format(len(values)))
print("Hay {0} espacios en uso".format(len(values)))
if len(values) != 0:
print("""<br>
<table>
<tr>
<th>ID</th>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
<th>State</th>
<th>Fecha</th>
<th>Hora de llegada</th>
<th>Hora de salida</th>
<th>Estado</th>
</tr>""")
for m in values:
id_s = m[0]
date= m[1]
startime= m[2]
endtime= m[3]
reserved= m[5]
if int(m[4])== 1:
estado = "in use"
estado = "Ocupado"
elif int(m[4]) == 0:
estado = "free"
estado = "Libre"
if (int(m[5] == 1)):
estado = "Reservado"
print( "<tr><td>{0}</td> <td>{1}</td> <td>{2}</td> <td>{3}</td><td>{4}</td></tr>".format(id_s, date,startime, endtime, estado))
print("</table>")
@ -47,11 +50,10 @@ def output_query(sql):
available="select * from spaces where usedID = False"
all_values="select * from spaces where usedID = True"
last_value="select * from sensor where id_SENSOR = (select max(id_SENSOR) from sensor)"
last_10 = "SELECT * FROM (select * from sensor order by ID_sensor DESC LIMIT 10) section order by ID_sensor ASC"
# last_value="select * from sensor where id_SENSOR = (select max(id_SENSOR) from sensor)"
# last_10 = "SELECT * FROM (select * from sensor order by ID_sensor DESC LIMIT 10) section order by ID_sensor ASC"
print("<h1> Free spaces </h1>")
print("<h1> Espacios libres </h1>")
output_query(available)
print("<h1> Used spaces </h1>")
print("<h1> Espacios en uso </h1>")
output_query(all_values)

View File

@ -9,7 +9,7 @@
</head>
<body>
<main>
<h1> Parking Lot database</h1>
<h1>Estacionamiento</h1>
<?php
if($_POST['entrance'] and $_SERVER['REQUEST_METHOD'] == "POST"){
@ -25,16 +25,17 @@ if($_POST['space_num'] and $_SERVER['REQUEST_METHOD'] == "POST"){
echo "$output2";
}
?>
<p>Welcome to the parking lot. Make sure to update de database to check if there are any spaces available</p>
<p>Bienvenido al registro de estacionamiento. Actualiza la pagina para checar si hay espacios disponibles</p>
<p>Si eres un cliente frecuente, <a href="login/">inicia sesión aqui</a>.</p>
<form action="index.php" method="POST">
<input type="submit" value="Update database">
<input type="submit" value="Actualizar datos">
</form>
<p>If you want to enter the parking lot, press the button below to know your assigned spot and enter when the entrance opens.</p>
<p>Si quieres ingresar al estacionamiento, presiona el boton para asignarte un espacio y permitir la entrada</p>
<form action="index.php" method="POST">
<input type="submit" value="Access parking lot" name="entrance" id="entrance">
</form>
<p>If you want to exit from the parking lot, input your spot number to open the exit.</p>
<p>Si quieres salir del estacionamiento, ingresa el lugar que se te asigno.</p>
<form action="index.php" method="POST" autocomplete="off">
<input type="number" id="space_num" name="space_num" >
<input type="submit" value="Exit parking lot">
@ -42,11 +43,8 @@ if($_POST['space_num'] and $_SERVER['REQUEST_METHOD'] == "POST"){
<?php
$command = escapeshellcmd("sudo ./database_output.py");
$output = shell_exec($command);
echo "$output <br>";
?>
</main>
</body>
</html>

23
login/index.php Normal file
View File

@ -0,0 +1,23 @@
<!doctype html>
<html>
<head>
<title>Acceso VIP estacionamiento</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Iniciar sesión</h1>
<form method="POST" action="/web/auth/index.php" name="reg-form">
<label>User</label><br>
<input type="text" name="loginname" pattern="[a-zA-Z0-9]+" required/><br>
<label>Password</label><br>
<input type="password" name="userpassword" required/><br>
<button type="submit" name="register" value="registro">Iniciar sesión</button>
</form>
<p><a href="/web/register/">Registrar usuario frecuente</a></p>
</body>
</html>

View File

@ -78,7 +78,7 @@ if pressed == 1:
elif pressed == 2:
try:
pressed = 0
query = "select spaceID from spaces where usedID = False order by spaceID desc"
query = "select spaceID from spaces where usedID = False and reserved = False order by spaceID desc"
cur.execute(query)
db.commit()
spaces = cur.fetchall()
@ -102,5 +102,3 @@ servo.stop()
GPIO.cleanup()
db.close()
sys.exit(0)

22
unused/ejemplo/index.html Normal file
View File

@ -0,0 +1,22 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale =1'>
<title>Titulo de pagina </title>
<link rel='stylesheet' type='text/css' href='main.css'>
</head>
<body>
<nav>
<ul>
<li><a href="login.html"> Home</a></li>
<li><a href="#"> About</a></li>
<li><a href="#"> Sensores</a></li>
<li><a href="#"> GPIO</a></li>
<li><a href="#"> Actuadores</a></li>
</ul>
<ul>
</nav>
</body>
</html>

36
unused/ejemplo/login.html Normal file
View File

@ -0,0 +1,36 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale =1'>
<title>Titulo de pagina </title>
<link rel='stylesheet' type='text/css' href='main.css'>
</head>
<body>
<nav>
<ul>
<li><a href="#"> Home</a></li>
<li><a href="#"> About</a></li>
<li><a href="#"> Sensores</a></li>
<li><a href="#"> GPIO</a></li>
<li><a href="#"> Actuadores</a></li>
</ul>
<ul>
</nav>
<main>
<form>
<label class="title">sistemas embebidos</label>
<div>
<label>username</label>
<input type="text" name="login" required>
</div>
<div>
<label>password</label>
<input type="password" name="passwd" required>
</div>
<button type="submit" name="register">Registro</button>
</form>
</main>
</body>
</html>

108
unused/ejemplo/main.css Normal file
View File

@ -0,0 +1,108 @@
:root {
--sans-font: "IBM Plex Sans","Roboto",-apple-system,BlinkMacSystemFont,"Nimbus Sans L",Avenir,"Noto Sans", "Segoe UI",Arial,Helvetica,"Helvetica Neue",sans-serif;
--mono-font: "IBM Plex Mono","Roboto Mono","Ubuntu Mono","Fira Code","Overpass Mono", Monaco,"Droid Sans Mono",monospace;
--base-fontsize: 1.35rem;
--header-scale: 1.185; --line-height: 1.618;
--bg: #FFFFFF;
--accent-bg: #F2F4F8;
--text: #4c566a;
--text-light: #4c566a;
--border: #3b4252;
--accent: #5e81ac;
--accent-light: #5e81ac;
--code: #2e3440;
--code-bg: #e5e9f0;
}
@media (prefers-color-scheme:dark) {
:root {
--bg: #242933;
--accent-bg: #3b4252;
--text: #eceff4;
--text-light: #d8dee9;
--border: #88c0d0;
--accent: #a3be8c;
--accent-light: #bf616a;
--code: #ebcb8b;
--code-bg: #2e3440;
}
}
*{
font-family: var(--sans-font);
}
body{
background-color: var(--bg);
color: var(--text);
width: 100%;
}
nav a {
background: var(--code-bg);
margin: 1rem 0 0 .5rem;
color: var(--border) !important;
border-radius: 8px;
display: inline-block;
padding: .1rem .9rem;
text-decoration: none;
text-align: center;
transition: .4s;
width: 7rem;
}
nav{
/*margin: 0 auto;*/
width: 100%;
}
nav a:hover {
color: var(--bg) !important;
border-color: var(--border);
background: var(--border);
}
ul{
list-style: none;
padding: 0;
overflow: auto;
}
li{
/*float: left;*/
display: inline-block;
}
.title{
/*width: 250px;*/
text-align: center;
font-family: monospace;
}
label{
width: 90px;
display: inline-block;
}
form{
margin: 25px auto;
padding: 20px;
max-width: 30rem;
border: 1px solid var(--border);
background-color: var(--code-bg);
border-radius: 9px;
}
a button,
button,
input[type="submit"],
input[type="reset"],
input[type="button"] {
border: none;
border-radius: 5px;
background: var(--accent);
font-size: 1rem;
color: var(--bg);
text-decoration: none;
padding: .7rem .9rem;
margin: .5rem .1rem;
transition: .4s;
}
p.success, p.error{
color: var(--accent);
background-color: var(--accent);
}

13
unused/page/hija.php Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>Pagina hija</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
<h3>Pagina hija</h3>
<a href="padre.php">Link a la pagina padre</a><br>
<a href="hijo.php">Link a la pagina hijo</a>
</body>
</html>

23
unused/page/hijo.php Normal file
View File

@ -0,0 +1,23 @@
<?php
session_start();
if (!isset($_SESSION['user_id'])){
echo "Inicie sesión primero para acceder";
echo '<br><a href="padre.php">Inicie sesión</a>';
exit;
}
?>
<!doctype html>
<html>
<head>
<title>Pagina hijo</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
<h3>Pagina hijo</h3>
<a href="padre.php">Link a la pagina principal</a><br>
<a href="hija.php">Link a la pagina hija</a>
</body>
</html>

43
unused/page/login.php Normal file
View File

@ -0,0 +1,43 @@
<?php
session_start();
$login = $_POST['loginname'];
$userpassword = $_POST['userpassword'];
$servername = "localhost";
$username = "pi2";
$password = "emb20";
$database = "embebidos";
$conn = mysqli_connect($servername,$username,$password,$database);
if(!$conn){
echo "Connection failed";
die("Conection failed: ". mysqli_connect_error());
}
$results = $conn->query("select * from users where username ="."'$login'");
$results -> data_seek(0);
$fila = $results->fetch_assoc();
if($login == $fila['username'] && $userpassword == $fila['password']){
$_SESSION['user_id'] = $fila['id'];
echo "Hola mundo";
header("Location: hijo.php");
}
else{
echo "Adios mundo";
header("Location: padre.php");
}
mysqli_close($conn);
?>
<!doctype html>
<html>
<head>
<title>Login attempted</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<h3>Login check</h3>
</body>
</html>

21
unused/page/padre.php Normal file
View File

@ -0,0 +1,21 @@
<!doctype html>
<html>
<head>
<title>Pagina principal</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<h3>Pagina principal</h3>
<form method="POST" action="login.php" name="reg-form">
<label>User</label><br>
<input type="text" name="loginname" pattern="[a-zA-Z0-9]+" require /><br>
<label>Password</label><br>
<input type="password" name="userpassword" require /><br>
<button type="submit" name="register" value="registro"> Registro </button>
</form>
</body>
</html>

104
vip/index.php Normal file
View File

@ -0,0 +1,104 @@
<!doctype html>
<html>
<head>
<title>Usuarios frecuentes</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
input[type="time"]::-webkit-calendar-picker-indicator{
filter: invert(90%) sepia(13%) saturate(3207%) hue-rotate(130deg) brightness(95%) contrast(80%);
}
</style>
</head>
<body>
<h1>Pagina de usuarios frecuentes</h1>
<?php
session_start();
if (!isset($_SESSION['user_id'])){
echo '<p><a href="/web/login/">Inicie sesión</a> para acceder a esta pagina</p>';
exit;
}
?>
<h2>Apartar un lugar</h2>
<p>Adquiere derecho a un espacio de estacionamiento en una hora deseada.</p>
<form method="POST" action="/web/vip/index.php" name="reg-form">
<label>Espacio</label><br>
<input type="number" max="15" name="spacenum" required/><br>
<label>Hora de inicio</label><br>
<input type="time" name="starttime" value="00:00:00" required/><br>
<button type="submit" name="register" value="registro">Apartar espacio</button>
</form>
<?php
if($_POST['spacenum'] and $_SERVER['REQUEST_METHOD'] == "POST"){
$id = $_SESSION['user_id'];
$time = $_POST['starttime'];
$num = $_POST['spacenum'];
$servername = "localhost";
$username = "pi2";
$password = "emb20";
$database = "parking_lot";
$conn = mysqli_connect($servername,$username,$password,$database);
if(!$conn){
echo "Connection failed";
die("Conection failed: ". mysqli_connect_error());
}
$results = $conn->query("select spaceID from spaces where usedID = 0 and spaceID = $num");
$results -> data_seek(0);
$fila = $results->fetch_assoc();
if (sizeOf($fila['spaceID']) != 0){
//echo "Lugar disponible";
$request = $conn->query("update spaces set reserved = True, startTime = '$time', endTime = NULL where spaceID = '$num' and usedID = False");
$request = $conn->query("update users set place = $num, time = '$time' where id = '$id' and place is NULL");
//header("Location:/web/vip/");
}
else{
echo "Lugar no disponible";
}
mysqli_close($conn);
}
?>
<h2>Liberar un lugar</h2>
<p>Libera un espacio apartado que deseas cancelar.</p>
<form method="POST" action="/web/vip/index.php" name="reg-form">
<label>Espacio</label><br>
<input type="number" max="15" name="freenum" required/><br>
<button type="submit" name="register" value="registro">Apartar espacio</button>
</form>
<?php
if($_POST['freenum'] and $_SERVER['REQUEST_METHOD'] == "POST"){
$id = $_SESSION['user_id'];
$num = $_POST['freenum'];
$servername = "localhost";
$username = "pi2";
$password = "emb20";
$database = "parking_lot";
$conn = mysqli_connect($servername,$username,$password,$database);
if(!$conn){
echo "Connection failed";
die("Conection failed: ". mysqli_connect_error());
}
$results = $conn->query("select spaceID from spaces where usedID = 0 and spaceID = $num");
$results -> data_seek(0);
$fila = $results->fetch_assoc();
if (sizeOf($fila['spaceID']) != 0){
$request = $conn->query("update spaces set reserved = False, startTime = NULL, endTime = NULL where spaceID = '$num'");
$request = $conn->query("update users set place = NULL, time = NULL where id = '$id'");
//header("Location:/web/vip/");
}
else{
echo "Lugar no disponible";
}
mysqli_close($conn);
}
?>
</body>
</html>

14
vip/register-space.php Normal file
View File

@ -0,0 +1,14 @@
<body>
<h1>Iniciar sesión</h1>
<form method="POST" action="/web/vip/register-space.php" name="reg-form">
<label>Espacio</label><br>
<input type="number" name="spacenum" required/><br>
<label>Hora de inicio</label><br>
<input type="time" name="time" required/><br>
<button type="submit" name="register" value="registro">Apartar espacio</button>
</form>
<p><a href="/web/register/">Registrar usuario frecuente</a></p>
</body>

29
vip/register-space.py Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/python3
import RPi.GPIO as GPIO
import pymysql
import sys
import os
import time as t
from datetime import datetime as dt
pressed = int(sys.argv[1])
db = pymysql.connect(host='localhost', user='pi2', passwd='emb20', db='parking_lot')
cur = db.cursor()
try:
query = "select spaceID from spaces where usedID = False"
cur.execute(query)
db.commit()
spaces = cur.fetchall()
now = dt.now()
date = now.date()
query = "update spaces set date = '{0}', startTime = '{1}', endTime= NULL, usedID = True where spaceID = {2}".format(date, time, spaces[0][0])
cur.execute(query)
db.commit()
except IndexError:
status ="<script>alert('Sorry, but there are no spaces left')</script>"
print(status)
db.close()
sys.exit(0)