From 42234d86fd2723d9b15a9e1f716bf7b430097937 Mon Sep 17 00:00:00 2001 From: Artur Glavic Date: Wed, 25 Feb 2026 08:36:16 +0100 Subject: [PATCH] Add nicos request via socket as first alternative looking up devices --- eos/nicos_interface.py | 47 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/eos/nicos_interface.py b/eos/nicos_interface.py index b58efbf..644ac4d 100644 --- a/eos/nicos_interface.py +++ b/eos/nicos_interface.py @@ -2,30 +2,59 @@ Functions used to directly access information from nicos. """ +import socket import platform import logging import subprocess -if platform.node().startswith('amor'): - NICOS_CACHE_DIR = '/home/data/nicosdata/cache/' - GREP = '/usr/bin/grep "value"' -else: - NICOS_CACHE_DIR = None +ON_AMOR = platform.node().startswith('amor') +NICOS_CACHE_DIR = '/home/data/nicosdata/cache/' +GREP = '/usr/bin/grep "value"' def lookup_nicos_value(key, nicos_key, dtype=float, suffix='', year="2026"): # TODO: Implement direct communication to nicos to read the cache if nicos_key=='ignore': return dtype(0) - if NICOS_CACHE_DIR: + try: + logging.debug(f' trying socket request for device {nicos_key}') + response = nicos_single_request(nicos_key) + logging.info(f" using parameter {nicos_key} from nicos cache via socket") + return dtype(response) + except Exception as e: + logging.debug(f' socket request failed with {e!r}') + if ON_AMOR: + logging.debug(f" trying to extract {nicos_key} from nicos cache files") + call = f'{GREP} {NICOS_CACHE_DIR}nicos-{nicos_key}/{year}{suffix}' try: - logging.info(f" using parameter {nicos_key} from nicos cache") - call = f'{GREP} {NICOS_CACHE_DIR}nicos-{nicos_key}/{year}{suffix}' value = str(subprocess.getoutput(call)).split('\t')[-1] + logging.info(f" using parameter {nicos_key} from nicos cache file") return dtype(value) except Exception: - logging.error(f"Couldn't get value from nicos cache {nicos_key}, {call}") + logging.error(f" couldn't get value from nicos cache {nicos_key}, {call}") return dtype(0) else: logging.warning(f" parameter {key} not found, relpace by zero") return dtype(0) + +def nicos_single_request(device): + sentinel = b'\n' + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(1.0) + s.connect(('amor', 14869)) + + tosend = f'@nicos/{device}/value?\n' + + # write request + # self.log.debug("get_explicit: sending %r", tosend) + s.sendall(tosend.encode()) + + # read response + data = b'' + while not data.endswith(sentinel): + newdata = s.recv(8192) # blocking read + if not newdata: + raise IOError('cache closed connection') + data += newdata + s.shutdown(socket.SHUT_RDWR) + return data.decode('utf-8').split('=')[-1]