2021-03-14 11:52:46 +01:00
|
|
|
from cbpi.api.dataclasses import NotificationType
|
2019-01-02 21:20:44 +01:00
|
|
|
import logging
|
2021-01-09 15:20:56 +01:00
|
|
|
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
|
2021-08-11 17:12:13 +02:00
|
|
|
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)
|
|
|
|
|
2022-09-24 15:19:34 +02:00
|
|
|
self.path = cbpi.config_folder.get_dashboard_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:
|
2022-09-24 15:19:34 +02:00
|
|
|
self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_"+ str(dashboard_id) +".json")
|
2021-08-11 17:12:13 +02:00
|
|
|
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:
|
2021-08-11 17:12:13 +02:00
|
|
|
return {'elements': [], 'pathes': []}
|
2021-01-22 23:25:20 +01:00
|
|
|
|
2021-01-09 15:20:56 +01:00
|
|
|
async def add_content(self, dashboard_id, data):
|
2023-01-22 16:37:10 +01:00
|
|
|
#print(data)
|
2022-09-24 15:19:34 +02:00
|
|
|
self.path = self.cbpi.config_folder.get_dashboard_path("cbpi_dashboard_" + str(dashboard_id)+ ".json")
|
2021-01-26 20:21:53 +01:00
|
|
|
with open(self.path, 'w') as outfile:
|
2021-01-09 15:20:56 +01:00
|
|
|
json.dump(data, outfile, indent=4, sort_keys=True)
|
2021-08-11 17:12:13 +02:00
|
|
|
self.cbpi.notify(title="Dashboard {}".format(dashboard_id), message="Saved Successfully", type=NotificationType.SUCCESS)
|
2021-01-09 15:20:56 +01:00
|
|
|
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):
|
2022-09-24 15:19:34 +02:00
|
|
|
self.path = self.cbpi.config_folder.get_dashboard_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)
|
2021-08-11 17:12:13 +02:00
|
|
|
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):
|
2022-09-24 15:19:34 +02:00
|
|
|
path = self.cbpi.config_folder.get_dashboard_path("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
|
2021-08-11 17:12:13 +02:00
|
|
|
|
|
|
|
async def get_dashboard_numbers(self):
|
|
|
|
max_dashboard_number = self.cbpi.config.get("max_dashboard_number", 4)
|
|
|
|
return max_dashboard_number
|
2021-08-18 21:55:56 +02:00
|
|
|
|
|
|
|
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"}
|
2022-07-21 10:27:54 +02:00
|
|
|
|
|
|
|
async def get_slow_pipe_animation(self):
|
|
|
|
slow_pipe_animation = self.cbpi.config.get("slow_pipe_animation", "Yes")
|
|
|
|
return slow_pipe_animation
|