void JsonApiHandlerWS::processAudio(json_t *jdata, const string &client_id) { string msg = jansson_string_get(jdata, "audio_action"); if (msg == "get_playlist_size") audioGetPlaylistSize(jdata, [=](json_t *jret) { sendJson("audio", jret, client_id); }); else if (msg == "get_time") audioGetTime(jdata, [=](json_t *jret) { sendJson("audio", jret, client_id); }); else if (msg == "get_playlist_item") audioGetPlaylistItem(jdata, [=](json_t *jret) { sendJson("audio", jret, client_id); }); else if (msg == "get_cover_url") audioGetCoverInfo(jdata, [=](json_t *jret) { sendJson("audio", jret, client_id); }); else sendJson("audio", {{"error", "unkown audio_action" }} , client_id); }
void JsonApiHandlerWS::processGetIO(json_t *jdata, const string &client_id) { if (!jdata) { sendJson("get_io", nullptr, client_id); return; } sendJson("get_io", buildJsonGetIO(jdata), client_id); }
void JsonApiHandlerWS::processGetState(json_t *jdata, const string &client_id) { if (!jdata) { sendJson("get_state", nullptr, client_id); return; } buildJsonState(jdata, [=](json_t *jret) { sendJson("get_state", jret, client_id); }); }
void JsonApiHandlerHttp::processGetCameraPic() { int pid; Utils::from_string(jsonParam["camera_id"], pid); if (pid < 0 || pid >= CamManager::Instance().get_size()) { json_t *jret = json_object(); json_object_set_new(jret, "success", json_string("false")); json_object_set_new(jret, "error_str", json_string("camera_id not set")); sendJson(jret); return; } IPCam *camera = CamManager::Instance().get_camera(pid); string w, h; if (jsonParam.Exists("width")) w = jsonParam["width"]; if (jsonParam.Exists("height")) h = jsonParam["height"]; string cmd = "calaos_thumb \"" + camera->getPictureUrl() + "\" \"" + tempfname + "\""; if (w.empty() || h.empty()) cmd += " " + w + "x" + h; exe_thumb = ecore_exe_run(cmd.c_str(), nullptr); }
void JsonApiHandlerWS::processGetStates(const Params &jsonReq, const string &client_id) { buildJsonStates(jsonReq, [=](json_t *jret) { sendJson("get_states", jret, client_id); }); }
void scenesIdFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) { String sceneId = handler->getWildCard(1); LightGroup *scene = findScene(sceneId); switch (method) { case HTTP_GET: if (scene) { sendJson(scene->getSceneJson()); } else { sendError(3, "/scenes/"+sceneId, "Cannot retrieve scene that does not exist"); } break; case HTTP_PUT: // validate body, delete old group, create new group sceneCreationHandler(sceneId); // XXX not a valid response according to API sendUpdated(); break; case HTTP_DELETE: if (scene) { updateSceneSlot(findSceneIndex(sceneId), sceneId, ""); } else { sendError(3, requestUri, "Cannot delete scene that does not exist"); } sendSuccess(requestUri+" deleted"); break; default: sendError(4, requestUri, "Scene method not supported"); break; } }
void JsonApiHandlerWS::processQuery(const Params &jsonReq, const string &client_id) { buildQuery(jsonReq, [=](json_t *jret) { sendJson("query", jret, client_id); }); }
void JsonApiV3::processGetState(json_t *jdata, const string &client_id) { buildJsonState(jdata, [=](json_t *jret) { sendJson("get_state", jret, client_id); }); }
void JsonApiHandlerHttp::processQuery() { buildQuery(jsonParam, [=](json_t *jret) { sendJson(jret); }); }
void JsonApiHandlerHttp::processGetPlaylist() { decodeGetPlaylist(jsonParam, [=](json_t *jret) { sendJson(jret); }); }
void JsonApiHandlerHttp::processGetState(json_t *jroot) { buildJsonState(jroot, [=](json_t *jret) { sendJson(jret); }); }
void JsonApiHandlerHttp::processGetStates() { buildJsonStates(jsonParam, [=](json_t *jret) { sendJson(jret); }); }
void LuaIOBase::set_value(std::string val) const { Params p = {{ "id", params["id"] }, { "value", val }}; sendJson("set_state", p); }
void LuaIOBase::set_value(double val) const { Params p = {{ "id", params["id"] }, { "value", Utils::to_string(val) }}; sendJson("set_state", p); }
void LuaIOBase::set_value(bool val) const { Params p = {{ "id", params["id"] }, { "value", val?"true":"false" }}; sendJson("set_state", p); }
void JsonApiHandlerWS::processGetPlaylist(Params &jsonReq, const string &client_id) { decodeGetPlaylist(jsonReq, [=](json_t *jret) { sendJson("get_playlist", jret, client_id); }); }
void sendSuccess(String value) { aJsonObject *search = aJson.createArray(); aJsonObject *container = aJson.createObject(); aJson.addItemToArray(search, container); aJsonObject *succeed = aJson.createObject(); aJson.addStringToObject(container, "success", value.c_str()); sendJson(search); }
void LuaIOBase::set_param(const std::string &key, const std::string &val) { Params p = {{ "id", params["id"] }, { "param", key }, { "value", val }}; sendJson("set_param", p); }
void JsonRpc::sendResponse(const QVariant& id, const QVariant& result) { QVariantMap response; response["jsonrpc"] = "2.0"; response["id"] = id; response["result"] = result; emit sendJson(VariantToJson::parse(response)); }
void JsonApiHandlerWS::handleEvents(const CalaosEvent &event) { if (!loggedin) return; cDebugDom("network") << "Handling event: " << event.toString(); sendJson("event", event.toJson()); }
void groupsIdFn(WcFnRequestHandler *handler, String requestUri, HTTPMethod method) { String groupNumText = handler->getWildCard(1); int groupNum = atoi(groupNumText.c_str()) - 1; if ((groupNum == -1 && groupNumText != "0") || groupNum >= 16 || (groupNum >= 0 && !lightGroups[groupNum])) { // error, invalid group number sendError(3, requestUri, "Invalid group number"); return; } switch (method) { case HTTP_GET: if (groupNum != -1) { sendJson(lightGroups[groupNum]->getJson()); } else { aJsonObject *object = aJson.createObject(); aJson.addStringToObject(object, "name", "0"); aJsonObject *lightsArray = aJson.createArray(); aJson.addItemToObject(object, "lights", lightsArray); for (int i = 0; i < MAX_LIGHT_HANDLERS; i++) { if (!lightHandlers[i]) { continue; } // add light to list String lightNum = ""; lightNum += (i + 1); aJson.addItemToArray(lightsArray, aJson.createItem(lightNum.c_str())); } sendJson(object); } break; case HTTP_PUT: // validate body, delete old group, create new group updateGroupSlot(groupNum, HTTP->arg("plain")); sendUpdated(); break; case HTTP_DELETE: updateGroupSlot(groupNum, ""); sendSuccess(requestUri+" deleted"); break; default: sendError(4, requestUri, "Group method not supported"); break; } }
void JsonApiHandlerWS::processSetState(Params &jsonReq, const string &client_id) { bool res = decodeSetState(jsonReq); if (!client_id.empty()) { json_t *jret = json_object(); json_object_set_new(jret, "success", json_string(res?"true":"false")); sendJson("set_state", jret, client_id); } }
void JsonApiHandlerHttp::processGetHome() { json_t *jret = nullptr; jret = json_pack("{s:o, s:o, s:o}", "home", buildJsonHome(), "cameras", buildJsonCameras(), "audio", buildJsonAudio()); sendJson(jret); }
void JsonApiHandlerHttp::exeFinished(Ecore_Exe *exe, int exit_code) { if (exit_code != 0) { json_t *jret = json_object(); json_object_set_new(jret, "success", json_string("false")); json_object_set_new(jret, "error_str", json_string("unable to load data from url")); sendJson(jret); return; } ecore_exe_free(exe); json_t *jret = json_object(); json_object_set_new(jret, "success", json_string("true")); json_object_set_new(jret, "contenttype", json_string("image/jpeg")); json_object_set_new(jret, "encoding", json_string("base64")); json_object_set_new(jret, "data", json_string(Utils::getFileContentBase64(tempfname.c_str()).c_str())); sendJson(jret); }
void JsonApiHandlerWS::processGetHome(const Params &jsonReq, const string &client_id) { json_t *jret = nullptr; jret = json_pack("{s:o, s:o, s:o}", "home", buildJsonHome(), "cameras", buildJsonCameras(), "audio", buildJsonAudio()); sendJson("get_home", jret, client_id); }
void sendError(int type, String path, String description) { aJsonObject *root = aJson.createArray(); aJsonObject *errorContainer = aJson.createObject(); aJsonObject *errorObject = aJson.createObject(); aJson.addItemToObject(errorObject, "type", aJson.createItem(type)); aJson.addStringToObject(errorObject, "address", path.c_str()); aJson.addStringToObject(errorObject, "description", description.c_str()); aJson.addItemToObject(errorContainer, "error", errorObject); aJson.addItemToArray(root, errorContainer); sendJson(root); }
void CalaosConnection::sendCommand(const string &msg, json_t *jdata, CommandDone_cb callback, void *data) { string clientid = Utils::to_string(rand() & 0xffff); while (commands.find(clientid) != commands.end()) clientid = Utils::to_string(rand() & 0xffff); CalaosCmd *cmd = new CalaosCmd(callback, data, this, clientid); commands[clientid] = cmd; sendJson(msg, jdata, clientid); }
void JsonRpc::sendError(const QVariant& id, int errorCode, const QString& message, const QVariant& data) { QVariantMap error; error["code"] = errorCode; error["message"] = message; error["data"] = data; QVariantMap response; response["jsonrpc"] = "2.0"; response["id"] = id; response["error"] = error; emit sendJson(VariantToJson::parse(response)); }
void JsonApiHandlerHttp::processGetCover() { int pid; Utils::from_string(jsonParam["player_id"], pid); if (pid < 0 || pid >= AudioManager::Instance().get_size()) { json_t *jret = json_object(); json_object_set_new(jret, "success", json_string("false")); json_object_set_new(jret, "error_str", json_string("player_id not set")); sendJson(jret); return; } AudioPlayer *player = AudioManager::Instance().get_player(pid); string w, h; if (jsonParam.Exists("width")) w = jsonParam["width"]; if (jsonParam.Exists("height")) h = jsonParam["height"]; player->get_album_cover([=](AudioPlayerData data) { //do not start another exe if one is running already if (data.svalue == "" || exe_thumb) { json_t *jret = json_object(); json_object_set_new(jret, "success", json_string("false")); json_object_set_new(jret, "error_str", json_string("unable to get url")); sendJson(jret); return; } string cmd = "calaos_thumb \"" + data.svalue + "\" \"" + tempfname + "\""; if (w.empty() || h.empty()) cmd += " " + w + "x" + h; exe_thumb = ecore_exe_run(cmd.c_str(), nullptr); }); }
void JsonRpc::sendNotification(const QString& method, const QVariant& parameters) { Q_ASSERT(parameters.type() == QVariant::List || parameters.type() == QVariant::Map || parameters.isNull()); QVariantMap request; request["jsonrpc"] = "2.0"; request["method"] = method; // no ID for notifications. if(!parameters.isNull()) { request["params"] = parameters; } emit sendJson(VariantToJson::parse(request)); }