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
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2021-01-22 23:25:20 +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)
|
|
|
|
|
2021-01-26 20:21:53 +01:00
|
|
|
self.path = os.path.join(".", 'config', "cbpi_dashboard_1.json")
|
|
|
|
|
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:
|
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 {}
|
2021-01-22 23:25:20 +01:00
|
|
|
|
2021-01-09 15:20:56 +01:00
|
|
|
async def add_content(self, dashboard_id, data):
|
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)
|
|
|
|
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):
|
2021-01-26 20:21:53 +01:00
|
|
|
if os.path.exists(self.path):
|
|
|
|
os.remove(self.path)
|
|
|
|
|
2021-02-06 00:40:55 +01:00
|
|
|
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
|