2023-08-07 18:05:20 +00:00
|
|
|
try:
|
|
|
|
# python 3.11
|
|
|
|
from tomllib import loads as toml_load
|
|
|
|
except ImportError:
|
|
|
|
from rtoml import load as toml_load
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
LOG = logging.getLogger('Config')
|
|
|
|
|
|
|
|
|
2023-08-08 04:26:35 +00:00
|
|
|
class ConfigWrapper:
|
|
|
|
# simple class to wrap a dict into a object
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
|
2023-08-07 18:05:20 +00:00
|
|
|
def load_and_validate_config(path):
|
|
|
|
try:
|
|
|
|
with open(path, 'r') as cf:
|
|
|
|
config = toml_load(cf.read())
|
|
|
|
except Exception as e:
|
|
|
|
LOG.error(f'{path} is no valid toml configuration file')
|
|
|
|
exit(1)
|
|
|
|
|
2023-08-08 04:26:35 +00:00
|
|
|
if not config.get('mqtt', {}).get('host'):
|
|
|
|
LOG.error(
|
|
|
|
f'configuration option "mqtt" "host" is missing in config, but required to exist'
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
conf = ConfigWrapper(
|
|
|
|
mqtt=ConfigWrapper(
|
|
|
|
host=config['mqtt']['host'],
|
|
|
|
user=config['mqtt'].get('user'),
|
|
|
|
password=config['mqtt'].get('password'),
|
|
|
|
topic=config['mqtt'].get('topic', '/voc/alert'),
|
|
|
|
),
|
|
|
|
sacn=ConfigWrapper(
|
|
|
|
multicast=bool(config.get('sacn', {}).get('multicast', False) is True),
|
|
|
|
target=config.get('sacn', {}).get('target', '127.0.0.1'),
|
|
|
|
universe=int(config.get('sacn', {}).get('universe', 1)),
|
|
|
|
),
|
|
|
|
alerts=ConfigWrapper(
|
|
|
|
brightness=max(int(config.get('alerts', {}).get('brightness', 255)), 10),
|
2024-02-11 13:31:34 +00:00
|
|
|
filters=sorted(config.get('alerts', {}).get('filters', set())),
|
2023-08-08 04:26:35 +00:00
|
|
|
),
|
|
|
|
rainbow=ConfigWrapper(
|
|
|
|
enable=bool(config.get('rainbow', {}).get('enable', True) is True),
|
|
|
|
intensity=max(int(config.get('rainbow', {}).get('intensity', 100)), 10),
|
|
|
|
brightness=max(int(config.get('rainbow', {}).get('brightness', 150)), 10),
|
|
|
|
speed=int(config.get('rainbow', {}).get('speed', 25)),
|
|
|
|
),
|
|
|
|
lights=config.get('lights', {}),
|
|
|
|
)
|
|
|
|
|
|
|
|
if conf.alerts.brightness < conf.rainbow.brightness:
|
|
|
|
LOG.error('alerts brightness must be equal or above rainbow brightness')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
return conf
|