mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-10 01:17:42 +01:00
016c6d1adf
- 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
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from cbpi.api.dataclasses import NotificationType
|
|
import logging
|
|
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
|
|
|
|
class DashboardController:
|
|
|
|
|
|
def __init__(self, cbpi):
|
|
self.caching = False
|
|
self.cbpi = cbpi
|
|
self.logger = logging.getLogger(__name__)
|
|
self.cbpi.register(self)
|
|
|
|
self.path = os.path.join(".", 'config', "cbpi_dashboard_1.json")
|
|
|
|
async def init(self):
|
|
pass
|
|
|
|
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 {'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 {}".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
|