dream
56 строк · 2.0 Кб
1import logging
2import time
3from os import getenv
4
5import sentry_sdk
6
7from common.robot import check_if_command_performed
8from flask import Flask, request, jsonify
9
10
11sentry_sdk.init(getenv("SENTRY_DSN"))
12
13logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
14logger = logging.getLogger(__name__)
15
16app = Flask(__name__)
17
18ROS_FLASK_SERVER = getenv("ROS_FLASK_SERVER")
19SERVICE_PORT = int(getenv("SERVICE_PORT"))
20
21
22@app.route("/check", methods=["POST"])
23def respond():
24st_time = time.time()
25results = []
26dialogs = request.json.get("dialogs", [])
27
28for dialog in dialogs:
29command = dialog["human"]["attributes"].get("performing_command")
30
31if command:
32logger.info(f"robot_notifications: found command `{command}` sent to robot")
33result = False
34try:
35result = check_if_command_performed(command, ROS_FLASK_SERVER, dialog.get("dialog_id", "unknown"))
36except Exception as e:
37sentry_sdk.capture_exception(e)
38logger.exception(e)
39if result:
40# command was completed, so remove it from human attributes
41performed_commands = dialog["human"]["attributes"].get("performed_commands", [])
42performed_commands += [command]
43results += [
44{"human_attributes": {"performing_command": None, "performed_commands": performed_commands}}
45]
46else:
47# command is not completed, so do not update human attributes
48results += [{"human_attributes": {}}]
49logger.info(f"robot_notifications: status of command `{command}` performance: `{result}`")
50else:
51logger.info("robot_notifications: NO command found in human attributes")
52results += [{"human_attributes": {}}]
53
54total_time = time.time() - st_time
55logger.info(f"robot_notifications exec time: {total_time:.3f}s")
56return jsonify(results)
57