diff --git a/nicoscache.py b/nicoscache.py index 8768230..4d30370 100644 --- a/nicoscache.py +++ b/nicoscache.py @@ -56,8 +56,8 @@ class NicosStream(Stream): self.secnode_uri = {} super().__init__(uri, name) - def init(self): - self.tags['device'] = 'nicos' + def init(self, device=None, **kwds): + self.tags['device'] = device or 'nicos' self.send(f'@nicos/*\n{END_MARKER}{OP_ASK}') self._init = True diff --git a/secop.py b/secop.py index 4f85ec4..ed394d1 100644 --- a/secop.py +++ b/secop.py @@ -19,7 +19,7 @@ class EnumConvert(dict): class SecopStream(Stream): ping_time = 0 - def init(self): + def init(self, device=None, **kwds): self._buffer = [] self.send('*IDN?') resend = True @@ -41,7 +41,7 @@ class SecopStream(Stream): else: raise ValueError('missing describing message') self.descr = json.loads(match.group(1)) - self.device = self.descr['equipment_id'] + self.device = device or self.descr['equipment_id'] if self.device.endswith('psi.ch'): self.device[-6:] = [] self.tags['device'] = self.device @@ -110,13 +110,18 @@ class UdpStream(Base): except socket.error: # pragma: no cover return None msg = json.loads(msg.decode('utf-8')) - if msg['SECoP'] == 'for_other_node': - uri = msg['uri'] - elif msg['SECoP'] == 'node': + kind = msg.pop('SECoP', None) + if not kind: + continue + if kind == 'for_other_node': + uri = msg.pop('uri') + kwargs = msg + elif kind == 'node': uri = f"{addr[0]}:{msg['port']}" + kwargs = {'name': msg['equipment_id']} else: continue - yield SecopStream, uri, msg['equipment_id'] + yield SecopStream, uri, kwargs class ScanReply(UdpStream): diff --git a/streams.py b/streams.py index d00b0f9..10b2ba0 100644 --- a/streams.py +++ b/streams.py @@ -48,7 +48,7 @@ class Stream(Base): _deadline = INF _next_connect = 0 - def __init__(self, uri, name=None, timeout=5, encoding='latin-1'): + def __init__(self, uri, name=None, timeout=5, encoding='latin-1', **kwds): self.name = name or uri self.uri = uri self.tags = {} @@ -63,7 +63,7 @@ class Stream(Base): self.generator = self.event_generator() try: self.connect() - self.init() + self.init(**kwds) except Exception as e: print(self.uri, repr(e)) raise @@ -261,10 +261,10 @@ class EventStream: while 1: for stream in self.wait_ready(1): if not isinstance(stream, Stream): - for streamcls, uri, *args in stream.events(): + for streamcls, uri, kwargs in stream.events(): if uri not in self.streams: - print('add stream', uri, *args) - self.streams[uri] = streamcls(uri, *args) + print('add stream', uri, kwargs) + self.streams[uri] = streamcls(uri, **kwargs) for name, stream in self.streams.items(): try: if stream.get_events(events, maxevents):