diff --git a/Configurations/default.cfg b/Configurations/default.cfg new file mode 100644 index 0000000000000000000000000000000000000000..1841bf1ffbb817a7c0e0c79b67c613252fbd3be0 --- /dev/null +++ b/Configurations/default.cfg @@ -0,0 +1,92 @@ +[combpm] + +[combpm.cellnode-c01] +bpmfilter = 1 2 3 4 5 6 7 8 9 10 + 11 12 13 14 15 16 17 18 19 20 + 21 22 115 116 117 118 119 120 121 122 + +[combpm.cellnode-c06] +bpmfilter = 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 + 43 44 45 46 47 48 49 50 51 52 + +[combpm.cellnode-c09] +bpmfilter = 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 + 73 74 75 76 77 78 79 80 81 82 + +[combpm.cellnode-c14] +bpmfilter = 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 + 103 104 105 106 107 108 109 110 111 112 113 114 + +[ccn] +npsc = 100 +nbpm = 30 + +[ccn.cellnode-c14] +nbpm = 32 + + +[comcorr.cellnode-c01] +pscid = 50 100 49 99 + 255 255 255 255 + 2 52 1 51 + 255 255 255 255 + 56 6 3 53 + 4 54 5 55 + 10 60 7 57 + 8 58 9 59 + +[comcorr.cellnode-c06] +pscid = 12 62 11 61 + 255 255 255 255 + 14 64 13 63 + 255 255 255 255 + 68 18 15 65 + 16 66 17 67 + 22 72 19 69 + 20 70 21 71 + +[comcorr.cellnode-c09] +pscid = 24 74 23 73 + 255 255 255 255 + 26 76 25 75 + 255 255 255 255 + 80 30 27 77 + 28 78 29 79 + 34 84 31 81 + 32 82 33 83 + +[comcorr.cellnode-c14] +pscid = 36 86 35 85 + 37 87 38 88 + 40 90 39 89 + 255 255 255 255 + 94 44 41 91 + 42 92 43 93 + 48 98 45 95 + 46 96 47 97 + +[corr] +k1a_x = 256 +k1b_x = 0 +k1ic_x = 64 +k1d_x = 16300 +k1a_y = 256 +k1b_y = 0 +k1ic_y = 64 +k1d_y = 16300 +k2a_x = 128 +k2b_x = 0 +k2ic_x = 8192 +k2d_x = 0 +k2a_y = 128 +k2b_y = 0 +k2ic_y = 8192 +k2d_y = 0 +pscid = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 + 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 + 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 + 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 +numbpm = 122 diff --git a/FofbTool/CommandLineInterface.py b/FofbTool/CommandLineInterface.py index 30d364ece2436f10f46af55311b2ee917bd07191..b50d5859c99c9f0f38321e3306ad91b6ea8fac3e 100755 --- a/FofbTool/CommandLineInterface.py +++ b/FofbTool/CommandLineInterface.py @@ -32,6 +32,9 @@ if __name__ == '__main__': parser.add_argument("--log", default="info", help="Log level (error, warning, info, debug)") + parser.add_argument("--conf-file", type=str, + help="Path to config file to apply.") + parser.add_argument("--DS-init-opcua", action="store_true", help="Run init on opcua devices.") parser.add_argument("--DS-init-watcher", action="store_true", @@ -65,6 +68,10 @@ if __name__ == '__main__': logger.debug(args) + if "conf_file" in args: + FofbTool.Configuration.loadconfig(args.conf_file) + FofbTool.Configuration.logdebugconf() + ## Device Server related commands if args.DS_init_opcua: FofbTool.Utils.init_opcua() diff --git a/FofbTool/Configuration.py b/FofbTool/Configuration.py index a6e5065f27a1976122830ceae55260431384a93d..0b67c3c05785570ae04983d149986082ce490c54 100755 --- a/FofbTool/Configuration.py +++ b/FofbTool/Configuration.py @@ -8,13 +8,95 @@ import tango import logging +import configparser import numpy as np import FofbTool.Utils +import os # Get the module logger logger = logging.getLogger("FofbTool") +################################################################################################### +# CONFIGURATION PARSER +################################################################################################### + +# Configuration +config = configparser.ConfigParser() + +# Try to load default +defconfpath = os.path.abspath(os.path.join(os.path.dirname(__spec__.origin), "../Configurations/default.cfg")) +if len(config.read(defconfpath)) < 1: + logger.warning("Default configuration file not found: {}".format(defconfpath)) + +def logdebugconf(): + logger.debug("Display config:") + for s in config.sections(): + for o in config.options(s): + logger.debug("{:20} {:10} {}".format(s,o, config[s][o])) + +def loadconfig(filepath): + """ + Load a configuration file for the module. + It initialize the internal configuration. + + PARAMETERS + ---------- + filepath: str + Path to the config file + """ + global config + logger.info("Load configuration from file {}".format(filepath)) + if len(config.read(filepath)) < 1: + logger.warning("Configuration file not found: {}".format(filepath)) + + +def getconf(option, section, nodename="", t="", l=False): + """ + Helper method to retrieve an option in the loaded configuration. + It tries to find a section named section.nodename and fall back to section only if fails. + + PARAMETERS + ---------- + option: str + Name of the option to retrieve + section: str + Section in wich to search + nodename: str + Specify a particular nodename. If not found in config, fall back to section default. + t: str ('i', 'f', 'b', '') + Give the type to cast, integer, float, boolean or string. + l: boolean + Cast from a list + + RETURNS + ------- + roption: str, int, float or boolean + Read option + """ + if l: + if t=='b': + raise NotImplemented("List of boolean not implemented") + try: + val= np.asarray(config.get("{}.{}".format(section,nodename), option).split(), dtype=t) + except (configparser.NoSectionError, configparser.NoOptionError): + val= np.asarray(config.get(section, option).split(), dtype=t) + + else: + try: + func = getattr(config, {'i':"getint", 'f':"getfloat", 'b':"getboolean", '':"get"}[t]) + except KeyError as e: + log.debug(str(e)) + raise ValueError("Allowed t argument are 'i', 'f', 'b' or ''") + + try: + val= func("{}.{}".format(section,nodename), option) + except (configparser.NoSectionError, configparser.NoOptionError): + val= func(section, option) + + return val + + ################################################################################################### # CONFIGURE COM BPM @@ -31,7 +113,7 @@ bpmfilterlist = { def cellnode_configure_combpm(cellnodename, bpmallowed=None): """ Configure the combpm block of a CellNode. - The BPM data allowed through are either taken in the argument, or default to nominal values. + The BPM data allowed through are either taken in the argument, or default to configuration. PARAMETERS @@ -64,7 +146,7 @@ def cellnode_configure_combpm(cellnodename, bpmallowed=None): # Select BPM id allowed to pass through if bpmallowed is None: logger.debug("Default to nominal values for BPM filter") - bpmallowed = bpmfilterlist[cellnodename.lower()] + bpmallowed = getconf("bpmfilter", "combpm", cellnodename.lower(), 'i', True) logger.debug("Activate BPMs {} in the filter for {}".format(bpmallowed, p)) @@ -82,7 +164,7 @@ def cellnode_configure_combpm(cellnodename, bpmallowed=None): ################################################################################################### -def cellnode_configure_ccn(cellnodename, nbpm=None, npsc=100): +def cellnode_configure_ccn(cellnodename, nbpm=None, npsc=None): """ Configure the ComCellNode block on a cellnode. Automatically set the number of bpm/psc packets and MAC length. @@ -119,9 +201,15 @@ def cellnode_configure_ccn(cellnodename, nbpm=None, npsc=100): return False if nbpm is None: + logger.debug("Default to nominal values for number of BPM packets") # Get number of BPM activated in the filter - nbpm = int((prx["combpm_filter_table"].value == 0x80).sum()) - logger.debug("{} bpm allowed in the filter on {}".format(nbpm, p)) + nbpm = getconf("nbpm", "ccn", cellnodename, 'i') + logger.debug("{} bpm allowed in the ethernet frame on {}".format(nbpm, p)) + + if npsc is None: + logger.debug("Default to nominal values for number of PSC packets") + npsc = getconf("npsc", "ccn", cellnodename, 'i') + logger.debug("{} psc expected in the ethernet frame on {}".format(nbpm, p)) maclen = 10*nbpm+10 logger.debug("Configure packeter framesize (mac length) to {}".format(maclen)) @@ -137,7 +225,7 @@ def cellnode_configure_ccn(cellnodename, nbpm=None, npsc=100): logger.info("Configuration of CCN done on {}.".format(p)) return True -def centralnode_configure_ccn(nbpm=[30,30,30,32], npsc=100): +def centralnode_configure_ccn(nbpm=None, npsc=None): """ Configure the ComCellNode block on the centralnode. Automatically set the number of bpm/psc packets and MAC length. @@ -162,6 +250,17 @@ def centralnode_configure_ccn(nbpm=[30,30,30,32], npsc=100): logger.debug(str(e)) return False + if nbpm is None: + logger.debug("Use default value for nbpm") + nbpm = [getconf("nbpm", "ccn", n, 'i') for n in FofbTool.Utils.tangopath_cellnodes.keys()] + logger.debug("{} bpm expected in the ethernet frame on {}".format(nbpm, p)) + + if npsc is None: + logger.debug("Default to nominal values for number of BPM packets") + # Get number of BPM activated in the filter + npsc = getconf("npsc", "ccn", 'centralnode', 'i') + logger.debug("{} psc allowed in the ethernet frame on {}".format(nbpm, p)) + for n in range(4): maclen = npsc*6+10 logger.debug("Configure packeter {} framesize (mac length) to {}".format(n, maclen)) @@ -181,13 +280,6 @@ def centralnode_configure_ccn(nbpm=[30,30,30,32], npsc=100): # CONFIGURE COM CORR ################################################################################################### -pscidlist = { - 'cellnode-c01':[50,100,49,99,255,255,255,255,2,52,1,51,255,255,255,255,56,6,3,53,4,54,5,55,10,60,7,57,8,58,9,59], - 'cellnode-c06':[12,62,11,61,255,255,255,255,14,64,13,63,255,255,255,255,68,18,15,65,16,66,17,67,22,72,19,69,20,70,21,71], - 'cellnode-c09':[24,74,23,73,255,255,255,255,26,76,25,75,255,255,255,255,80,30,27,77,28,78,29,79,34,84,31,81,32,82,33,83], - 'cellnode-c14':[36,86,35,85,37,87,38,88,40,90,39,89,255,255,255,255,94,44,41,91,42,92,43,93,48,98,45,95,46,96,47,97] - } - def cellnode_configure_comcorr(cellnodename, pscid=None, enable=True): """ Configure the comcorr block of a CellNode. @@ -226,7 +318,7 @@ def cellnode_configure_comcorr(cellnodename, pscid=None, enable=True): if pscid is None: logger.debug("Default to nominal values for PSCID") - pscid = pscidlist[cellnodename.lower()] + pscid = getconf("pscid", "comcorr", cellnodename, 'i', True) else: if len(pscid) != 32: logger.error("pscid shall have length 32") @@ -260,32 +352,32 @@ def centralnode_configure_corr(): return False # Legacy - prx["corr_k1a_x"]=256 - prx["corr_k1b_x"]=0 - prx["corr_k1ic_x"]=64 - prx["corr_k1d_x"]=16300 + prx["corr_k1a_x"]=getconf( "k1a_x", "corr", t='i') + prx["corr_k1b_x"]=getconf( "k1b_x", "corr", t='i') + prx["corr_k1ic_x"]=getconf( "k1ic_x", "corr", t='i') + prx["corr_k1d_x"]=getconf( "k1d_x", "corr", t='i') - prx["corr_k1a_y"]=256 - prx["corr_k1b_y"]=0 - prx["corr_k1ic_y"]=64 - prx["corr_k1d_y"]=16300 + prx["corr_k1a_y"]=getconf( "k1a_y", "corr", t='i') + prx["corr_k1b_y"]=getconf( "k1b_y", "corr", t='i') + prx["corr_k1ic_y"]=getconf( "k1ic_y", "corr", t='i') + prx["corr_k1d_y"]=getconf( "k1d_y", "corr", t='i') # Unitary - prx["corr_k2a_x"]=128 - prx["corr_k2b_x"]=0 - prx["corr_k2ic_x"]=8192 - prx["corr_k2d_x"]=0 + prx["corr_k2a_x"]=getconf( "k2a_x", "corr", t='i') + prx["corr_k2b_x"]=getconf( "k2b_x", "corr", t='i') + prx["corr_k2ic_x"]=getconf( "k2ic_x", "corr", t='i') + prx["corr_k2d_x"]=getconf( "k2d_x", "corr", t='i') - prx["corr_k2a_y"]=128 - prx["corr_k2b_y"]=0 - prx["corr_k2ic_y"]=8192 - prx["corr_k2d_y"]=0 + prx["corr_k2a_y"]=getconf( "k2a_y", "corr", t='i') + prx["corr_k2b_y"]=getconf( "k2b_y", "corr", t='i') + prx["corr_k2ic_y"]=getconf( "k2ic_y", "corr", t='i') + prx["corr_k2d_y"]=getconf( "k2d_y", "corr", t='i') f= prx["corr_pscid"].value - f[0:100] = range(1,101) + f[0:100] = getconf('pscid', 'corr', t='i', l=True) prx["corr_pscid"] = f - prx["corr_num_bpm"] = 122 + prx["corr_num_bpm"] = getconf('numbpm', 'corr', t='i') logger.info("Configuration of Corr done on {}.".format(p)) return True