ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
OcmCtl.py
Go to the documentation of this file.
1 import datetime
2 
3 from robot.api import logger
4 from robot.utils import DotDict
5 from robot.utils import timestr_to_secs
6 
7 from elt import pymal
8 from ModDaqif.Daqif import (
9  DaqReply,
10  DaqStatus,
11 )
12 from ModDaqif.Daqif.OcmDaqControl import OcmDaqControlSync
13 from ModStdif.Stdif.StdCmds import StdCmdsSync
14 
15 
16 def get_rr_uri(arg):
17  if "@" in arg:
18  # Split name@rr-uri,ps-uri
19  uris = arg.split("@")[1]
20  return uris.split(",")[0]
21  return arg
22 
23 
24 def dict_from_daqreply(r: DaqReply):
25  return DotDict(id=r.getId(), error=r.getError())
26 
27 
28 def dict_from_daqstatus(r: DaqStatus):
29  # Note: To keep compatibility of C++, return only the stem from enum attr name
30  return DotDict(
31  id=r.getId(),
32  state=str(r.getState()).split(".")[1],
33  subState=str(r.getSubState()).split(".")[1],
34  error=r.getError(),
35  result=r.getResult(),
36  message=r.getMessage(),
37  timestamp=r.getTimestamp(),
38  )
39 
40 
41 class Clients:
42  daq = None
43  std = None
44 
45 
46 class OcmCtl:
47  """Robot library providing keywords for the daqif.OcmDaqControl and stdif interface"""
48 
49  # Global scope is necessary since MAL will shutdown protobuf library randomly
50  # otherwise, which eventually lead to segfaults. See ECII-304.
51  ROBOT_LIBRARY_SCOPE = "GLOBAL"
52 
53  def __init__(self):
54  logger.debug("Loading MAL and creating factory")
55  self.mal = pymal.loadMal("zpb", {})
56  self.factory = pymal.CiiFactory.getInstance()
57  self.factory.registerMal("zpb", self.mal)
58  logger.debug("Loading MAL and creating factory done")
59 
60  def ocmctl_create(self, root_uri, timeout=None):
61  """Creates RR clientss with a given ReplyTime timeout."""
62  timeout = timestr_to_secs(timeout) if timeout is not None else 5
63  root_uri = get_rr_uri(root_uri)
64  logger.info("Create client for uri %s with timeout %d" % (root_uri, timeout))
65  clients = Clients()
66  clients.daq = self.factory.getClient(
67  "%s/daq" % root_uri,
68  OcmDaqControlSync,
69  pymal.rr.qos.ReplyTime(datetime.timedelta(seconds=timeout)),
70  )
71  clients.std = self.factory.getClient(
72  "%s/std" % root_uri,
73  StdCmdsSync,
74  pymal.rr.qos.ReplyTime(datetime.timedelta(seconds=timeout)),
75  )
76  return clients
77 
78  def ocmctl_daq_get_status(self, clients: Clients, daq_id: str):
79  logger.info("OcmDaqControl.GetStatus(%s): Sending request", daq_id)
80  r = clients.daq.GetStatus(daq_id)
81  logger.info("OcmDaqControl.GetStatus(%s): Got reply", daq_id)
82  return dict_from_daqstatus(r)
83 
84  def ocmctl_daq_forceabort(self, clients: Clients, daq_id: str):
85  logger.info("OcmDaqControl.ForceAbort(%s): Sending request", daq_id)
86  r = clients.daq.ForceAbortDaq(daq_id)
87  logger.info("OcmDaqControl.ForceAbort(%s): Got reply", daq_id)
88  return dict_from_daqreply(r)
89 
90  def ocmctl_std_get_state(self, clients: Clients):
91  logger.info("StdCmds.GetState(): Sending request")
92  r = clients.std.GetState()
93  logger.info("StdCmds.GetState(): Got reply")
94  return r
95 
96  def ocmctl_daq_get_active_list(self, clients: Clients):
97  logger.info("OcmDaqControl.GetActive(): Sending request")
98  daqs = clients.daq.GetActiveList()
99  logger.info("OcmDaqControl.GetActive(): Got reply")
100  return [dict_from_daqstatus(daq) for daq in daqs]
OcmCtl.OcmCtl.factory
factory
Definition: OcmCtl.py:56
OcmCtl.OcmCtl.ocmctl_daq_get_active_list
def ocmctl_daq_get_active_list(self, Clients clients)
Definition: OcmCtl.py:96
OcmCtl.dict_from_daqstatus
def dict_from_daqstatus(DaqStatus r)
Definition: OcmCtl.py:28
OcmCtl.OcmCtl.ocmctl_daq_get_status
def ocmctl_daq_get_status(self, Clients clients, str daq_id)
Definition: OcmCtl.py:78
OcmCtl.OcmCtl.ocmctl_create
def ocmctl_create(self, root_uri, timeout=None)
Creates RR clientss with a given ReplyTime timeout.
Definition: OcmCtl.py:61
OcmCtl.OcmCtl
Robot library providing keywords for the daqif.OcmDaqControl and stdif interface.
Definition: OcmCtl.py:47
OcmCtl.get_rr_uri
def get_rr_uri(arg)
Definition: OcmCtl.py:16
OcmCtl.Clients
Definition: OcmCtl.py:41
OcmCtl.OcmCtl.ocmctl_daq_forceabort
def ocmctl_daq_forceabort(self, Clients clients, str daq_id)
Definition: OcmCtl.py:84
OcmCtl.OcmCtl.__init__
def __init__(self)
Definition: OcmCtl.py:53
OcmCtl.OcmCtl.mal
mal
Definition: OcmCtl.py:55
OcmCtl.dict_from_daqreply
def dict_from_daqreply(DaqReply r)
Definition: OcmCtl.py:24
OcmCtl.OcmCtl.ocmctl_std_get_state
def ocmctl_std_get_state(self, Clients clients)
Definition: OcmCtl.py:90