From 016c6d1adfbf4c03fc7baa4f7e60f67b123dcf42 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Wed, 11 Aug 2021 17:12:13 +0200 Subject: [PATCH] Allow Multiple Dashboards - Up to 10 Dashboards possible - Config parameter max_dashboard_number added - Default is 4 dashboards - Config parameter will be added automatically via extension if not in config.json --- cbpi/controller/dashboard_controller.py | 16 ++++++++++++++-- cbpi/extension/ConfigUpdate/__init__.py | 21 ++++++++++++++++++++- cbpi/http_endpoints/http_dashboard.py | 14 +++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cbpi/controller/dashboard_controller.py b/cbpi/controller/dashboard_controller.py index 7cde13a..1020365 100644 --- a/cbpi/controller/dashboard_controller.py +++ b/cbpi/controller/dashboard_controller.py @@ -4,6 +4,8 @@ import json import os from os import listdir from os.path import isfile, join +from cbpi.api.base import CBPiBase +from cbpi.api.config import ConfigType from voluptuous.schema_builder import message @@ -23,24 +25,34 @@ class DashboardController: async def get_content(self, dashboard_id): try: + self.path = os.path.join(".", 'config', "cbpi_dashboard_"+ str(dashboard_id) +".json") + logging.info(self.path) with open(self.path) as json_file: data = json.load(json_file) return data except: - return {} + return {'elements': [], 'pathes': []} async def add_content(self, dashboard_id, data): print(data) + self.path = os.path.join(".", 'config', "cbpi_dashboard_" + str(dashboard_id)+ ".json") with open(self.path, 'w') as outfile: json.dump(data, outfile, indent=4, sort_keys=True) - self.cbpi.notify(title="Dashboard", message="Saved Successfully", type=NotificationType.SUCCESS) + self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Saved Successfully", type=NotificationType.SUCCESS) return {"status": "OK"} async def delete_content(self, dashboard_id): + self.path = os.path.join(".", 'config', "cbpi_dashboard_"+ str(dashboard_id)+ ".json") if os.path.exists(self.path): os.remove(self.path) + self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Deleted Successfully", type=NotificationType.SUCCESS) + async def get_custom_widgets(self): path = os.path.join(".", 'config', "dashboard", "widgets") onlyfiles = [os.path.splitext(f)[0] for f in listdir(path) if isfile(join(path, f)) and f.endswith(".svg")] return onlyfiles + + async def get_dashboard_numbers(self): + max_dashboard_number = self.cbpi.config.get("max_dashboard_number", 4) + return max_dashboard_number diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index 1a668ab..7e83256 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -33,6 +33,8 @@ class ConfigUpdate(CBPiExtension): mashout_step = self.cbpi.config.get("steps_mashout", None) boil_step = self.cbpi.config.get("steps_boil", None) cooldown_step = self.cbpi.config.get("steps_cooldown", None) + max_dashboard_number = self.cbpi.config.get("max_dashboard_number", None) + if boil_temp is None: logger.info("INIT Boil Temp Setting") @@ -90,7 +92,24 @@ class ConfigUpdate(CBPiExtension): except: logger.warning('Unable to update database') - ## Check if AtuoMode for Steps is in config + if max_dashboard_number is None: + logger.info("INIT Max Dashboard Numbers for multiple dashboards") + try: + await self.cbpi.config.add("max_dashboard_number", 4, ConfigType.SELECT, "Max Number of Dashboards", + [{"label": "1", "value": 1}, + {"label": "2", "value": 2}, + {"label": "3", "value": 3}, + {"label": "4", "value": 4}, + {"label": "5", "value": 5}, + {"label": "6", "value": 6}, + {"label": "7", "value": 7}, + {"label": "8", "value": 8}, + {"label": "9", "value": 9}, + {"label": "10", "value": 10}]) + except: + logger.warning('Unable to update database') + + ## Check if AtuoMode for Steps is in config AutoMode = self.cbpi.config.get("AutoMode", None) if AutoMode is None: logger.info("INIT AutoMode") diff --git a/cbpi/http_endpoints/http_dashboard.py b/cbpi/http_endpoints/http_dashboard.py index d28ba5e..3007252 100644 --- a/cbpi/http_endpoints/http_dashboard.py +++ b/cbpi/http_endpoints/http_dashboard.py @@ -109,4 +109,16 @@ class DashBoardHttpEndpoints: return web.json_response(await self.cbpi.dashboard.get_custom_widgets(), dumps=json_dumps) - + + @request_mapping(path="/numbers", method="GET", auth_required=False) + async def get_dashboard_numbers(self, request): + """ + --- + description: Get Dashboard Numbers + tags: + - Dashboard + responses: + "200": + description: successful operation + """ + return web.json_response(await self.cbpi.dashboard.get_dashboard_numbers(), dumps=json_dumps)