#!/usr/bin/env python3 import json import logging import pathlib import queue import threading from . import config from . import controller from . import extensions from . import dect from . import routing from . import ywsd logger = logging.getLogger("fieldpoc.fieldpoc") class FieldPOC: config = None extensions = None temp_extensions = {} def __init__(self, config_file_path): logger.info("loading configuration") self.config_file_path = pathlib.Path(config_file_path) self._load_config() self._load_extensions() logger.info("initialising queues") self.queues = { "controller": queue.Queue(), "dect": queue.Queue(), "routing": queue.Queue(), } logger.info("initialising components") self._controller = controller.Controller(self) self._dect = dect.Dect(self) self._routing = routing.Routing(self) self._ywsd = ywsd.Ywsd(self) logger.info("initialising threads") self.threads = { "controller": threading.Thread(target=self._controller.run), "dect": threading.Thread(target=self._dect.run) if self.config.dect.enabled == True else None, "routing": threading.Thread(target=self._routing.run), "ywsd": threading.Thread(target=self._ywsd.run, daemon=True) if self.config.yate.enabled == True else None, } # Set thread names for name, thread in self.threads.items(): if thread is None: continue thread.name = name def queue_all(self, msg): """ Send message to every queue """ for queue in self.queues.values(): queue.put(msg) def init(self): """ Prepare some data structures. Run this once before using FieldPOC. """ logger.info("initialize datastructures") self._ywsd.init() logger.info("initialization complete") def run(self): """ Start FieldPOC """ logger.info("starting threads") for name, thread in self.threads.items(): if thread is None: continue thread.start() logger.info("started threads") def reload_extensions(self): self._load_extensions() self.queue_all({"type": "sync"}) def _load_config(self): self.config = config.Config(json.loads(self.config_file_path.read_text())) def _load_extensions(self): try: new_extensions = extensions.Extensions(json.loads(pathlib.Path(self.config.extensions.file).read_text())) except: logger.exception("loading extensions failed") else: self.extensions = new_extensions