Skip to content

Commit

Permalink
Add model.getAllSignals to python api (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Mar 17, 2022
1 parent c803efc commit 47fd5e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/OMSimulatorPython/Model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing
import xml.etree.ElementTree as ET

from OMSimulator import Scope, System, Types


Expand Down Expand Up @@ -61,6 +64,17 @@ def reset(self):
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

def getAllSignals(self):
allSignals = {}
signalFilter = self.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']
allSignals[name] = {'type': type_, 'kind': kind}
return allSignals

def getBoolean(self, cref: str):
value, status = Scope._capi.getBoolean(self.cref + '.' + cref)
if Types.Status(status) != Types.Status.OK:
Expand Down
18 changes: 5 additions & 13 deletions src/OMSimulatorServer/OMSimulatorServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import threading
import time
import xml.etree.ElementTree as ET

import OMSimulator as oms
import zmq
Expand All @@ -27,10 +26,13 @@ def close_socket_gracefully(socket):

def mogrify(topic: str, msg: dict):
'''Combines a topic identifier and a json representation of a dictionary'''
return '%s %s' % (topic, json.dumps(msg))
return f'{topic} {json.dumps(msg)}'

class Server:
def __init__(self, model, result_file, interactive, endpoint_pub, endpoint_rep):
self.print(f'OMS Server {__version__}')
self.print(f'ZMQ {zmq.zmq_version()}')

self._alive = True
self._context = zmq.Context()
self._model = oms.importFile(model)
Expand All @@ -45,17 +47,7 @@ def __init__(self, model, result_file, interactive, endpoint_pub, endpoint_rep):
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()))
self._signals = self._model.getAllSignals()

if interactive and not endpoint_rep:
self.print('flag --endpoint-rep is mandatory in interactive simulation mode')
Expand Down

0 comments on commit 47fd5e0

Please sign in to comment.