dream

Форк
0
56 строк · 2.0 Кб
1
import logging
2
import time
3
from os import getenv
4

5
import sentry_sdk
6

7
from common.robot import check_if_command_performed
8
from flask import Flask, request, jsonify
9

10

11
sentry_sdk.init(getenv("SENTRY_DSN"))
12

13
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
14
logger = logging.getLogger(__name__)
15

16
app = Flask(__name__)
17

18
ROS_FLASK_SERVER = getenv("ROS_FLASK_SERVER")
19
SERVICE_PORT = int(getenv("SERVICE_PORT"))
20

21

22
@app.route("/check", methods=["POST"])
23
def respond():
24
    st_time = time.time()
25
    results = []
26
    dialogs = request.json.get("dialogs", [])
27

28
    for dialog in dialogs:
29
        command = dialog["human"]["attributes"].get("performing_command")
30

31
        if command:
32
            logger.info(f"robot_notifications: found command `{command}` sent to robot")
33
            result = False
34
            try:
35
                result = check_if_command_performed(command, ROS_FLASK_SERVER, dialog.get("dialog_id", "unknown"))
36
            except Exception as e:
37
                sentry_sdk.capture_exception(e)
38
                logger.exception(e)
39
            if result:
40
                # command was completed, so remove it from human attributes
41
                performed_commands = dialog["human"]["attributes"].get("performed_commands", [])
42
                performed_commands += [command]
43
                results += [
44
                    {"human_attributes": {"performing_command": None, "performed_commands": performed_commands}}
45
                ]
46
            else:
47
                # command is not completed, so do not update human attributes
48
                results += [{"human_attributes": {}}]
49
            logger.info(f"robot_notifications: status of command `{command}` performance: `{result}`")
50
        else:
51
            logger.info("robot_notifications: NO command found in human attributes")
52
            results += [{"human_attributes": {}}]
53

54
    total_time = time.time() - st_time
55
    logger.info(f"robot_notifications exec time: {total_time:.3f}s")
56
    return jsonify(results)
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.