parking_lot_website/parking.py

105 lines
3.2 KiB
Python
Executable File

#!/usr/bin/python3
import RPi.GPIO as GPIO
import pymysql
import sys
import os
import time as t
from gtts import gTTS as gt
from datetime import datetime as dt
def angle_to_duty(angle):
return (angle / 18 + 2)
def allow_entrance():
servo.start(0)
for i in range(0,91):
servo.ChangeDutyCycle(angle_to_duty(i))
t.sleep(0.004)
t.sleep(1)
for i in range(90,-1, -1):
servo.ChangeDutyCycle(angle_to_duty(i))
t.sleep(0.006)
t.sleep(0.5)
pressed = int(sys.argv[1])
db = pymysql.connect(host='localhost', user='pi2', passwd='emb20', db='parking_lot')
cur = db.cursor()
servo_pin = 4
pwm_freq = 50 # Nos referimos a un rango en Hz
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin, GPIO.OUT)
servo = GPIO.PWM(servo_pin, pwm_freq)
if pressed == 1:
try:
space_num = int(sys.argv[2])
pressed = 0
query= "select spaceID from spaces where usedID = True"
cur.execute(query)
db.commit()
spaces = cur.fetchall()
if (len(spaces)) == 0:
output="There are no cars inside"
print(output)
else:
spaceID = space_num
query= "select usedID from spaces where spaceID = {0}".format(spaceID)
cur.execute(query)
db.commit()
spaces = cur.fetchall()
if spaces[0][0] == True:
print("<h2>Have a nice day!</h2>")
print("<p>We hope to see you again!</p>")
now = dt.now()
date = now.date()
time = now.time().replace(microsecond=0)
query = "update spaces set date = '{0}', endTime = '{1}', usedID = False where spaceID = {2}".format(date, time, spaceID)
cur.execute(query)
db.commit()
allow_entrance()
else:
print("<h2>This place is already empty!</h2>")
output = "<p>Please input a valid number.</p>"
print(output)
pressed = 1
except IndexError:
print("<h2>This number is out of bounds!</h2>")
output = "Please, input the spot you were assigned when you entered."
print("<script>alert('{0}')</script>".format(output))
print(output)
pressed = 1
elif pressed == 2:
try:
pressed = 0
query = "select spaceID from spaces where usedID = False and reserved = False order by spaceID desc"
cur.execute(query)
db.commit()
spaces = cur.fetchall()
status= "<p>Park your vehicle in slot <b>{0}</b>.".format(spaces[0][0])
print("<h2>Remember to social distance!</h2>")
print(status)
print("Enjoy your visit!</p>")
now = dt.now()
date = now.date()
time = now.time().replace(microsecond=0)
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()
allow_entrance()
except IndexError:
status ="<script>alert('Sorry, but there are no spaces left')</script>"
print(status)
servo.stop()
GPIO.cleanup()
db.close()
sys.exit(0)