fieldpoc/fieldpoc/fieldpoc.py

104 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import json
2022-07-02 03:19:32 +02:00
import logging
import pathlib
2022-06-25 18:51:11 +02:00
import queue
import threading
from . import config
2022-06-25 17:03:13 +02:00
from . import controller
2022-09-02 21:51:55 +02:00
from . import extensions
from . import dect
2022-07-06 13:15:31 +02:00
from . import routing
2022-07-06 13:05:06 +02:00
from . import ywsd
2022-07-02 03:19:32 +02:00
logger = logging.getLogger("fieldpoc.fieldpoc")
class FieldPOC:
config = None
extensions = None
2022-07-20 15:44:11 +02:00
temp_extensions = {}
def __init__(self, config_file_path):
2022-07-02 03:19:32 +02:00
logger.info("loading configuration")
self.config_file_path = pathlib.Path(config_file_path)
self._load_config()
self._load_extensions()
2022-07-02 03:19:32 +02:00
logger.info("initialising queues")
2022-06-25 18:51:11 +02:00
self.queues = {
"controller": queue.Queue(),
"dect": queue.Queue(),
2022-07-06 13:15:31 +02:00
"routing": queue.Queue(),
2022-06-25 18:51:11 +02:00
}
2022-06-25 17:03:13 +02:00
2022-07-02 03:19:32 +02:00
logger.info("initialising components")
2022-06-25 17:03:13 +02:00
self._controller = controller.Controller(self)
self._dect = dect.Dect(self)
2022-07-06 13:15:31 +02:00
self._routing = routing.Routing(self)
2022-07-06 13:05:06 +02:00
self._ywsd = ywsd.Ywsd(self)
2022-09-02 21:07:39 +02:00
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,
2022-09-02 21:07:39 +02:00
"routing": threading.Thread(target=self._routing.run),
"ywsd": threading.Thread(target=self._ywsd.run, daemon=True) if self.config.yate.enabled == True else None,
2022-09-02 21:07:39 +02:00
}
# Set thread names
for name, thread in self.threads.items():
if thread is None:
continue
2022-09-02 21:07:39 +02:00
thread.name = name
2022-06-25 18:51:11 +02:00
def queue_all(self, msg):
"""
Send message to every queue
"""
for queue in self.queues.values():
queue.put(msg)
2022-07-06 13:05:06 +02:00
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):
2022-09-02 21:07:39 +02:00
"""
Start FieldPOC
"""
2022-09-02 21:07:39 +02:00
logger.info("starting threads")
2022-07-06 13:15:31 +02:00
2022-09-02 21:07:39 +02:00
for name, thread in self.threads.items():
if thread is None:
continue
2022-09-02 21:07:39 +02:00
thread.start()
2022-07-06 13:05:06 +02:00
2022-09-02 21:07:39 +02:00
logger.info("started threads")
2022-07-02 03:19:32 +02:00
def reload_extensions(self):
2022-07-20 23:39:58 +02:00
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