diff --git a/backend/app/classes/ChameleonCamera.py b/backend/app/classes/ChameleonCamera.py index 3c98a18bd..3bdf4b379 100644 --- a/backend/app/classes/ChameleonCamera.py +++ b/backend/app/classes/ChameleonCamera.py @@ -6,8 +6,5 @@ class ChameleonCamera: def __init__(self, dic): self.pipelines = {} - for pipeline_id in dic["pipelines"]: - self.pipelines[pipeline_id] = ChameleonPipeline(dic[pipeline_id]) - - def change_value(self, pipline_id, key, value): - self.pipelines[pipline_id][key] = value \ No newline at end of file + for pipeline_id in dic: + self.pipelines[pipeline_id] = ChameleonPipeline(dic[pipeline_id]) \ No newline at end of file diff --git a/backend/app/classes/ChameleonPipeLine.py b/backend/app/classes/ChameleonPipeLine.py index e91fd882c..f1de833a7 100644 --- a/backend/app/classes/ChameleonPipeLine.py +++ b/backend/app/classes/ChameleonPipeLine.py @@ -3,6 +3,5 @@ class ChameleonPipeline: def __init__(self, dic): - self.id = dic["id"] self.exposure = dic["exposure"] self.brightness = dic["brightness"] diff --git a/backend/app/handlers/SocketHandler.py b/backend/app/handlers/SocketHandler.py index e0d512d55..35dc80ebd 100644 --- a/backend/app/handlers/SocketHandler.py +++ b/backend/app/handlers/SocketHandler.py @@ -1,52 +1,61 @@ import tornado.websocket import json +import os from app.classes.ChameleonCamera import ChameleonCamera class ChameleonWebSocket(tornado.websocket.WebSocketHandler): + actions = {} + settings = {} + cams = {} + + def __init__(self, application, request, **kwargs): + super().__init__(application, request, **kwargs) + self.settings_path = os.path.join(os.getcwd(), "settings") + self.cams_path = os.path.join(self.settings_path, "cams") + self.init_settings() + self.init_actions() + + def init_settings(self): + with open(os.path.join(self.settings_path, 'settings.json')) as setting_file: + self.settings = json.load(setting_file) + + self.init_cameras_settings() + + def init_cameras_settings(self): + + for curr_dir in os.listdir(self.cams_path): + pipelines = {} + for file in os.listdir(os.path.join(self.cams_path, curr_dir)): + with open(os.path.join(self.cams_path, curr_dir, file)) as pipeline: + pipelines[os.path.splitext(file)[0]] = json.load(pipeline) + + self.cams[curr_dir] = pipelines + + def init_actions(self): + self.actions["change_pipeline_value"] = self.change_pipeline_value + def check_origin(self, origin): return True def open(self): - - # TODO: read setting from default file store them - setting = json.load("file.json") - self.cam_name = setting["curr_cam"] - self.pipe_line = setting["curr_pipeline"] - - # TODO: get current camera pipeline file - curr_setting = json.load(self.cam_name + "/" + self.pipe_line) - - # TODO: wrtie current pipeline setting to client - self.write_message(curr_setting) + self.write_message(self.cams[self.settings["curr_camera"]][self.settings["curr_pipeline"]]) print("WebSocket opened") def on_message(self, message): - cams = [] - dic = { - 'cam1': { - 'pipelines': { - "1": {"exposure": 45, "brightness": 123123}, - "2": {"exposure": 12, "brightness": 123}, - "3": {"exposure": 23, "brightness": 12} - } - }, - 'cam2': { - 'pipelines': { - "1": {"exposure": 15, "brightness": 1233}, - "2": {"exposure": 68, "brightness": 453} - } - } - } - - for cam in dic: - cams.append(ChameleonCamera(dic[cam])) + for key in message: + if key in self.actions: + self.actions[key](message[key]) print(message) self.write_message(message) def on_close(self): print("WebSocket closed") + + def change_pipeline_value(self, dic): + for key in dic: + self.cams[self.settings["curr_camera"]][self.settings["curr_pipeline"]][key] = dic[key] diff --git a/backend/settings/cams/cam1/pipeline1 b/backend/settings/cams/cam1/pipeline1 new file mode 100644 index 000000000..d9fdb4afa --- /dev/null +++ b/backend/settings/cams/cam1/pipeline1 @@ -0,0 +1,4 @@ +{ + "exposure": 45, + "brightness": 123123 +} \ No newline at end of file diff --git a/backend/settings/cams/cam1/pipeline2.json b/backend/settings/cams/cam1/pipeline2.json new file mode 100644 index 000000000..64016bfed --- /dev/null +++ b/backend/settings/cams/cam1/pipeline2.json @@ -0,0 +1,4 @@ +{ + "exposure": 23, + "brightness": 12 +} \ No newline at end of file diff --git a/backend/settings/cams/cam2/pipeline1 b/backend/settings/cams/cam2/pipeline1 new file mode 100644 index 000000000..05f38c3c9 --- /dev/null +++ b/backend/settings/cams/cam2/pipeline1 @@ -0,0 +1,4 @@ +{ + "exposure": 2, + "brightness": 4 +} \ No newline at end of file diff --git a/backend/settings/cams/cam2/pipeline2.json b/backend/settings/cams/cam2/pipeline2.json new file mode 100644 index 000000000..b93a2766b --- /dev/null +++ b/backend/settings/cams/cam2/pipeline2.json @@ -0,0 +1,4 @@ +{ + "exposure": 2, + "brightness": 3 +} \ No newline at end of file diff --git a/backend/settings/settings.json b/backend/settings/settings.json new file mode 100644 index 000000000..24ad50700 --- /dev/null +++ b/backend/settings/settings.json @@ -0,0 +1,5 @@ +{ + "team_number": 1577, + "curr_camera": "cam1", + "curr_pipeline": "pipeline1" +} \ No newline at end of file