Skip to content

Commit

Permalink
Send simulation results for interactive simulations (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Aug 28, 2021
1 parent 014799c commit 738b794
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/OMSimulatorPython/NewAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ def setLogFile(path: str, size=None) -> None:
raise Exception('error {}'.format(Types.Status(status)))
if size:
setMaxLogFileSize(size)

def getReal(cref: str) -> float:
value, status = Scope._capi.getReal(cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return value

def getInteger(cref: str) -> int:
value, status = Scope._capi.getInteger(cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return value

def getBoolean(cref: str) -> bool:
value, status = Scope._capi.getBoolean(cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return value
38 changes: 28 additions & 10 deletions src/OMSimulatorServer/OMSimulatorServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,21 @@ def __init__(self, model, result_file, interactive, endpoint_pub, endpoint_rep):
self._socket_rep = None
self._socket_pub = None
self._thread = None
self._activeSignals = set()

if result_file:
self._model.resultFile = result_file

# extract all available signals
self._signals = {}
signalFilter = self._model.exportSnapshot(':resources/signalFilter.xml')
root = ET.fromstring(signalFilter)
for var in root[0][0]:
name = var.attrib['name']
type_ = var.attrib['type']
kind = var.attrib['kind']
self._signals[name] = {'type': type_, 'kind': kind}

self.print('OMS Server {}'.format(__version__))
self.print('ZMQ {}'.format(zmq.zmq_version()))

Expand All @@ -65,16 +76,6 @@ def __init__(self, model, result_file, interactive, endpoint_pub, endpoint_rep):
self._thread = threading.Thread(target=self._main, daemon=True)
self._thread.start()

# extract all available signals
self._signals = {}
signalFilter = self._model.exportSnapshot(':resources/signalFilter.xml')
root = ET.fromstring(signalFilter)
for var in root[0][0]:
name = var.attrib['name']
type_ = var.attrib['type']
kind = var.attrib['kind']
self._signals[name] = {'type': type_, 'kind': kind}

def print(self, msg):
print('server: {}'.format(msg), flush=True)

Expand Down Expand Up @@ -117,6 +118,12 @@ def _main(self):
arg = msg['arg']
if 'available' == arg:
answer = {'status': 'ack', 'result': self._signals}
elif 'enable' == arg:
self._activeSignals.add(msg['cref'])
answer = {'status': 'ack'}
elif 'disable' == arg:
self._activeSignals.remove(msg['cref'])
answer = {'status': 'ack'}

try:
self._socket_rep.send_json(answer)
Expand All @@ -140,6 +147,17 @@ def run(self):
else:
progress = math.floor((time_-startTime) / (stopTime-startTime) * 100)
self.pub_msg('status', {'progress': progress})
if self._activeSignals:
results = {'time': time_}
for signal in self._activeSignals:
type_ = self._signals[signal]['type']
if type_ == 'Real':
results[signal] = oms.getReal(signal)
elif type_ == 'Integer':
results[signal] = oms.getInteger(signal)
elif type_ == 'Boolean':
results[signal] = oms.getBoolean(signal)
self.pub_msg('results', results)
with self._mutex:
self._model.doStep()
time_ = self._model.time
Expand Down

0 comments on commit 738b794

Please sign in to comment.