|
|
@ -1,11 +1,24 @@ |
|
|
|
#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras |
|
|
|
#=================================================================== |
|
|
|
#!/usr/bin/env python |
|
|
|
|
|
|
|
import time |
|
|
|
import serial |
|
|
|
import requests |
|
|
|
import json |
|
|
|
|
|
|
|
API_ENDPOINT = 'http://192.168.1.13:8080/parkingStatus' |
|
|
|
parkingCode = "1" |
|
|
|
# Open file to configure communication with server. |
|
|
|
with open('data.json', 'r') as json_file: |
|
|
|
json_data = json_file.read() |
|
|
|
|
|
|
|
if json_data != "" and json_data != None: |
|
|
|
server_par = json.loads(json_data.replace('\n','').replace(' ','')) |
|
|
|
|
|
|
|
server_ip = server_par['ip'] |
|
|
|
server_port = server_par['port'] |
|
|
|
|
|
|
|
if server_ip != None and server_port != None: |
|
|
|
API_ENDPOINT = 'http://' + server_ip + ':' + server_port + '/parkingStatus' |
|
|
|
ser = serial.Serial( |
|
|
|
port='/dev/ttyACM0', |
|
|
|
baudrate = 9600, |
|
|
@ -19,17 +32,22 @@ counter=0 |
|
|
|
prev_status = ser.readline() |
|
|
|
while 1: |
|
|
|
park_status = ser.readline() |
|
|
|
park_status_data = str(park_status).split("#") |
|
|
|
|
|
|
|
parkingCode = park_status_data[0].replace('b\'','') |
|
|
|
parkingStatus = park_status_data[1].replace('\\r\\n\'', '') |
|
|
|
|
|
|
|
try: |
|
|
|
if park_status != prev_status: |
|
|
|
if b'1' in park_status: |
|
|
|
data = """{"no":""" + parkingCode + ""","status":1}""" |
|
|
|
r = requests.post(url = API_ENDPOINT, data = data) |
|
|
|
if parkingStatus == "1": |
|
|
|
print("parking reserved. RESPONSE :", r.status_code, "\n") |
|
|
|
elif b'0' in park_status: |
|
|
|
data = """{"no":""" + parkingCode + ""","status":0}""" |
|
|
|
r = requests.post(url = API_ENDPOINT, data = data) |
|
|
|
elif parkingStatus == "0": |
|
|
|
print("parking not reserved. RESPONSE : ", r.status_code, "\n") |
|
|
|
data = """{"no":""" + parkingCode + ""","status":""" + parkingStatus + """}""" |
|
|
|
r = requests.post(url = API_ENDPOINT, data = data) |
|
|
|
except NameError: |
|
|
|
print("**Not already prev variable.\n") |
|
|
|
prev_status = park_status |
|
|
|
else: |
|
|
|
print("Create a *.json configuration like: {'ip' : 'xxx.xxx.xxx.xxx', 'port': 'xxxx'}") |
|
|
|
|
|
|
|