#include "voice_package_base.h"

#include "base_ipc/include/base_ipc.h" #include "protocol/include/ldprotocol.h" #include "device_api.h"

#include "tuya_iot_sdk.h" #include "tuya_send.h" #include "info_pipe_tuya.h" #include "protocol_iot.h" #include "iot_object_id.h" #include "iot_object_message.h"

VoicePackageBase::VoicePackageBase() : LDObject(OBJECT_ID_VOICE_PACKAGE_HANDLE) { SubscribeMSG(SUBSCRIBE_MSG_NET_DISCONNECT_TO_CLOUD); SubscribeMSG(SUBSCRIBE_MSG_NET_CONNECT_TO_CLOUD); }

VoicePackageBase::~VoicePackageBase() { UnSubscribeMSG(SUBSCRIBE_MSG_NET_DISCONNECT_TO_CLOUD); UnSubscribeMSG(SUBSCRIBE_MSG_NET_CONNECT_TO_CLOUD); }

void VoicePackageBase::HandleSubscribeMSG(int i_msg_id, Json::Value json_param) { FLOGD("handle msg id :%d", i_msg_id); switch (i_msg_id) { case SUBSCRIBE_MSG_NET_CONNECT_TO_CLOUD: //监听到联网事件触发一次上传 SendVoicePackageId(); break; default: break; } }

void VoicePackageBase::Init() { mResponseJson["infoType"] = (int)IP_VOICE_PACKAGE; }

void VoicePackageBase::Start() { //程序启动上报一次 SendVoicePackageId(); }

void VoicePackageBase::Stop() { }

void VoicePackageBase::HandleVoicePackage(const Json::Value& dataJson, const Json::Value& dInfoJson) { mUserInfoJson = dInfoJson;

if (dataJson["cmd"].empty())
{
    SendVoicePackageResult("fail", "", VPEC_COMMAND_NOT_FOUND);
    return;
}

if (dataJson["cmd"].asString() == "getVoicePackageInfo")
{
    GetVoicePackageInfo();
}
else if (dataJson["cmd"].asString() == "downloadAndApply")
{
    //涂鸦的语音包是 "id,描述", id表示序号,","隔开后面是描述,我们只需要用到id
    std::string id = dataJson["id"].asString();
    id = id.substr(0, id.find(","));
    id == "" ? "0" : id;
    DownloadAndApplyVoicePackage(id, dataJson["downUrl"].asString(), dataJson["md5sum"].asString());
}
else
{
    SendVoicePackageResult("fail", "", VPEC_COMMAND_NOT_FOUND);
}

}

void VoicePackageBase::GetVoicePackageInfo() { std::string curName, defName, str_md5; VoicePackage::Instance()->GetCurVoicePackage(curName); VoicePackage::Instance()->GetDefaultVoipName(defName); VoicePackage::Instance()->GetCurVoicePackageMD5(str_md5);

if (curName == FAULT_VOICE_PACKAGE)
{
    LOGD("What, you don't have the current voice package.");
    SendVoicePackageList("fail");
    return;
}

Json::Value item, list = Json::Value(Json::arrayValue);

if (!curName.empty())
{
    item["id"] = curName;
    item["state"] = "saved";
    item["type"] = "normal";
    list.append(item);
}

if (!defName.empty())
{
    item["id"] = defName;
    item["state"] = "saved";
    item["type"] = "default";
    list.append(item);
}

mResponseJson.clear();
mResponseJson["infoType"] = (int)IP_VOICE_PACKAGE;
mResponseJson["message"] = "ok";
mResponseJson["data"]["cmd"] = "getVoicePackageInfo";
mResponseJson["data"]["voicePackageList"] = list;
mResponseJson["data"]["curVoicePackage"] = curName;
mResponseJson["data"]["curVoicePackageMD5"] = str_md5;
TUYA_IOT_SDK->SendGetVoicePackageResult(127, mResponseJson.toStyledString().c_str(), mResponseJson.toStyledString().size(), mUserInfoJson);

SendVoicePackageId();

}

void VoicePackageBase::DownloadAndApplyVoicePackage(std::string id, std::string downUrl, std::string md5sum) { if (!CheckIfCanDownload()) { SendVoicePackageResult("fail", "downloadAndApply", VPEC_DOWNLOAD_IN_PROGRESS); return; }

if (id.empty() || downUrl.empty() || md5sum.empty())
{
    SendVoicePackageResult("fail", "downloadAndApply", VPEC_MISSING_PARAMETERS);
    return;
}

mDownloadInfo = VoipDownlaodInfo(id, downUrl, md5sum);

//downlaod and check, send message inside VoicePackage Class
//apply inside
DownloadFileCheckAndApply();

}

VoicePackageErrorCode VoicePackageBase::DownloadFileCheckAndApply() { mDownloadThread = std::thread([=](void* param) { FLOGD("start download music package.....");

    INFO_PIPE->SetIfDuringDownloadVoicePackage(true);
    HttpsBase::Instance()->SetDownloadAndCheckTimeout(300);
    int start = GetCurrentTickMs();
    int ret = HttpsBase::Instance()->DownloadAndCheck(mDownloadInfo.mDownUrl, mDownloadInfo.mDownMd5, VOIP_DOWNLOAD_FILE, (void*)VoicePackageBase::DownloadCallback, this);
    int end = GetCurrentTickMs();
    LOGD("download music package ret %d, using time %d ms", ret, end - start);

    //apply inside
    if (ret == HSE_SUCCESS)
    {
        SendVoicePackageResult("ok", "downloadAndApply", VPEC_OK, 100);
        ApplyVoicePackage();
    }
    else
    {
        SendVoicePackageResult("fail", "downloadAndApply", VPEC_DOWNLOAD_FAIL);
    }

    INFO_PIPE->SetIfDuringDownloadVoicePackage(false);
}, this);
mDownloadThread.detach();

return VPEC_OK;

}

void VoicePackageBase::ApplyVoicePackage() { //把VOIP_DOWNLOAD_FILE 应用到 VOIP_PACKAGE_PATH/id名称 下 VoicePackageResultCode ret = (VoicePackageResultCode)VoicePackage::Instance()->SaveAndApplyPackage(mDownloadInfo.mDownId.c_str(), VOIP_DOWNLOAD_FILE, VPAT_ONLY_ONE_COVER_CURRENT);

if (ret == VPRC_OPERATE_OK)
{
    SendVoicePackageResult("ok", "downloadAndApply", VPEC_OK, 100);
}
else if (ret == VPRC_SAVE_TOO_BIG_OR_EMPTY || ret == VPRC_SAVE_TOO_BIG)
{
    SendVoicePackageResult("fail", "downloadAndApply", VPEC_PACKAGE_TOO_LARGE);
}
else
{
    SendVoicePackageResult("fail", "downloadAndApply", VPEC_DOWNLOAD_FAIL);
}

}

bool VoicePackageBase::CheckIfCanDownload() { if (INFO_PIPE->GetIfDuringDownloadVoicePackage()) { return false; } else { return true; } }

int VoicePackageBase::DownloadCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { VoicePackageBase* subClass = static_cast<VoicePackageBase*>(clientp);

static int lastTime = GetCurrentTickMs();
int curTime = GetCurrentTickMs();

if (dltotal != 0 && curTime > lastTime + 400)
{
    lastTime = curTime;
    float progress = (100 * dlnow / dltotal);
    FLOGD("progress %.2f%%", progress);

    //打包上传下载进度
#include voice_package_baseh#include base_ipcincludebase_ipch#include protocolincludeldprotocolh#include device_apih#include tuya_iot_sdkh#include tuya_sendh#include info_pipe_tuyah#include protocol_i

原文地址: https://www.cveoy.top/t/topic/bV8a 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录