~aleteoryx/muditaos

ref: 767c1f22868b27cec54b6f2c45ac0f34d37a344b muditaos/module-services/service-desktop/parser/MessageHandler.cpp -rw-r--r-- 2.3 KiB
767c1f22 — KacperLewandowski [EGD-3314] PR changes 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "MessageHandler.hpp"
#include "Contacts.hpp"
#include "EndpointHandler.hpp"
#include "log/log.hpp"
#include "ParserUtils.hpp"

using namespace ParserStateMachine;

xQueueHandle MessageHandler::sendQueue;

MessageHandler::MessageHandler(std::string &message, sys::Service *OwnerService) : OwnerServicePtr(OwnerService)
{
    try {
        messageJson = json11::Json::parse(message, JsonErrorMsg);
    }
    catch (const std::exception &e) {
        LOG_ERROR("Cannot create MessageHandler! err:%s", e.what());
    }
}

void MessageHandler::processMessage()
{
    auto body          = messageJson[json::body];
    int endpoint       = messageJson[json::endpoint].number_value();
    unsigned long uuid = messageJson[json::uuid].number_value();
    int method         = messageJson[json::method].number_value();

    LOG_DEBUG(
        "[MsgHandler]\nmethod: %d\nendpoint: %d\nuuid: %lu\nbody: %s\n", method, endpoint, uuid, body.dump().c_str());

    std::string responseStr;

    switch (static_cast<Endpoint>(endpoint)) {
    case Endpoint::deviceInfo:
        EndpointHandler::deviceInfo(method, uuid, body, responseStr, OwnerServicePtr);
        LOG_DEBUG("[ENDPOINT] DeviceInfo: %s", responseStr.c_str());
        putToSendQueue(responseStr);
        break;
    case Endpoint::update:
        EndpointHandler::update(method, uuid, body, responseStr, OwnerServicePtr);
        LOG_DEBUG("[ENDPOINT] Update: %s", responseStr.c_str());
        putToSendQueue(responseStr);
        break;
    case Endpoint::backup:
        EndpointHandler::backup(method, uuid, body, responseStr, OwnerServicePtr);
        LOG_DEBUG("[ENDPOINT] Backup: %s", responseStr.c_str());
        putToSendQueue(responseStr);
        break;
    case Endpoint::restore:
        EndpointHandler::restore(method, uuid, body, responseStr, OwnerServicePtr);
        LOG_DEBUG("[ENDPOINT] Restore: %s", responseStr.c_str());
        putToSendQueue(responseStr);
        break;
    case Endpoint::contacts:
        EndpointHandler::contacts(method, uuid, body, responseStr, OwnerServicePtr);
        break;
    default:
        break;
    }
}

void MessageHandler::putToSendQueue(const std::string msg)
{
#if defined(TARGET_RT1051)
    if (uxQueueSpacesAvailable(sendQueue) != 0) {
        auto *responseString = new std::string(msg);
        xQueueSend(sendQueue, &responseString, portMAX_DELAY);
    }
#endif
}