Source code for tcsutils

'''tcsutils.py
Provides useful tools (or maybe not)
'''
from __future__ import print_function

import os, sys
import random
import tcssubsystem as ts
import TCSNamed


#
# Fixed Server IP addresses and overrides
#
try:
    namedIP = os.environ['HET_NAMED_IP']
except:
    namedIP = '192.168.66.99'

#
# Remove log_relay_route once all scripts have switched over
# to using tcsnamed.
#
log_relayIP  = '192.168.66.99'

#
# System route port numbers
#
named_route_port         = '30000'
named_event_route_port   = '30001'

log_relay_route_port        = '31000'
log_relay_event_route_port  = '31001'

#
# Define the routes to various pieces
#
named_route           = 'tcp://' + namedIP + ':' + named_route_port
named_event_route     = 'tcp://' + namedIP + ':' + named_event_route_port

log_relay_route       = 'tcp://' + log_relayIP + ':' + log_relay_route_port
log_relay_event_route = 'tcp://' + log_relayIP + ':' + log_relay_event_route_port


#
# Location of the executables
#
try:
    het_deploy_dir = os.environ['HET_DEPLOY_DIR']
except:
    het_deploy_dir = '/opt/het/hetdex'

#
# Useful function definitions
#
def kill_all_subs():
    os.killpg( 0, 2 )

def kill_remaining_subs( popens, sig=2 ):
    for sub in popens:
        if popens[sub].returncode:
            print(sub + " exited with status of " + popens[sub].returncode)
        else:
            popens[sub].send_signal(sig)

#
# start_clients
#
[docs]def start_clients(tcs=False, tracker=False, legacy=False, pas=False, pfip=False, apc=False, lrs2=False, virus=False, vdas=False, named=False, verbose=False ): '''Start client communications as requested. Returns a dictionary with the client handles. May be called multiple times with different arguments.''' return_dict = {} basename = os.path.basename(__file__) + str(random.random()) tnmd = TCSNamed.TCSNamed(named_route) # tcs_server sys_hdl = None route = None if tcs: route = tnmd.lookup('tcs-route') try: sys_hdl = ts.TCSSubSystem( basename + "_tcs", route ) except: pass return_dict['tcs'] = sys_hdl # tracker_server sys_hdl = None route = None if tracker: route = tnmd.lookup('tracker-route') try: sys_hdl = ts.TCSSubSystem( basename + "_tracker", route ) except: pass return_dict['tracker'] = sys_hdl # legacyServer sys_hdl = None route = None if legacy: route = tnmd.lookup('legacy-route') try: sys_hdl = ts.TCSSubSystem( basename + "_legacy", route ) except: pass return_dict['legacy'] = sys_hdl # pasServer sys_hdl = None route = None if pas: route = tnmd.lookup('pas-route') try: sys_hdl = ts.TCSSubSystem( basename + "_pas", route ) except: pass return_dict['pas'] = sys_hdl # pfipServer sys_hdl = None route = None if pfip: route = tnmd.lookup('pfip-route') try: sys_hdl = ts.TCSSubSystem( os.path.basename(__file__) + "_pfip", route ) except: pass return_dict['pfip'] = sys_hdl # lrs2 camra server sys_hdl = None route = None if lrs2: route = tnmd.lookup('lrs2-route') try: sys_hdl = ts.TCSSubSystem( basename + "_lrs2", route ) except: pass return_dict['lrs2'] = sys_hdl # virus camra server sys_hdl = None route = None if virus or vdas: route = tnmd.lookup('virus-route') try: sys_hdl = ts.TCSSubSystem( basename + "_virus", route ) except: pass return_dict['vdas'] = sys_hdl return_dict['virus'] = sys_hdl # tcsnamed sys_hdl = None if named: try: # don't lookup named route since we use this section to do tnmd.lookup() sys_hdl = ts.TCSSubSystem( basename + "_named", named_route ) except: pass return_dict['named'] = sys_hdl # apc server sys_hdl = None route = None if apc: route = tnmd.lookup('apc-route') if route is None: route = apc_route try: sys_hdl = ts.TCSSubSystem( basename + "_apc", route ) except: pass return_dict['apc'] = sys_hdl return return_dict
''' # # print if verbose is true # def printVerbose( verbose, printStr, fp=sys.stdout ): if verbose: print( printStr ) # # print if quiet is not True # def printErr( quiet, printStr ): if not quiet: print( 'Error: ' + printStr ) def getposition(tcs=None): """Use the handler tcs.tracker_position() to get the current position of the tracker. If the argument tcs is not None, then we use it as the client handle, otherwise we try to open a client ourselves.""" if tcs is not None: # # use the one passed in... # tcs_cl = tcs else: # # ...otherwise acquire a subsystem object # cli_list = start_clients(tcs=True) tcs_cl = cli_list['tcs'] if Tcs_cl is None: print('Can not open tcs client!') sys.exit(-1) try: coords = tcs_cl.tracker_position() except: coords = None return coords # # moveStructure # Move the structure -- Not used any more since move_structure # flag added to go_next() handler # # Inputs; # az -- the azimuth in degrees (0.0 <= az < 360.0) # legacy -- a handle to a client of the legacy subsystem # # Return: # az_deg -- the current structure azimuth at the end of the move # # def moveStructure( az_deg, legacy ): """Send a structure motion command to move the structure to az_deg position through the control system client legacy. Return az, el""" az = legacy.structure_Status( update_state='true' )['Azimuth'] if (abs(az - az_deg)) > 0.1: # use circular calculation here! legacy.structure_MoveToTrueAz( target=az_deg ) status = legacy.structure_Status( update_state='true' ) az = status['Azimuth'] el = status['Elevation'] return az, el ''' if __name__ == '__main__': def dummy(name): print('tcsutils.py: trying to get client for', name) if name == 'tracker': cli = start_clients(tracker=True) if cli['tracker'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'tcs': cli = start_clients(tcs=True) if cli['tcs'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'legacy': cli = start_clients(legacy=True) if cli['legacy'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'pfip': cli = start_clients(pfip=True) if cli['pfip'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'pas': cli = start_clients(pas=True) if cli['pas'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'lrs2': cli = start_clients(lrs2=True) if cli['lrs2'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'virus' or name == 'vdas': cli = start_clients(virus=True) if cli['virus'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) if name == 'apc': cli = start_clients(apc=True) if cli['apc'] is None: print('tcsutils.py: Got none...') else: print('tcsutils.py: got', name) tnmd = TCSNamed.TCSNamed(named_route) print('tcsutils.py: tcsnamed returns tcs-route as', tnmd.lookup('tcs-route')) print('tcsutils.py: tcsnamed returns tcs-event-route as', tnmd.lookup('tcs-event-route')) print('tcsutils.py: tcsnamed returns pas-route as', tnmd.lookup('pas-route')) print('tcsutils.py: tcsnamed returns pas-event-route as', tnmd.lookup('pas-event-route')) dummy(name='tracker') dummy(name='tcs') dummy(name='legacy') dummy(name='pfip') dummy(name='pas') dummy(name='lrs2') dummy(name='virus') dummy(name='vdas') dummy(name='apc')