lasp/python_src/lasp/lasp_daqconfigs.py

146 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
from .lasp_cpp import DaqConfiguration
from .lasp_version import LASP_VERSION_MAJOR
"""!
Author: J.A. de Jong - ASCEE
Description:
Data Acquistiion (DAQ) device descriptors, and the DAQ devices themselves
"""
__all__ = ["DaqConfigurations"]
import json
from .lasp_common import Qty, SIQtys, lasp_shelve
from .lasp_cpp import DaqChannel, DaqConfiguration
class DaqConfigurations:
"""
DaqConfigurations stores a set containing an input configuration and an
output configuration.
"""
def __init__(
self,
duplex_mode: bool,
input_config: DaqConfiguration,
output_config: DaqConfiguration,
):
"""
Initialize set of DaqConfigurations.
Args:
duplex_mode: If true, the input configuration is used for output as
well. This makes only sense when the device is capable of having
simultaneous input / output.
input_config: The configuration settings for the input
output_config: The configuration settoutgs for the output
"""
self.input_config = input_config
self.output_config = output_config
self.duplex_mode = duplex_mode
@staticmethod
def getNames():
"""
Get a list of all names of DaqConfigurations sets.
Returns:
list of names
"""
with lasp_shelve() as sh:
configs_ser = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
return list(configs_ser.keys())
@staticmethod
def loadAll():
"""
Returns a dictionary of all configurations presets. The dictionary keys
are the names of the configurations
Returns:
all configurations, as a dictionary
"""
with lasp_shelve() as sh:
configs_ser = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
configs = {}
for name, val in configs_ser.items():
configs[name] = DaqConfigurations.load(name)
return configs
@staticmethod
def load(name: str):
"""
Load a single configuration preset, containing input config and output config
Args:
name: The name of the configuration to load.
"""
with lasp_shelve() as sh:
configs_str = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
config_str = configs_str[name]
duplex_mode = config_str[0]
input_config = DaqConfiguration.fromTOML(config_str[1])
output_config = DaqConfiguration.fromTOML(config_str[2])
return DaqConfigurations(duplex_mode, input_config, output_config)
@staticmethod
def loadRaw():
"""
Returns configurations presets in the raw form they are stored.
Returns:
all configurations, raw
"""
with lasp_shelve() as sh:
configs_raw = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
return configs_raw
def save(self, name: str):
"""
Save the current set of configurations to the shelve store.
Args:
name: The name of the configuration set.
"""
with lasp_shelve() as sh:
# Convert to TOML
input_str = self.input_config.toTOML()
output_str = self.output_config.toTOML()
configs_str = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
configs_str[name] = [self.duplex_mode, input_str, output_str]
sh.store(f"daqconfigs_v{LASP_VERSION_MAJOR}", configs_str)
@staticmethod
def saveRaw(configs_raw):
"""
Save configurations presets, using already formatted data
Arg:
all configurations, raw data format in form they are stored
"""
with lasp_shelve() as sh:
sh.store(f"daqconfigs_v{LASP_VERSION_MAJOR}", configs_raw)
@staticmethod
def delete(name: str):
"""
Delete a DaqConfigurations set from the store.
"""
with lasp_shelve() as sh:
configs_str = sh.load(f"daqconfigs_v{LASP_VERSION_MAJOR}", {})
del configs_str[name]
sh.store(f"daqconfigs_v{LASP_VERSION_MAJOR}", configs_str)