Save and load Configuration

openDAQ™ provides features to save the current Configuration to JSON format. The Configuration is defined as the current state of the complete openDAQ tree. It consists of:

  • Devices and their properties,

  • Device Channels and their properties,

  • Device custom components and their properties,

  • Subdevices (recursively) and their properties,

  • Function Blocks and their properties,

  • Signals and their properties,

  • Input Ports with connections to Signals.

When loading a Configuration from JSON format all components except Function Blocks are matched by their local ID and the properties are applied from JSON to the objects themselves. Function Blocks are first constructed from their type ID (if a module exists) and then the properties are applied from JSON.

Save Configuration

The SDK provides a method on the instance interface to save the Configuration to a string. The application can use this string to save the Configuration to a file or any other media.

  • Cpp

  • Python

  • C#

// Save Configuration to string
std::string jsonStr = instance.saveConfiguration();
// Write Configuration string to file
std::ofstream configFile("config.json");
configFile << jsonStr;
configFile.close();
# Save Configuration to string
json_str = instance.save_configuration()
# Write Configuration string to file
config_file = open("config.json", "w")
config_file.write(json_str)
config_file.close()
// Save Configuration to string
string jsonStr = instance.SaveConfiguration();
// Write Configuration string to file
File.WriteAllText("openDAQconfig.json", jsonStr, System.Text.Encoding.UTF8);

Saving Configuration will produce a string similar to this:

"refch0": {
    "__type": "Channel",
    "typeId": "ref_channel",
    "sig": {
        "__type": "Folder",
        "items": {
            "ai0": {
                "__type": "Signal",
                "domainSignalId": "dev/ref_dev0/io/ai/refch0/sig/ai0_time"
            },
            "ai0_time": {
                "__type": "Signal"
            }
        }
    },
    "propValues": {
        "Amplitude": 1.0,
        "TestReadOnly": true
    }
}

Because the Configuration string can be very long, the one shown above has been trimmed to show only a part of it.

Load Configuration

Once the Configuration has been saved, it can be applied to the openDAQ instance. The Configuration is passed as a string parameter which can be read from a file or any other media.

  • Cpp

  • Python

  • C#

// Read Configuration from file
std::ifstream configFile("config.json");
std::stringstream jsonStr;
jsonStr << configFile.rdbuf();
// Load Configuration from string
instance.loadConfiguration(jsonStr.str());
# Read Configuration from file
config_file = open("config.json", "r")
json_str = config_file.read()
config_file.close()
# Load Configuration from string
instance.load_configuration(json_str)
// Read Configuration from file
string jsonStr = File.ReadAllText("openDAQconfig.json", System.Text.Encoding.UTF8);
// Load Configuration from string
instance.LoadConfiguration(jsonStr);

While loading the Configuration, the SDK checking the circular dependencies of signal (for example output signal of function block connected as input signal to the same function block). At that case LoadConfiguration method will not stop the proccess but will notify user with the warning message of the trace of the circular dependencies.

Load Configuration with custom parameters

Load congiguration method has an optional parameter of IUpdateParameters which has set of paramters that gived developer control over how the configuration is loaded. The following parameters are available:

  • setReAddDevicesEnabled(bool) - If the instance has a device which is also presented in the configuration, with this parameter the developer can decide if the device should be re-added to the instance or not. Default is false.

  • Cpp

  • Python

  • C#

// Read Configuration from file
std::ifstream configFile("config.json");
std::stringstream jsonStr;
jsonStr << configFile.rdbuf();

// Set custom load parameters
auto loadConfig = UpdateParameters();
loadConfig.setReAddDevicesEnabled(true);

// Load Configuration from string
instance.loadConfiguration(jsonStr.str(), loadConfig);
# Read Configuration from file
config_file = open("config.json", "r")
json_str = config_file.read()
config_file.close()

# Set custom load parameters
load_config = UpdateParameters()
load_config.re_add_devices_enabled = True
# Load Configuration from string
instance.load_configuration(json_str, load_config)
// Read Configuration from file
string jsonStr = File.ReadAllText("openDAQconfig.json", System.Text.Encoding.UTF8);

// Set custom load parameters
var loadConfig = OpenDAQFactory.UpdateParameters();
loadConfig.SetReAddDevicesEnabled(true);

// Load Configuration from string
instance.LoadConfiguration(jsonStr);

Saving and loading Configuration of specific components

openDAQ SDK also supports storing and loading of Configuration for individual components (Device, Channel, Function Block, etc …​)