Smart and Autonomous parking.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.0 KiB

#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras
5 years ago
#===================================================================
# using flask restful
from flask import Flask, request, jsonify, session
from flask_restful import Resource, Api
from json import dumps
import json
from flask_cors import CORS
5 years ago
import mysql.connector
# ==================================================================
# ==================================================================
# creating the flask app
app = Flask(__name__)
CORS(app)
# creating an API object
api = Api(app)
5 years ago
# Initialize the database Connection
mydb = mysql.connector.connect(
host = "127.0.0.1",#"q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com",
user = "root",#"zsgmj50h7zgz9ioq",
password = "rootP",#"omk5l1hrwsgvlcez",
database = "PARKING"#"g0s9cnmdkziq6fsp"
5 years ago
)
myCursor = mydb.cursor()
# ==================================
# Define our functions.
# Define a function that gets the parking status
# for all parking codes.
def getParkings():
5 years ago
parks = []
5 years ago
myCursor.execute("SELECT * FROM PARKING")
myRes = myCursor.fetchall()
for res in myRes:
5 years ago
if res[1] == 1:
parks.append({"no": res[0], "status": True})
else:
parks.append({"no": res[0], "status": False})
5 years ago
return parks
def isMember(username, password):
myCursor.execute("SELECT * FROM USERS")
myRes = myCursor.fetchall()
isValid = False
for res in myRes:
if res[1] == username and res[2] == password:
isValid = True
break
return isValid
# ==================================================================
# making a class for a particular resource
# the get, post methods correspond to get and post requests
# they are automatically mapped by flask_restful.
# other methods include put, delete, etc.
class Parking(Resource):
def get(self):
5 years ago
parks = getParkings()
return parks, 200
class ParkingStatus(Resource):
def get(self):
return """<html>
<head><title>ERROR</title></head>
<body><h1>Not get at '/parkingStatus'.</h1></body>
</html>"""
def post(self):
5 years ago
# Gets the data into as a JSON Object from HTTP request.
data = json.loads(request.data)
5 years ago
# SQL get all Parking places status.
parks = getParkings()
5 years ago
currentParking = {}
for park in parks:
if park['no'] == data['no']:
currentParking = park
break;
5 years ago
thereIs = False
toUpdate = False
try:
5 years ago
if currentParking['status'] != data['status']:
5 years ago
toUpdate = True
thereIs = True
except IndexError:
# handle Index Error
thereIs = False
5 years ago
toUpdate = False
5 years ago
except KeyError:
# handle the KeyError
thereIs = False
5 years ago
toUpdate = False
5 years ago
if not thereIs:
# Make a new insert entry for a new Parking Code.
values = (int(data['no']), int(data['status']))
myCursor.execute("INSERT INTO PARKING (PARKING_CODE, PARKING_STATUS) VALUES (%s, %s)", values)
mydb.commit()
parks = getParkings()
elif toUpdate:
# Make an Update status for Parking Code that availability changed.
values = (int(data['status']), int(data['no']))
myCursor.execute("UPDATE PARKING SET PARKING_STATUS=%s WHERE PARKING_CODE=%s", values)
mydb.commit()
parks = getParkings()
5 years ago
return currentParking, 201
# ==================================================================
5 years ago
# adding the defined resources along with their corresponding urls to REST APIs
api.add_resource(Parking, '/')
api.add_resource(ParkingStatus, '/parkingStatus')
# ==================================================================
# driver function
if __name__ == '__main__':
app.run(
debug=True,
host=app.config.get("HOST", "localhost"),
port=app.config.get("PORT", "8080")
)
# END