(Probably) implemented transmit API point
This commit is contained in:
36
app.py
36
app.py
@ -4,11 +4,18 @@ A simple Flask application for interacting with WHSPAH shockers.
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import json
|
||||
import os
|
||||
|
||||
import waitress.server
|
||||
import flask
|
||||
|
||||
import whspah
|
||||
|
||||
ACTION_MAP = {'shock': whspah.MODES.SHOCK,
|
||||
'vibrate': whspah.MODES.VIBRATE,
|
||||
'beep': whspah.MODES.BEEP}
|
||||
|
||||
app = flask.Flask(__name__, static_folder='./www/')
|
||||
|
||||
@app.route('/', defaults={'path': 'index.html'})
|
||||
@ -23,6 +30,33 @@ def staticPage(path):
|
||||
|
||||
return flask.send_from_directory(app.static_folder, path)
|
||||
|
||||
@app.route('/transmit', methods=['POST'], strict_slashes=False)
|
||||
def transmit():
|
||||
"""
|
||||
Transmits the data contained within the POST request through WHSPAH.
|
||||
"""
|
||||
|
||||
data = json.loads(flask.request.data)
|
||||
|
||||
try:
|
||||
txID = int(data['transmitterID'])
|
||||
channel = int(data['channel'])
|
||||
action = ACTION_MAP[data['action']]
|
||||
intensity = int(data.get('intensity', 0))
|
||||
lucal = bool(data.get('lucalEncoded', False))
|
||||
except (ValueError, KeyError):
|
||||
return {'success': False, 'message': 'Request must contain the following keys:\n'+
|
||||
'txID: int,\n'+
|
||||
'channel: int,\n'+
|
||||
'action: "shock", "vibrate" or "beep",\n'+
|
||||
'intensity (optional): int\n'+
|
||||
'lucalEncoded (optional): bool'}, 400
|
||||
|
||||
tx: whspah.Transmitter = app.config['transmitter']
|
||||
tx.transmit(txID, channel, action, intensity, lucal)
|
||||
|
||||
return {'success': True}, 200
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
@ -36,6 +70,8 @@ if __name__ == '__main__':
|
||||
else:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
app.config['transmitter'] = whspah.Transmitter()
|
||||
|
||||
if args.debug:
|
||||
app.run(host=args.ip, port=args.port, debug=True)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user