39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
#!/usr/bin/python3
|
|
|
|
from flask import request
|
|
from flask_api import FlaskAPI
|
|
import RPi.GPIO as GPIO
|
|
|
|
from xml_reader import Xml_reader
|
|
from interface_handler import Interface_handler
|
|
|
|
xml_reader = Xml_reader('XML/config.xml','XML/config.xsd')
|
|
interface_handler = Interface_handler(xml_reader)
|
|
|
|
device_names = xml_reader.get_device_names()
|
|
|
|
app = FlaskAPI(__name__)
|
|
|
|
@app.route('/', methods=["GET"])
|
|
def api_root():
|
|
return {
|
|
"device_url": request.url + "device/<device_name>",
|
|
"device_url_POST": {"output": "<value>"}
|
|
}
|
|
|
|
@app.route('/device/<device_name>/', methods=["GET", "POST"])
|
|
def api_leds_control(device_name):
|
|
if request.method == "POST":
|
|
# if color in LEDS:
|
|
# GPIO.output(LEDS[color], int(request.data.get("state")))
|
|
try:
|
|
response = interface_handler.write(device_name,request.data.get("output"))
|
|
return {'output': response}
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
# return {color: GPIO.input(LEDS[color])}
|
|
return {'state': interface_handler.read(device_name)}
|
|
|
|
if __name__ == "__main__":
|
|
app.run(port=8080,host="0.0.0.0")
|