summaryrefslogtreecommitdiffstats
path: root/homescreen/src/homescreenhandler.cpp
blob: 2a690349c23dae4c7c76b3bc31f3af95ef364fc2 (plain)
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
71
72
73
74
75
76
77
78
79
#include "homescreenhandler.h"
#include <functional>

void* HomescreenHandler::myThis = 0;

HomescreenHandler::HomescreenHandler(QObject *parent) :
    QObject(parent),
    mp_hs(NULL)
{

}

HomescreenHandler::~HomescreenHandler()
{
    if (mp_hs != NULL) {
        delete mp_hs;
    }
}

void HomescreenHandler::init(int port, const char *token)
{
    mp_hs = new LibHomeScreen();
    mp_hs->init(port, token);

    myThis = this;

//    mp_hs->registerCallback(HomescreenHandler::onEv_static, HomescreenHandler::onRep_static);
//    mp_hs->subscribe(LibHomeScreen::event_list[0]);
//    mp_hs->subscribe(LibHomeScreen::event_list[1]);

    mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);

    mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](const char* display_message){
        qDebug("set_event_handler Event_OnScreenMessage display_message = %s", display_message);
    });

	mp_hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [this](const char* application_name){
        if(strcmp(application_name, "Home") == 0){
            emit this->homeButton();
        }
	});

}

void HomescreenHandler::tapShortcut(QString application_name)
{
    qDebug("tapShortcut %s", qPrintable(application_name));
    mp_hs->tapShortcut(application_name.toStdString().c_str());
}

void HomescreenHandler::onRep_static(struct json_object* reply_contents)
{
    static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
}

void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
{
    static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
}

void HomescreenHandler::onRep(struct json_object* reply_contents)
{
    const char* str = json_object_to_json_string(reply_contents);
    qDebug("HomeScreen onReply %s", str);
}

void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
{
    const char* str = json_object_to_json_string(event_contents);
    qDebug("HomeScreen onEv %s, contents: %s", event.c_str(), str);

    if (event.compare("homescreen/on_screen_message") == 0) {
        struct json_object *json_data = json_object_object_get(event_contents, "data");
        struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
        const char* display_message = json_object_get_string(json_display_message);

        qDebug("display_message = %s", display_message);
    }
}