Files
BStrd/bscache.py
2021-09-08 10:52:08 +02:00

74 lines
1.5 KiB
Python

from time import sleep
from bsread import source
from bsvar import BSVar
from prodthread import ProdThread
class BSCache:
def __init__(self):
self.channels = set()
self.data = None
self.pt = ProdThread(self.run)
def __iter__(self):
return self
def __next__(self):
self.pt.start()
self.data = data = self.pt.queue.get()
return data
def run(self, queue, running):
channels = self.channels
with source(channels=channels, receive_timeout=-1) as src:
while running.is_set():
msg = src.receive()
data = repack(channels, msg)
if data:
queue.put(data)
def stop(self, *args):
print("\n\nstopping\n\n", args)
self.pt.stop()
def get_var(self, name):
if name is not "pid" and not name in self.channels:
self.update_source(name)
return BSVar(name, self)
def update_source(self, name):
self.pt.stop()
print("add channel", name)
self.channels.add(name)
self.pt.start()
def repack(channels, message):
data = message.data.data
pulse_id = message.data.pulse_id
res = {n: data[n].value for n in channels}
# res = {k: v for k, v in res.items() if v is not None}
#TODO: should this be a ValueError?
if any(v is None for v in res.values()):
return None
if not res:
return None
res["pid"] = pulse_id
return res