code to call recorder functions from frointend

This commit is contained in:
2019-12-12 18:44:07 +01:00
parent da200f95b8
commit bb4db25dcd
6 changed files with 160 additions and 79 deletions

View File

@@ -12,13 +12,14 @@ from flask_jwt_extended import jwt_required, get_current_user, get_jwt_claims
from flask_restplus import fields, Resource
from backend import db
from backend.api import api_control, get_jwt_identity
from backend.api import api_control, get_jwt_identity, Recorder, RecorderCommand, pprint
from backend.recorder_adapters.helpers import execute_recorder_command
control_command_response_model = api_control.model('Control Command Response', {
'time': fields.DateTime(required=False, description='Creation date of the recorder'),
'state': fields.String(min_length=3, required=True, description='The recorder\'s name'),
'output': fields.String(required=False, description='The recorder\'s description'),
'error': fields.String(required=False, description='The recorder\'s description'),
'ok': fields.Boolean(required=True, description='Field indicating whether command execution was successful.'),
'output': fields.String(required=False, description='Command output in case of success'),
'error': fields.String(required=False, description='Error description in case of a problem.'),
})
@@ -27,16 +28,28 @@ class ControlCommand(Resource):
control_command_parser = api_control.parser()
control_command_parser.add_argument('recorder_id', type=int, default=1, required=True)
control_command_parser.add_argument('command_id', type=int, default=1, required=True)
control_command_parser.add_argument('parameters', default=json.dumps({'p1': 'v1'}), type=dict, required=False,
control_command_parser.add_argument('parameters', default={}, type=dict, required=False,
location='json')
@jwt_required
#@jwt_required
@api_control.doc('run_command')
@api_control.expect(control_command_parser)
@api_control.marshal_with(control_command_response_model, skip_none=False, code=201)
def post(self):
print(get_current_user())
print(get_jwt_identity())
current_user = {'user': get_current_user(), 'claims': get_jwt_claims()}
#print(get_current_user())
#print(get_jwt_identity())
#current_user = {'user': get_current_user(), 'claims': get_jwt_claims()}
#TODO: right check! (acl, etc.)
args = self.control_command_parser.parse_args()
return {'time': datetime.utcnow(), 'output': args, 'state': current_user}
recorder = Recorder.get_by_identifier(args.get('recorder_id'))
if recorder is None:
api_control.abort(404, "Recorder not found!")
command = RecorderCommand.get_by_identifier(args.get('command_id'))
if command is None:
api_control.abort(404, "Command not found!")
success, output = execute_recorder_command(recorder, command, args.get('parameters', None))
if success:
return {'time': datetime.utcnow(), 'output': output, 'ok': success}
return {'time': datetime.utcnow(), 'error': output, 'ok': success}