#include "WSUser.h" #include "WSServer.h" #include "ManagerV2.h" #include "base/HGInfo.h" #include "base/HGUtility.h" #include "sha1.h" #include "base64.h" #include "HGString.h" namespace ver_2 { static int GetJsonIntValue(cJSON* json, const std::string& key, bool *find = NULL) { int ret = 0; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_Number) { ret = p->valueint; if (NULL != find) *find = true; } break; } p = p->next; } return ret; } static double GetJsonDoubleValue(cJSON* json, const std::string& key, bool* find = NULL) { double ret = 0.0; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_Number) { ret = p->valuedouble; if (NULL != find) *find = true; } break; } p = p->next; } return ret; } static bool GetJsonBoolValue(cJSON* json, const std::string& key, bool* find = NULL) { bool ret = false; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_True || p->type == cJSON_False) { ret = (p->type == cJSON_True) ? true : false; if (NULL != find) *find = true; } break; } p = p->next; } return ret; } static std::string GetJsonStringValue(cJSON* json, const std::string& key, bool* find = NULL) { std::string ret; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_String) { ret = p->valuestring; if (NULL != find) *find = true; } break; } p = p->next; } return ret; } static std::vector GetJsonStringListValue(cJSON* json, const std::string& key, bool* find = NULL) { std::vector ret; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_Array) { cJSON* pEx = p->child; while (NULL != pEx) { if (pEx->type == cJSON_String) ret.push_back(pEx->valuestring); pEx = pEx->next; } if (NULL != find) *find = true; } break; } p = p->next; } return ret; } static std::vector GetJsonIntListValue(cJSON* json, const std::string& key, bool* find = NULL) { std::vector ret; if (NULL != find) *find = false; cJSON* p = json->child; while (NULL != p) { if (0 == strcmp(p->string, key.c_str())) { if (p->type == cJSON_Array) { cJSON* pEx = p->child; while (NULL != pEx) { if (pEx->type == cJSON_Number) ret.push_back(pEx->valueint); pEx = pEx->next; } if (NULL != find) *find = true; } break; } p = p->next; } return ret; } #if defined(HG_CMP_MSC) WSUser::WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn) #else WSUser::WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn) #endif : WebUser(server, id, ip, port, sockConn) { m_initDeviceIden.clear(); m_initDeviceHaveIden = false; m_startScanIden.clear(); m_startScanHaveIden = false; m_startScanIsGetBase64 = false; GetManager()->SetSaneEvent(SaneEvent2, this); GetManager()->SetSaneImageCallback(SaneImageCallback2, this); } WSUser::~WSUser() { std::string errInfo; GetManager()->DeinitDevice(errInfo); GetManager()->ResetSaneImageCallback(); GetManager()->ResetSaneEvent(); } ManagerV2* WSUser::GetManager() { return ((WSServer*)m_server)->GetManger(); } void WSUser::HandleCmd(const WSCmdParam* param) { assert(NULL != param && m_id == param->usrId); std::string cmdData((const char*)param->data, param->size); cJSON* json = cJSON_Parse(cmdData.c_str()); if (NULL != json) { std::string func = GetJsonStringValue(json, "func"); std::string iden = GetJsonStringValue(json, "iden"); if ("set_global_config" == func) { SetGlobalConfig(json); } else if ("get_global_config" == func) { GetGlobalConfig(json); } else if ("load_local_image" == func) { LoadLocalImage(json); } else if ("save_local_image" == func) { SaveLocalImage(json); } else if ("delete_local_file" == func) { DeleteLocalFile(json); } else if ("clear_global_file_save_path" == func) { ClearGlobalFileSavePath(json); } else if ("merge_local_image" == func) { MergeLocalImage(json); } else if ("local_make_multi_image" == func) { LocalMakeMultiImage(json); } else if ("split_local_image" == func) { SplitLocalImage(json); } else if ("local_make_zip_file" == func) { LocalMakeZipFile(json); } else if ("local_image_deskew" == func) { LocalImageDeskew(json); } else if ("local_image_add_watermark" == func) { LocalImageAddWatermark(json); } else if ("upload_local_file" == func) { UploadLocalFile(json); } else if ("init_device" == func) { InitDevice(json); } else if ("deinit_device" == func) { DeinitDevice(json); } else if ("get_device_name_list" == func) { GetDeviceNameList(json); } else if ("open_device" == func) { OpenDevice(json); } else if ("close_device" == func) { CloseDevice(json); } else if ("set_device_param" == func) { SetDeviceParam(json); } else if ("get_device_param" == func) { GetDeviceParam(json); } else if ("reset_device_param" == func) { ResetDeviceParam(json); } else if ("get_curr_device_name" == func) { GetCurrDeviceName(json); } else if ("start_scan" == func) { StartScan(json); } else if ("stop_scan" == func) { StopScan(json); } else if ("get_batch_id_list" == func) { GetBatchIdList(json); } else if ("open_batch" == func) { OpenBatch(json); } else if ("delete_batch" == func) { DeleteBatch(json); } else if ("new_batch" == func) { NewBatch(json); } else if ("get_curr_batch_id" == func) { GetCurrBatchId(json); } else if ("modify_batch_id" == func) { ModifyBatchId(json); } else if ("bind_folder" == func) { BindFolder(json); } else if ("stop_bind_folder" == func) { StopBindFolder(json); } else if ("get_image_thumbnail_list" == func) { GetImageThumbnailList(json); } else if ("get_image_count" == func) { GetImageCount(json); } else if ("load_image" == func) { LoadImage(json); } else if ("save_image" == func) { SaveImage(json); } else if ("insert_local_image" == func) { InsertLocalImage(json); } else if ("insert_image" == func) { InsertImage(json); } else if ("modify_image_tag" == func) { ModifyImageTag(json); } else if ("delete_image" == func) { DeleteImage(json); } else if ("clear_image_list" == func) { ClearImageList(json); } else if ("modify_image" == func) { ModifyImage(json); } else if ("modify_image_by_local" == func) { ModifyImageByLocal(json); } else if ("move_image" == func) { MoveImage(json); } else if ("exchange_image" == func) { ExchangeImage(json); } else if ("image_book_sort" == func) { ImageBookSort(json); } else if ("merge_image" == func) { MergeImage(json); } else if ("make_multi_image" == func) { MakeMultiImage(json); } else if ("split_image" == func) { SplitImage(json); } else if ("make_zip_file" == func) { MakeZipFile(json); } else if ("image_deskew" == func) { ImageDeskew(json); } else if ("image_add_watermark" == func) { ImageAddWatermark(json); } cJSON_Delete(json); } } void WSUser::HandleEvent(const WSEvtParam* param) { assert(NULL != param && m_id == param->usrId); SendResponse(param->data, param->size, HGTRUE); } void WSUser::PostCmdMsg(const HGByte* data, HGUInt dataSize) { WSCmdParam* param = new WSCmdParam; param->svr = (WSServer*)m_server; param->usrId = m_id; param->data = new HGByte[dataSize]; param->size = dataSize; memcpy(param->data, data, dataSize); HGMsg msg; msg.id = MSGID_WS_COMMAND; msg.data = param; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) { delete[] param->data; param->size = 0; delete param; } } void WSUser::PostEventMsg(const HGByte* data, HGUInt dataSize) { WSEvtParam* param = new WSEvtParam; param->svr = (WSServer*)m_server; param->usrId = m_id; param->data = new HGByte[dataSize]; param->size = dataSize; memcpy(param->data, data, dataSize); HGMsg msg; msg.id = MSGID_WS_EVENT; msg.data = param; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) { delete[] param->data; param->size = 0; delete param; } } void WSUser::ThreadFunc() { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WSUser::ThreadFunc"); char chBuffer[2048]; const char* pBuffer = chBuffer; int nBufferSize = 0; bool bConnect = false; unsigned char connectDataTail[4] = { '\r', '\n', '\r', '\n' }; unsigned int connectDataTailLen = 0; std::string connectData; uint8_t* pData = NULL; int nDataSize = 0; uint8_t* pDataEx = NULL; int nRemainSize = 0; uint8_t headData[20]; uint32_t nHeadDataLen = 0; uint8_t vMask[4]; uint32_t nMaskCount = 0; bool bHandle = false; std::vector vAllData; while (1) { if (0 == nBufferSize) { int len = recv(m_sockConn, chBuffer, 2048, 0); if (len <= 0) { // 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开 PostDisConnectMsg(); break; } else { pBuffer = chBuffer; nBufferSize = len; } } assert(nBufferSize > 0); if (!bConnect) { unsigned char b = *pBuffer; ++pBuffer; --nBufferSize; connectData.push_back(b); if (b == connectDataTail[connectDataTailLen]) { ++connectDataTailLen; } else { connectDataTailLen = 0; if (b == connectDataTail[connectDataTailLen]) { ++connectDataTailLen; } } if (4 == connectDataTailLen) { connectDataTailLen = 0; bool shakeRet = ShakeHand(connectData); connectData.clear(); if (!shakeRet) { PostDisConnectMsg(); break; } bConnect = true; } } else { if (NULL == pData) { assert(0 == nDataSize); uint8_t b = *pBuffer; ++pBuffer; --nBufferSize; headData[nHeadDataLen] = b; ++nHeadDataLen; if (1 == nHeadDataLen) { if ((0x80 | 0x08) == headData[0]) // 断开连接 { PostDisConnectMsg(); break; } else if ((0x80 | 0x09) == headData[0]) // PING帧 { // } else if ((0x80 | 0x0A) == headData[0]) // PONG帧 { // } else if ((0x00 | 0x01) == headData[0] || (0x00 | 0x02) == headData[0] || (0x00 | 0x00) == headData[0] || (0x80 | 0x00) == headData[0] || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) // 数据帧 { if ((0x80 | 0x00) == headData[0] || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) { // 分片结束 bHandle = true; } else { // 分片帧 bHandle = false; } } else // 帧错误,断开连接 { PostDisConnectMsg(); break; } } else if (2 == nHeadDataLen) { if (0 == (headData[1] & 0x80)) // 必须经过掩码处理 { PostDisConnectMsg(); break; } if ((0x80 | 0x09) == headData[0]) // PING帧 { if (0x80 != headData[1]) { PostDisConnectMsg(); break; } } else if ((0x80 | 0x0A) == headData[0]) // PONG帧 { if (0x80 != headData[1]) { PostDisConnectMsg(); break; } } else { if ((headData[1] & 0x7F) <= 125) { uint32_t nCmdSize = (headData[1] & 0x7F); nHeadDataLen = 0; if (0 == nCmdSize) { PostDisConnectMsg(); break; } nDataSize = nCmdSize; nRemainSize = nCmdSize; pData = new uint8_t[nDataSize]; pDataEx = pData; } } } else if (4 == nHeadDataLen) { if ((0x80 | 0x09) == headData[0]) // PING帧 { // } else if ((0x80 | 0x0A) == headData[0]) // PONG帧 { // } else { if ((headData[1] & 0x7F) == 126) { uint32_t nCmdSize = ntohs(*(uint16_t*)&headData[2]); nHeadDataLen = 0; if (0 == nCmdSize) { PostDisConnectMsg(); break; } nDataSize = nCmdSize; nRemainSize = nCmdSize; pData = new uint8_t[nDataSize]; pDataEx = pData; } } } else if (6 == nHeadDataLen) { if ((0x80 | 0x09) == headData[0]) // PING帧 { nHeadDataLen = 0; Pong(); } else if ((0x80 | 0x0A) == headData[0]) // PONG帧 { nHeadDataLen = 0; } } else if (10 == nHeadDataLen) { if ((headData[1] & 0x7F) == 127) // 这里一定会等于127 { uint32_t nCmdSizeHigh = ntohl(*(uint32_t*)&headData[2]); uint32_t nCmdSize = ntohl(*(uint32_t*)&headData[6]); nHeadDataLen = 0; if ((0 != nCmdSizeHigh) || (0 == nCmdSize)) { PostDisConnectMsg(); break; } nDataSize = nCmdSize; nRemainSize = nCmdSize; pData = new uint8_t[nDataSize]; pDataEx = pData; } } } else { if (4 != nMaskCount) { uint8_t b = *pBuffer; ++pBuffer; --nBufferSize; vMask[nMaskCount] = b; ++nMaskCount; } else { int nWriteSize = HGMIN(nBufferSize, nRemainSize); memcpy(pDataEx, pBuffer, nWriteSize); pBuffer += nWriteSize; nBufferSize -= nWriteSize; pDataEx += nWriteSize; nRemainSize -= nWriteSize; if (0 == nRemainSize) { assert(pDataEx == pData + nDataSize); for (int i = 0; i < nDataSize; ++i) { int j = i % 4; pData[i] = pData[i] ^ vMask[j]; vAllData.push_back(pData[i]); } delete[] pData; pData = NULL; nDataSize = 0; nMaskCount = 0; if (bHandle) { PostCmdMsg(&vAllData[0], (HGUInt)vAllData.size()); bHandle = false; vAllData.clear(); } } } } } } if (NULL != pData) { delete[] pData; pData = NULL; nDataSize = 0; nMaskCount = 0; } } void WSUser::SaneEvent2(int code, const char* str, bool err, void* param) { WSUser* p = (WSUser*)param; if (code == SANEEVENT_ARRIVE) { std::string fmt; fmt += "{\"func\":\"%s\", "; if (p->m_initDeviceHaveIden) fmt += "\"iden\":\"%s\", "; fmt += "\"device_name\":\"%s\"}"; char resp[1024]; if (p->m_initDeviceHaveIden) sprintf(resp, fmt.c_str(), "device_arrive", p->m_initDeviceIden.c_str(), str); else sprintf(resp, fmt.c_str(), "device_arrive", str); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); } else if (code == SANEEVENT_REMOVE) { std::string fmt; fmt += "{\"func\":\"%s\", "; if (p->m_initDeviceHaveIden) fmt += "\"iden\":\"%s\", "; fmt += "\"device_name\":\"%s\"}"; char resp[1024]; if (p->m_initDeviceHaveIden) sprintf(resp, fmt.c_str(), "device_remove", p->m_initDeviceIden.c_str(), str); else sprintf(resp, fmt.c_str(), "device_remove", str); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); } else if (code == SANEEVENT_WORKING) { char resp[1024]; if (p->m_startScanHaveIden) sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\"}", "scan_begin", p->m_startScanIden.c_str()); else sprintf(resp, "{\"func\":\"%s\"}", "scan_begin"); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); if (p->m_startScanHaveIden) sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\", \"is_error\":%s, \"info\":\"%s\"}", "scan_info", p->m_startScanIden.c_str(), err ? "true" : "false", str); else sprintf(resp, "{\"func\":\"%s\", \"is_error\":%s, \"info\":\"%s\"}", "scan_info", err ? "true" : "false", str); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); } else if (code == SANEEVENT_FINISH) { char resp[1024]; if (p->m_startScanHaveIden) sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\", \"is_error\":%s, \"info\":\"%s\"}", "scan_info", p->m_startScanIden.c_str(), err ? "true" : "false", str); else sprintf(resp, "{\"func\":\"%s\", \"is_error\":%s, \"info\":\"%s\"}", "scan_info", err ? "true" : "false", str); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); if (p->m_startScanHaveIden) sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\"}", "scan_end", p->m_startScanIden.c_str()); else sprintf(resp, "{\"func\":\"%s\"}", "scan_end"); p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); } } void WSUser::SaneImageCallback2(const char* path, void* param) { WSUser* p = (WSUser*)param; std::string base64; if (p->m_startScanIsGetBase64) { std::string errInfo; p->GetManager()->LoadLocalImage(path, base64, errInfo); } char* resp = new char[1024 + base64.size()]; if (p->m_startScanHaveIden) { if (p->m_startScanIsGetBase64) sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\", \"image_path\":\"%s\", \"image_base64\":\"%s\"}", "scan_image", p->m_startScanIden.c_str(), StdStringToUtf8(strToJson(path)).c_str(), base64.c_str()); else sprintf(resp, "{\"func\":\"%s\", \"iden\":\"%s\", \"image_path\":\"%s\"}", "scan_image", p->m_startScanIden.c_str(), StdStringToUtf8(strToJson(path)).c_str()); } else { if (p->m_startScanIsGetBase64) sprintf(resp, "{\"func\":\"%s\", \"image_path\":\"%s\", \"image_base64\":\"%s\"}", "scan_image", StdStringToUtf8(strToJson(path)).c_str(), base64.c_str()); else sprintf(resp, "{\"func\":\"%s\", \"image_path\":\"%s\"}", "scan_image", StdStringToUtf8(strToJson(path)).c_str()); } p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); delete[] resp; } std::string WSUser::strToJson(const std::string str) { std::string ret; for (size_t i = 0; i < str.size(); i++) { char c = str[i]; switch (c) { case '\"': ret.append("\\\""); break; case '\\': ret.append("\\\\"); break; case '\b': ret.append("\\b"); break; case '\f': ret.append("\\f"); break; case '\n': ret.append("\\n"); break; case '\r': ret.append("\\r"); break; case '\t': ret.append("\\t"); break; default: ret.push_back(c); } } return ret; } bool WSUser::ShakeHand(const std::string& head) { std::string requestMethod; std::string requestURIPath; HttpPairs requestURIQueryInfos; std::string requestURIFragment; std::string httpVersion; HttpPairs headInfos; HttpHead::AnalysisHead(head, requestMethod, requestURIPath, requestURIQueryInfos, requestURIFragment, httpVersion, headInfos); if (std::string::npos == HttpHead::GetValue(headInfos, "Connection").find("Upgrade")) return false; if ("websocket" != HttpHead::GetValue(headInfos, "Upgrade")) return false; std::string key = HttpHead::GetValue(headInfos, "Sec-WebSocket-Key"); if (key.empty()) return false; key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; unsigned int message_digest[5]; SHA1 sha; sha.Reset(); sha << key.c_str(); sha.Result(message_digest); for (int i = 0; i < 5; ++i) message_digest[i] = htonl(message_digest[i]); std::string serverKey = base64_encode((const unsigned char*)message_digest, 20); std::string handShakeResp = "HTTP/1.1 101 Switching Protocols\r\n"; handShakeResp += "Upgrade: websocket\r\n"; handShakeResp += "Connection: Upgrade\r\n"; handShakeResp += "Sec-WebSocket-Accept:"; handShakeResp += serverKey; handShakeResp += "\r\n\r\n"; send(m_sockConn, handShakeResp.c_str(), (int)handShakeResp.length(), 0); return true; } void WSUser::Pong() { uint8_t vHead[2]; vHead[0] = 0x80 | 0x0A; vHead[1] = 0; HGBase_EnterLock(m_cs); send(m_sockConn, (const char*)vHead, 2, 0); HGBase_LeaveLock(m_cs); } bool WSUser::SendResponse(const HGByte* data, HGUInt size, HGBool text) { if (NULL == data || 0 == size) { return false; } uint32_t nHeadLen = 0; uint8_t vHead[20] = { 0 }; vHead[0] = text ? (0x80 | 0x01) : (0x80 | 0x02); if (size <= 125) { vHead[1] = (uint8_t)size; nHeadLen = 2; } else if (size <= 0xFFFF) { vHead[1] = 126; uint16_t payloadLength16b = htons((uint16_t)size); memcpy(&vHead[2], &payloadLength16b, 2); nHeadLen = 4; } else { vHead[1] = 127; vHead[2] = 0; vHead[3] = 0; vHead[4] = 0; vHead[5] = 0; uint32_t payloadLength32b = htonl(size); memcpy(&vHead[6], &payloadLength32b, 4); nHeadLen = 10; } HGBase_EnterLock(m_cs); send(m_sockConn, (const char*)vHead, nHeadLen, 0); send(m_sockConn, (const char*)data, size, 0); HGBase_LeaveLock(m_cs); return true; } void WSUser::SetGlobalConfig(cJSON* json) { assert(NULL != json); GlobalConfig cfg; HGUInt mask = 0; bool find = false; cfg.fileSavePath = Utf8ToStdString(GetJsonStringValue(json, "file_save_path", &find)); if (find) mask |= GlobalConfig::fileSavePathMask; cfg.fileNamePrefix = Utf8ToStdString(GetJsonStringValue(json, "file_name_prefix", &find)); if (find) mask |= GlobalConfig::fileNamePrefixMask; cfg.fileNameMode = GetJsonStringValue(json, "file_name_mode", &find); if (find) mask |= GlobalConfig::fileNameModeMask; cfg.imageFormat = GetJsonStringValue(json, "image_format", &find); if (find) mask |= GlobalConfig::imageFormatMask; cfg.imageJpegQuality = GetJsonIntValue(json, "image_jpeg_quality", &find); if (find) mask |= GlobalConfig::imageJpegQualityMask; cfg.imageTiffCompression = GetJsonStringValue(json, "image_tiff_compression", &find); if (find) mask |= GlobalConfig::imageTiffCompressionMask; cfg.imageTiffJpegQuality = GetJsonIntValue(json, "image_tiff_jpeg_quality", &find); if (find) mask |= GlobalConfig::imageTiffJpegQualityMask; std::string errInfo; int ret = GetManager()->SetGlobalConfig(cfg, mask, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = {0}; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "set_global_config", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "set_global_config", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "set_global_config", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "set_global_config", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetGlobalConfig(cJSON* json) { assert(NULL != json); GlobalConfig cfg; std::string errInfo; int ret = GetManager()->GetGlobalConfig(cfg, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"file_save_path\":\"%s\", "; fmt += "\"file_name_prefix\":\"%s\", "; fmt += "\"file_name_mode\":\"%s\", "; fmt += "\"image_format\":\"%s\", "; fmt += "\"image_jpeg_quality\":%d, "; fmt += "\"image_tiff_compression\":\"%s\", "; fmt += "\"image_tiff_jpeg_quality\":%d}"; } char resp[2048] = {0}; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_global_config", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_global_config", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_global_config", iden.c_str(), ret, StdStringToUtf8(strToJson(cfg.fileSavePath)).c_str(), StdStringToUtf8(strToJson(cfg.fileNamePrefix)).c_str(), cfg.fileNameMode.c_str(), cfg.imageFormat.c_str(), cfg.imageJpegQuality, cfg.imageTiffCompression.c_str(), cfg.imageTiffJpegQuality); else sprintf(resp, fmt.c_str(), "get_global_config", ret, StdStringToUtf8(strToJson(cfg.fileSavePath)).c_str(), StdStringToUtf8(strToJson(cfg.fileNamePrefix)).c_str(), cfg.fileNameMode.c_str(), cfg.imageFormat.c_str(), cfg.imageJpegQuality, cfg.imageTiffCompression.c_str(), cfg.imageTiffJpegQuality); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::LoadLocalImage(cJSON* json) { assert(NULL != json); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); std::string imageBase64; std::string errInfo; int ret = GetManager()->LoadLocalImage(imagePath, imageBase64, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_base64\":\"%s\"}"; } char* resp = new char[1024 + imageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "load_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "load_local_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "load_local_image", iden.c_str(), ret, imageBase64.c_str()); else sprintf(resp, fmt.c_str(), "load_local_image", ret, imageBase64.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::SaveLocalImage(cJSON* json) { assert(NULL != json); std::string imageBase64 = GetJsonStringValue(json, "image_base64"); std::string imagePath; std::string errInfo; int ret = GetManager()->SaveLocalImage(imageBase64, imagePath, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_path\":\"%s\"}"; } char resp[1024] = {0}; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "save_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "save_local_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "save_local_image", iden.c_str(), ret, StdStringToUtf8(strToJson(imagePath)).c_str()); else sprintf(resp, fmt.c_str(), "save_local_image", ret, StdStringToUtf8(strToJson(imagePath)).c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::DeleteLocalFile(cJSON* json) { assert(NULL != json); std::string filePath = Utf8ToStdString(GetJsonStringValue(json, "file_path")); std::string errInfo; int ret = GetManager()->DeleteLocalFile(filePath, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = {0}; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "delete_local_file", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "delete_local_file", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "delete_local_file", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "delete_local_file", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ClearGlobalFileSavePath(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->ClearGlobalFileSavePath(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = {0}; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "clear_global_file_save_path", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "clear_global_file_save_path", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "clear_global_file_save_path", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "clear_global_file_save_path", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::MergeLocalImage(cJSON* json) { assert(NULL != json); std::vector imagePathList = GetJsonStringListValue(json, "image_path_list"); for (int i = 0; i < (int)imagePathList.size(); ++i) imagePathList[i] = Utf8ToStdString(imagePathList[i]); bool find = false; std::string mode = GetJsonStringValue(json, "mode", &find); if (!find) mode = "horz"; std::string align = GetJsonStringValue(json, "align", &find); if (!find) align = "center"; int interval = GetJsonIntValue(json, "interval", &find); if (!find) interval = 0; std::string outImagePath; std::string errInfo; int ret = GetManager()->MergeLocalImage(imagePathList, mode, align, interval, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "merge_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "merge_local_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "merge_local_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "merge_local_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "merge_local_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "merge_local_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::LocalMakeMultiImage(cJSON* json) { assert(NULL != json); std::vector imagePathList = GetJsonStringListValue(json, "image_path_list"); for (int i = 0; i < (int)imagePathList.size(); ++i) imagePathList[i] = Utf8ToStdString(imagePathList[i]); bool find = false; std::string format = GetJsonStringValue(json, "format", &find); if (!find) format = "tif"; std::string tiffCompression = GetJsonStringValue(json, "tiff_compression", &find); if (!find) tiffCompression = "lzw"; int tiffJpegQuality = GetJsonIntValue(json, "tiff_jpeg_quality", &find); if (!find) tiffJpegQuality = 80; std::string outImagePath; std::string errInfo; int ret = GetManager()->LocalMakeMultiImage(imagePathList, format, tiffCompression, tiffJpegQuality, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "local_make_multi_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "local_make_multi_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "local_make_multi_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_make_multi_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "local_make_multi_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_make_multi_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::SplitLocalImage(cJSON* json) { assert(NULL != json); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); bool find = false; std::string mode = GetJsonStringValue(json, "mode", &find); if (!find) mode = "horz"; int location = GetJsonIntValue(json, "location", &find); if (!find) location = 0; std::vector outImagePathList; std::string errInfo; int ret = GetManager()->SplitLocalImage(imagePath, mode, location, outImagePathList, errInfo); std::vector outImageBase64List; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { for (int i = 0; i < (int)outImagePathList.size(); ++i) { std::string outImageBase64; std::string errInfo2; GetManager()->LoadLocalImage(outImagePathList[i], outImageBase64, errInfo2); outImageBase64List.push_back(outImageBase64); } } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path_list\":%s, "; fmt += "\"image_base64_list\":%s}"; } else { fmt += "\"image_path_list\":%s}"; } } std::string pathList = "["; for (int i = 0; i < (int)outImagePathList.size(); ++i) { pathList += "\""; pathList += StdStringToUtf8(strToJson(outImagePathList[i])); pathList += "\""; if (i != (int)outImagePathList.size() - 1) { pathList += ", "; } } pathList += "]"; std::string base64List = "["; for (int i = 0; i < (int)outImageBase64List.size(); ++i) { base64List += "\""; base64List += outImageBase64List[i]; base64List += "\""; if (i != (int)outImageBase64List.size() - 1) { base64List += ", "; } } base64List += "]"; char* resp = new char[1024 + base64List.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "split_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "split_local_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "split_local_image", iden.c_str(), ret, pathList.c_str(), base64List.c_str()); else sprintf(resp, fmt.c_str(), "split_local_image", iden.c_str(), ret, pathList.c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "split_local_image", ret, pathList.c_str(), base64List.c_str()); else sprintf(resp, fmt.c_str(), "split_local_image", ret, pathList.c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::LocalMakeZipFile(cJSON* json) { assert(NULL != json); std::vector filePathList = GetJsonStringListValue(json, "file_path_list"); for (int i = 0; i < (int)filePathList.size(); ++i) filePathList[i] = Utf8ToStdString(filePathList[i]); std::vector nameList; std::string outZipPath; std::string errInfo; int ret = GetManager()->LocalMakeZipFile(filePathList, nameList, outZipPath, errInfo); std::string outZipBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { GetManager()->LoadBase64(outZipPath, outZipBase64); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"zip_path\":\"%s\", "; fmt += "\"zip_base64\":\"%s\"}"; } else { fmt += "\"zip_path\":\"%s\"}"; } } char* resp = new char[1024 + outZipBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "local_make_zip_file", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "local_make_zip_file", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "local_make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str(), outZipBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "local_make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str(), outZipBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::LocalImageDeskew(cJSON* json) { assert(NULL != json); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); std::string outImagePath; std::string errInfo; int ret = GetManager()->LocalImageDeskew(imagePath, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "local_image_deskew", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "local_image_deskew", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "local_image_deskew", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_image_deskew", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "local_image_deskew", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_image_deskew", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::LocalImageAddWatermark(cJSON* json) { assert(NULL != json); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); std::string text = Utf8ToStdString(GetJsonStringValue(json, "text")); bool find; std::string textColor = GetJsonStringValue(json, "text_color", &find); if (!find) textColor = "#000000"; int textOpacity = GetJsonIntValue(json, "text_opacity", &find); if (!find) textOpacity = 255; std::string textPos = GetJsonStringValue(json, "text_pos", &find); if (!find) textPos = "location"; int marginLeft = GetJsonIntValue(json, "margin_left", &find); int marginTop = GetJsonIntValue(json, "margin_top", &find); int marginRight = GetJsonIntValue(json, "margin_right", &find); int marginBottom = GetJsonIntValue(json, "margin_bottom", &find); int locationX = GetJsonIntValue(json, "location_x", &find); int locationY = GetJsonIntValue(json, "location_y", &find); std::string fontName = Utf8ToStdString(GetJsonStringValue(json, "font_name", &find)); if (!find) fontName = "宋体"; int fontSize = GetJsonIntValue(json, "font_size", &find); if (!find) fontSize = 20; bool fontBold = GetJsonBoolValue(json, "font_bold"); bool fontUnderline = GetJsonBoolValue(json, "font_underline"); bool fontItalic = GetJsonBoolValue(json, "font_italic"); bool fontStrikeout = GetJsonBoolValue(json, "font_strikeout"); std::string outImagePath; std::string errInfo; int ret = GetManager()->LocalImageAddWatermark(imagePath, text, textColor, textOpacity, textPos, marginLeft, marginTop, marginRight, marginBottom, locationX, locationY, fontName, fontSize, fontBold, fontUnderline, fontItalic, fontStrikeout, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "local_image_add_watermark", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "local_image_add_watermark", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "local_image_add_watermark", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_image_add_watermark", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "local_image_add_watermark", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "local_image_add_watermark", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::UploadLocalFile(cJSON* json) { assert(NULL != json); std::string filePath = Utf8ToStdString(GetJsonStringValue(json, "file_path")); bool find = false; std::string remoteFilePath = Utf8ToStdString(GetJsonStringValue(json, "remote_file_path", &find)); if (!find) { HGChar fileName[256]; HGBase_GetFileName(filePath.c_str(), fileName, 256); remoteFilePath.clear(); remoteFilePath += "/"; remoteFilePath += fileName; } std::string uploadMode = GetJsonStringValue(json, "upload_mode", &find); if (!find) uploadMode = "http"; std::string httpHost = GetJsonStringValue(json, "http_host", &find); int httpPort = GetJsonIntValue(json, "http_port", &find); if (!find) httpPort = 80; std::string httpPath = GetJsonStringValue(json, "http_path", &find); if (!find) httpPath = "/upload.cgi"; std::string ftpUser = GetJsonStringValue(json, "ftp_user", &find); std::string ftpPassword = GetJsonStringValue(json, "ftp_password", &find); std::string ftpHost = GetJsonStringValue(json, "ftp_host", &find); int ftpPort = GetJsonIntValue(json, "ftp_port", &find); if (!find) ftpPort = 21; std::string errInfo; int ret = GetManager()->UploadLocalFile(filePath, remoteFilePath, uploadMode, httpHost, httpPort, httpPath, ftpUser, ftpPassword, ftpHost, ftpPort, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "upload_local_file", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "upload_local_file", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "upload_local_file", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "upload_local_file", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::InitDevice(cJSON* json) { assert(NULL != json); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); if (!GetManager()->IsDeviceInit()) { m_initDeviceIden = iden; m_initDeviceHaveIden = findIden; } std::string errInfo; int ret = GetManager()->InitDevice(errInfo); if (0 != ret) { m_initDeviceIden.clear(); m_initDeviceHaveIden = false; } std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "init_device", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "init_device", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "init_device", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "init_device", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::DeinitDevice(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->DeinitDevice(errInfo); m_initDeviceIden.clear(); m_initDeviceHaveIden = false; bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "deinit_device", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "deinit_device", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "deinit_device", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "deinit_device", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetDeviceNameList(cJSON* json) { assert(NULL != json); std::vector devNameList; std::string errInfo; int ret = GetManager()->GetDeviceNameList(devNameList, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"device_name_list\":%s}"; } std::string nameList = "["; for (int i = 0; i < (int)devNameList.size(); ++i) { nameList += "\""; nameList += devNameList[i]; nameList += "\""; if (i != (int)devNameList.size() - 1) { nameList += ", "; } } nameList += "]"; char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_device_name_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_device_name_list", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_device_name_list", iden.c_str(), ret, nameList.c_str()); else sprintf(resp, fmt.c_str(), "get_device_name_list", ret, nameList.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::OpenDevice(cJSON* json) { assert(NULL != json); bool find = false; std::string devName = GetJsonStringValue(json, "device_name", &find); if (!find) { std::vector devNameList; std::string errInfo; GetManager()->GetDeviceNameList(devNameList, errInfo); if (!devNameList.empty()) devName = devNameList[0]; } std::string errInfo; int ret = GetManager()->OpenDevice(devName, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "open_device", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "open_device", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "open_device", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "open_device", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::CloseDevice(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->CloseDevice(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "close_device", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "close_device", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "close_device", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "close_device", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::SetDeviceParam(cJSON* json) { assert(NULL != json); std::vector devParams; cJSON* p = json->child; while (NULL != p) { if (0 != strcmp(p->string, "device_param")) { p = p->next; continue; } if (p->type != cJSON_Array) { break; } cJSON* pEx = p->child; while (NULL != pEx) { if (pEx->type != cJSON_Object) { pEx = pEx->next; continue; } DeviceParam devParam; cJSON* pEx2 = pEx->child; while (NULL != pEx2) { if (0 == strcmp(pEx2->string, "name") && pEx2->type == cJSON_String) { devParam.title = pEx2->valuestring; } else if (0 == strcmp(pEx2->string, "value")) { if (pEx2->type == cJSON_String) devParam.stringValue = pEx2->valuestring; else if (pEx2->type == cJSON_Number) { devParam.intValue = pEx2->valueint; devParam.doubleValue = pEx2->valuedouble; } else if (pEx2->type == cJSON_True) devParam.boolValue = true; else if (pEx2->type == cJSON_False) devParam.boolValue = false; } pEx2 = pEx2->next; } devParams.push_back(devParam); pEx = pEx->next; } break; } std::string errInfo; int ret = GetManager()->SetDeviceParam(devParams, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "set_device_param", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "set_device_param", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "set_device_param", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "set_device_param", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetDeviceParam(cJSON* json) { assert(NULL != json); std::vector devParams; std::string errInfo; int ret = GetManager()->GetDeviceParam(devParams, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"device_param\":%s}"; } std::string deviceParams = "["; for (int k = 0; k < (int)devParams.size(); ++k) { std::string groupParams; groupParams += "{\"group_name\":\""; groupParams += devParams[k].groupName; groupParams += "\", \"group_param\":["; for (int i = 0; i < (int)devParams[k].devParams.size(); ++i) { char param[1024] = { 0 }; if (1 == devParams[k].devParams[i].valueType) { assert(1 == devParams[k].devParams[i].rangeType); std::string valueList = "["; for (int j = 0; j < (int)devParams[k].devParams[i].stringValueList.size(); ++j) { valueList += "\""; valueList += devParams[k].devParams[i].stringValueList[j]; valueList += "\""; if (j != (int)devParams[k].devParams[i].stringValueList.size() - 1) { valueList += ", "; } } valueList += "]"; sprintf(param, "{\"name\":\"%s\", \"value_type\":\"string\", \"value\":\"%s\", \"range_type\":\"list\", \"value_list\":%s}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].stringValue.c_str(), valueList.c_str()); } else if (2 == devParams[k].devParams[i].valueType) { if (2 == devParams[k].devParams[i].rangeType) { std::string valueList = "["; for (int j = 0; j < (int)devParams[k].devParams[i].intValueList.size(); ++j) { char v[32] = { 0 }; sprintf(v, "%d", devParams[k].devParams[i].intValueList[j]); valueList += v; if (j != (int)devParams[k].devParams[i].intValueList.size() - 1) { valueList += ", "; } } valueList += "]"; sprintf(param, "{\"name\":\"%s\", \"value_type\":\"int\", \"value\":%d, \"range_type\":\"list\", \"value_list\":%s}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].intValue, valueList.c_str()); } else if (4 == devParams[k].devParams[i].rangeType) { sprintf(param, "{\"name\":\"%s\", \"value_type\":\"int\", \"value\":%d, \"range_type\":\"min_max\", \"value_min\":%d, \"value_max\":%d}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].intValue, devParams[k].devParams[i].intValueMin, devParams[k].devParams[i].intValueMax); } else { assert(0 == devParams[k].devParams[i].rangeType); sprintf(param, "{\"name\":\"%s\", \"value_type\":\"int\", \"value\":%d}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].intValue); } } else if (3 == devParams[k].devParams[i].valueType) { if (3 == devParams[k].devParams[i].rangeType) { std::string valueList = "["; for (int j = 0; j < (int)devParams[k].devParams[i].doubleValueList.size(); ++j) { char v[32] = { 0 }; sprintf(v, "%f", devParams[k].devParams[i].doubleValueList[j]); valueList += v; if (j != (int)devParams[k].devParams[i].doubleValueList.size() - 1) { valueList += ", "; } } valueList += "]"; sprintf(param, "{\"name\":\"%s\", \"value_type\":\"double\", \"value\":%f, \"range_type\":\"list\", \"value_list\":%s}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].doubleValue, valueList.c_str()); } else if (5 == devParams[k].devParams[i].rangeType) { sprintf(param, "{\"name\":\"%s\", \"value_type\":\"double\", \"value\":%f, \"range_type\":\"min_max\", \"value_min\":%f, \"value_max\":%f}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].doubleValue, devParams[k].devParams[i].doubleValueMin, devParams[k].devParams[i].doubleValueMax); } else { assert(0 == devParams[k].devParams[i].rangeType); sprintf(param, "{\"name\":\"%s\", \"value_type\":\"double\", \"value\":%f}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].doubleValue); } } else { assert(4 == devParams[k].devParams[i].valueType); assert(0 == devParams[k].devParams[i].rangeType); sprintf(param, "{\"name\":\"%s\", \"value_type\":\"bool\", \"value\":%s}", devParams[k].devParams[i].title.c_str(), devParams[k].devParams[i].boolValue ? "true" : "false"); } groupParams += param; if (i != (int)devParams[k].devParams.size() - 1) { groupParams += ", "; } } groupParams += "]}"; deviceParams += groupParams; if (k != (int)devParams.size() - 1) { deviceParams += ", "; } } deviceParams += "]"; char *resp = new char [1024 + deviceParams.size()] ; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_device_param", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_device_param", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_device_param", iden.c_str(), ret, deviceParams.c_str()); else sprintf(resp, fmt.c_str(), "get_device_param", ret, deviceParams.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::ResetDeviceParam(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->ResetDeviceParam(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "reset_device_param", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "reset_device_param", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "reset_device_param", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "reset_device_param", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetCurrDeviceName(cJSON* json) { assert(NULL != json); std::string devName; std::string errInfo; int ret = GetManager()->GetCurrDeviceName(devName, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"device_name\":\"%s\"}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_curr_device_name", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_curr_device_name", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_curr_device_name", iden.c_str(), ret, devName.c_str()); else sprintf(resp, fmt.c_str(), "get_curr_device_name", ret, devName.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::StartScan(cJSON* json) { assert(NULL != json); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (!GetManager()->IsScanning()) { m_startScanIden = iden; m_startScanHaveIden = findIden; m_startScanIsGetBase64 = getBase64; } std::string errInfo; int ret = GetManager()->StartScan(errInfo); if (0 != ret) { m_startScanIden.clear(); m_startScanHaveIden = false; m_startScanIsGetBase64 = false; } std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "start_scan", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "start_scan", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "start_scan", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "start_scan", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::StopScan(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->StopScan(errInfo); m_startScanIden.clear(); m_startScanHaveIden = false; m_startScanIsGetBase64 = false; bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "stop_scan", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "stop_scan", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "stop_scan", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "stop_scan", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetBatchIdList(cJSON* json) { assert(NULL != json); std::vector batchIdList; std::string errInfo; int ret = GetManager()->GetBatchIdList(batchIdList, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"batch_id_list\":%s}"; } std::string idList = "["; for (int i = 0; i < (int)batchIdList.size(); ++i) { idList += "\""; idList += batchIdList[i]; idList += "\""; if (i != (int)batchIdList.size() - 1) { idList += ", "; } } idList += "]"; char *resp = new char[1024 + idList.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_batch_id_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_batch_id_list", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_batch_id_list", iden.c_str(), ret, idList.c_str()); else sprintf(resp, fmt.c_str(), "get_batch_id_list", ret, idList.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::OpenBatch(cJSON* json) { assert(NULL != json); bool find = false; std::string batchId = GetJsonStringValue(json, "batch_id", &find); if (!find) { std::vector batchIdList; std::string errInfo; GetManager()->GetBatchIdList(batchIdList, errInfo); if (!batchIdList.empty()) batchId = batchIdList[batchIdList.size() - 1]; } std::string errInfo; int ret = GetManager()->OpenBatch(batchId, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "open_batch", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "open_batch", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "open_batch", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "open_batch", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::DeleteBatch(cJSON* json) { assert(NULL != json); std::string batchId = GetJsonStringValue(json, "batch_id"); std::string errInfo; int ret = GetManager()->DeleteBatch(batchId, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "delete_batch", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "delete_batch", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "delete_batch", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "delete_batch", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::NewBatch(cJSON* json) { assert(NULL != json); int ret; std::string newBatchId; std::string errInfo; bool find = false; std::string batchId = GetJsonStringValue(json, "batch_id", &find); if (!find) { while (1) { HGChar uuid[256]; HGBase_GetUuid(uuid, 256); batchId = std::string(uuid, 6); ret = GetManager()->NewBatch(batchId, errInfo); if (0 == ret) { newBatchId = batchId; break; } } } else { ret = GetManager()->NewBatch(batchId, errInfo); if (0 == ret) newBatchId = batchId; } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"batch_id\":\"%s\"}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "new_batch", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "new_batch", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "new_batch", iden.c_str(), ret, newBatchId.c_str()); else sprintf(resp, fmt.c_str(), "new_batch", ret, newBatchId.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetCurrBatchId(cJSON* json) { assert(NULL != json); std::string batchId; std::string errInfo; int ret = GetManager()->GetCurrBatchId(batchId, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"batch_id\":\"%s\"}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_curr_batch_id", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_curr_batch_id", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_curr_batch_id", iden.c_str(), ret, batchId.c_str()); else sprintf(resp, fmt.c_str(), "get_curr_batch_id", ret, batchId.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ModifyBatchId(cJSON* json) { assert(NULL != json); bool find = false; std::string batchId = GetJsonStringValue(json, "batch_id", &find); if (!find) { std::string errInfo; GetManager()->GetCurrBatchId(batchId, errInfo); } std::string newBatchId = GetJsonStringValue(json, "new_batch_id"); std::string errInfo; int ret = GetManager()->ModifyBatchId(batchId, newBatchId, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "modify_batch_id", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "modify_batch_id", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "modify_batch_id", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "modify_batch_id", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::BindFolder(cJSON* json) { assert(NULL != json); std::string folder = Utf8ToStdString(GetJsonStringValue(json, "folder")); bool find = false; std::string nameMode = GetJsonStringValue(json, "name_mode"); if (!find) nameMode = "order"; int nameWidth = GetJsonIntValue(json, "name_width", &find); if (!find) nameWidth = 1; int nameBase = GetJsonIntValue(json, "name_base", &find); if (!find) nameBase = 0; std::string errInfo; int ret = GetManager()->BindFolder(folder, nameMode, nameWidth, nameBase, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "bind_folder", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "bind_folder", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "bind_folder", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "bind_folder", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::StopBindFolder(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->StopBindFolder(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "stop_bind_folder", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "stop_bind_folder", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "stop_bind_folder", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "stop_bind_folder", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::GetImageThumbnailList(cJSON* json) { assert(NULL != json); std::vector imageThumbList; std::string errInfo; int ret = GetManager()->GetImageThumbnailList(imageThumbList, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_thumbnail_list\":%s}"; } std::string thumbList = "["; for (int i = 0; i < (int)imageThumbList.size(); ++i) { thumbList += "{\"image_tag\":\""; thumbList += imageThumbList[i].tag; thumbList += "\", \"image_base64\":\""; thumbList += imageThumbList[i].thumbBase64; thumbList += "\"}"; if (i != (int)imageThumbList.size() - 1) { thumbList += ", "; } } thumbList += "]"; char* resp = new char[1024 + thumbList.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", iden.c_str(), ret, thumbList.c_str()); else sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", ret, thumbList.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::GetImageCount(cJSON* json) { assert(NULL != json); int imageCount = 0; std::string errInfo; int ret = GetManager()->GetImageCount(imageCount, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_count\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "get_image_count", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "get_image_count", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "get_image_count", iden.c_str(), ret, imageCount); else sprintf(resp, fmt.c_str(), "get_image_count", ret, imageCount); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::LoadImage(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string imageTag, imageBase64; std::string errInfo; int ret = GetManager()->LoadImage(imageIndex, imageTag, imageBase64, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_tag\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } char *resp = new char [1024 + imageTag.size() + imageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "load_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "load_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "load_image", iden.c_str(), ret, imageTag.c_str(), imageBase64.c_str()); else sprintf(resp, fmt.c_str(), "load_image", ret, imageTag.c_str(), imageBase64.c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::SaveImage(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string imagePath; std::string errInfo; int ret = GetManager()->SaveImage(imageIndex, imagePath, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; fmt += "\"image_path\":\"%s\"}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "save_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "save_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "save_image", iden.c_str(), ret, StdStringToUtf8(strToJson(imagePath)).c_str()); else sprintf(resp, fmt.c_str(), "save_image", ret, StdStringToUtf8(strToJson(imagePath)).c_str()); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::InsertLocalImage(cJSON* json) { assert(NULL != json); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); bool find = false; int insertPos = GetJsonIntValue(json, "insert_pos", &find); if (!find) insertPos = -1; std::string imageTag = GetJsonStringValue(json, "image_tag"); std::string errInfo; int ret = GetManager()->InsertLocalImage(imagePath, insertPos, imageTag, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "insert_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "insert_local_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "insert_local_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "insert_local_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::InsertImage(cJSON* json) { assert(NULL != json); std::string imageBase64 = GetJsonStringValue(json, "image_base64"); bool find = false; int insertPos = GetJsonIntValue(json, "insert_pos", &find); if (!find) insertPos = -1; std::string imageTag = GetJsonStringValue(json, "image_tag"); std::string errInfo; int ret = GetManager()->InsertImage(imageBase64, insertPos, imageTag, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "insert_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "insert_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "insert_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "insert_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ModifyImageTag(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); std::vector imageTagList = GetJsonStringListValue(json, "image_tag_list"); std::string errInfo; int ret = GetManager()->ModifyImageTag(imageIndexList, imageTagList, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "modify_image_tag", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "modify_image_tag", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "modify_image_tag", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "modify_image_tag", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::DeleteImage(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); std::string errInfo; int ret = GetManager()->DeleteImage(imageIndexList, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "delete_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "delete_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "delete_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "delete_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ClearImageList(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->ClearImageList(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "clear_image_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "clear_image_list", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "clear_image_list", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "clear_image_list", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ModifyImage(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string imageBase64 = GetJsonStringValue(json, "image_base64"); std::string errInfo; int ret = GetManager()->ModifyImage(imageIndex, imageBase64, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "modify_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "modify_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "modify_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "modify_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ModifyImageByLocal(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); std::string errInfo; int ret = GetManager()->ModifyImageByLocal(imageIndex, imagePath, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "modify_image_by_local", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "modify_image_by_local", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "modify_image_by_local", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "modify_image_by_local", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::MoveImage(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); bool find = false; std::string mode = GetJsonStringValue(json, "mode", &find); if (!find) mode = "index"; int target = GetJsonIntValue(json, "target"); std::string errInfo; int ret = GetManager()->MoveImage(imageIndexList, mode, target, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "move_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "move_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "move_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "move_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ExchangeImage(cJSON* json) { assert(NULL != json); int imageIndex1 = GetJsonIntValue(json, "image_index_1"); int imageIndex2 = GetJsonIntValue(json, "image_index_2"); std::string errInfo; int ret = GetManager()->ExchangeImage(imageIndex1, imageIndex2, errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024] = { 0 }; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "exchange_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "exchange_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "exchange_image", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "exchange_image", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::ImageBookSort(cJSON* json) { assert(NULL != json); std::string errInfo; int ret = GetManager()->ImageBookSort(errInfo); bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d}"; } char resp[1024]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "image_book_sort", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "image_book_sort", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) sprintf(resp, fmt.c_str(), "image_book_sort", iden.c_str(), ret); else sprintf(resp, fmt.c_str(), "image_book_sort", ret); } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } void WSUser::MergeImage(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); bool find = false; std::string mode = GetJsonStringValue(json, "mode", &find); if (!find) mode = "horz"; std::string align = GetJsonStringValue(json, "align", &find); if (!find) align = "center"; int interval = GetJsonIntValue(json, "interval", &find); if (!find) interval = 0; std::string outImagePath; std::string errInfo; int ret = GetManager()->MergeImage(imageIndexList, mode, align, interval, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "merge_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "merge_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "merge_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "merge_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "merge_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "merge_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::MakeMultiImage(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); bool find = false; std::string format = GetJsonStringValue(json, "format", &find); if (!find) format = "tif"; std::string tiffCompression = GetJsonStringValue(json, "tiff_compression", &find); if (!find) tiffCompression = "lzw"; int tiffJpegQuality = GetJsonIntValue(json, "tiff_jpeg_quality", &find); if (!find) tiffJpegQuality = 80; std::string outImagePath; std::string errInfo; int ret = GetManager()->MakeMultiImage(imageIndexList, format, tiffCompression, tiffJpegQuality, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "make_multi_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "make_multi_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "make_multi_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "make_multi_image", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "make_multi_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "make_multi_image", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::SplitImage(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); bool find = false; std::string mode = GetJsonStringValue(json, "mode", &find); if (!find) mode = "horz"; int location = GetJsonIntValue(json, "location", &find); if (!find) location = 0; std::vector outImagePathList; std::string errInfo; int ret = GetManager()->SplitImage(imageIndex, mode, location, outImagePathList, errInfo); std::vector outImageBase64List; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { for (int i = 0; i < (int)outImagePathList.size(); ++i) { std::string outImageBase64; std::string errInfo2; GetManager()->LoadLocalImage(outImagePathList[i], outImageBase64, errInfo2); outImageBase64List.push_back(outImageBase64); } } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path_list\":%s, "; fmt += "\"image_base64_list\":%s}"; } else { fmt += "\"image_path_list\":%s}"; } } std::string pathList = "["; for (int i = 0; i < (int)outImagePathList.size(); ++i) { pathList += "\""; pathList += StdStringToUtf8(strToJson(outImagePathList[i])); pathList += "\""; if (i != (int)outImagePathList.size() - 1) { pathList += ", "; } } pathList += "]"; std::string base64List = "["; for (int i = 0; i < (int)outImageBase64List.size(); ++i) { base64List += "\""; base64List += outImageBase64List[i]; base64List += "\""; if (i != (int)outImageBase64List.size() - 1) { base64List += ", "; } } base64List += "]"; char* resp = new char[1024 + base64List.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "split_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "split_image", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "split_image", iden.c_str(), ret, pathList.c_str(), base64List.c_str()); else sprintf(resp, fmt.c_str(), "split_image", iden.c_str(), ret, pathList.c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "split_image", ret, pathList.c_str(), base64List.c_str()); else sprintf(resp, fmt.c_str(), "split_image", ret, pathList.c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::MakeZipFile(cJSON* json) { assert(NULL != json); std::vector imageIndexList = GetJsonIntListValue(json, "image_index_list"); std::string outZipPath; std::string errInfo; int ret = GetManager()->MakeZipFile(imageIndexList, outZipPath, errInfo); std::string outZipBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { GetManager()->LoadBase64(outZipPath, outZipBase64); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"zip_path\":\"%s\", "; fmt += "\"zip_base64\":\"%s\"}"; } else { fmt += "\"zip_path\":\"%s\"}"; } } char* resp = new char[1024 + outZipBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "make_zip_file", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "make_zip_file", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str(), outZipBase64.c_str()); else sprintf(resp, fmt.c_str(), "make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str(), outZipBase64.c_str()); else sprintf(resp, fmt.c_str(), "make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::ImageDeskew(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string outImagePath; std::string errInfo; int ret = GetManager()->ImageDeskew(imageIndex, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "image_deskew", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "image_deskew", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "image_deskew", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "image_deskew", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "image_deskew", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "image_deskew", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } void WSUser::ImageAddWatermark(cJSON* json) { assert(NULL != json); int imageIndex = GetJsonIntValue(json, "image_index"); std::string text = Utf8ToStdString(GetJsonStringValue(json, "text")); bool find; std::string textColor = GetJsonStringValue(json, "text_color", &find); if (!find) textColor = "#000000"; int textOpacity = GetJsonIntValue(json, "text_opacity", &find); if (!find) textOpacity = 255; std::string textPos = GetJsonStringValue(json, "text_pos", &find); if (!find) textPos = "location"; int marginLeft = GetJsonIntValue(json, "margin_left", &find); int marginTop = GetJsonIntValue(json, "margin_top", &find); int marginRight = GetJsonIntValue(json, "margin_right", &find); int marginBottom = GetJsonIntValue(json, "margin_bottom", &find); int locationX = GetJsonIntValue(json, "location_x", &find); int locationY = GetJsonIntValue(json, "location_y", &find); std::string fontName = Utf8ToStdString(GetJsonStringValue(json, "font_name", &find)); if (!find) fontName = "宋体"; int fontSize = GetJsonIntValue(json, "font_size", &find); if (!find) fontSize = 20; bool fontBold = GetJsonBoolValue(json, "font_bold"); bool fontUnderline = GetJsonBoolValue(json, "font_underline"); bool fontItalic = GetJsonBoolValue(json, "font_italic"); bool fontStrikeout = GetJsonBoolValue(json, "font_strikeout"); std::string outImagePath; std::string errInfo; int ret = GetManager()->ImageAddWatermark(imageIndex, text, textColor, textOpacity, textPos, marginLeft, marginTop, marginRight, marginBottom, locationX, locationY, fontName, fontSize, fontBold, fontUnderline, fontItalic, fontStrikeout, outImagePath, errInfo); std::string outImageBase64; bool getBase64 = GetJsonBoolValue(json, "get_base64"); if (0 == ret && getBase64) { std::string errInfo2; GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2); } bool findIden = false; std::string iden = GetJsonStringValue(json, "iden", &findIden); std::string fmt; fmt += "{\"func\":\"%s\", "; if (findIden) fmt += "\"iden\":\"%s\", "; if (0 != ret) { fmt += "\"ret\":%d, "; fmt += "\"err_info\":\"%s\"}"; } else { fmt += "\"ret\":%d, "; if (getBase64) { fmt += "\"image_path\":\"%s\", "; fmt += "\"image_base64\":\"%s\"}"; } else { fmt += "\"image_path\":\"%s\"}"; } } char* resp = new char[1024 + outImageBase64.size()]; if (0 != ret) { if (findIden) sprintf(resp, fmt.c_str(), "image_add_watermark", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str()); else sprintf(resp, fmt.c_str(), "image_add_watermark", ret, StdStringToUtf8(errInfo).c_str()); } else { if (findIden) { if (getBase64) sprintf(resp, fmt.c_str(), "image_add_watermark", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "image_add_watermark", iden.c_str(), ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } else { if (getBase64) sprintf(resp, fmt.c_str(), "image_add_watermark", ret, StdStringToUtf8(strToJson(outImagePath)).c_str(), outImageBase64.c_str()); else sprintf(resp, fmt.c_str(), "image_add_watermark", ret, StdStringToUtf8(strToJson(outImagePath)).c_str()); } } SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); delete[] resp; } }