cs161079
5 years ago
6 changed files with 111 additions and 51 deletions
@ -0,0 +1,4 @@ |
|||||
|
{ |
||||
|
"ip": "192.168.89.14", |
||||
|
"port": "8080" |
||||
|
} |
@ -1,35 +1,53 @@ |
|||||
|
#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras |
||||
|
#=================================================================== |
||||
#!/usr/bin/env python |
#!/usr/bin/env python |
||||
|
|
||||
import time |
import time |
||||
import serial |
import serial |
||||
import requests |
import requests |
||||
|
import json |
||||
|
|
||||
API_ENDPOINT = 'http://192.168.1.13:8080/parkingStatus' |
# Open file to configure communication with server. |
||||
parkingCode = "1" |
with open('data.json', 'r') as json_file: |
||||
ser = serial.Serial( |
json_data = json_file.read() |
||||
port='/dev/ttyACM0', |
|
||||
baudrate = 9600, |
|
||||
parity=serial.PARITY_NONE, |
|
||||
stopbits=serial.STOPBITS_ONE, |
|
||||
bytesize=serial.EIGHTBITS, |
|
||||
timeout=1 |
|
||||
) |
|
||||
counter=0 |
|
||||
|
|
||||
prev_status = ser.readline() |
if json_data != "" and json_data != None: |
||||
while 1: |
server_par = json.loads(json_data.replace('\n','').replace(' ','')) |
||||
park_status = ser.readline() |
|
||||
try: |
server_ip = server_par['ip'] |
||||
if park_status != prev_status: |
server_port = server_par['port'] |
||||
if b'1' in park_status: |
|
||||
data = """{"no":""" + parkingCode + ""","status":1}""" |
if server_ip != None and server_port != None: |
||||
r = requests.post(url = API_ENDPOINT, data = data) |
API_ENDPOINT = 'http://' + server_ip + ':' + server_port + '/parkingStatus' |
||||
print("parking reserved. RESPONSE :", r.status_code, "\n") |
ser = serial.Serial( |
||||
elif b'0' in park_status: |
port='/dev/ttyACM0', |
||||
data = """{"no":""" + parkingCode + ""","status":0}""" |
baudrate = 9600, |
||||
|
parity=serial.PARITY_NONE, |
||||
|
stopbits=serial.STOPBITS_ONE, |
||||
|
bytesize=serial.EIGHTBITS, |
||||
|
timeout=1 |
||||
|
) |
||||
|
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 parkingStatus == "1": |
||||
|
print("parking reserved. RESPONSE :", r.status_code, "\n") |
||||
|
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) |
r = requests.post(url = API_ENDPOINT, data = data) |
||||
print("parking not reserved. RESPONSE : ", r.status_code, "\n") |
except NameError: |
||||
except NameError: |
print("**Not already prev variable.\n") |
||||
print("**Not already prev variable.\n") |
prev_status = park_status |
||||
prev_status = park_status |
else: |
||||
|
print("Create a *.json configuration like: {'ip' : 'xxx.xxx.xxx.xxx', 'port': 'xxxx'}") |
||||
|
|
||||
|
Loading…
Reference in new issue