diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..ed0fa36 --- /dev/null +++ b/.env.dist @@ -0,0 +1,4 @@ +TOKEN=sigbvddvujkbsihxhktp +LED_PIN=18 +TIMES_TO_BLINK=5 +SECONDS_TO_BLINK_ON=2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/api.py b/api.py new file mode 100644 index 0000000..4e22ef7 --- /dev/null +++ b/api.py @@ -0,0 +1,28 @@ +from flask import Flask, request, make_response, abort +from ledapi import led +from dotenv import load_dotenv +import os + +load_dotenv() +app = Flask(__name__) + +TOKEN = os.getenv('TOKEN') +LED_PIN = os.getenv('LED_PIN') +TIMES_TO_BLINK = os.getenv('TIMES_TO_BLINK') +SECONDS_TO_BLINK_ON = os.getenv('SECONDS_TO_BLINK_ON') + + +@app.route('/warning-light/', methods=['POST']) +def warning_light(token): + if request.method == 'POST': + if token == os.getenv(TOKEN): + led.go(LED_PIN, num_times=TIMES_TO_BLINK, seconds_on=SECONDS_TO_BLINK_ON) + return make_response(('', 202)) + else: + abort(401) + else: + abort(501) + + +if __name__ == '__main__': + app.run(debug=True, port=5000) diff --git a/ledapi/__init__.py b/ledapi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ledapi/led.py b/ledapi/led.py new file mode 100644 index 0000000..ca01e50 --- /dev/null +++ b/ledapi/led.py @@ -0,0 +1,33 @@ +import RPi.GPIO as GPIO +import time + + +def setup(led_pin): + # setup + GPIO.setmode(GPIO.BOARD) + GPIO.setwarnings(False) + GPIO.setup(led_pin, GPIO.OUT, initial=GPIO.LOW) + + +def blink_led(led_pin, num_times=1, seconds_on=1): + i = 1 + while i <= num_times: + # led on + GPIO.output(led_pin, GPIO.HIGH) + time.sleep(seconds_on) + # led off + GPIO.output(led_pin, GPIO.LOW) + + +def tear_down(): + GPIO.cleanup() + + +def go(led_pin, num_times, seconds_on): + setup(led_pin) + blink_led(led_pin, num_times=num_times, seconds_on=seconds_on) + tear_down() + + +if __name__ == '__main__': + go(18, 5, 2) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..91e98b1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +Click==7.0 +Flask==1.0.2 +itsdangerous==1.1.0 +Jinja2==2.10 +MarkupSafe==1.1.0 +pkg-resources==0.0.0 +RPi.GPIO==0.6.5 +Werkzeug==0.14.1