00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 __facility__ = "Online"
00011 __abstract__ = "Base class for all command database wrapper classes"
00012 __author__ = "Selim Tuvi <stuvi@slac.stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__ = "2005/07/23 00:08:27"
00014 __updated__ = "$Date: 2006/01/25 23:11:06 $"
00015 __version__ = "$Revision: 1.15 $"
00016 __release__ = "$Name: HEAD $"
00017 __credits__ = "SLAC"
00018
00019 import LICOS.copyright_SLAC
00020
00021 import logging as log
00022
00023 class CmdDb(object):
00024 """!\brief Base class for Telecommand wrapper classes.
00025
00026 This class takes a FSW generated Python module as its constructor.
00027 All package classes and its member command mnemonic methods are
00028 instantiated here.
00029 """
00030
00031 def __init__(self, vsc):
00032 """!\brief CmdDb constructor.
00033
00034 \param vsc The VSC proxy
00035 """
00036 self.__vsc = vsc
00037
00038 def getPackages(self):
00039 """!\brief Retrieve list of available packages
00040
00041 This method returns a list of available packages for the
00042 database instantiated.
00043
00044 \return List of packages.
00045 """
00046 return self.getCmdDict().mnemonics.keys()
00047
00048 def getVsc(self):
00049 return self.__vsc
00050
00051 class CmdPkg(object):
00052 """!\brief Base class for package classes
00053
00054 There is a corresponding package class for each of the packages provided by the
00055 command and telemetry database. The telecommand mnemonics are grouped by these
00056 packages and LICOS syntax requires that they be specified when queueing telecommands
00057 to the VSC.
00058
00059 See <a target="_blank"
00060 href="http://www.slac.stanford.edu/exp/glast/flight/web/a_cat/prod/WMA/hide/cmd_kwp_L_pkt.shtml">this link</a>
00061 for a list of packages and their telecommands.
00062
00063 """
00064
00065 def __init__(self, db):
00066 """!\brief CmdPkg constructor.
00067
00068 \param db Generated module containing command and
00069 telemetry database classes.
00070
00071 The constructor extracts the class corresponding to each telecommand
00072 mnemonic belonging to the package provided by the sub-class and
00073 converts it to a method member of that package subclass.
00074
00075 For example if LHK is the sub-class then the three telecommand classes
00076 from the generated LCAT_cmd_db module are converted to member methods
00077 of the LHK class.
00078
00079 Each member method contains named arguments where each argument is named
00080 after the corresponding telecommand payload. As with Python methods, the
00081 payload names are optional when passing the arguments to the telecommands.
00082 """
00083 self.__mneObj = {}
00084 self.__db = db
00085 self.__cmdCount = 0
00086
00087
00088 cmdDict = db.getCmdDict().mnemonics
00089 mneClass = self.__class__.__name__
00090 if mneClass in cmdDict:
00091 self.__cmdList = cmdDict[mneClass]
00092 else:
00093 raise RuntimeError, "%s does not exist in database %s" % (mneClass, db)
00094
00095
00096
00097
00098 for name in self.__cmdList:
00099 self.__mneObj[name] = getattr(db.getDbModule(), name)()
00100 setattr(self, name, self.__methodClosure(name))
00101
00102 def __methodClosure(self, name):
00103
00104
00105
00106 def method(*args, **kwargs):
00107
00108
00109
00110
00111 kwList = self.__mneObj[name].get_payload_list()
00112 valList = len(kwList) * [None]
00113 i = 0
00114
00115 for val in args:
00116 valList[i] = val
00117 i += 1
00118
00119 for (arg, val) in kwargs.items():
00120
00121 if arg in kwList:
00122 i = kwList.index(arg)
00123 if valList[i] is None:
00124 valList[i] = val
00125 else:
00126 raise TypeError, "%s() got multiple values for keyword argument '%s'" % (name, arg)
00127 else:
00128 raise TypeError, "%s() got an unexpected keyword argument '%s'. Available arguments are %s" % (name, arg, kwList)
00129
00130 numkw = 0
00131 for val in valList:
00132 if val is not None:
00133 numkw += 1
00134 if numkw != len(valList):
00135 raise TypeError, "%s() takes exactly %d arguments (%d given)" % (name, len(valList)+1, numkw+1)
00136
00137
00138
00139 for arg in kwList:
00140 i = kwList.index(arg)
00141 try:
00142 self.__mneObj[name].set_payload(arg, valList[i])
00143 except:
00144 log.error("Error in setting payload %s to %s" % (arg, valList[i]))
00145 raise
00146
00147
00148
00149
00150
00151
00152
00153 packet = self.__db.getVsc().executeTeleCmd(self.__mneObj[name])
00154 self.__cmdCount+=1
00155 return packet
00156 return method
00157
00158 def getPayloadValue(self, cmd, mnem):
00159 """!\brief Retrieve the payload value of a telecommand
00160
00161 This method returns the payload value as set by the last
00162 call to the telecommand method.
00163
00164 \param cmd Telecommand mnemonic.
00165 \param mnem Payload mnemonic.
00166
00167 \return Payload value
00168 """
00169 return self.__mneObj[cmd].get_payload(mnem)
00170
00171 def getPayloadDef(self, cmd, mnem):
00172 """!\brief Retrieve the payload definion
00173
00174 This method retrieves the list containing the payload definition
00175 as defined in the telecommand class.
00176
00177 \param cmd Telecommand mnemonic.
00178 \param mnem Payload mnemonic.
00179
00180 \return A list containing the payload declaration.
00181 Right now the items in this list used by LICOS are
00182 payload mnemonic (index 0) and brief (index 2).
00183
00184 """
00185 if cmd in self.__mneObj:
00186 for data in self.__mneObj[cmd].mnemList:
00187 if data[0] == mnem:
00188 return data
00189
00190 def getPayloadList(self, cmd):
00191 """!\brief Retrieve payload mnemonics
00192
00193 This method retrieves all the payload mnemonics
00194 that are needed for the telecommand \a cmd.
00195
00196 \param cmd Telecommand mnemonic.
00197
00198 \return List of payload mnemonics.
00199 """
00200 return self.__mneObj[cmd].get_payload_list()
00201
00202 def getCmds(self):
00203 """!\brief Retrieve telecommand mnemonics
00204
00205 This method retrieves all the telecommand mnemonics
00206 that are declared for this package.
00207
00208 \return List of telecommand mnemonics.
00209 """
00210 return self.__cmdList
00211
00212 def getCmdObj(self, cmd):
00213 """!\brief Retrieve the telecommand object
00214
00215 This method retrieves the telecommand object
00216 for the telecommand \a cmd. It can be used
00217 to access members not exposed by this class.
00218
00219 \param cmd Telecommand mnemonic.
00220
00221 \return An instance of a telecommand class
00222 """
00223 return self.__mneObj[cmd]
00224
00225 def getCmdDb(self):
00226 """!\brief Retrieve the module containing command database classes.
00227
00228 \return Tele-command database module
00229 """
00230 return self.__db
00231
00232 def getCmdCount(self):
00233 """!\brief Retrieve number of commands sent to the VSC
00234 since the start of the ScriptEngine
00235
00236 \return Command count for this package since the start of the ScriptEngine
00237 """
00238 return self.__cmdCount
00239
00240 def getName(self):
00241 """!\brief Retrieve the package name.
00242
00243 \return Package name
00244 """
00245 return self.__class__.__name__
00246
00247 class TlmDb(object):
00248 def __init__(self):
00249 pass