Commit 23face1e authored by Jan Luebbe's avatar Jan Luebbe

split model from view for backend collections

parent 152199db
......@@ -4,6 +4,7 @@ import asyncio
import gbulb
gbulb.install()
import collections
import functools
import json
import signal
......@@ -207,8 +208,8 @@ class Video(object):
self.snapshot = snapshot
#link_many(self.pipeline, camera, videotee)
#link_many(self.pipeline, player, videotee)
link_many(self.pipeline, img, videotee)
link_many(self.pipeline, player, videotee)
#link_many(self.pipeline, img, videotee)
link_many(self.pipeline, videotee, viewer)
link_many(self.pipeline, videotee, recorder)
link_many(self.pipeline, videotee, snapshot)
......@@ -237,34 +238,49 @@ def index(request):
data = open('../frontend/index.html', 'rb').read()
return web.Response(body=data)
INPUTS = {
1: {
'id': 1,
'name': 'Foo Input',
'type': 'Stream',
}
}
class IndexedCollection(collections.UserDict):
def __setitem__(self, k, v):
if 'id' in v:
assert v['id'] == k
else:
v['id'] = k
self.data[k] = v
class RESTCollectionView(web.View):
next_id = 0
def add(self, v):
next = max((x['id'] for x in self.data.values())) + 1 if self.data else 0
id = v.get('id', next)
self[id] = v
INPUTS = IndexedCollection()
INPUTS.add({
'id': 1,
'name': 'Foo Input',
'type': 'Stream',
})
OUTPUTS = IndexedCollection()
OUTPUTS.add({
'id': 1,
'name': 'Foo Output',
'type': 'Stream',
})
@classmethod
def _get_next_id(cls):
print(cls.next_id)
id = cls.next_id
cls.next_id += 1
return id
SCENES = IndexedCollection()
SCENES.add({
'id': 1,
'name': 'Foo Scene',
'type': 'Stream',
})
class RESTCollectionView(web.View):
def __init__(self, *args, **kwargs):
super(RESTCollectionView, self).__init__(*args, **kwargs)
id = self.request.match_info.get('id')
self.id = int(id) if id else None
def _set(self, data):
id = data.get('id')
self.collection[id] = data
@asyncio.coroutine
def get(self):
if self.id is None:
......@@ -280,11 +296,9 @@ class RESTCollectionView(web.View):
def post(self):
if self.id is None:
data = (yield from self.request.json())[self.topic]
id = self._get_next_id()
self.collection['id'] = data
path = self.request.app.router[self.topic].url(
parts={"id": id})
data['id'] = id
self._set(data)
parts={"id": data['id']})
self.request.app.wse.send_all({
'method': 'POST',
'path': path,
......@@ -300,7 +314,7 @@ class RESTCollectionView(web.View):
else:
data = (yield from self.request.json())[self.topic]
assert self.id == data['id']
self._set(data)
self.collection[self.id] = data
self.request.app.wse.send_all({
'method': 'PUT',
'path': self.request.path,
......@@ -322,19 +336,16 @@ class RESTCollectionView(web.View):
class InputRESTView(RESTCollectionView):
topic = 'input'
collection = INPUTS
next_id = 1 + max((x['id'] for x in collection.values()))
class OutputRESTView(RESTCollectionView):
topic = 'output'
collection = {}
next_id = 1
collection = OUTPUTS
class SceneRESTView(RESTCollectionView):
topic = 'scene'
collection = {}
next_id = 1
collection = SCENES
class WebSocketEndpoint(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment