From fb14b3ece414981e5ae51a92c009653b4c58d8d3 Mon Sep 17 00:00:00 2001 From: Evangelos Oulis Date: Tue, 21 Jan 2020 22:57:47 +0200 Subject: [PATCH] Update v1.3 --- gatewayNode/data.json | 5 +- gatewayNode/parking.py | 7 +- serverNode/__pycache__/serv.cpython-37.pyc | Bin 0 -> 3965 bytes serverNode/serv.py | 161 +++++++++++++-------- 4 files changed, 111 insertions(+), 62 deletions(-) create mode 100644 serverNode/__pycache__/serv.cpython-37.pyc diff --git a/gatewayNode/data.json b/gatewayNode/data.json index 9d62f9c..4c2f44e 100644 --- a/gatewayNode/data.json +++ b/gatewayNode/data.json @@ -1,4 +1,7 @@ { "ip": "192.168.89.14", - "port": "8080" + "port": "8080", + "device": "1", + "username": "root", + "password": "root" } diff --git a/gatewayNode/parking.py b/gatewayNode/parking.py index daa906b..f796675 100644 --- a/gatewayNode/parking.py +++ b/gatewayNode/parking.py @@ -31,6 +31,11 @@ if json_data != "" and json_data != None: ser.readline() prev_status = "-1" + + device_session = requests.session() + data = """{"username" :""" + server_par['username'] + """, "password":""" + server_par['password'] + """, "device": """ + server_par['device'] + """}""" + + s.post(url = API_ENDPOINT, data = data) while 1: park_status = ser.readline() park_status_data = str(park_status).split("#") @@ -41,7 +46,7 @@ if json_data != "" and json_data != None: try: if parkingStatus != prev_status: data = """{"no":""" + parkingCode + ""","status":""" + parkingStatus + """}""" - r = requests.post(url = API_ENDPOINT, data = data) + r = s.post(url = API_ENDPOINT, data = data) if parkingStatus == "0": print("parking reserved. RESPONSE :", r.status_code, "\n") elif parkingStatus == "1": diff --git a/serverNode/__pycache__/serv.cpython-37.pyc b/serverNode/__pycache__/serv.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ea353662f537db738fe0d18e056c6f33a77627bc GIT binary patch literal 3965 zcma)9TXPf16`r0MjYhJ32V=m%z%Bt1VgZ#9ia0L7*sQZ4yDS;9v)R>-)omhGJcpvgYg-TvhsjZa#xPKzg$v;TVYo7cU_9@?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gVq1Za1RJ^{dWNRIXKEFdj9iX58R>kx?{a# z;Y>6}j&WAoV@kvPdu&_p+R^y_1h+dj1=%RQKY4(;d*`m->m*^5{PFnXIRHoDEERZJmt%v`{F0y)iL6O8|=d+;ccv94_0o=nRI`4eGzE&uH;pB#!o$ZPY zqxs5T0mST-V%FHC93@WF0*{B%!0fR}DTHDx`7_id1wwh9M3%%25Xj2zRX9l-T}J&~;7nPB)2gAjN9v+EK9C zz;;Azz4mIzsea&!vDrWp^f_hUqTZtLx-lU_{=<+BNy!!q&~iQ6O;RsM!F#gfNDF~uEpY@&q3V^}32spZ3r z`4iDBGaMiX+(L5>xaA+Hw6-Ysh!Dt#Rp5mTX&ZU>AD~l8ccfD~r}W6|jq3I9>6HA* z=cr*s1QuBUYYAbvvU8=;ywR+isbkG7k zpCgJMI%OqzEu7wv9$NMGDFv@#m?NQ9ox{=h*ln{JmD0#x(V7Hosjx@MOfMPvNpNS) zA-rCscL=XTm#prWF8&>gN<}oL1AQN}DIjc2w{#mQY)=jyO3e2_Wd0CDKZ>6P%>8GY zzaMX@{yodJ$4*AE_UH*Wrx@>hm3paEbj?pd197Srl)V~#Fu3OW+6J2M-MV*+@@X!% znlEJr^O7&gDEm>I*ru--NlW(ZMnF?0b4OKI32Aq<-KOAn{G(KFUh)TPn<)Wve$R|$rUsl`$Mo}LM zJ&rj~tE)S`Od2I9%Eu&`%FTG3({B48T(Vh@!AJG&%F7?kabkO+mlZA2u|t|>7Sur` pp@_nQ8ygwJHY`vI{cOXw>`^;oXC_h;_a{zeFfL=J73E~u{{Z=za9aQX literal 0 HcmV?d00001 diff --git a/serverNode/serv.py b/serverNode/serv.py index a80bdc5..dc321b2 100644 --- a/serverNode/serv.py +++ b/serverNode/serv.py @@ -7,6 +7,7 @@ from json import dumps import json from flask_cors import CORS import mysql.connector +import os # ================================================================== # ================================================================== @@ -15,6 +16,9 @@ import mysql.connector app = Flask(__name__) CORS(app) +# create the secret key for session +app.secret_key = os.urandom(24) + # creating an API object api = Api(app) @@ -33,17 +37,17 @@ myCursor = mydb.cursor() # Define a function that gets the parking status # for all parking codes. def getParkings(): - parks = [] - - myCursor.execute("SELECT * FROM PARKING") - myRes = myCursor.fetchall() + parks = [] + + 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 + 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 def isMember(username, password): myCursor.execute("SELECT * FROM USERS") @@ -57,6 +61,12 @@ def isMember(username, password): return isValid +def isAuthenticated(): + if 'device_id' in session: + return True + else: + return False + # ================================================================== # making a class for a particular resource @@ -64,64 +74,95 @@ def isMember(username, password): # they are automatically mapped by flask_restful. # other methods include put, delete, etc. class Parking(Resource): - def get(self): - parks = getParkings() - return parks, 200 + def get(self): + try: + parks = getParkings() + except mysql.connector.errors.DatabaseError as e: + mydb.reconnect(attempts=1, delay=0) + + return parks, 200 class ParkingStatus(Resource): - def get(self): - return """ - ERROR -

Not get at '/parkingStatus'.

- """ - def post(self): - # Gets the data into as a JSON Object from HTTP request. - data = json.loads(request.data) - - # SQL get all Parking places status. - parks = getParkings() - - currentParking = {} - for park in parks: - if park['no'] == data['no']: - currentParking = park - break; - - thereIs = False - toUpdate = False - try: - if currentParking['status'] != data['status']: - toUpdate = True - thereIs = True - except IndexError: - # handle Index Error - thereIs = False - toUpdate = False - except KeyError: - # handle the KeyError - thereIs = False - toUpdate = False - - 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() - - return currentParking, 201 + def get(self): + return """ + ERROR +

Not get at '/parkingStatus'.

+ """ + def post(self): + if isAuthenticated(): + # Gets the data into as a JSON Object from HTTP request. + data = json.loads(request.data) + + try: + # SQL get all Parking places status. + parks = getParkings() + except mysql.connector.errors.DatabaseError as e: + mydb.reconnect(attempts=1, delay=0) + + currentParking = {} + for park in parks: + if park['no'] == data['no']: + currentParking = park + break; + + thereIs = False + toUpdate = False + try: + if currentParking['status'] != data['status']: + toUpdate = True + thereIs = True + except IndexError: + # handle Index Error + thereIs = False + toUpdate = False + except KeyError: + # handle the KeyError + thereIs = False + toUpdate = False + + try: + 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() + except mysql.connector.errors.DatabaseError as e: + mydb.reconnect(attempts=1, delay=0) + + return currentParking, 201 + else: + return "Error! You aren't authenticated. [POST] /authenticate first.", 403 +class Authenticate(Resource): + def post(self): + try: + #Get the credencial from body of request. + data = json.loads(request.data) + + if data['username'] != None and data['password'] != None and data['device'] != None: + isValid = isMember(data['username'], data['password']) + + if isValid: + session['device_id'] = data['device'] + else: + return "Not Authenticatiove device", 403 + else: + return "Error authentication", 403 + except mysql.connector.errors.DatabaseError as e: + mydb.reconnect(attempts=1, delay=0) # ================================================================== # adding the defined resources along with their corresponding urls to REST APIs api.add_resource(Parking, '/') api.add_resource(ParkingStatus, '/parkingStatus') +api.add_resource(Authenticate, '/authenticate') # ================================================================== # driver function