use a generic unpickler

This commit is contained in:
Jakob Ketterl 2021-09-13 00:14:38 +02:00
parent 1d2ee127e0
commit 1c937e147e
2 changed files with 19 additions and 23 deletions

View File

@ -15,13 +15,10 @@ from owrx.config import Config
from owrx.waterfall import WaterfallOptions
from owrx.websocket import Handler
from queue import Queue, Full, Empty
from js8py import Js8Frame
from abc import ABCMeta, abstractmethod
from io import BytesIO
import json
import threading
import struct
import pickle
import logging
@ -379,12 +376,8 @@ class OpenWebRxReceiverClient(OpenWebRxClient, SdrSourceEventClient):
self.send(bytes([0x04]) + data)
def write_s_meter_level(self, level):
if isinstance(level, memoryview):
# may contain more than one sample, so only take the last 4 bytes = 1 float
level, = struct.unpack('f', level[-4:])
if not isinstance(level, float):
logger.warning("s-meter value has unexpected type")
return
try:
self.send({"type": "smeter", "value": level})
except ValueError:
@ -400,12 +393,7 @@ class OpenWebRxReceiverClient(OpenWebRxClient, SdrSourceEventClient):
self.send(bytes([0x03]) + data)
def write_secondary_demod(self, message):
io = BytesIO(message.tobytes())
try:
while True:
self.send({"type": "secondary_demod", "value": pickle.load(io)})
except EOFError:
pass
self.send({"type": "secondary_demod", "value": message})
def write_secondary_dsp_config(self, cfg):
self.send({"type": "secondary_config", "value": cfg})
@ -420,12 +408,7 @@ class OpenWebRxReceiverClient(OpenWebRxClient, SdrSourceEventClient):
self.send({"type": "features", "value": features})
def write_metadata(self, metadata):
io = BytesIO(metadata.tobytes())
try:
while True:
self.send({"type": "metadata", "value": pickle.load(io)})
except EOFError:
pass
self.send({"type": "metadata", "value": metadata})
def write_dial_frequencies(self, frequencies):
self.send({"type": "dial_frequencies", "value": frequencies})

View File

@ -18,8 +18,10 @@ from csdr.chain.digimodes import AudioChopperDemodulator, PacketDemodulator, Poc
from pycsdr.modules import Buffer, Writer
from pycsdr.types import Format
from typing import Union
from io import BytesIO
import threading
import re
import pickle
import logging
@ -525,8 +527,8 @@ class DspManager(Output, SdrSourceEventClient):
"hd_audio": self.handler.write_hd_audio,
"smeter": self.handler.write_s_meter_level,
"secondary_fft": self.handler.write_secondary_fft,
"secondary_demod": self.handler.write_secondary_demod,
"meta": self.handler.write_metadata,
"secondary_demod": self._unpickle(self.handler.write_secondary_demod),
"meta": self._unpickle(self.handler.write_metadata),
}
write = writers[t]
@ -537,6 +539,17 @@ class DspManager(Output, SdrSourceEventClient):
self.readers[t] = reader
threading.Thread(target=self.pump(reader.read, write), name="dsp_pump_{}".format(t)).start()
def _unpickle(self, callback):
def unpickler(data):
io = BytesIO(data.tobytes())
try:
while True:
callback(pickle.load(io))
except EOFError:
pass
return unpickler
def stop(self):
self.chain.stop()
self.chain = None