diff options
Diffstat (limited to 'qolab/hardware/scpi.py')
-rw-r--r-- | qolab/hardware/scpi.py | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/qolab/hardware/scpi.py b/qolab/hardware/scpi.py index ed7e30e..2054410 100644 --- a/qolab/hardware/scpi.py +++ b/qolab/hardware/scpi.py @@ -3,6 +3,13 @@ provide basic class to operate SCPI capable instruments """ import re +import logging +import time + +logging.basicConfig(format='%(asctime)s %(levelname)8s %(name)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S') +logger = logging.getLogger('qolab.hardware.scpi') +# logger.setLevel(logging.INFO) +logger.setLevel(logging.DEBUG) def response2numStr(strIn, firstSeparator=None, unit=None): # Often an instrument reply is in the form 'TDIV 2.00E-08S' (for example Siglent Scope) @@ -32,10 +39,12 @@ class SCPI_PROPERTY(property): doc - short description of property, for example 'Internal lockin frequency' no_getter - typical for some sort of command no_setter - typical for measured values (i.e. external to the instrument) + tsdb_logging - logging to time series DB (True/False) """ - def __init__(self, scpi_prfx=None, ptype=str, doc=None, no_getter=False, no_setter=False): + def __init__(self, scpi_prfx=None, ptype=str, doc=None, no_getter=False, no_setter=False, tsdb_logging=True): self.no_getter = no_getter self.no_setter = no_setter + self.tsdb_logging = tsdb_logging if no_getter: fget=None else: @@ -53,11 +62,34 @@ class SCPI_PROPERTY(property): self.public_name = name self.private_name = '_' + name + def log_to_tsdb(self, owner, action=None, val=None): + if owner.tsdb_ingester is None or not self.tsdb_logging: + return + if owner.config['DeviceNickname'] is not None: + measurement=owner.config['DeviceNickname'] + else: + measurement=owner.config['Device type'] + ts = time.time() + ts_ms = int(ts*1000) + var_name = self.public_name + tags = {'action': action } + fields = {var_name: val} + try: + msg=f"{ts_ms=}, {measurement=}, {tags=}, {fields=}" + logger.debug(msg) + owner.tsdb_ingester.append(ts_ms, measurement=measurement, tags=tags, **fields) + except ValueError as err: + logger.error(f'{err=} in function {f.__name__}: {var_name} = {val}') + + def get_scpi(self, owner): - return self.ptype( owner.query(f'{self.scpi_prfx}?') ) + val = self.ptype( owner.query(f'{self.scpi_prfx}?') ) + self.log_to_tsdb(owner, action='get', val=val) + return val def set_scpi(self, owner, val): - return owner.write(f'{self.scpi_prfx} {val}') + owner.write(f'{self.scpi_prfx} {val}') + self.log_to_tsdb(owner, action='set', val=val) def __repr__(self): sargs= [] @@ -66,6 +98,7 @@ class SCPI_PROPERTY(property): sargs.append( f'doc={self.__doc__}') sargs.append( f'no_getter={self.no_getter}') sargs.append( f'no_setter={self.no_setter}') + sargs.append( f'tsdb_logging={self.tsdb_logging}') sargs =', '.join(sargs) s = ''.join( [ f'{self.__class__.__name__}(' , sargs, ')' ] ) return s @@ -143,7 +176,7 @@ if __name__ == '__main__': print(f'query: {str=}') return '123' - x = SCPI_PROPERTY(scpi_prfx='SETX', ptype=str, doc='property X') + x = SCPI_PROPERTY(scpi_prfx='SETX', ptype=str, doc='property X', tsdb_logging=False) y = SCPI_PROPERTY(scpi_prfx='SETY', ptype=int, no_setter=True, doc='property Y') z = SCPI_PROPERTY(scpi_prfx='SETY', ptype=int, no_getter=True, doc='property Z') |