VscCvt.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2004
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Current Value Table Server"
00012 __author__  = "Jim Panetta <panetta@slac.stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = ("$Date: 2006/03/19 00:00:11 $").split(' ')[1]
00014 __version__ = "$Revision: 1.16 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__ = "SLAC"
00017 
00018 import LATTE.copyright_SLAC
00019 
00020 import logging as log
00021 import struct, time, threading, socket, asyncore, sys
00022 from ConfigParser import *
00023 import VSC, CCSDS
00024 
00025 from LICOS.lib.currValTable.CurrValServer   import CurrentValueFactory
00026 from LICOS.lib.currValTable.CurrValTable    import CurrentValueTable
00027 from LICOS.tools.proxy.VscProxyTools        import encodePacket, getip
00028 from LICOS.lib.cmdTlmDb.LCATtlmDb           import ccsdsTlmHdr, LICOS_VscTlmPacketFactory
00029 from LICOS.tools.proxy.VscProxyPorts        import VscProxyPorts
00030 
00031 import LICOS.util.gOptions as gOptions
00032 
00033 killEvent = threading.Event()
00034 killEvent.clear()
00035 
00036 def asyncore_loop(timeout=1.0, use_poll=False, map=None):
00037   """replace the asyncore central loop and run it its own thread.
00038      Valid for Python 2.3.x
00039      Python 2.4 has an extra agument for loop, count.
00040   """
00041   if map is None:
00042       map = asyncore.socket_map
00043 
00044   if use_poll:
00045     if hasattr(select, 'poll'):
00046       poll_fun = asyncore.poll3
00047     else:
00048       poll_fun = asyncore.poll2
00049   else:
00050     poll_fun = asyncore.poll
00051 
00052   while map and not killEvent.isSet():
00053     poll_fun(timeout, map)
00054     sys.stdout.flush()
00055 
00056 
00057 class CvtRouter(VSC.VscRouting.TelemetryRouter):
00058   def __init__(self, cvt):
00059     VSC.VscRouting.TelemetryRouter.__init__(self)
00060     self.__packet = CCSDS.Ccsds.Telemetry(0, 0, 0)
00061     self.__cvt = cvt
00062     self.count = 0
00063 
00064   def allocate(self):
00065     return self.__packet
00066 
00067   def catchall(self, dat):
00068     apIdPkt = LICOS_VscTlmPacketFactory(dat)
00069     if apIdPkt is None:
00070       hdr = ccsdsTlmHdr(struct.unpack('!H', packet[0:2])[0])
00071       print "ccsdsCVT received apID %d = 0x%03x which is unrecognized as telemetry" % (hdr.apid, hdr.apid)
00072 
00073     aTime = apIdPkt.secs + apIdPkt.usecs/1E6
00074     #print apIdPkt, aTime
00075 
00076     apIdPkt.decode_payload()
00077     # toss each mnemonic in the cvt and then...
00078     for mnem in apIdPkt.get_payload_list():
00079       # print mnem, apIdPkt.get_payload(mnem)
00080       self.__cvt.setValue(mnem, apIdPkt.get_payload(mnem), aTime)
00081     # add in the packet as well
00082     cvt.setValue(apIdPkt.name.upper(), encodePacket(dat), aTime)
00083 
00084     if self.count%500 >= 0:
00085       # print self.__class__, dir(self), self.__str__()
00086       log.debug( "%s found %d packets.  Last packet=%s at %s" %( self.__class__, self.count, apIdPkt, aTime))
00087     self.count += 1
00088 
00089 class CvtProxy(VSC.VscProxy.Proxy):
00090   """Define our own proxy
00091   """
00092   def __init__(self, ip_vsc, vscPort=39900, cvt=None):
00093     VSC.VscProxy.Proxy.__init__(self, ip_vsc, vscPort)
00094 
00095     # create the master routers
00096     self.tlmRouter     = CvtRouter(cvt)
00097     self.vscRouter     = CvtRouter(cvt)
00098 
00099     # create the subsidiary handlers and register them with the routers
00100     print "CvtProxy inserting telemrouters"
00101     self.latTelemetry(self.tlmRouter)
00102     self.vscTelemetry(self.vscRouter)
00103 
00104 class VscCVT(CurrentValueTable):
00105   def __init__(self, ip_vsc, vscPort):
00106     super(VscCVT, self).__init__()
00107     self.__ip_vsc = ip_vsc
00108     print "Creating CVT proxy, connecting to %x:%d" % ( ip_vsc, vscPort )
00109     self.__proxy = CvtProxy(ip_vsc, vscPort, cvt=self)
00110 
00111 
00112 
00113 if __name__ == '__main__':
00114   options = gOptions.Options(['proxyHost', 'config'])
00115   try:
00116     options.parse()
00117   except Exception, msg:
00118     options.usage(str(msg))
00119     exit
00120 
00121   ip_vsc = getip(options.proxyHost)
00122   configFile = options.config
00123   config = ConfigParser()
00124   config.read(configFile)
00125 
00126   # create port offsets
00127   portTool = VscProxyPorts(config.getint('vsc', 'proxyPortBase'))
00128   cvtPort = portTool.cvtOut()
00129   vscPort = portTool.cvt()
00130 
00131   # Build CVT proxy client and CVT server
00132   cvt = VscCVT(ip_vsc, vscPort)
00133   cvs = CurrentValueFactory(cvtPort, cvt)
00134 
00135   # start asynchronous processing loop
00136   asyncore_loop()
00137 
00138   killEvent.set()
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 #eof

Generated on Thu Apr 27 20:52:44 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO