Skip to content

Commit

Permalink
treewide: optimize build_msg
Browse files Browse the repository at this point in the history
  • Loading branch information
stintel committed Dec 19, 2023
1 parent 5df5034 commit bd5e34e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions app/internal/was.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

def build_msg(config, container):
try:
msg = json.dumps({container: json.loads(config)}, sort_keys=True)
msg = json.dumps({container: config}, sort_keys=True)
return msg
except Exception as e:
log.error(f"Failed to build config message: {e}")
Expand Down Expand Up @@ -314,7 +314,7 @@ async def post_config(request, apply=False):
if 'hostname' in data:
hostname = data["hostname"]
data = get_config_db()
msg = build_msg(json.dumps(data), "config")
msg = build_msg(data, "config")
try:
ws = request.app.connmgr.get_client_by_hostname(hostname)
await ws.send_text(msg)
Expand All @@ -329,7 +329,6 @@ async def post_config(request, apply=False):
log.debug(f"wis_tts_url_v2: {data['wis_tts_url_v2']}")

save_config_to_db(data)
data = json.dumps(data)
msg = build_msg(data, "config")
log.debug(str(msg))
if apply:
Expand All @@ -342,7 +341,7 @@ async def post_nvs(request, apply=False):
if 'hostname' in data:
hostname = data["hostname"]
data = get_nvs_db()
msg = build_msg(json.dumps(data), "nvs")
msg = build_msg(data, "nvs")
try:
ws = request.app.connmgr.get_client_by_hostname(hostname)
await ws.send_text(msg)
Expand All @@ -352,7 +351,6 @@ async def post_nvs(request, apply=False):
return "Error"
else:
save_nvs_to_db(data)
data = json.dumps(data)
msg = build_msg(data, "nvs")
log.debug(str(msg))
if apply:
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ async def websocket_endpoint(
log.error("WAS Command Endpoint not active")

elif msg["cmd"] == "get_config":
asyncio.ensure_future(websocket.send_text(build_msg(json.dumps(get_config_db()), "config")))
asyncio.ensure_future(websocket.send_text(build_msg(get_config_db(), "config")))

elif "goodbye" in msg:
app.connmgr.disconnect(websocket)
Expand Down

0 comments on commit bd5e34e

Please sign in to comment.