Add latest device recording route
parent
609086cb8a
commit
d9c66ed0ab
|
@ -19,6 +19,7 @@ def add_resources():
|
|||
from .resources.token import TokenResource, ValidateTokenResource
|
||||
from .resources.device import (DeviceResource,
|
||||
DeviceRecordingResource,
|
||||
DeviceLatestRecordingResource,
|
||||
DeviceRecordingQueryResource,
|
||||
DeviceListResource,
|
||||
DeviceTypeResource,
|
||||
|
@ -49,6 +50,8 @@ def add_resources():
|
|||
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')
|
||||
api.add_resource(DeviceRecordingResource,
|
||||
'/v1/devices/<int:device_id>/recordings')
|
||||
api.add_resource(DeviceLatestRecordingResource,
|
||||
'/v1/devices/<int:device_id>/recordings/latest')
|
||||
api.add_resource(DeviceRecordingQueryResource,
|
||||
'/v1/devices/<int:device_id>/recordings/jsonql')
|
||||
api.add_resource(DeviceListResource, '/v1/devices')
|
||||
|
|
|
@ -129,6 +129,14 @@ class DeviceRecordingResource(ProtectedResource):
|
|||
return RecordingsSchema().dump(created_recording), 201
|
||||
|
||||
|
||||
class DeviceLatestRecordingResource(ProtectedResource):
|
||||
@swag_from('swagger/get_latest_device_recording_spec.yaml')
|
||||
def get(self, device_id):
|
||||
validate_device_ownership(device_id)
|
||||
return RecordingsSchema().dump(
|
||||
devices.get_latest_device_recording(device_id)), 200
|
||||
|
||||
|
||||
class DeviceRecordingQueryResource(ProtectedResource):
|
||||
@use_args(RecordingsQuerySchema(), locations=('json',))
|
||||
@swag_from('swagger/create_device_recording_query_spec.yaml')
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
Gets latest recording for given device
|
||||
---
|
||||
tags:
|
||||
- Device
|
||||
- Recording
|
||||
parameters:
|
||||
- in: path
|
||||
name: device_id
|
||||
required: true
|
||||
type: integer
|
||||
description: Id of the device
|
||||
responses:
|
||||
200:
|
||||
description: Success
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
$ref: '#/definitions/Recording'
|
|
@ -142,6 +142,23 @@ def get_device_recordings_filtered(device_id, record_type=None,
|
|||
start_date, end_date)
|
||||
|
||||
|
||||
def get_latest_device_recording(device_id):
|
||||
"""
|
||||
Tries to get most recent recording for device with given parameters. Raises
|
||||
error on failure
|
||||
|
||||
:param device_id: Id of device
|
||||
:type device_id: int
|
||||
:returns: Single recording (last recording)
|
||||
:rtpe: Recording
|
||||
:raises: ValueError if device does not exist
|
||||
"""
|
||||
if not Device.exists(id=device_id):
|
||||
raise NotPresentError("Device with id %s does not exist" % device_id)
|
||||
|
||||
return Recording.get_latest(device_id)
|
||||
|
||||
|
||||
def get_device(device_id):
|
||||
"""
|
||||
Tries to get device with given parameters. Raises error on failure
|
||||
|
|
|
@ -108,6 +108,13 @@ class Recording(db.Model):
|
|||
"""
|
||||
return Recording.query.filter_by(**kwargs).first_or_404()
|
||||
|
||||
@staticmethod
|
||||
def get_latest(device_id):
|
||||
"""
|
||||
Get latest recording for device with id device_id
|
||||
"""
|
||||
return Recording.query.order_by('recorded_at desc').first_or_404()
|
||||
|
||||
def __repr__(self):
|
||||
return '<Recording (value=%s, recorded_at=%s)>' % (
|
||||
self.record_value, self.recorded_at)
|
||||
|
|
Loading…
Reference in New Issue