fieldpoc/fieldpoc/fieldpoc.py

83 lines
2.2 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
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
def __init__(self, config_file_path, extensions_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.extensions_file_path = pathlib.Path(extensions_file_path)
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-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-07-06 13:05:06 +02:00
logger.info("starting components")
2022-07-02 03:19:32 +02:00
2022-06-25 17:03:13 +02:00
self._controller_thread = threading.Thread(target=self._controller.run)
self._controller_thread.start()
self._dect_thread = threading.Thread(target=self._dect.run)
self._dect_thread.start()
2022-07-06 13:15:31 +02:00
self._routing_thread = threading.Thread(target=self._routing.run)
self._routing_thread.start()
2022-07-06 13:05:06 +02:00
self._ywsd_thread = threading.Thread(target=self._ywsd.run, daemon=True)
self._ywsd_thread.start()
2022-07-02 03:19:32 +02:00
logger.info("started components")
def _load_config(self):
self.config = config.Config(json.loads(self.config_file_path.read_text()))
def _load_extensions(self):
self.extensions = config.Extensions(json.loads(self.extensions_file_path.read_text()))