Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active March 9, 2016 21:23
Show Gist options
  • Save LarsBergqvist/5b193e43d33e5e81ced0 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/5b193e43d33e5e81ced0 to your computer and use it in GitHub Desktop.
webrelay.py
import RPi.GPIO as GPIO
from flask import Flask, jsonify, abort, request, render_template
from relaydefinitions import relays, relayIdToPin
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
relayStateToGPIOState = {
'off' : GPIO.LOW,
'on' : GPIO.HIGH
}
def Setup():
for relay in relays:
GPIO.setup(relayIdToPin[relay['id']],GPIO.OUT)
GPIO.output(relayIdToPin[relay['id']],relayStateToGPIOState[relay['state']])
def UpdatePinFromRelayObject(relay):
GPIO.output(relayIdToPin[relay['id']],relayStateToGPIOState[relay['state']])
@app.route('/WebRelay/api/relays', methods=['GET'])
def get_relays():
return jsonify({'relays': relays})
@app.route('/WebRelay/api/relays/<int:relay_id>', methods=['GET'])
def get_relay(relay_id):
matchingRelays = [relay for relay in relays if relay['id'] == relay_id]
if len(matchingRelays) == 0:
abort(404)
return jsonify({'relay': matchingRelays[0]})
@app.route('/WebRelay/api/relays/<int:relay_id>', methods=['PUT'])
def update_relay(relay_id):
matchingRelays = [relay for relay in relays if relay['id'] == relay_id]
if len(matchingRelays) == 0:
abort(404)
if not request.json:
abort(400)
if not 'state' in request.json:
abort(400)
relay = matchingRelays[0]
relay['state']=request.json.get('state')
UpdatePinFromRelayObject(relay)
return jsonify({'relay': relay})
if __name__ == "__main__":
print("starting...")
try:
Setup()
app.run(host='0.0.0.0',port=80,debug=False)
finally:
print("cleaning up")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment