Skip to content

Commit

Permalink
Merge pull request #59 from openEDI/jm/broker_status
Browse files Browse the repository at this point in the history
Add status REST command to broker
  • Loading branch information
josephmckinsey authored Mar 20, 2024
2 parents ff49f17 + caf81fc commit 62c418a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions broker/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from fastapi.exceptions import HTTPException
from fastapi.responses import FileResponse, JSONResponse
from oedisi.types.common import HeathCheck, ServerReply
from oedisi.tools.broker_utils import get_time_data

app = FastAPI()

Expand Down Expand Up @@ -143,6 +144,7 @@ def run_simulation():
initstring = f"-f {len(component_map)} --name=mainbroker --loglevel=trace --local_interface={broker_ip} --localport=23404"
logging.info(f"Broker initaialization string: {initstring}")
broker = h.helicsCreateBroker("zmq", "", initstring)
app.state.broker = broker
logging.info(broker)
isconnected = h.helicsBrokerIsConnected(broker)
logging.info(f"Broker connected: {isconnected}")
Expand Down Expand Up @@ -177,6 +179,22 @@ async def run_feeder(background_tasks: BackgroundTasks):
raise HTTPException(status_code=404, detail=str(err))


@app.get("/status/")
async def status():
try:
name_2_timedata = {}
connected = h.helicsBrokerIsConnected(app.state.broker)
if connected:
for time_data in get_time_data(app.state.broker):
if (time_data.name not in name_2_timedata) or (
name_2_timedata[time_data.name] != time_data
):
name_2_timedata[time_data.name] = time_data
return {"connected": connected, "timedata": name_2_timedata, "error": False}
except AttributeError as e:
return {"reply": str(e), "error": True}


if __name__ == "__main__":
port = int(sys.argv[2])
uvicorn.run(app, host="0.0.0.0", port=port)

0 comments on commit 62c418a

Please sign in to comment.