hunting/middleware/AppManager/src/Protocol/SixFrame/SixFrameHandle.cpp
2024-06-15 08:35:07 +08:00

1026 lines
43 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SixFrameHandle.h"
#include "ILog.h"
#include <cstring>
#include <sstream>
#include <stdio.h>
#include <time.h>
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
// using std::placeholders::_4;
// clang-format off
const char *TCP_RESULT_MSGID = "msgid";
const char *CJSON_INFO_STRING = "info";
const char *CJSON_ITEMS_STRING = "items";
const char *CJSON_INDEX_STRING = "index";
const char *CJSON_FILES_STRING = "files";
const char *APP_GET_PRODUCT_INFO = "/app/getproductinfo";
const char *APP_GET_DEVICE_ATTR = "/app/getdeviceattr";
const char *APP_GET_MEDIA_INFO = "/app/getmediainfo";
const char *APP_GET_SD_CARD_INFO = "/app/getsdinfo";
const char *APP_GET_BATTERY_INFO = "/app/getbatteryinfo";
const char *APP_GET_PARAM_VALUE = "/app/getparamvalue";
const char *APP_GET_PARAM_ITEMS = "/app/getparamitems";
const char *APP_GET_CAPABILITY = "/app/capability";
const char *APP_GET_LOCK_VIDEO_STATUS = "/app/getlockvideostatus";
const char *APP_GET_STORAGE_INFO = "/app/getstorageinfo";
const char *APP_GET_FILE_LIST = "/app/getfilelist";
const char *APP_SET_DATE_TIME = "/app/setsystime";
const char *APP_SET_TIME_ZONE = "/app/settimezone";
const char *APP_SET_PARAM_VALUE = "/app/setparamvalue";
const char *APP_ENTER_RECORDER = "/app/enterrecorder";
const char *APP_PLAYBACK = "/app/playback";
const char *APP_UPLOAD_FILE = "/upload";
const char *APP_GET_THUMBNAIL = "/app/getthumbnail";
// /app/getparamvalue?param=rec
// /app/exitrecorder
// clang-format on
constexpr bool SET_REQUEST_RESPONSE = true;
class CjsonAutoFree
{
public:
CjsonAutoFree(cJSON *json) : mJson(json)
{
}
~CjsonAutoFree()
{
if (mJson != nullptr) {
cJSON_Delete(mJson);
}
}
private:
cJSON *mJson;
};
SixFrameHandle::SixFrameHandle()
{
mAppMonitor = std::make_shared<VAppMonitor>();
mResquesHandleFunc[APP_GET_PRODUCT_INFO] = std::bind(&SixFrameHandle::RequestGetProductInfo, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_DEVICE_ATTR] = std::bind(&SixFrameHandle::RequestGetDeviceAttr, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_MEDIA_INFO] = std::bind(&SixFrameHandle::RequestGetMediaInfo, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_SD_CARD_INFO] = std::bind(&SixFrameHandle::RequestGetSdCardInfo, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_BATTERY_INFO] = std::bind(&SixFrameHandle::RequestGetBatteryInfo, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_PARAM_VALUE] = std::bind(&SixFrameHandle::RequestGetParamValue, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_PARAM_ITEMS] = std::bind(&SixFrameHandle::RequestGetParamItems, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_CAPABILITY] = std::bind(&SixFrameHandle::RequestGetCapability, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_LOCK_VIDEO_STATUS] =
std::bind(&SixFrameHandle::RequestGetLockVideoStatus, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_STORAGE_INFO] = std::bind(&SixFrameHandle::RequestGetStorageInfo, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_FILE_LIST] = std::bind(&SixFrameHandle::RequestGetFileList, this, _1, _2, _3);
mResquesHandleFunc[APP_SET_DATE_TIME] = std::bind(&SixFrameHandle::RequestSetDateTime, this, _1, _2, _3);
mResquesHandleFunc[APP_SET_TIME_ZONE] = std::bind(&SixFrameHandle::RequestSetTimeZone, this, _1, _2, _3);
mResquesHandleFunc[APP_SET_PARAM_VALUE] = std::bind(&SixFrameHandle::RequestSetParamValue, this, _1, _2, _3);
mResquesHandleFunc[APP_ENTER_RECORDER] = std::bind(&SixFrameHandle::RequestEnterRecorder, this, _1, _2, _3);
mResquesHandleFunc[APP_PLAYBACK] = std::bind(&SixFrameHandle::RequestPlayback, this, _1, _2, _3);
mResquesHandleFunc[APP_UPLOAD_FILE] = std::bind(&SixFrameHandle::RequestUpload, this, _1, _2, _3);
mResquesHandleFunc[APP_GET_THUMBNAIL] = std::bind(&SixFrameHandle::RequestGetThumbnail, this, _1, _2, _3);
// mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _);
}
void SixFrameHandle::RequestHandle(const char *url, const unsigned int &urlLength, ResponseHandle responseHandle,
void *context)
{
const std::string urlStr2 = url;
LogInfo("URL = %s\n", urlStr2.c_str());
size_t queryStartPos = urlStr2.find('?');
std::string command = "";
if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) {
command = urlStr2.substr(0, queryStartPos);
}
else {
command = urlStr2.substr(0, urlStr2.length());
}
LogInfo("command = %s\n", command.c_str());
RequestHandle2(command, urlStr2, responseHandle, context);
}
void SixFrameHandle::RequestHandleTcp(const char *data, const unsigned int &dataLength, ResponseHandle responseHandle,
void *context)
{
cJSON *parsed = nullptr;
std::string command = "";
parsed = cJSON_Parse(data);
if (nullptr == parsed) {
LogError("cJSON_Parse failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
cJSON *msgid = cJSON_GetObjectItem(parsed, "msgid");
if (cJSON_IsString(msgid)) {
command = cJSON_GetStringValue(msgid);
}
LogInfo("command = %s\n", command.c_str());
RequestTcpHandle2(command, parsed, responseHandle, context);
cJSON_Delete(parsed);
}
void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, ParseUrlResultFunc resultHandle,
std::shared_ptr<VParseUrl> &context)
{
size_t queryStartPos = url.find('?');
if (queryStartPos != std::string::npos && queryStartPos + 1 < url.length()) {
std::string paramsStr = url.substr(queryStartPos + 1);
std::istringstream iss(paramsStr);
std::string token;
while (getline(iss, token, '&')) {
size_t equalSignPos = token.find('=');
if (equalSignPos != std::string::npos) {
std::string key = token.substr(0, equalSignPos);
std::string value = token.substr(equalSignPos + 1);
LogInfo("url get [%s] = %s\n", key.c_str(), value.c_str());
resultHandle(key, value, context);
// paramsMap.insert({key, value});
}
}
}
}
void SixFrameHandle::RequestHandle2(const std::string command, const std::string &url, ResponseHandle responseHandle,
void *context)
{
auto result = mResquesHandleFunc.find(command);
if (result != mResquesHandleFunc.end()) {
(*result).second(url, responseHandle, context);
}
else {
LogError("Unknown command.\n");
DoNothing(url, responseHandle, context);
}
}
void SixFrameHandle::DoNothing(const std::string &url, ResponseHandle responseHandle, void *context)
{
cJSON *result = MakeResponseResult(ResposeResult::FAILED);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void SixFrameHandle::RequestGetProductInfo(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetProductInfo.\n");
AppGetProductInfo param;
mAppMonitor->GetProductInfo(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetProductInfo(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetProductInfo(cJSON *result, const AppGetProductInfo &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddStringToObject(info, "model", param.mModel.c_str());
cJSON_AddStringToObject(info, "company", param.mCompany.c_str());
cJSON_AddStringToObject(info, "soc", param.mSoc.c_str());
cJSON_AddStringToObject(info, "sp", param.mSp.c_str());
}
void SixFrameHandle::RequestGetDeviceAttr(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetDeviceAttr.\n");
AppGetDeviceAttr param;
mAppMonitor->GetDeviceAttr(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetDeviceAttr(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetDeviceAttr(cJSON *result, const AppGetDeviceAttr &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddStringToObject(info, "uuid", param.mUUID.c_str());
cJSON_AddStringToObject(info, "softver", param.mSoftVersion.c_str());
cJSON_AddStringToObject(info, "otaver", param.mOtaVersion.c_str());
cJSON_AddStringToObject(info, "hwver", param.mHardwareVersion.c_str());
cJSON_AddStringToObject(info, "ssid", param.mSSID.c_str());
cJSON_AddStringToObject(info, "bssid", param.mBSSID.c_str());
cJSON_AddStringToObject(info, "camnum", param.mCameraNumber.c_str());
cJSON_AddStringToObject(info, "curcamid", param.mCurrentCameraID.c_str());
cJSON_AddStringToObject(info, "wifireboot", param.mWifiReboot.c_str());
}
void SixFrameHandle::RequestGetMediaInfo(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetMediaInfo.\n");
AppGetMediaInfo param;
mAppMonitor->GetMediaInfo(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetMediaInfo(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetMediaInfo(cJSON *result, const AppGetMediaInfo &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddStringToObject(info, "rtsp", param.mRtspUrl.c_str());
cJSON_AddStringToObject(info, "transport", param.mTransport.c_str());
cJSON_AddNumberToObject(info, "port", param.mPort);
cJSON_AddNumberToObject(info, "page", 1); // TODO: 1 means app will paging to retrieve files.
}
void SixFrameHandle::RequestGetSdCardInfo(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetDeviceAttr.\n");
AppGetSdCardInfo param;
mAppMonitor->GetSdCardInfo(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetSdCardInfo(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetSdCardInfo(cJSON *result, const AppGetSdCardInfo &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddNumberToObject(info, "status", static_cast<int>(param.mStatus));
cJSON_AddNumberToObject(info, "free", param.mFree);
cJSON_AddNumberToObject(info, "total", param.mTotal);
}
void SixFrameHandle::RequestGetBatteryInfo(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetDeviceAttr.\n");
AppGetBatteryInfo param;
mAppMonitor->GetBatteryInfo(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetBatteryInfo(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetBatteryInfo(cJSON *result, const AppGetBatteryInfo &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddNumberToObject(info, "charge", static_cast<int>(param.mChargeStatus));
cJSON_AddNumberToObject(info, "capacity", param.mCapacity);
}
void inline SixFrameHandle::RequestParamValueParse(const std::string &url, std::map<std::string, bool> &paramList)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<std::map<std::string, bool>>> parseImpl =
std::dynamic_pointer_cast<ParseUrl<std::map<std::string, bool>>>(parse);
if ("param" == key) {
if ("all" == value) {
parseImpl->mData["all"] = true; // means app want to get all value.
}
}
if ("param" == key) {
if ("rec" == value) {
parseImpl->mData["rec"] = true; // means app want to get rec value.
}
}
if ("param" == key) {
if ("mic" == value) {
parseImpl->mData["mic"] = true; // means app want to get mic value.
}
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<std::map<std::string, bool>>>();
std::shared_ptr<ParseUrl<std::map<std::string, bool>>> parseImpl =
std::dynamic_pointer_cast<ParseUrl<std::map<std::string, bool>>>(parse);
ExtractParamsFromUrl(url, parseFunc, parse);
paramList = parseImpl->mData;
}
void SixFrameHandle::RequestGetParamValue(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetParamValue.\n");
std::map<std::string, bool> paramList;
RequestParamValueParse(url, paramList);
AppParamValue paramDevice;
mAppMonitor->GetParamValue(paramDevice);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetParamValue(result, paramDevice, paramList);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetParamValue(cJSON *result, const AppParamValue &param,
const std::map<std::string, bool> &paramList)
{
auto it = paramList.find("all");
if (it != paramList.end()) {
cJSON *info = cJSON_CreateArray();
if (nullptr == info) {
LogError("cJSON_CreateArray failed.\n");
return;
}
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info);
cJSON *mic = cJSON_CreateObject();
if (nullptr != mic) {
cJSON_AddItemToArray(info, mic);
cJSON_AddStringToObject(mic, "name", "mic");
cJSON_AddNumberToObject(mic, "value", static_cast<int>(param.mMicStatus));
}
cJSON *rec = cJSON_CreateObject();
if (nullptr != rec) {
cJSON_AddItemToArray(info, rec);
cJSON_AddStringToObject(rec, "name", "rec");
cJSON_AddNumberToObject(rec, "value", static_cast<int>(param.mRec));
}
cJSON *osd = cJSON_CreateObject();
if (nullptr != osd) {
cJSON_AddItemToArray(info, osd);
cJSON_AddStringToObject(osd, "name", "osd");
cJSON_AddNumberToObject(osd, "value", 1);
}
}
else {
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
{
auto it = paramList.find("mic");
if (it != paramList.end()) {
cJSON_AddNumberToObject(info, "value", static_cast<int>(param.mMicStatus));
return;
}
}
{
auto it = paramList.find("rec");
if (it != paramList.end()) {
cJSON_AddNumberToObject(info, "value", static_cast<int>(param.mRec));
return;
}
}
}
}
void SixFrameHandle::RequestGetParamItems(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetParamItems.\n");
std::map<std::string, bool> paramList;
RequestParamValueParse(url, paramList);
AppGetCapability paramDevice;
// mAppMonitor->GetParamValue(paramDevice);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetParamItems(result, paramDevice, paramList);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void SixFrameHandle::ResponseGetParamItems(cJSON *result, const AppGetCapability &param,
const std::map<std::string, bool> &paramList)
{
auto it = paramList.find("all");
if (it != paramList.end()) {
cJSON *info = cJSON_CreateArray();
if (nullptr == info) {
LogError("cJSON_CreateArray failed.\n");
return;
}
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info);
{
cJSON *mic = cJSON_CreateObject();
cJSON *items = cJSON_CreateArray();
cJSON *index = cJSON_CreateArray();
if (nullptr != mic && nullptr != items && nullptr != index) {
cJSON_AddItemToArray(info, mic);
cJSON_AddStringToObject(mic, "name", "mic");
cJSON_AddItemToObject(mic, CJSON_ITEMS_STRING, items);
cJSON_AddItemToObject(mic, CJSON_INDEX_STRING, index);
cJSON_AddItemToArray(items, cJSON_CreateString("on"));
cJSON_AddItemToArray(items, cJSON_CreateString("off"));
cJSON_AddItemToArray(index, cJSON_CreateNumber(0));
cJSON_AddItemToArray(index, cJSON_CreateNumber(1));
}
}
// {
// cJSON *rec = cJSON_CreateObject();
// cJSON *items = cJSON_CreateArray();
// cJSON *index = cJSON_CreateArray();
// if (nullptr != rec && nullptr != items && nullptr != index) {
// cJSON_AddItemToArray(info, rec);
// cJSON_AddStringToObject(rec, "name", "rec");
// cJSON_AddItemToObject(rec, CJSON_ITEMS_STRING, items);
// cJSON_AddItemToObject(rec, CJSON_INDEX_STRING, index);
// cJSON_AddItemToArray(items, cJSON_CreateString("on"));
// cJSON_AddItemToArray(items, cJSON_CreateString("off"));
// cJSON_AddItemToArray(index, cJSON_CreateNumber(0));
// cJSON_AddItemToArray(index, cJSON_CreateNumber(1));
// }
// }
}
else {
cJSON *info = cJSON_CreateObject();
if (nullptr == info) {
LogError("cJSON_CreateObject failed.\n");
return;
}
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info);
{
auto it = paramList.find("mic");
if (it != paramList.end()) {
cJSON *items = cJSON_CreateArray();
cJSON *index = cJSON_CreateArray();
if (nullptr == items || nullptr == index) {
LogError("cJSON_CreateArray failed.\n");
return;
}
cJSON_AddItemToObject(info, CJSON_ITEMS_STRING, items);
cJSON_AddItemToObject(info, CJSON_INDEX_STRING, index);
cJSON_AddItemToArray(items, cJSON_CreateString("on"));
cJSON_AddItemToArray(items, cJSON_CreateString("off"));
cJSON_AddItemToArray(index, cJSON_CreateNumber(0));
cJSON_AddItemToArray(index, cJSON_CreateNumber(1));
return;
}
}
}
}
void SixFrameHandle::RequestGetCapability(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetCapability.\n");
AppGetCapability param;
mAppMonitor->GetCapability(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetCapability(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetCapability(cJSON *result, const AppGetCapability &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
std::ostringstream value;
value << static_cast<int>(param.mGpsCapability);
value << static_cast<int>(param.mDeviceType);
value << static_cast<int>(param.mAlbum);
value << static_cast<int>(param.mAppLock);
value << static_cast<int>(param.mDeleteLock);
value << static_cast<int>(param.mDeviceMode);
value << static_cast<int>(param.mPlaybackType);
value << static_cast<int>(param.mPhotographCapability);
value << static_cast<int>(param.mWifiCapability);
value << static_cast<int>(param.mFileCopy);
cJSON_AddStringToObject(info, "value", value.str().c_str());
}
void SixFrameHandle::RequestGetLockVideoStatus(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetLockVideoStatus.\n");
LockVideoStatus param;
mAppMonitor->GetLockVideoStatus(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetLockVideoStatus(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetLockVideoStatus(cJSON *result, const LockVideoStatus &param)
{
cJSON *info = nullptr;
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info = cJSON_CreateObject());
cJSON_AddNumberToObject(info, "status", static_cast<int>(param));
}
void SixFrameHandle::RequestGetStorageInfo(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetStorageInfo.\n");
std::vector<AppGetStorageInfo> param;
mAppMonitor->GetStorageInfo(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetStorageInfo(result, param);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetStorageInfo(cJSON *result, const std::vector<AppGetStorageInfo> &param)
{
cJSON *info = cJSON_CreateArray();
if (nullptr == info) {
LogError("cJSON_CreateArray failed.\n");
return;
}
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info);
for (const auto &storageInfo : param) {
cJSON *storage = nullptr;
storage = cJSON_CreateObject();
if (nullptr != storage) {
cJSON_AddItemToArray(info, storage);
cJSON_AddNumberToObject(storage, "index", static_cast<int>(storageInfo.mIndex));
cJSON_AddStringToObject(storage, "name", storageInfo.mName.c_str());
cJSON_AddNumberToObject(storage, "type", static_cast<int>(storageInfo.mType));
cJSON_AddNumberToObject(storage, "free", storageInfo.mFree);
cJSON_AddNumberToObject(storage, "total", storageInfo.mTotal);
}
}
}
AppGetFileInfo inline SixFrameHandle::RequestGetFileListParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<AppGetFileInfo>> parseImpl =
std::dynamic_pointer_cast<ParseUrl<AppGetFileInfo>>(parse);
if ("folder" == key) {
if ("loop" == value) {
parseImpl->mData.mEvent = StorageFileEvent::LOOP;
}
if ("emr" == value) {
parseImpl->mData.mEvent = StorageFileEvent::EMR;
}
if ("event" == value) {
parseImpl->mData.mEvent = StorageFileEvent::EVENT;
}
if ("park" == value) {
parseImpl->mData.mEvent = StorageFileEvent::PARK;
}
}
if ("start" == key) {
parseImpl->mData.mStartIndex = std::stoi(value);
}
if ("end" == key) {
parseImpl->mData.mStopIndex = std::stoi(value);
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<AppGetFileInfo>>();
std::shared_ptr<ParseUrl<AppGetFileInfo>> parseImpl = std::dynamic_pointer_cast<ParseUrl<AppGetFileInfo>>(parse);
ExtractParamsFromUrl(url, parseFunc, parse);
return parseImpl->mData;
}
void SixFrameHandle::RequestGetFileList(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetFileList.\n");
std::vector<AppGetFileList> files;
AppGetFileInfo info = RequestGetFileListParse(url);
mAppMonitor->GetStorageFileList(info, files);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseGetFileList(result, files, info);
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void inline SixFrameHandle::ResponseGetFileList(cJSON *result, const std::vector<AppGetFileList> &param,
const AppGetFileInfo &fileInfo)
{
cJSON *info = cJSON_CreateArray();
cJSON *folder = cJSON_CreateObject();
cJSON *files = cJSON_CreateArray();
if (nullptr == info || nullptr == folder || nullptr == files) {
LogError("cJSON_CreateArray failed.\n");
return;
}
cJSON_AddItemToObject(result, CJSON_INFO_STRING, info);
cJSON_AddItemToArray(info, folder);
for (const auto &fileList : param) {
cJSON_AddStringToObject(folder, "folder", PrintfFileEvent(fileInfo));
cJSON_AddNumberToObject(folder, "count", param.size());
cJSON *file = nullptr;
file = cJSON_CreateObject();
if (nullptr != file) {
cJSON_AddItemToArray(files, file);
cJSON_AddStringToObject(file, "name", fileList.mName.c_str());
cJSON_AddNumberToObject(file, "duration", fileList.mDuration);
cJSON_AddNumberToObject(file, "size", fileList.mSize);
cJSON_AddNumberToObject(file, "createtime", fileList.mCreateTime_s);
cJSON_AddNumberToObject(file, "type", static_cast<int>(fileList.mType));
}
}
cJSON_AddItemToObject(folder, CJSON_FILES_STRING, files);
}
AppSetDateTime inline SixFrameHandle::RequestSetDateTimeParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
if ("date" == key) {
parseImpl->mData = value;
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<std::string>>();
ExtractParamsFromUrl(url, parseFunc, parse);
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
if (14 != parseImpl->mData.length()) {
LogError("date parse failed.\n");
return AppSetDateTime(0, 0, 0, 0, 0, 0);
}
std::string yearStr = parseImpl->mData.substr(0, 4);
std::string monthStr = parseImpl->mData.substr(4, 2);
std::string dayStr = parseImpl->mData.substr(6, 2);
std::string hourStr = parseImpl->mData.substr(8, 2);
std::string minuteStr = parseImpl->mData.substr(10, 2);
std::string secondStr = parseImpl->mData.substr(12, 2);
unsigned int year = std::stoi(yearStr);
unsigned int month = std::stoi(monthStr);
unsigned int day = std::stoi(dayStr);
unsigned int hour = std::stoi(hourStr);
unsigned int minute = std::stoi(minuteStr);
unsigned int second = std::stoi(secondStr);
return AppSetDateTime(year, month, day, hour, minute, second);
}
void SixFrameHandle::RequestSetDateTime(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetDeviceAttr.\n");
AppSetDateTime param = RequestSetDateTimeParse(url);
mAppMonitor->SetDateTime(param);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
int inline SixFrameHandle::RequestSetTimeZoneParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
if ("timezone" == key) {
parseImpl->mData = value;
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<std::string>>();
ExtractParamsFromUrl(url, parseFunc, parse);
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
if (2 <= parseImpl->mData.length()) {
LogError("date parse failed.\n");
return 0;
}
int zone = std::stoi(parseImpl->mData);
return zone;
}
void SixFrameHandle::RequestSetTimeZone(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestSetTimeZone.\n");
int zone = RequestSetTimeZoneParse(url);
mAppMonitor->SetTimeZone(zone);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
AppSetParamValue inline SixFrameHandle::RequestSetParamValueParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<AppSetParamValue>> parseImpl =
std::dynamic_pointer_cast<ParseUrl<AppSetParamValue>>(parse);
if ("switchcam" == key) {
parseImpl->mData.mName = "switchcam";
parseImpl->mData.mValue = std::stoi(value);
}
if ("rec" == key) {
parseImpl->mData.mName = "rec";
parseImpl->mData.mValue = std::stoi(value);
}
if ("mic" == key) {
parseImpl->mData.mName = "mic";
parseImpl->mData.mValue = std::stoi(value);
}
};
AppSetParamValue value;
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<AppSetParamValue>>();
ExtractParamsFromUrl(url, parseFunc, parse);
std::shared_ptr<ParseUrl<AppSetParamValue>> parseImpl =
std::dynamic_pointer_cast<ParseUrl<AppSetParamValue>>(parse);
return parseImpl->mData;
}
void SixFrameHandle::RequestSetParamValue(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestSetParamValue.\n");
AppSetParamValue value = RequestSetParamValueParse(url);
mAppMonitor->SetParamValue(value);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
PlayBackEvent inline SixFrameHandle::RequestPlaybackParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<PlayBackEvent>> parseImpl = std::dynamic_pointer_cast<ParseUrl<PlayBackEvent>>(parse);
if ("param" == key) {
if ("enter" == value) {
parseImpl->mData = PlayBackEvent::START;
}
if ("exit" == value) {
parseImpl->mData = PlayBackEvent::STOP;
}
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<PlayBackEvent>>();
std::shared_ptr<ParseUrl<PlayBackEvent>> parseImpl = std::dynamic_pointer_cast<ParseUrl<PlayBackEvent>>(parse);
parseImpl->mData = PlayBackEvent::END;
ExtractParamsFromUrl(url, parseFunc, parse);
return parseImpl->mData;
}
void SixFrameHandle::RequestPlayback(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestPlayback.\n");
PlayBackEvent value = RequestPlaybackParse(url);
mAppMonitor->AppPlayback(value);
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void SixFrameHandle::RequestEnterRecorder(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestEnterRecorder.\n");
mAppMonitor->EnterRecorder();
cJSON *result = MakeResponseResult(ResposeResult::SUCCESSFUL, SET_REQUEST_RESPONSE);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
void SixFrameHandle::RequestUpload(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestUpload.\n");
AppUploadFile info("path", UploadCommand::UPGRADE_CPU);
mAppMonitor->UploadFile(info);
cJSON *result = MakeResponseResult(info.mResult);
if (nullptr == result) {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
return;
}
ResponseJsonString(result, responseHandle, context);
cJSON_Delete(result);
}
std::string inline SixFrameHandle::RequestGetThumbnailParse(const std::string &url)
{
auto parseFunc = [](const std::string &key, const std::string &value, std::shared_ptr<VParseUrl> &parse) {
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
if ("file" == key) {
parseImpl->mData = value;
}
};
std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<std::string>>();
std::shared_ptr<ParseUrl<std::string>> parseImpl = std::dynamic_pointer_cast<ParseUrl<std::string>>(parse);
ExtractParamsFromUrl(url, parseFunc, parse);
return parseImpl->mData;
}
void SixFrameHandle::RequestGetThumbnail(const std::string &url, ResponseHandle responseHandle, void *context)
{
LogInfo("RequestGetThumbnail.\n");
const std::string file = RequestGetThumbnailParse(url);
AppGetThumbnail respon(file);
mAppMonitor->GetThumbnail(respon);
// respon.mThumbnail = "./34a396526922a33e97906920dbfef2a5.jpg";
responseHandle(respon.mThumbnail.c_str(), context);
}
void SixFrameHandle::RequestTcpHandle2(const std::string command, const cJSON *const data,
ResponseHandle responseHandle, void *context)
{
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetRecordingStatus(const RecordingStatus &status)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "rec");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "value", static_cast<int>(status));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetMicrophoneStatus(const MicrophoneStatus &status)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "mic");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "value", static_cast<int>(status));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetBatteryStatus(const BatteryStatus &status,
const int &batteryCapacity)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "battery");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "charge", static_cast<int>(status));
cJSON_AddNumberToObject(info, "capacity", batteryCapacity);
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::SetSdCardStatus(const SdCardStatus &status)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "sd");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddNumberToObject(info, "status", static_cast<int>(status));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::DeletedFileMessage(const std::string &file, const StorageFileType &type)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "file_del");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddStringToObject(info, "name", file.c_str());
cJSON_AddNumberToObject(info, "type", static_cast<int>(type));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::CreatedFileMessage(const std::string &file, const StorageFileType &type)
{
cJSON *resultCJSON = nullptr;
CjsonAutoFree autoJson(resultCJSON = cJSON_CreateObject());
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
return std::make_shared<ProtocolPacket>();
}
cJSON_AddStringToObject(resultCJSON, TCP_RESULT_MSGID, "file_add");
cJSON *info = cJSON_CreateObject();
if (nullptr != info) {
cJSON_AddStringToObject(info, "name", file.c_str());
cJSON_AddNumberToObject(info, "type", static_cast<int>(type));
cJSON_AddItemToObject(resultCJSON, CJSON_INFO_STRING, info);
}
AddTimestamp(resultCJSON);
return MakePacket(resultCJSON);
}
cJSON *SixFrameHandle::MakeResponseResult(const ResposeResult result, const bool requestSet)
{
const char *RESPONSE_RESULT = "result";
cJSON *resultCJSON = cJSON_CreateObject();
if (nullptr == resultCJSON) {
LogError("cJSON_CreateObject failed.\n");
goto END;
}
cJSON_AddNumberToObject(resultCJSON, RESPONSE_RESULT, static_cast<int>(result));
if (false == requestSet) {
goto END;
}
if (ResposeResult::SUCCESSFUL == result) {
cJSON_AddStringToObject(resultCJSON, CJSON_INFO_STRING, "set success.");
}
else {
cJSON_AddStringToObject(resultCJSON, CJSON_INFO_STRING, "set failed."); // TODO: what the failed string is?
}
END:
return resultCJSON;
}
void SixFrameHandle::ResponseJsonString(cJSON *json, ResponseHandle responseHandle, void *context)
{
char *resultStr = nullptr;
resultStr = cJSON_Print(json);
if (nullptr != resultStr) {
LogInfo("ResponseJsonString:\n %s\n", resultStr);
responseHandle(resultStr, context);
free(resultStr);
}
else {
LogError("MakeResponseResult failed.\n");
responseHandle("Device run out of memory.", context);
}
}
const char *SixFrameHandle::PrintfFileEvent(const AppGetFileInfo &fileInfo)
{
switch (fileInfo.mEvent) {
case StorageFileEvent::LOOP: {
return "loop";
break;
}
default: {
LogWarning("Unknown event.\n");
break;
}
}
return "unknown event";
}
void SixFrameHandle::AddTimestamp(cJSON *json)
{
time_t current_time;
current_time = time(nullptr);
if (current_time == -1) {
LogError("Error getting the time\n");
constexpr long TIME_ERROR = -1;
cJSON_AddNumberToObject(json, "time", TIME_ERROR);
return;
}
cJSON_AddNumberToObject(json, "time", current_time);
}
std::shared_ptr<ProtocolPacket> SixFrameHandle::MakePacket(const cJSON *json)
{
char *resultStr = nullptr;
resultStr = cJSON_Print(json);
if (nullptr != resultStr) {
return std::make_shared<ProtocolPacket>(resultStr, strlen(resultStr));
}
else {
LogError("MakePacket failed.\n");
return std::make_shared<ProtocolPacket>();
}
}
void SixFrameHandle::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor)
{
if (monitor) {
mAppMonitor = monitor;
}
else {
LogError("SetAppMonitor failed.\n");
}
}