2020-07-06 23:41:26 +02:00

57 lines
1.9 KiB
Python

from flask_restful import Resource, Api, reqparse
from flask import request
from bson import json_util, ObjectId
import json
import echo_db.database as EchoDB
import echo_api.auth as Auth
import echo_controller.controller as Controller
class DeviceInfoAPI(Resource):
def get(self, uuid):
# Verify Auth
if not Auth.Auth.verify(request):
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
# Get Device
collection = EchoDB.EchoDB.database['Devices']
device = collection.find_one({"uuid": uuid})
# Check if Device Exists
if device == None:
return {'status': 'failed', 'message': 'Device not found!', 'data': {}}, 404
# Format and respond
device_json = json.loads(json_util.dumps(device))
return {'status': 'success', 'data': device_json}, 200
class DeviceControlAPI(Resource):
def post(self):
if not Auth.Auth.verify(request):
return {'status': 'failed', 'message': 'Not Authenticated!'}, 401
parser = reqparse.RequestParser()
parser.add_argument('uuid', required = True)
parser.add_argument('action', required = True)
parser.add_argument('data', required = False, default = {})
# Parse the arguments into an object
args = parser.parse_args()
# Get Device
collection = EchoDB.EchoDB.database['Devices']
device = collection.find_one({'uuid': args['uuid']})
# Check if Device Exists
if device == None:
return {'status': 'failed', 'message': 'Device not found!'}, 200
if args['action'] not in device['actions']:
return {'status': 'failed', 'message': 'Action not found!'}, 200
# Send Action to Echo Controller
return Controller.Controller.handle_action(device, args['action'], args['data'])