#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras #=================================================================== # using flask restful from flask import Flask, request, jsonify from flask_restful import Resource, Api from json import dumps import json from flask_cors import CORS import mysql.connector from base64 import b64encode from os import urandom # ================================================================== # ================================================================== # creating the flask app app = Flask(__name__) CORS(app) # Session list sessions = [] # Chart values chart = [] # creating an API object api = Api(app) # Initialize the database Connection (Classified) class mySqlConnect: def __init__(self): self.con = con = 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" ) self.cur = self.con.cursor(); def __del__(self): self.cur.close() self.con.close() @property def cursor(self): return self.cur @property def connection(self): return self.con # ================================== # Define our functions. # Define a function that gets the parking status # for all parking codes. def getParkings(): parks = [] sql = mySqlConnect() myCursor = sql.cur myCursor.execute("SELECT * FROM PARKING") myRes = myCursor.fetchall() for res in myRes: if res[1] == 1: parks.append({"no": res[0], "status": True}) else: parks.append({"no": res[0], "status": False}) return parks # Define a function that get if a user with exiting credencials # username and password is authenticated. def isMember(username, password): mysql = mySqlConnect() myCursor = mysql.cur 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 # Function that return if the requested user is authenticated # or not (True / False). def isAuthenticated(data): try: if data['cookie'] in sessions: return True else: return False except KeyError as e: return False # For the chart Data. def updateChart(): parks = getParkings() all_parks = len(parks) full = 0 for p in parks: if p['status'] == False: full+=1 j = 1 if len(chart) < 16 : chart.append(full) print (chart) elif len(chart) == 16: for i in chart: chart[j] = i j+=1 if j == 16: break chart[0] = full return True # ================================================================== # 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. # Resource that returns th whole parking status in a JSON class Parking(Resource): def get(self): parks = None try: parks = getParkings() except (mysql.connector.errors.DatabaseError, mysql.connector.errors.InterfaceError) as e: print ("An error") return parks, 200 # Update parking status resource from authenticated only Node class ParkingStatus(Resource): def get(self): return """