ifw-daq  1.0.0
IFW Data Acquisition modules
entrypoint.py
Go to the documentation of this file.
1 """
2 @file
3 @ingroup daq_recififsim
4 @copyright 2021 ESO - European Southern Observatory
5 """
6 # pylint: disable=no-name-in-module,import-error,invalid-name
7 import argparse
8 import signal
9 import logging
10 import threading
11 import elt.pymal as pymal
12 
13 from ModDaqsimif.Daqsimif.SimCtl import SimCtlSyncService
14 
15 from . import __version__
16 from . import simulator
17 
18 
19 def LoadMalForUri(uri, malSpecificProperties):
20  """Load MAL mapping based on the first three characters of the uri
21  @param uri string uri
22  @param malSpecificProperties map of properties to use to initialize mapping
23  @return reference to mal.CiiFactory instance
24  @throw RuntimeError when mapping could not be loaded
25  """
26  scheme = uri[0:3]
27  malName = scheme
28  if malName == 'opc':
29  malName = 'opcua'
30  mal = pymal.loadMal(malName, malSpecificProperties)
31  factory = pymal.CiiFactory.getInstance()
32  factory.registerMal(scheme, mal)
33  return mal, factory
34 
35 
36 def main(args): # pylint: disable=unused-argument,too-many-locals
37  """Main entrypoint for simulator
38  """
39  logger = logging.getLogger("recifsim")
40  parser = argparse.ArgumentParser()
41  parser.add_argument(
42  "--version",
43  action="version",
44  version="%(prog)s " "{version}".format(version=__version__),
45  help="print version number and exit",
46  )
47  parser.add_argument(
48  "-v",
49  "--verbose",
50  action="count",
51  default=0,
52  help="set the verbosity level: -v INFO, -vv DEBUG",
53  )
54  parser.add_argument(
55  "name",
56  help="server process name/id",
57  )
58  parser.add_argument(
59  "uri",
60  help="server URI where simulator will accept requests, without the service path",
61  )
62 
63  # Parse basic arguments to set up logging
64  main_args, _ = parser.parse_known_args()
65  verbose = main_args.verbose
66  level = logging.WARNING
67  if verbose >= 1:
68  level = logging.INFO
69  if verbose >= 2:
70  level = logging.DEBUG
71  # Configure console logging:
72  if main_args.verbose > 0 and False:
73  chdlr = logging.StreamHandler()
74  if verbose == 1:
75  chdlr.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
76  else:
77  chdlr.setFormatter(
78  logging.Formatter("%(name)s: %(levelname)s: %(message)s")
79  )
80  chdlr.setLevel(level)
81  logging.getLogger().addHandler(chdlr)
82 
83  logging.basicConfig(level=level, format="%(levelname)s: %(message)s")
84  logger.info("Started recifsim %s", __version__)
85 
86  name = main_args.name
87  uri = main_args.uri
88  mal, factory = LoadMalForUri(uri, {})
89 
90  logger.info("Creating server mal")
91  with factory.createServer(uri, pymal.rr.qos.DEFAULT, {}) as server:
92  try:
93  def shutdown(signum, frame): # pylint: disable=unused-argument
94  logger.info("Closing server due to signal %d", signum)
95  server.close()
96 
97  signal.signal(signal.SIGINT, shutdown)
98  signal.signal(signal.SIGTERM, shutdown)
99 
100  sim_ctl = simulator.SimulatorCtl(name, mal, server)
101 
102  try:
103  logger.info("Registering service 'simctl' on %s", uri)
104  server.registerService("simctl", SimCtlSyncService, sim_ctl)
105 
106  logger.info("Running server...")
107 
108  # Run mal in different thread to avoid it stealing signals.
109  # In addition it deadlocks when trying to shut down.
110  def run():
111  server.run()
112 
113  thr = threading.Thread(target=run, daemon=True)
114  thr.start()
115  thr.join()
116  logger.info("Server done.")
117  return 0
118  except Exception: # pylint: disable=bare-except
119  logging.exception("Exception")
120  return -2
121  except Exception:
122  logging.exception("Exception")
123  return -1
recifsim.entrypoint.main
def main(args)
Main entrypoint for simulator.
Definition: entrypoint.py:38
recifsim.simulator.SimulatorCtl
Simulator controller that implements ModDaqsimif and sets up simulation behaviour for Simulator.
Definition: simulator.py:230
recifsim.entrypoint.LoadMalForUri
def LoadMalForUri(uri, malSpecificProperties)
Load MAL mapping based on the first three characters of the uri.
Definition: entrypoint.py:25