Adding a new server to the Tango Database

Adding a new server to the Tango Database#

In the previous lesson you saw how to run a Tango Databaseds. You could start TangoTest because the device sys/tg_test/1 and the test device server instance were already defined in the database.

Adding a server#

If you want to run a new server, you first have to add the device and instance to the database. Let’s add our MegaCoffee3k server using tango-admin.

Warning

Make sure the TANGO_HOST variable is defined in your environment before to run any of the following command.

(tango-tut) $ tango_admin --add-server MegaCoffee3k/test MegaCoffee3k sys/coffee/1
(tango-tut) $ echo $?
0

Note

The tango_admin command isn’t very verbose. Even in case of failure, it won’t print any error message. You need to print the return code to check if it succeeded (0) or not. If you set an invalid TANGO_HOST, the command will fail:

(tango-tut) $ TANGO_HOST=unknown:10000 tango_admin --add-server MegaCoffee3k/test MegaCoffee3k sys/coffee/1
(tango-tut) $ echo $?
255

Given the example from the first steps, you can run it using:

main.py#
from tango.server import Device

class MegaCoffee3k(Device):
    pass


if __name__ == "__main__":
    MegaCoffee3k.run_server()
(tango-tut) $ python main.py test
Ready to accept request

test is the name of the instance you defined above.

If you try to start the server with another instance name, you’ll get an error:

(tango-tut) $ python main.py foo
The device server MegaCoffee3k/foo is not defined in database. Exiting!

You can’t start a server which isn’t defined in the database.

Adding properties#

If you take the example from device properties, and run it with python main.py test, you’ll get the same error as when running PyTango’s test_context the first time.

main.py#
from tango.server import Device, device_property


class MegaCoffee3k(Device):

    host: str = device_property(mandatory=True)
    port: int = device_property(default_value=9788)
    location: str = device_property()

    def init_device(self):
        print(f"init_device before super: {self.host}:{self.port} @ {self.location}")
        super().init_device()
        print(f"init_device after super: {self.host}:{self.port} @ {self.location}")


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

This server requires a mandatory property that you need to define in the database. Let’s add the host property using tango_admin:

(tango-tut) $ tango_admin --add-property sys/coffee/1 host localhost

Now that the property is defined, you can start the server:

(tango-tut) $ python main.py test
init_device before super: None:None @ None
init_device after super: localhost:9788 @ None
Ready to accept request

This server is using other properties. Let’s change the port value using Jive. Open Jive. Go to the sys/coffee/1 device properties and click New property. Enter port in the pop-up window.

Jive Add new property

Press OK. Click on the new property value column and enter the new value to overwrite the default.

Jive Add property value

Click Apply.

For the server to read the new property, you could just restart it. Another way is to run the Init command. Let’s do that from Jive as you have it opened.

Jive Init command

In the terminal where you started the Tango server, you should see the following output:

init_device before super: localhost:9788 @ None
init_device after super: localhost:9700 @ None

The port defined in the database overwrites the default value defined in the code.

You saw that a server shall be defined in the database before to start it. We used tango_admin, a simple command line tool, but it can also be done using Jive or json2tango.