/  Python Yamcs Client  /  Examples  /  write_mdb.py

write_mdb.py

If a system is marked as writable in the loader tree, Yamcs becomes responsible for updating the file, and the HTTP API can then be used to update it. This capability exists only for XTCE files.

Note that this functionality is experimental, and does not offer the same degree of customizations as when loading the MDB from a predefined XTCE file.

To demonstrate this capability, add a writable subsystem test to the Yamcs Quickstart example:

mdb/yamcs.myproject.yaml
mdb:
  - type: xtce
    args:
      file: mdb/xtce.xml
    subLoaders:
      - type: xtce
        writable: true
        args:
          file: /path/to/test.xml

Because the quickstart example works on a copy of the files (in target/yamcs/), we specify test.xml as an absolute rather than relative reference.

The initial content can just be an empty XTCE SpaceSystem, specifying its name:

mdb/test.xml
<?xml version="1.0" encoding="UTF-8"?>
<SpaceSystem xmlns="http://www.omg.org/spec/XTCE/20180204" name="test">
</SpaceSystem>

Then you can add parameter types and values like this:

from yamcs.client import NotFound, YamcsClient

"""
The following example requires a _writable_ system.

In this example the system ``/myproject/test`` is writable, whereas
the top-level system ``/myproject`` is populated based on a predefined
XTCE file (not managed by Yamcs itself).
"""

if __name__ == "__main__":
    client = YamcsClient("localhost:8090")
    mdb = client.get_mdb(instance="myproject")

    system = "/myproject/test"
    try:
        mdb.get_parameter_type(f"{system}/float_t")
    except NotFound:
        mdb.create_parameter_type(f"{system}/float_t", eng_type="float")

    try:
        mdb.get_parameter(f"{system}/testparam")
    except NotFound:
        mdb.create_parameter(
            f"{system}/testparam",
            data_source="LOCAL",
            parameter_type=f"{system}/float_t",
        )

    # MDB is present, now publish a parameter value
    processor = client.get_processor("myproject", "realtime")
    processor.set_parameter_value(f"{system}/testparam", 123.4)

Afterwards you should see that test.xml has been updated automatically:

mdb/test.xml
<?xml version="1.0" encoding="UTF-8"?>
<SpaceSystem xmlns="http://www.omg.org/spec/XTCE/20180204" name="test">
  <TelemetryMetaData>
    <ParameterTypeSet>
      <FloatParameterType sizeInBits="32" name="float_t">
        <UnitSet/>
      </FloatParameterType>
    </ParameterTypeSet>
    <ParameterSet>
      <Parameter parameterTypeRef="float_t" name="testparam">
        <ParameterProperties dataSource="local" persistence="false"/>
      </Parameter>
    </ParameterSet>
  </TelemetryMetaData>
</SpaceSystem>