Beispiel #1
0
void OwCtrl::processNewMessage(const string &msg)
{
    json_error_t jerr;
    json_t *jroot = json_loads(msg.c_str(), 0, &jerr);

    if (!jroot || !json_is_array(jroot))
    {
        cWarningDom("1wire") << "Error parsing json from sub process: " << jerr.text;
        if (jroot)
            json_decref(jroot);
        return;
    }

    int idx;
    json_t *value;

    json_array_foreach(jroot, idx, value)
    {
        Params p;
        jansson_decode_object(value, p);

        if (p.Exists("id"))
        {
            if (p.Exists("value"))
                mapValues[p["id"]] = p["value"];
            if (p.Exists("type"))
                mapValues[p["id"]] = p["type"];
        }
    }
void JsonApiHandlerWS::processApi(const string &data, const Params &paramsGET)
{
    VAR_UNUSED(paramsGET); //not used for websocket

    Params jsonRoot;
    Params jsonData;

    //parse the json data
    json_error_t jerr;
    json_t *jroot = json_loads(data.c_str(), 0, &jerr);

    if (!jroot || !json_is_object(jroot))
    {
        cDebugDom("network") << "Error loading json : " << jerr.text;
        return;
    }

    char *d = json_dumps(jroot, JSON_INDENT(4));
    if (d)
    {
        cDebugDom("network") << d;
        free(d);
    }

    //decode the json root object into jsonParam
    jansson_decode_object(jroot, jsonRoot);

    json_t *jdata = json_object_get(jroot, "data");
    if (jdata)
        jansson_decode_object(jdata, jsonData);

    //Format: { msg: "type", msg_id: id, data: {} }

    if (jsonRoot["msg"] == "login")
    {
        //check for if username/password matches
        string user = Utils::get_config_option("calaos_user");
        string pass = Utils::get_config_option("calaos_password");

        if (Utils::get_config_option("cn_user") != "" &&
            Utils::get_config_option("cn_pass") != "")
        {
            user = Utils::get_config_option("cn_user");
            pass = Utils::get_config_option("cn_pass");
        }

        //Not logged in, need to wait for a correct login
        if (user != jsonData["cn_user"] || pass != jsonData["cn_pass"])
        {
            cDebugDom("network") << "Login failed!";

            json_t *jret = json_object();
            json_object_set_new(jret, "success", json_string("false"));

            sendJson("login", jret, jsonRoot["msg_id"]);

            //Close the connection on login failure
            closeConnection.emit(WebSocketFrame::CloseCodeNormal, "login failed!");
        }
        else
        {
            json_t *jret = json_object();
            json_object_set_new(jret, "success", json_string("true"));

            sendJson("login", jret, jsonRoot["msg_id"]);

            loggedin = true;
        }
    }
    else if (loggedin) //only process other api if loggedin
    {
        if (jsonRoot["msg"] == "get_home")
            processGetHome(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_state")
            processGetState(jdata, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_states")
            processGetStates(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "query")
            processQuery(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_param")
            processGetParam(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "set_param")
            processSetParam(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "del_param")
            processDelParam(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "set_state")
            processSetState(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_playlist")
            processGetPlaylist(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_io")
            processGetIO(jdata, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "audio")
            processAudio(jdata, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "audio_db")
            processAudioDb(jdata, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "get_timerange")
            processGetTimerange(jsonData, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "set_timerange")
            processSetTimerange(jdata, jsonRoot["msg_id"]);
        else if (jsonRoot["msg"] == "autoscenario")
            processAutoscenario(jdata, jsonRoot["msg_id"]);

//        else if (jsonParam["action"] == "get_cover")
//            processGetCover();
//        else if (jsonParam["action"] == "get_camera_pic")
//            processGetCameraPic();
//        else if (jsonParam["action"] == "config")
//            processConfig(jroot);
    }

    json_decref(jroot);
}
void JsonApiHandlerHttp::processApi(const string &data, const Params &paramsGET)
{
    jsonParam.clear();

    //parse the json data
    json_error_t jerr;
    json_t *jroot = json_loads(data.c_str(), 0, &jerr);

    if (!jroot || !json_is_object(jroot))
    {
        cDebugDom("network") << "Error loading json : " << jerr.text << ". No JSON, trying with GET parameters.";

        jsonParam = paramsGET;

        if (jroot)
        {
            json_decref(jroot);
            jroot = nullptr;
        }
    }
    else
    {
        char *d = json_dumps(jroot, JSON_INDENT(4));
        if (d)
        {
            cDebugDom("network") << d;
            free(d);
        }

        //decode the json root object into jsonParam
        jansson_decode_object(jroot, jsonParam);
    }


    //check for if username/password matches
    string user = Utils::get_config_option("calaos_user");
    string pass = Utils::get_config_option("calaos_password");

    if (Utils::get_config_option("cn_user") != "" &&
            Utils::get_config_option("cn_pass") != "")
    {
        user = Utils::get_config_option("cn_user");
        pass = Utils::get_config_option("cn_pass");
    }

    if (user != jsonParam["cn_user"] || pass != jsonParam["cn_pass"])
    {
        cDebugDom("network") << "Login failed!";

        Params headers;
        headers.Add("Connection", "close");
        headers.Add("Content-Type", "text/html");
        string res = httpClient->buildHttpResponse(HTTP_400, headers, HTTP_400_BODY);
        sendData.emit(res);
        closeConnection.emit(0, string());

        if (jroot)
        {
            json_decref(jroot);
            jroot = nullptr;
        }

        return;
    }

    //check action now
    if (jsonParam["action"] == "get_home")
        processGetHome();
    else if (jsonParam["action"] == "get_state")
        processGetState(jroot);
    else if (jsonParam["action"] == "get_states")
        processGetStates();
    else if (jsonParam["action"] == "query")
        processQuery();
    else if (jsonParam["action"] == "get_param")
        processGetParam();
    else if (jsonParam["action"] == "set_param")
        processSetParam();
    else if (jsonParam["action"] == "del_param")
        processDelParam();
    else if (jsonParam["action"] == "set_state")
        processSetState();
    else if (jsonParam["action"] == "get_playlist")
        processGetPlaylist();
    else if (jsonParam["action"] == "poll_listen")
        processPolling();
    else if (jsonParam["action"] == "get_cover")
        processGetCover();
    else if (jsonParam["action"] == "get_camera_pic")
        processGetCameraPic();
    else if (jsonParam["action"] == "get_timerange")
        processGetTimerange();
    else if (jsonParam["action"] == "camera")
        processCamera();
    else
    {
        if (!jroot)
        {
            Params headers;
            headers.Add("Connection", "close");
            headers.Add("Content-Type", "text/html");
            string res = httpClient->buildHttpResponse(HTTP_400, headers, HTTP_400_BODY);
            sendData.emit(res);
            closeConnection.emit(0, string());

            if (jroot)
                json_decref(jroot);

            return;
        }

        if (jsonParam["action"] == "config")
            processConfig(jroot);
        else if (jsonParam["action"] == "get_io")
            processGetIO(jroot);
        else if (jsonParam["action"] == "audio")
            processAudio(jroot);
        else if (jsonParam["action"] == "audio_db")
            processAudioDb(jroot);
        else if (jsonParam["action"] == "set_timerange")
            processSetTimerange(jroot);
        else if (jsonParam["action"] == "autoscenario")
            processAutoscenario(jroot);
    }

    if (jroot)
        json_decref(jroot);
}
Beispiel #4
0
void CalaosConnection::onMessageReceived(const string &data)
{
    Params jsonRoot;
    Params jsonData;

    //parse the json data
    json_error_t jerr;
    json_t *jroot = json_loads(data.c_str(), 0, &jerr);

    if (!jroot || !json_is_object(jroot))
    {
        cDebugDom("network") << "Error loading json : " << jerr.text;
        return;
    }

    char *d = json_dumps(jroot, JSON_INDENT(4));
    if (d)
    {
        cDebugDom("network") << d;
        free(d);
    }

    //decode the json root object into jsonParam
    jansson_decode_object(jroot, jsonRoot);

    json_t *jdata = json_object_get(jroot, "data");
    if (jdata)
        jansson_decode_object(jdata, jsonData);

    //Format: { msg: "type", msg_id: id, data: {} }

    if (jsonRoot["msg"] == "login" &&
        jsonData["success"] == "true" &&
        con_state == CALAOS_CON_LOGIN)
    {
        DELETE_NULL(timeout);
        con_state = CALAOS_CON_OK;

        cDebugDom("network.connection") << "Successfully logged in.";

        connection_ok.emit();
        return;
    }

    //a message id was sent, get the corresponding
    //query and call the callback
    if (!jsonRoot["msg_id"].empty())
    {
        if (commands.find(jsonRoot["msg_id"]) == commands.end())
        {
            cErrorDom("network") << "msg_id " << jsonRoot["msg_id"] << " not found in commands.";
        }
        else
        {
            CalaosCmd *cmd = commands[jsonRoot["msg_id"]];
            cmd->callback(jdata, cmd->user_data);
            commands.erase(jsonRoot["msg_id"]);
            delete cmd;
        }
    }

    if (jsonRoot["msg"] == "event")
    {
        Params eventData;
        jansson_decode_object(json_object_get(jdata, "data"), eventData);
        string ev = jsonData["type_str"];

        if (ev == "input_added" ||
            ev == "output_added")
            notify_io_new.emit(ev, eventData);

        else if (ev == "input_deleted" ||
                 ev == "output_deleted")
            notify_io_delete.emit(ev, eventData);

        else if (ev == "input_changed" ||
                 ev == "output_changed" ||
                 ev == "input_prop_deleted" ||
                 ev == "output_prop_deleted" ||
                 ev == "timerange_changed")
            notify_io_change.emit(ev, eventData);

        else if (ev == "room_added")
            notify_room_new.emit(ev, eventData);

        else if (ev == "room_deleted")
            notify_room_delete.emit(ev, eventData);

        else if (ev == "room_changed" ||
                 ev == "room_prop_deleted")
            notify_room_change.emit(ev, eventData);

        else if (ev == "scenario_deleted")
            notify_scenario_del.emit(ev, eventData);

        else if (ev == "scenario_added")
            notify_scenario_add.emit(ev, eventData);

        else if (ev == "scenario_changed")
            notify_scenario_change.emit(ev, eventData);

        else if (ev == "audio_song_changed" ||
                 ev == "playlist_tracks_added" ||
                 ev == "playlist_tracks_deleted" ||
                 ev == "playlist_tracks_moved" ||
                 ev == "playlist_reload" ||
                 ev == "playlist_cleared" ||
                 ev == "audio_status_changed" ||
                 ev == "audio_volume_changed")
            notify_audio_change.emit(ev, eventData);
    }
}