2018-09-20 13:04:02 +00:00
|
|
|
from flask_restful import abort
|
2018-05-08 14:45:09 +00:00
|
|
|
from marshmallow import Schema, fields
|
|
|
|
from webargs.flaskparser import use_args
|
|
|
|
from flasgger import swag_from
|
2018-09-19 21:31:21 +00:00
|
|
|
from flask import g, request
|
2018-10-06 11:50:53 +00:00
|
|
|
import app.devices.api as devices
|
2018-10-06 12:07:40 +00:00
|
|
|
from app.api.auth_protection import ProtectedResource
|
2018-10-08 19:50:34 +00:00
|
|
|
from app.api.schemas import BaseResourceSchema
|
2018-05-08 14:45:09 +00:00
|
|
|
|
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
class BasicDeviceTypeSchema(Schema):
|
2018-09-17 22:51:26 +00:00
|
|
|
id = fields.Integer(dump_only=True)
|
|
|
|
name = fields.Str(required=True)
|
|
|
|
|
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
class DeviceTypeSchema(BaseResourceSchema, BasicDeviceTypeSchema):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceSchema(BaseResourceSchema):
|
2018-05-08 14:45:09 +00:00
|
|
|
id = fields.Integer(dump_only=True)
|
|
|
|
name = fields.Str(required=True)
|
2018-10-08 19:50:34 +00:00
|
|
|
device_type = fields.Nested(BasicDeviceTypeSchema, dump_only=True)
|
2018-09-19 20:13:14 +00:00
|
|
|
device_type_id = fields.Integer(load_only=True, missing=1)
|
2018-05-08 14:45:09 +00:00
|
|
|
|
|
|
|
|
2018-09-19 22:23:22 +00:00
|
|
|
class DeviceWithConfigurationSchema(DeviceSchema):
|
|
|
|
configuration = fields.Raw(dump_only=True)
|
|
|
|
|
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
class RecordingsSchema(BaseResourceSchema):
|
2018-05-08 14:45:09 +00:00
|
|
|
recorded_at = fields.DateTime()
|
|
|
|
record_type = fields.Integer()
|
2018-10-30 19:16:27 +00:00
|
|
|
record_value = fields.Float()
|
|
|
|
|
|
|
|
|
|
|
|
class RecordingsQuerySchema(Schema):
|
|
|
|
selections = fields.Raw()
|
|
|
|
filters = fields.Raw()
|
|
|
|
groups = fields.Raw()
|
2018-05-08 14:45:09 +00:00
|
|
|
|
|
|
|
|
2018-10-29 21:47:03 +00:00
|
|
|
class DeviceSecretSchema(BaseResourceSchema):
|
|
|
|
device_secret = fields.String(dump_only=True)
|
|
|
|
secret_algorithm = fields.String()
|
|
|
|
|
|
|
|
|
2018-09-20 13:04:02 +00:00
|
|
|
def validate_device_ownership(device_id):
|
|
|
|
if not devices.can_user_access_device(g.current_account.id, device_id):
|
|
|
|
abort(403, message='You are not allowed to access this device',
|
|
|
|
status='error')
|
|
|
|
|
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
class DeviceResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_device_spec.yaml')
|
|
|
|
def get(self, device_id):
|
2018-09-20 13:04:02 +00:00
|
|
|
validate_device_ownership(device_id)
|
2018-10-24 20:11:55 +00:00
|
|
|
return DeviceWithConfigurationSchema().dump(
|
2018-10-08 19:50:34 +00:00
|
|
|
devices.get_device(device_id)), 200
|
2018-05-08 14:45:09 +00:00
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
@swag_from('swagger/delete_device_spec.yaml')
|
|
|
|
def delete(self, device_id):
|
2018-09-20 13:04:02 +00:00
|
|
|
validate_device_ownership(device_id)
|
2018-09-16 20:50:54 +00:00
|
|
|
devices.delete_device(device_id)
|
|
|
|
return '', 204
|
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
|
2018-09-17 22:51:26 +00:00
|
|
|
class DeviceTypeResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_device_type_spec.yaml')
|
|
|
|
def get(self, device_type_id):
|
2018-10-08 19:50:34 +00:00
|
|
|
return DeviceTypeSchema().dump(
|
|
|
|
devices.get_device_type(device_type_id)), 200
|
2018-09-17 22:51:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceTypeListResource(ProtectedResource):
|
2018-10-08 19:50:34 +00:00
|
|
|
@use_args(DeviceTypeSchema(), locations=('json',))
|
2018-09-17 22:51:26 +00:00
|
|
|
@swag_from('swagger/create_device_type_spec.yaml')
|
|
|
|
def post(self, args):
|
2018-09-20 13:04:02 +00:00
|
|
|
if g.current_account.role_id != 1:
|
|
|
|
abort(403, message='Only admin may create device types',
|
|
|
|
status='error')
|
2018-10-24 20:11:55 +00:00
|
|
|
created_device_type = devices.create_device_type(
|
2018-09-17 22:51:26 +00:00
|
|
|
args['name'])
|
2018-10-24 20:11:55 +00:00
|
|
|
return DeviceTypeSchema().dump(created_device_type), 201
|
2018-09-17 22:51:26 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/get_device_types_spec.yaml')
|
|
|
|
def get(self):
|
2018-10-08 19:50:34 +00:00
|
|
|
return DeviceTypeSchema().dump(devices.get_device_types(),
|
|
|
|
many=True), 200
|
2018-09-17 22:51:26 +00:00
|
|
|
|
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
class DeviceRecordingResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_device_recordings_spec.yaml')
|
|
|
|
def get(self, device_id):
|
2018-09-20 13:04:02 +00:00
|
|
|
validate_device_ownership(device_id)
|
2018-09-22 16:14:45 +00:00
|
|
|
request_args = request.args
|
2018-10-08 19:50:34 +00:00
|
|
|
return RecordingsSchema().dump(
|
2018-09-22 16:14:45 +00:00
|
|
|
devices.get_device_recordings_filtered(
|
|
|
|
device_id,
|
|
|
|
request_args.get('record_type'),
|
|
|
|
request_args.get('start_date'),
|
2018-10-08 19:50:34 +00:00
|
|
|
request_args.get('end_date')), many=True), 200
|
2018-05-08 14:45:09 +00:00
|
|
|
|
2018-09-20 22:16:01 +00:00
|
|
|
@swag_from('swagger/create_device_recording_spec.yaml')
|
|
|
|
def post(self, device_id):
|
|
|
|
validate_device_ownership(device_id)
|
2018-10-24 20:11:55 +00:00
|
|
|
created_recording = devices.create_recording_and_return(
|
|
|
|
device_id, request.json)
|
|
|
|
return RecordingsSchema().dump(created_recording), 201
|
2018-09-20 22:16:01 +00:00
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
|
2018-10-30 19:16:27 +00:00
|
|
|
class DeviceRecordingQueryResource(ProtectedResource):
|
|
|
|
@use_args(RecordingsQuerySchema(), locations=('json',))
|
|
|
|
@swag_from('swagger/create_device_recording_query_spec.yaml')
|
|
|
|
def post(self, args, device_id):
|
|
|
|
validate_device_ownership(device_id)
|
|
|
|
try:
|
|
|
|
return devices.run_custom_query(device_id, args), 200
|
|
|
|
except ValueError as e:
|
|
|
|
abort(400, message=str(e), status='error')
|
|
|
|
|
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
class DeviceListResource(ProtectedResource):
|
2018-10-08 19:50:34 +00:00
|
|
|
@use_args(DeviceSchema(), locations=('json',))
|
2018-05-08 14:45:09 +00:00
|
|
|
@swag_from('swagger/create_device_spec.yaml')
|
|
|
|
def post(self, args):
|
2018-10-24 20:11:55 +00:00
|
|
|
created_device = devices.create_device(
|
2018-08-24 12:32:31 +00:00
|
|
|
args['name'],
|
2018-09-19 20:13:14 +00:00
|
|
|
g.current_account.id,
|
|
|
|
args['device_type_id'])
|
2018-10-24 20:11:55 +00:00
|
|
|
return DeviceSchema().dump(created_device), 201
|
2018-08-24 12:32:31 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/get_devices_spec.yaml')
|
|
|
|
def get(self):
|
2018-10-08 19:50:34 +00:00
|
|
|
return DeviceSchema().dump(
|
|
|
|
devices.get_devices(g.current_account.id), many=True), 200
|
2018-09-19 21:31:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceConfigurationResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/update_device_configuration_spec.yaml')
|
|
|
|
def put(self, device_id):
|
2018-09-20 13:04:02 +00:00
|
|
|
validate_device_ownership(device_id)
|
2018-10-24 20:11:55 +00:00
|
|
|
updated_device = devices.set_device_configuration(
|
|
|
|
device_id, request.json)
|
|
|
|
return DeviceWithConfigurationSchema().dump(
|
|
|
|
updated_device), 200
|
2018-09-19 21:31:21 +00:00
|
|
|
|
|
|
|
@swag_from('swagger/get_device_configuration_spec.yaml')
|
|
|
|
def get(self, device_id):
|
2018-09-20 13:04:02 +00:00
|
|
|
validate_device_ownership(device_id)
|
2018-09-19 21:31:21 +00:00
|
|
|
return devices.get_device_configuration(device_id), 200
|
2018-10-29 21:47:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceSecretResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/get_device_secret_spec.yaml')
|
|
|
|
def get(self, device_id):
|
|
|
|
validate_device_ownership(device_id)
|
|
|
|
return DeviceSecretSchema().dump(devices.get_device(device_id)), 200
|