Add API routes for POSTing temperature data

This commit is contained in:
Tanner Collin 2020-02-18 07:06:33 +00:00
parent 50497b83b0
commit cd27c1ee4b
2 changed files with 23 additions and 3 deletions

View File

@ -337,14 +337,34 @@ class IpnView(views.APIView):
return Response(200) return Response(200)
class StatsView(views.APIView): class StatsViewSet(viewsets.ViewSet, List):
def get(self, request): def list(self, request):
stats_keys = utils_stats.DEFAULTS.keys() stats_keys = utils_stats.DEFAULTS.keys()
cached_stats = cache.get_many(stats_keys) cached_stats = cache.get_many(stats_keys)
stats = utils_stats.DEFAULTS.copy() stats = utils_stats.DEFAULTS.copy()
stats.update(cached_stats) stats.update(cached_stats)
return Response(stats) return Response(stats)
@action(detail=False, methods=['post'])
def bay_108_temp(self, request):
try:
cache.set('bay_108_temp', round(float(request.data['data']), 1))
return Response(200)
except ValueError:
raise exceptions.ValidationError(dict(data='Invalid float.'))
except KeyError:
raise exceptions.ValidationError(dict(data='This field is required.'))
@action(detail=False, methods=['post'])
def bay_110_temp(self, request):
try:
cache.set('bay_110_temp', round(float(request.data['data']), 1))
return Response(200)
except ValueError:
raise exceptions.ValidationError(dict(data='Invalid float.'))
except KeyError:
raise exceptions.ValidationError(dict(data='This field is required.'))
class BackupView(views.APIView): class BackupView(views.APIView):
def get(self, request): def get(self, request):

View File

@ -11,6 +11,7 @@ IPN_ROUTE = r'^ipn/{}/'.format(secrets.IPN_RANDOM)
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'door', views.DoorViewSet, basename='door') router.register(r'door', views.DoorViewSet, basename='door')
router.register(r'cards', views.CardViewSet, basename='card') router.register(r'cards', views.CardViewSet, basename='card')
router.register(r'stats', views.StatsViewSet, basename='stats')
router.register(r'search', views.SearchViewSet, basename='search') router.register(r'search', views.SearchViewSet, basename='search')
router.register(r'members', views.MemberViewSet, basename='members') router.register(r'members', views.MemberViewSet, basename='members')
router.register(r'courses', views.CourseViewSet, basename='course') router.register(r'courses', views.CourseViewSet, basename='course')
@ -29,7 +30,6 @@ urlpatterns = [
url(r'^password/change/', views.PasswordChangeView.as_view(), name='rest_password_change'), url(r'^password/change/', views.PasswordChangeView.as_view(), name='rest_password_change'),
url(r'^user/', views.UserView.as_view(), name='user'), url(r'^user/', views.UserView.as_view(), name='user'),
url(r'^ping/', views.PingView.as_view(), name='ping'), url(r'^ping/', views.PingView.as_view(), name='ping'),
url(r'^stats/', views.StatsView.as_view(), name='stats'),
url(r'^paste/', views.PasteView.as_view(), name='paste'), url(r'^paste/', views.PasteView.as_view(), name='paste'),
url(r'^backup/', views.BackupView.as_view(), name='backup'), url(r'^backup/', views.BackupView.as_view(), name='backup'),
url(IPN_ROUTE, views.IpnView.as_view(), name='ipn'), url(IPN_ROUTE, views.IpnView.as_view(), name='ipn'),