|
|
|
#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras
|
|
|
|
#===================================================================
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import time
|
|
|
|
import serial
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
# Open file to configure communication with server.
|
|
|
|
with open('/home/pi/project/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(' ',''))
|
|
|
|
|
|
|
|
# Initialize server IP and Port to make the connection.
|
|
|
|
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'
|
|
|
|
|
|
|
|
# Initialize Serial Connection with Arduino Uno
|
|
|
|
ser = serial.Serial(
|
|
|
|
port='/dev/ttyACM0',
|
|
|
|
baudrate = 9600,
|
|
|
|
parity=serial.PARITY_NONE,
|
|
|
|
stopbits=serial.STOPBITS_ONE,
|
|
|
|
bytesize=serial.EIGHTBITS,
|
|
|
|
timeout=1
|
|
|
|
)
|
|
|
|
counter=0
|
|
|
|
|
|
|
|
ser.readline()
|
|
|
|
prev_status = "-1"
|
|
|
|
|
|
|
|
# Authentication body content.
|
|
|
|
data = """{"username" :""" + server_par['username'] + """, "password":""" + server_par['password'] + """, "device": """ + server_par['device'] + """}"""
|
|
|
|
|
|
|
|
# Request to Authenticate Node.
|
|
|
|
resp = requests.post(url = API_ENDPOINT, data = data)
|
|
|
|
|
|
|
|
# Unpack our data to a JSON object
|
|
|
|
data = json.loads(resp.content)
|
|
|
|
try:
|
|
|
|
# Check if node is Authenticated. "cookie
|
|
|
|
if data['cookie'] != None:
|
|
|
|
while 1:
|
|
|
|
# For a while we request to get Parking Status
|
|
|
|
# witch is a JSON object like [{"no": 1, "status": false}, {"no": 2, "status": true}, ..., {"no": n, "status": true}]
|
|
|
|
park_status = ser.readline()
|
|
|
|
|
|
|
|
# Substring the input data from arduino.
|
|
|
|
# Data's notation is: <parking_no>#<parking_status>
|
|
|
|
park_status_data = str(park_status).split("#")
|
|
|
|
|
|
|
|
# Decode and clear serial input data encoding.
|
|
|
|
parkingCode = park_status_data[0].replace('b\'','')
|
|
|
|
parkingStatus = park_status_data[1].replace('\\r\\n\'', '')
|
|
|
|
|
|
|
|
try:
|
|
|
|
if parkingStatus != prev_status:
|
|
|
|
# If parking status changed from previous status
|
|
|
|
# server would informed about this change.
|
|
|
|
data = """{"no":""" + parkingCode + ""","status":""" + parkingStatus + """}"""
|
|
|
|
r = s.post(url = API_ENDPOINT, data = data)
|
|
|
|
|
|
|
|
# IO : node prints to output the current
|
|
|
|
if parkingStatus == "0":
|
|
|
|
print("parking reserved. RESPONSE :", r.status_code, "\n")
|
|
|
|
elif parkingStatus == "1":
|
|
|
|
print("parking not reserved. RESPONSE : ", r.status_code, "\n")
|
|
|
|
except NameError as e:
|
|
|
|
print("**Not already prev variable. <Error> : ", str(e), "\n")
|
|
|
|
prev_status = parkingStatus
|
|
|
|
except TypeError as e:
|
|
|
|
print("Node must be authenticate first.")
|
|
|
|
else:
|
|
|
|
print("Create a *.json configuration like: {'ip' : 'xxx.xxx.xxx.xxx', 'port': 'xxxx'}")
|
|
|
|
|