aboutsummaryrefslogtreecommitdiff
path: root/qolab/hardware/basic.py
blob: 5e8d17d5197d1675f020a18047054d3fe657a086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import yaml
from qolab.file_utils import get_next_data_file
from qolab.tsdb import tsdb_append_metric_for_class_setter_or_getter

class BasicInstrument:
    def __init__(self, config={}, device_nickname=None, tsdb_ingester=None):
        self.config = {}
        self.config['Device type'] = 'Basic Instrument'
        self.config['Device model'] = 'Model is unset'
        self.config['DeviceId'] = None
        self.config['DeviceNickname'] = device_nickname; # to separate similar instruments
        self.config['FnamePrefix'] = 'basicInstrument'
        self.config['SavePath'] = './data'
        for k, v in config.items():
            self.config[k]=v
        self.tsdb_ingester = tsdb_ingester
        # deviceProperties must have 'get' and preferably 'set' methods available,
        # i.e. 'SampleRate' needs getSampleRate() and love to have setSampleRate(value)
        # they will be used to obtain config and set device according to it
        # self.deviceProperties = ['SampleRate', 'TimePerDiv', 'TrigDelay', ];
        self.deviceProperties = [];

    def __repr__(self):
        s = ''
        s += f'{self.__class__.__name__}('
        s += 'config={'
        cstr=[]
        for k, v in self.config.items():
            if v is not None:
                cstr.append(
                    f"'{k}'"
                    ': '
                    f"'{v}'")
        s += ', '.join(cstr)
        s += '}'
        if self.tsdb_ingester is not None:
            s += f', tsdb_ingester={self.tsdb_ingester}'
        s += ')'
        return(s)

    def tsdb_append(f):
        return tsdb_append_metric_for_class_setter_or_getter()(f)

    def getConfig(self):
        config = self.config.copy()
        dconfig = {}
        for p in self.deviceProperties:
            getter = f'get{p}'
            if  not hasattr(self, getter):
                print(f'warning no getter for {p}, i.e. {getter} is missing')
                continue
            res = getattr(self, getter)() 
            dconfig[p] = res
        config['DeviceConfig'] = dconfig
        return config

    def getHeader(self):
        header = yaml.dump(self.getConfig(), default_flow_style=False, sort_keys=False)
        header = header.split('\n')
        return header
    
    def getNextDataFile(self, extention='dat'):
        fname = get_next_data_file(self.config['FnamePrefix'], self.config['SavePath'], extention=extention)
        return fname