craftbeerpi4-pione/cbpi/controller/dashboard_controller.py

67 lines
2.5 KiB
Python
Raw Normal View History

2021-03-14 11:52:46 +01:00
from cbpi.api.dataclasses import NotificationType
2019-01-02 21:20:44 +01:00
import logging
import json
2021-01-17 22:49:18 +01:00
import os
2021-02-06 00:40:55 +01:00
from os import listdir
from os.path import isfile, join
from cbpi.api.base import CBPiBase
from cbpi.api.config import ConfigType
2019-01-02 21:20:44 +01:00
2021-03-07 22:11:25 +01:00
from voluptuous.schema_builder import message
2021-02-16 20:37:51 +01:00
class DashboardController:
2019-01-02 21:20:44 +01:00
def __init__(self, cbpi):
2019-08-16 21:36:55 +02:00
self.caching = False
2019-01-02 21:20:44 +01:00
self.cbpi = cbpi
self.logger = logging.getLogger(__name__)
self.cbpi.register(self)
self.path = cbpi.config_folder.get_file_path("cbpi_dashboard_1.json")
2021-01-26 20:21:53 +01:00
2021-01-22 23:25:20 +01:00
async def init(self):
pass
2019-01-07 22:05:52 +01:00
2019-01-02 21:20:44 +01:00
async def get_content(self, dashboard_id):
2021-01-17 22:49:18 +01:00
try:
self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_"+ str(dashboard_id) +".json")
logging.info(self.path)
2021-01-26 20:21:53 +01:00
with open(self.path) as json_file:
2021-01-17 22:49:18 +01:00
data = json.load(json_file)
return data
except:
return {'elements': [], 'pathes': []}
2021-01-22 23:25:20 +01:00
async def add_content(self, dashboard_id, data):
2021-03-14 11:52:46 +01:00
print(data)
self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_" + str(dashboard_id)+ ".json")
2021-01-26 20:21:53 +01:00
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"}
2019-01-02 21:20:44 +01:00
2021-01-17 22:49:18 +01:00
async def delete_content(self, dashboard_id):
self.path = self.cbpi.config_folder.get_file_path("cbpi_dashboard_"+ str(dashboard_id)+ ".json")
2021-01-26 20:21:53 +01:00
if os.path.exists(self.path):
os.remove(self.path)
self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Deleted Successfully", type=NotificationType.SUCCESS)
2021-01-26 20:21:53 +01:00
2021-02-06 00:40:55 +01:00
async def get_custom_widgets(self):
path = os.path.join(self.cbpi.config_folder.get_file_path("dashboard"), "widgets")
2022-02-21 17:54:22 +01:00
onlyfiles = [os.path.splitext(f)[0] for f in sorted(listdir(path)) if isfile(join(path, f)) and f.endswith(".svg")]
2021-02-06 00:40:55 +01:00
return onlyfiles
async def get_dashboard_numbers(self):
max_dashboard_number = self.cbpi.config.get("max_dashboard_number", 4)
return max_dashboard_number
async def get_current_dashboard(self):
current_dashboard_number = self.cbpi.config.get("current_dashboard_number", 1)
return current_dashboard_number
async def set_current_dashboard(self, dashboard_id=1):
await self.cbpi.config.set("current_dashboard_number", dashboard_id)
return {"status": "OK"}