fix problems with sdr device failover detection

This commit is contained in:
Jakob Ketterl 2020-05-04 20:36:17 +02:00
parent efa9771ad7
commit eab3bf780e
2 changed files with 47 additions and 30 deletions

View File

@ -15,6 +15,7 @@ from owrx.modes import Modes, DigitalMode
from multiprocessing import Queue from multiprocessing import Queue
from queue import Full from queue import Full
from js8py import Js8Frame from js8py import Js8Frame
from abc import ABC, abstractmethod
import json import json
import threading import threading
@ -23,7 +24,7 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Client(object): class Client(ABC):
def __init__(self, conn): def __init__(self, conn):
self.conn = conn self.conn = conn
self.multiprocessingPipe = Queue(100) self.multiprocessingPipe = Queue(100)
@ -52,6 +53,7 @@ class Client(object):
except Full: except Full:
self.close() self.close()
@abstractmethod
def handleTextMessage(self, conn, message): def handleTextMessage(self, conn, message):
pass pass
@ -145,8 +147,12 @@ class OpenWebRxReceiverClient(Client):
self.startDsp() self.startDsp()
if "params" in message: if "params" in message:
dsp = self.getDsp()
if dsp is None:
logger.warning("DSP not available; discarding client data")
else:
params = message["params"] params = message["params"]
self.getDsp().setProperties(params) dsp.setProperties(params)
elif message["type"] == "config": elif message["type"] == "config":
if "params" in message: if "params" in message:
@ -172,6 +178,7 @@ class OpenWebRxReceiverClient(Client):
logger.warning("message is not json: {0}".format(message)) logger.warning("message is not json: {0}".format(message))
def setSdr(self, id=None): def setSdr(self, id=None):
while True:
next = None next = None
if id is not None: if id is not None:
next = SdrService.getSource(id) next = SdrService.getSource(id)
@ -179,6 +186,7 @@ class OpenWebRxReceiverClient(Client):
next = SdrService.getFirstSource() next = SdrService.getFirstSource()
if next is None: if next is None:
# exit condition: no sdrs available # exit condition: no sdrs available
logger.warning("no more SDR devices available")
self.handleNoSdrsAvailable() self.handleNoSdrsAvailable()
return return
@ -194,6 +202,15 @@ class OpenWebRxReceiverClient(Client):
self.sdr = next self.sdr = next
self.getDsp()
# found a working sdr, exit the loop
if self.sdr.getState() != SdrSource.STATE_FAILED:
break
logger.warning('SDR device "%s" has failed, selecing new device', self.sdr.getName())
self.write_log_message('SDR device "{0}" has failed, selecting new device'.format(self.sdr.getName()))
# send initial config # send initial config
self.getDsp().setProperties(self.connectionProperties) self.getDsp().setProperties(self.connectionProperties)
@ -227,13 +244,7 @@ class OpenWebRxReceiverClient(Client):
self.write_sdr_error("No SDR Devices available") self.write_sdr_error("No SDR Devices available")
def startDsp(self): def startDsp(self):
while True:
self.getDsp().start() self.getDsp().start()
if self.sdr.getState() == SdrSource.STATE_FAILED:
self.write_log_message('SDR device "{0}" has failed, selecting new device'.format(self.sdr.getName()))
self.setSdr()
else:
break
def close(self): def close(self):
self.stopDsp() self.stopDsp()
@ -271,7 +282,7 @@ class OpenWebRxReceiverClient(Client):
pass pass
def getDsp(self): def getDsp(self):
if self.dsp is None: if self.dsp is None and self.sdr is not None:
self.dsp = DspManager(self, self.sdr) self.dsp = DspManager(self, self.sdr)
return self.dsp return self.dsp

View File

@ -199,9 +199,15 @@ class WebSocketConnection(object):
data = bytes([b ^ masking_key[index % 4] for (index, b) in enumerate(data)]) data = bytes([b ^ masking_key[index % 4] for (index, b) in enumerate(data)])
if opcode == OPCODE_TEXT_MESSAGE: if opcode == OPCODE_TEXT_MESSAGE:
message = data.decode("utf-8") message = data.decode("utf-8")
try:
self.messageHandler.handleTextMessage(self, message) self.messageHandler.handleTextMessage(self, message)
except Exception:
logger.exception("Exception in websocket handler handleTextMessage()")
elif opcode == OPCODE_BINARY_MESSAGE: elif opcode == OPCODE_BINARY_MESSAGE:
try:
self.messageHandler.handleBinaryMessage(self, data) self.messageHandler.handleBinaryMessage(self, data)
except Exception:
logger.exception("Exception in websocket handler handleBinaryMessage()")
elif opcode == OPCODE_PING: elif opcode == OPCODE_PING:
self.sendPong() self.sendPong()
elif opcode == OPCODE_PONG: elif opcode == OPCODE_PONG: