2023-11-09 21:11:53 +00:00
|
|
|
import threading
|
|
|
|
import data_handler
|
2023-11-23 09:14:11 +00:00
|
|
|
import frame_dispatcher
|
2023-11-09 21:11:53 +00:00
|
|
|
import modem
|
|
|
|
import structlog
|
2023-11-10 15:02:38 +00:00
|
|
|
import audio
|
2023-11-12 21:35:10 +00:00
|
|
|
import ujson as json
|
2023-11-12 22:22:53 +00:00
|
|
|
import explorer
|
2023-11-12 21:35:10 +00:00
|
|
|
|
2023-11-09 21:11:53 +00:00
|
|
|
|
|
|
|
class SM:
|
|
|
|
def __init__(self, app):
|
|
|
|
self.log = structlog.get_logger("service")
|
|
|
|
|
|
|
|
self.modem = False
|
|
|
|
self.data_handler = False
|
2023-11-19 08:56:00 +00:00
|
|
|
self.app = app
|
|
|
|
self.config = self.app.config_manager.read()
|
2023-11-09 21:11:53 +00:00
|
|
|
self.modem_events = app.modem_events
|
|
|
|
self.modem_fft = app.modem_fft
|
2023-11-23 06:02:56 +00:00
|
|
|
self.enable_fft = False
|
2023-11-09 21:11:53 +00:00
|
|
|
self.modem_service = app.modem_service
|
2023-11-22 17:05:31 +00:00
|
|
|
self.states = app.state_manager
|
2023-11-10 15:02:38 +00:00
|
|
|
|
2023-11-09 21:11:53 +00:00
|
|
|
runner_thread = threading.Thread(
|
|
|
|
target=self.runner, name="runner thread", daemon=True
|
|
|
|
)
|
|
|
|
runner_thread.start()
|
|
|
|
|
2023-11-12 22:22:53 +00:00
|
|
|
# optionally start explorer module
|
|
|
|
if self.config['STATION']['enable_explorer']:
|
2023-11-19 11:31:56 +00:00
|
|
|
explorer.explorer(self.app, self.config, self.states)
|
2023-11-12 22:22:53 +00:00
|
|
|
|
2023-11-09 21:11:53 +00:00
|
|
|
def runner(self):
|
|
|
|
while True:
|
|
|
|
cmd = self.modem_service.get()
|
|
|
|
if cmd in ['start'] and not self.modem:
|
2023-11-12 21:35:10 +00:00
|
|
|
self.log.info("------------------ FreeDATA ------------------")
|
|
|
|
self.log.info("------------------ MODEM ------------------")
|
|
|
|
self.start_modem()
|
2023-11-23 06:02:56 +00:00
|
|
|
|
2023-11-12 21:35:10 +00:00
|
|
|
elif cmd in ['stop'] and self.modem:
|
|
|
|
self.stop_modem()
|
|
|
|
# we need to wait a bit for avoiding a portaudio crash
|
|
|
|
threading.Event().wait(0.5)
|
2023-11-12 18:56:15 +00:00
|
|
|
|
2023-11-12 21:35:10 +00:00
|
|
|
elif cmd in ['restart']:
|
|
|
|
self.stop_modem()
|
|
|
|
# we need to wait a bit for avoiding a portaudio crash
|
|
|
|
threading.Event().wait(0.5)
|
|
|
|
if self.start_modem():
|
|
|
|
self.modem_events.put(json.dumps({"freedata": "modem-event", "event": "restart"}))
|
2023-11-23 06:02:56 +00:00
|
|
|
elif cmd in ['fft:true']:
|
2023-11-23 09:14:11 +00:00
|
|
|
#self.modem.set_FFT_stream(True)
|
2023-11-23 06:02:56 +00:00
|
|
|
self.enable_fft=True
|
|
|
|
elif cmd in ['fft:false']:
|
2023-11-23 09:14:11 +00:00
|
|
|
#self.modem.set_FFT_stream(False)
|
2023-11-23 06:02:56 +00:00
|
|
|
self.enable_fft=False
|
2023-11-09 21:11:53 +00:00
|
|
|
else:
|
2023-11-12 21:35:10 +00:00
|
|
|
self.log.warning("[SVC] modem command processing failed", cmd=cmd, state=self.states.is_modem_running)
|
|
|
|
|
|
|
|
|
|
|
|
def start_modem(self):
|
2023-11-19 08:56:00 +00:00
|
|
|
# read config
|
|
|
|
self.config = self.app.config_manager.read()
|
|
|
|
|
|
|
|
# test audio devices
|
2023-11-12 21:35:10 +00:00
|
|
|
audio_test = self.test_audio()
|
2023-11-19 08:56:00 +00:00
|
|
|
|
2023-11-12 21:35:10 +00:00
|
|
|
if False not in audio_test and None not in audio_test and not self.states.is_modem_running:
|
|
|
|
self.log.info("starting modem....")
|
|
|
|
self.modem = modem.RF(self.config, self.modem_events, self.modem_fft, self.modem_service, self.states)
|
2023-11-23 09:14:11 +00:00
|
|
|
#self.data_handler = data_handler.DATA(self.config, self.modem_events, self.states)
|
|
|
|
self.frame_dispatcher = frame_dispatcher.DISPATCHER(self.config, self.modem_events, self.states)
|
2023-11-23 15:59:53 +00:00
|
|
|
self.frame_dispatcher.start()
|
2023-11-12 21:35:10 +00:00
|
|
|
self.states.set("is_modem_running", True)
|
2023-11-23 06:02:56 +00:00
|
|
|
self.modem.set_FFT_stream(self.enable_fft)
|
2023-11-12 21:35:10 +00:00
|
|
|
return True
|
|
|
|
elif self.states.is_modem_running:
|
|
|
|
self.log.warning("modem already running")
|
|
|
|
return False
|
2023-11-23 06:02:56 +00:00
|
|
|
|
2023-11-12 21:35:10 +00:00
|
|
|
else:
|
|
|
|
self.log.warning("starting modem failed", input_test=audio_test[0], output_test=audio_test[1])
|
|
|
|
self.states.set("is_modem_running", False)
|
|
|
|
self.modem_events.put(json.dumps({"freedata": "modem-event", "event": "failed"}))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def stop_modem(self):
|
|
|
|
self.log.info("stopping modem....")
|
|
|
|
del self.modem
|
2023-11-23 09:14:11 +00:00
|
|
|
#del self.data_handler
|
2023-11-12 21:35:10 +00:00
|
|
|
self.modem = False
|
2023-11-23 09:14:11 +00:00
|
|
|
#self.data_handler = False
|
2023-11-12 21:35:10 +00:00
|
|
|
self.states.set("is_modem_running", False)
|
|
|
|
|
|
|
|
def test_audio(self):
|
|
|
|
audio_test = audio.test_audio_devices(self.config['AUDIO']['input_device'],
|
|
|
|
self.config['AUDIO']['output_device'])
|
|
|
|
self.log.info("tested audio devices", result=audio_test)
|
|
|
|
|
|
|
|
return audio_test
|