2018-12-29 00:27:19 +01:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
from aiohttp import web
|
|
|
|
from cbpi_api import request_mapping
|
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
from core.controller.crud_controller import CRUDController
|
|
|
|
from core.database.model import ConfigModel
|
2018-12-29 00:27:19 +01:00
|
|
|
from utils import load_config, json_dumps
|
|
|
|
from cbpi_api.config import ConfigType
|
|
|
|
|
|
|
|
class ConfigHTTPController():
|
|
|
|
@request_mapping(path="/{name}/", method="POST", auth_required=False)
|
|
|
|
async def http_post(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
name = request.match_info['name']
|
|
|
|
data = await request.json()
|
|
|
|
print(data)
|
|
|
|
await self.set(name=name, value=data.get("value"))
|
|
|
|
return web.Response(status=204)
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
@request_mapping(path="/", auth_required=False)
|
|
|
|
async def http_get_all(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return web.json_response(self.cache, dumps=json_dumps)
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
class ConfigController(ConfigHTTPController):
|
2018-11-18 15:40:10 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
The main actor controller
|
|
|
|
'''
|
|
|
|
model = ConfigModel
|
|
|
|
|
|
|
|
def __init__(self, cbpi):
|
2018-12-29 00:27:19 +01:00
|
|
|
self.cache = {}
|
|
|
|
self.logger = logging.getLogger(__name__)
|
2018-11-18 15:40:10 +01:00
|
|
|
self.cbpi = cbpi
|
|
|
|
self.cbpi.register(self, "/config")
|
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
async def init(self):
|
|
|
|
this_directory = os.path.dirname(__file__)
|
|
|
|
self.static = load_config(os.path.join(this_directory, '../../config/config.yaml'))
|
|
|
|
items = await self.model.get_all()
|
|
|
|
for key, value in items.items():
|
|
|
|
self.cache[value.name] = value
|
|
|
|
|
|
|
|
|
2018-12-31 00:22:00 +01:00
|
|
|
def get(self, name, default=None):
|
2018-12-29 00:27:19 +01:00
|
|
|
self.logger.info("GET CONFIG VALUE %s (default %s)" % (name,default))
|
|
|
|
if name in self.cache and self.cache[name].value is not None:
|
|
|
|
return self.cache[name].value
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
async def set(self, name, value):
|
|
|
|
self.logger.debug("SET %s = %s" % (name, value))
|
|
|
|
if name in self.cache:
|
|
|
|
|
|
|
|
self.cache[name].value = value
|
|
|
|
m = await self.model.update(**self.cache[name].__dict__)
|
|
|
|
await self.cbpi.bus.fire(topic="config/%s/update" % name, name=name, value=value)
|
|
|
|
|
2018-11-18 15:40:10 +01:00
|
|
|
|
2018-12-29 00:27:19 +01:00
|
|
|
async def add(self, name, value, type: ConfigType, description, options=None):
|
|
|
|
m = await self.model.insert(name=name, value=value, type=type.value, description=description, options=options)
|
|
|
|
await self.cbpi.bus.fire(topic="config/%s/add" % name, name=name, value=value)
|