hook up service handling to new device events

This commit is contained in:
Jakob Ketterl 2021-03-20 01:56:07 +01:00
parent 792f76f831
commit cfeab98620

View File

@ -12,7 +12,7 @@ from owrx.source.resampler import Resampler
from owrx.property import PropertyLayer, PropertyDeleted from owrx.property import PropertyLayer, PropertyDeleted
from js8py import Js8Frame from js8py import Js8Frame
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from .schedule import ServiceScheduler from owrx.service.schedule import ServiceScheduler
from owrx.modes import Modes from owrx.modes import Modes
import logging import logging
@ -319,28 +319,48 @@ class Js8Handler(object):
class Services(object): class Services(object):
handlers = [] handlers = {}
schedulers = [] schedulers = {}
@staticmethod @staticmethod
def start(): def start():
config = Config.get() config = Config.get()
config.wireProperty("services_enabled", Services._receiveEvent) config.wireProperty("services_enabled", Services._receiveEnabledEvent)
for source in SdrService.getActiveSources().values(): activeSources = SdrService.getActiveSources()
Services.schedulers.append(ServiceScheduler(source)) activeSources.wire(Services._receiveDeviceEvent)
for key, source in activeSources.items():
Services.schedulers[key] = ServiceScheduler(source)
@staticmethod @staticmethod
def _receiveEvent(state): def _receiveEnabledEvent(state):
if state: if state:
for source in SdrService.getActiveSources().values(): for key, source in SdrService.getActiveSources().__dict__().items():
Services.handlers.append(ServiceHandler(source)) Services.handlers[key] = ServiceHandler(source)
else: else:
while Services.handlers: for handler in Services.handlers.values():
Services.handlers.pop().shutdown() handler.shutdown()
Services.handlers = {}
@staticmethod
def _receiveDeviceEvent(changes):
for key, source in changes.items():
if source is PropertyDeleted:
if key in Services.handlers:
Services.handlers[key].shutdown()
del Services.handlers[key]
if key in Services.schedulers:
Services.schedulers[key].shutdown()
del Services.schedulers[key]
else:
Services.schedulers[key] = ServiceScheduler(source)
if Config.get()["services_enabled"]:
Services.handlers[key] = ServiceHandler(source)
@staticmethod @staticmethod
def stop(): def stop():
while Services.handlers: for handler in Services.handlers.values():
Services.handlers.pop().shutdown() handler.shutdown()
while Services.schedulers: Services.handlers = {}
Services.schedulers.pop().shutdown() for scheduler in Services.schedulers.values():
scheduler.shutdown()
Services.schedulers = {}