Memorized attributes

Memorized attributes#

Sometimes you want the value of an attribute to be saved so it can be applied automatically after restarting your server. This is what memorized attributes are for.

Note

This only applies to attributes of the scalar data format, which are READ_WRITE. You can’t have 1D or 2D memorized attributes.

main.py#
from tango import DevState
from tango.server import Device, attribute


class MegaCoffee3k(Device):

    def init_device(self):
        super().init_device()
        self.set_state(DevState.OFF)
        self._brewing_temperature = 94.4
        print("init_device done")

    @attribute(memorized=True, hw_memorized=True)
    def brewingTemperature(self) -> float:
        print("reading brewing temperature")
        return self._brewing_temperature

    @brewingTemperature.setter
    def brewingTemperature(self, temperature: float):
        print(f"writing brewing temperature: {temperature}")
        self._brewing_temperature = temperature


if __name__ == "__main__":
    MegaCoffee3k.run_server()

Sorry, still TODO!

Sorry, still TODO!

Define the brewingTemperature attribute with both the memorized and hw_memorized arguments set to True:

  • memorized: if True, the latest written value is stored in the database

  • hw_memorized: if True, the memorized value will be restored by calling the write method at startup and after each Init command

Warning

When using hw_memorized, make sure writing the value after Init is possible. If you have an is-allowed method, that could make restoring the value fail.

As the value of memorized attributes is saved in the tango database, you need a running database.

(tango-tut) $ python main.py test
init_device done
Ready to accept request

In a second terminal, use a device proxy client to read and write the attribute:

>>> import tango
>>> dp = tango.DeviceProxy("sys/coffee/1")
>>> dp.brewingTemperature
94.4
>>> dp.brewingTemperature = 95.1

In the terminal where you started the tango device server, you should see:

reading brewing temperature
writing brewing temperature: 95.1

Stop that server, by pressing CTRL-C and start it again:

(tango-tut) $ python main.py test
init_device done
writing brewing temperature: 95.1
Ready to accept request

You should see that the write method is called automatically, after init_device. If you read the value from your second terminal, you’ll get 95.1:

>>> dp.brewingTemperature
95.1

If you run the Init command, the same thing will happen:

>>> dp.Init()
init_device done
writing brewing temperature: 95.1

You can check the database to see where this value is stored:

>>> db = tango.Database()
>>> db.get_device_attribute_property("sys/coffee/1", "brewingTemperature")
{'brewingTemperature': {'__value': ['95.1']}}

When writing a memorized attribute, the value is stored in the Tango database as an attribute property, named __value.

Warning

Every write to a memorized attribute requires a round-trip to the Tango Database. Be careful if the attribute will be written at a high rate, or if there will be a large numbers of these devices updating memorized attributes simultaneously. This can have a performance impact.

You coffee machine will now remember your preferred brewing temperature even after a server restart!