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);
}