3 @ingroup daq_sim_metadaqsim
4 @copyright 2021 ESO - European Southern Observatory
6 @brief Implements simulation of metadaqif
15 from ModMetadaqif.Metadaqif
import (
23 from ModMetadaqif.Metadaqif.MetaDaq
import MetaDaqSyncService
24 from ModDaqsimif.Daqsimif
import Error
as SimCtlError
28 """Holds simulated status for DAQ
33 self.
state = DaqState.NotStarted
39 """Simulator that implements Modmetadaqsimif and sets up
40 simulation behaviour for Simulator.
42 Registered hooks are run once and then removed before executed.
43 Each hook is a callable that takes the result from the default implementation
44 as well as the arguments given to the original implementation.
45 The hook can then choose to modify the existing result or replace it.
56 def StartDaq(self, daq_id: str) -> DaqReply:
57 """@implements MetaDaq.StartDaq
59 logging.info(
"Request: StartDaq(%s)", daq_id)
60 if daq_id
in self.
daqs:
61 raise DaqException(daq_id,
"Data acquisition with id already exist")
63 daq.state = DaqState.Acquiring
64 self.
daqs[daq_id] = daq
68 def StopDaq(self, daq_id: str) -> DaqStopReply:
69 """@implements MetaDaq.StopDaq
71 logging.info(
"Request: StopDaq(%s)", daq_id)
73 daq.files = [
"/tmp/%s.fits" % daq_id]
77 def AbortDaq(self, daq_id: str) -> DaqReply:
78 """@implements MetaDaq.AbortDaq
80 logging.info(
"Request: AbortDaq(%s)", daq_id)
82 daq.state = DaqState.Failed
83 self.
daqs[daq_id] = daq
88 """@implements MetaDaq.GetDaqStatus
90 logging.info(
"Request: GetDaqStatus(%s)", daq_id)
94 def get_daq(self, daq_id) -> Daq:
95 """Get daq or raise DaqException.
97 if daq_id
in self.
daqs:
98 return self.
daqs[daq_id]
99 raise DaqException(daq_id,
"Unknown daq id '%s'" % daq_id)
102 logging.debug(
"Creating return type: DaqReply")
103 reply = self.
mal.createDataEntity(DaqReply)
105 logging.debug(
"Creating return type: Done")
109 """Create DaqStatus instance
112 status = self.
mal.createDataEntity(DaqStatus)
114 status.setState(daq.state)
115 status.setFiles(daq.files)
116 status.setTimestamp(calendar.timegm())
120 """Create DaqStopReply instance
123 reply = self.
mal.createDataEntity(DaqStopReply)
125 reply.setFiles(daq.files)
126 reply.setKeywords(daq.keywords)
130 """Add the simulation hook `hook` for the specified command `cmd_name` (e.g.
133 Any previous hooks will be replaced.
138 """Reset simulator to initial default state.
145 """Remove all simulation hooks.
150 """Runs simulation hook that may modify or replace the default implementation.
152 The hook to execute is automatically determined from the call stack, so
153 this should only ever be executed directly from the service command
154 implementation method.
156 cmd_name = inspect.stack()[1][3]
158 logging.debug(
"Checking for hook for command %s", cmd_name)
160 logging.debug(
"Running simulation hook for command %s", cmd_name)
162 return hook(result, *args)
167 """Simulator controller that implements Modmetadaqsimif and sets up
168 simulation behaviour for Simulator.
177 logging.info(
"Registering service 'daq'")
178 self.
server.registerService(
"daq", MetaDaqSyncService, self.
sim)
180 def Setup(self, spec):
181 """@implements SimCtl.Setup.
183 logging.info(
'SimCtl.Setup: spec="%s"', spec)
186 cmd = s.get(
"command")
187 action = s.get(
"action")
188 if action ==
"throw":
189 def throw(res, *args):
190 raise DaqException(self.
sim.current_daq.id,
"Simulated error")
191 self.
sim.add_sim_hook(cmd, throw)
193 raise SimCtlError(
"Unknown action '%s' provided" % action)
194 logging.info(
'CimCtl.Setup: Returning empty string')
195 return json.dumps(
"")
198 except Exception
as e:
199 raise SimCtlError(
"\"%s\"" % str(e))
202 """@implements SimCtl.Reset
206 except Exception
as e:
207 raise SimCtlError(str(e))
210 """@implements SimCtl.ForceExit.
213 logging.warning(
"Force Exiting without cleanup!")