100 lines
3.3 KiB
Python
100 lines
3.3 KiB
Python
#!/usr/bin/python3
|
|
|
|
from flask import request, send_from_directory
|
|
from flask_api import FlaskAPI
|
|
import RPi.GPIO as GPIO
|
|
import shutil
|
|
import os
|
|
|
|
#local imports
|
|
from xml_reader import Xml_reader
|
|
from interface_handler import Interface_handler
|
|
|
|
# setup xml_reader and interface_handler
|
|
dir = os.path.dirname(os.path.abspath(__file__))
|
|
xml_reader = Xml_reader(str(dir + '/XML/config.xml'),
|
|
str(dir + '/XML/config.xsd'))
|
|
interface_handler = Interface_handler(xml_reader)
|
|
|
|
app = FlaskAPI(__name__)
|
|
|
|
@app.route('/', methods=["GET"])
|
|
def api_root():
|
|
"""method for the root of the API
|
|
|
|
Returns:
|
|
html: a welcom message
|
|
"""
|
|
return "<h1>Success. You have reached the root of the APED API.</h1>"
|
|
|
|
@app.route('/XML/', methods=["GET","POST"])
|
|
def xml_transfer():
|
|
"""method for transfering the config.xml files to and from the raspberry
|
|
|
|
Returns:
|
|
the config.xml or the occured errors
|
|
"""
|
|
try:
|
|
#return the current config.xml
|
|
if request.method == "GET":
|
|
return send_from_directory(str(dir + '/XML'), 'config.xml')
|
|
|
|
# set a new config.xml
|
|
if request.method == "POST":
|
|
if not os.path.exists('/tmp/aped_device'):
|
|
os.mkdir("/tmp/aped_device")
|
|
tmp_file = open("/tmp/aped_device/config.xml","w")
|
|
data = request.get_data(as_text=True)
|
|
tmp_file.write(data)
|
|
tmp_file.close()
|
|
#check config.xml against the XSD file
|
|
if xml_reader.validate('/tmp/aped_device/config.xml',
|
|
str(dir + '/XML/config.xsd')):
|
|
shutil.move(str(dir + '/XML/config.xml'),
|
|
str(dir + '/XML/config_old.xml'))
|
|
shutil.move('/tmp/aped_device/config.xml',
|
|
str(dir + '/XML/config.xml'))
|
|
xml_reader.set_root(str(dir + '/XML/config.xml'))
|
|
interface_handler.init_gpio()
|
|
else:
|
|
SyntaxError('the config.xml has invalid content')
|
|
return {'error': ''}
|
|
except Exception as e:
|
|
return {'error' : str(e)}
|
|
|
|
@app.route('/device/', methods=["GET"])
|
|
def api_general_device_info():
|
|
"""method to read all the available device names
|
|
|
|
Returns:
|
|
JSON: the available device names
|
|
"""
|
|
return {"device_names": str(xml_reader.get_device_names())}
|
|
|
|
@app.route('/device/<device_name>/', defaults={'buffering': None},
|
|
methods=["GET", "POST"])
|
|
@app.route('/device/<device_name>/<buffering>/', methods=["GET", "POST"])
|
|
def api_devices(device_name: str, buffering: str):
|
|
"""the method for reading and writing to and from the connected devices
|
|
|
|
Args:
|
|
device_name (str): the name of the relevant device
|
|
buffering (str): whether or not the buffer of the device is requested
|
|
|
|
Returns:
|
|
the relevant information about the device in JSON format
|
|
"""
|
|
try:
|
|
if request.method == "POST":
|
|
response = interface_handler.write(device_name,
|
|
request.data.get("output"))
|
|
if buffering == 'buffer':
|
|
return {'buffer': interface_handler.read_buffer(device_name)}
|
|
else:
|
|
return {'state': interface_handler.read(device_name)}
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
if __name__ == "__main__":
|
|
app.run(port=8080,host="0.0.0.0")
|