Friday, 7 October 2016

PyTango: Launching DeviceServers, tips and tricks

http://www.esrf.eu/computing/cs/tango/tango_doc/kernel_doc/ds_prog/node9.html#SECTION00962000000000000000

Using an specific TCP port for ORBendPoint

The TCP for ORBendPoint can be specified at launchtime:
python PySignalSimulator.py $1 $2 -ORBendPoint giop:tcp::50100
Or from Python itself before initializing Tango:
os.environ["ORBendPoint"] = "giop:tcp::50004"
We choose to store it as an ORBendPoint@['Server/Instance']@ free property in the Tango database.
To retrieve it:
CMD="python PySignalSimulator.py $1 $2"
INSTANCE=$(basename $0 .py)/$1

OEP=$(python -c "import PyTango,sys;sys.stdout.write(PyTango.Database().get_property('ORBendPoint',['$INSTANCE']).values()[0][0])" 2> /dev/null)
if [ $OEP ] ; then  CMD="$CMD -ORBendPoint $OEP" ; fi

screen -dm -S $(basename $0 .py)-$1 $CMD
From a .py file:
try:
 os.environ["ORBendPoint"] = PyTango.Database().get_property('ORBendPoint',['Pool/'+instance_name]).values()[0][0]
except: pass

Forcing in which host the device is exported (e.g. when host have multiple IP address)

This environment variable must be set before launching the device
user@bash:/> export OMNIORB_USEHOSTNAME=10.0.0.10
It can be also stored in the database and applied in the __main__ method before calling PyTango.PyUtil:
if __name__ == '__main__':
    try:
        db = PyTango.Database()
        server = 'SQLServer/%s'%sys.argv[1]
        name = fun.first(n for n in db.get_device_class_list(server) if '/' in n and 'dserver' not in n.lower())
        DeviceHost = db.get_device_property(name,['DeviceHost'])['DeviceHost'][0]
        os.environ['OMNIORB_USEHOSTNAME'] = DeviceHost

        py = PyTango.PyUtil(sys.argv)
        SQLServer,SQLServerClass=FullTangoInheritance('SQLServer',SQLServer,SQLServerClass,DynamicDS,DynamicDSClass,ForceDevImpl=True)
        py.add_TgClass(SQLServerClass,SQLServer,'SQLServer')
        U = PyTango.Util.instance()
        U.server_init()
        U.server_run()
    except PyTango.DevFailed,e:
        print '-------> Received a DevFailed exception:',traceback.format_exc()
    except Exception,e:
        print '-------> An unforeseen exception occured....',traceback.format_exc()

Loading a Cpp / C++ Class into a Python device server

By E.Taurel
You have to compile the Tango C++ class as a shared lib. If you are using a Pogo generated Makefile, change the OUTPUT_TYPE from DEVICE to SHARED_LIB, then run make and you should have the shared lib generated.
Then you have to modify the main part of the Python class.
Here is an example (Old way of using PyTango?)
        try:
                py = PyTango.Util(sys.argv)

                py.add_Cpp_TgClass('Modbus','Modbus')
                py.add_TgClass(HqpsPyPLCClass,HqpsPyPLC,'HqpsPyPLC')

                U = PyTango.Util.instance()
                U.server_init()
                U.server_run()

        except PyTango.DevFailed,e:
                print '-------> Received a DevFailed exception:',e
        except Exception,e:
                print '-------> An unforeseen exception occured....',e
The difference is the use of the add_Cpp_TgClass()


Creating and Launching a Device Server from ipython

Having defined your device in MyDS.py (in Tango7):
from MyDS import *
py = PyTango.Util(['MyDS.py','InstanceName'])
py.add_TgClass(MyDSClass,MyDS,'MyDS')
U = PyTango.Util.instance()
U.server_init()
## U.server_run() #Needed to run events, it will block the terminal

from PySignalSimulator import *
py = PyTango.Util(['PySignalSimulator.py','test3'])
py.add_TgClass(PySignalSimulatorClass,PySignalSimulator,'PySignalSimulator')
U = PyTango.Util.instance()
U.server_init()
## U.server_run() #Needed to run events, it will block the terminal

No comments:

Post a Comment