From 9c8764757d6ddca1de3f4f27b1128da1ca668f85 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Wed, 25 Sep 2013 17:35:52 +0300 Subject: [PATCH] config request improvements --- doc/todo.txt | 2 +- src/config.py | 7 +++---- src/handlers.py | 50 ++++++++++++++++++++++++++++++++++++------------- src/server.py | 4 ++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/doc/todo.txt b/doc/todo.txt index 84d9c46..0ce2294 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,4 +1,4 @@ --> move target dir to device config +-> group @config rules to top -> browser compatibility test -> hint text next to section titles -> authentication diff --git a/src/config.py b/src/config.py index d7ec91f..61f945c 100644 --- a/src/config.py +++ b/src/config.py @@ -253,15 +253,14 @@ def add_camera(device): # add the default camera config data = OrderedDict() - name = 'Camera' + str(camera_id) - data['@name'] = name + data['@name'] = 'Camera' + str(camera_id) data['@proto'] = proto data['videodevice'] = device # write the configuration to file set_camera(camera_id, data) - return camera_id, name, data + return camera_id, data def rem_camera(camera_id): @@ -683,7 +682,7 @@ def _dict_to_conf(lines, data, list_names=[]): def _set_default_motion(data): - data.setdefault('@general_enabled', True) + data.setdefault('@enabled', True) data.setdefault('@show_advanced', False) data.setdefault('@admin_username', 'admin') data.setdefault('@admin_password', '') diff --git a/src/handlers.py b/src/handlers.py index 870a890..655f958 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -27,13 +27,22 @@ class MainHandler(BaseHandler): class ConfigHandler(BaseHandler): def get(self, camera_id=None, op=None): + if camera_id is not None: + camera_id = int(camera_id) + if op == 'get': self.get_config(camera_id) + + elif op == 'list': + self.list_cameras() else: raise HTTPError(400, 'unknown operation') def post(self, camera_id=None, op=None): + if camera_id is not None: + camera_id = int(camera_id) + if op == 'set': self.set_config(camera_id) @@ -47,25 +56,21 @@ class ConfigHandler(BaseHandler): raise HTTPError(400, 'unknown operation') def get_config(self, camera_id): - general_config = config.get_main() - if camera_id: logging.debug('getting config for camera %(id)s' % {'id': camera_id}) - cameras = general_config.get('cameras', {}) - if camera_id not in cameras: + camera_ids = config.get_camera_ids() + if camera_id not in camera_ids: raise HTTPError(404, 'no such camera') self.finish_json(config.get_camera(camera_id)) else: - logging.debug('getting general config') + logging.debug('getting main config') - self.finish_json(general_config) + self.finish_json(config.get_main()) def set_config(self, camera_id): - general_config = config.get_main() - try: data = json.loads(self.request.body) @@ -77,14 +82,14 @@ class ConfigHandler(BaseHandler): if camera_id: logging.debug('setting config for camera %(id)s' % {'id': camera_id}) - cameras = general_config.get('cameras', {}) - if camera_id not in cameras: + camera_ids = config.get_camera_ids() + if camera_id not in camera_ids: raise HTTPError(404, 'no such camera') config.set_camera(camera_id, data) else: - logging.debug('setting general config') + logging.debug('setting main config') try: data = json.loads(self.request.body) @@ -94,14 +99,33 @@ class ConfigHandler(BaseHandler): raise - general_config.update(data) - config.set_main(general_config) + config.set_main(data) + + def list_cameras(self): + logging.debug('listing cameras') + + cameras = [] + for camera_id in config.get_camera_ids(): + data = config.get_camera(camera_id) + data['@id'] = camera_id + cameras.append(data) + + self.finish_json({'cameras': cameras}) def add_camera(self): logging.debug('adding new camera') + + device = self.get_argument('device') + camera_id, data = config.add_camera(device) + + data['@id'] = camera_id + + self.finish_json(data) def rem_camera(self, camera_id): logging.debug('removing camera %(id)s' % {'id': camera_id}) + + config.rem_camera(camera_id) class SnapshotHandler(BaseHandler): diff --git a/src/server.py b/src/server.py index 9daeb52..864507c 100644 --- a/src/server.py +++ b/src/server.py @@ -9,9 +9,9 @@ import template application = Application( [ (r'^/$', handlers.MainHandler), - (r'^/config/general/(?Pset|get)/?$', handlers.ConfigHandler), + (r'^/config/main/(?Pset|get)/?$', handlers.ConfigHandler), (r'^/config/(?P\d+)/(?Pget|set|rem)/?$', handlers.ConfigHandler), - (r'^/config/(?Padd)/?$', handlers.ConfigHandler), + (r'^/config/(?Padd|list)/?$', handlers.ConfigHandler), (r'^/snapshot/(?P\d+)/(?Pcurrent|list)/?$', handlers.SnapshotHandler), (r'^/snapshot/(?P\d+)/(?Pdownload)/(?P.+)/?$', handlers.SnapshotHandler), (r'^/movie/(?P\d+)/(?Plist)/?$', handlers.MovieHandler), -- 2.39.5