diff --git a/app/scanner/app_cfg.cpp b/app/scanner/app_cfg.cpp index d63feff7..10b4d3cd 100644 --- a/app/scanner/app_cfg.cpp +++ b/app/scanner/app_cfg.cpp @@ -2,6 +2,7 @@ #include "base/HGDef.h" #include "base/HGInc.h" #include "base/HGUtility.h" +#include "base/HGIni.h" #include "HGUIGlobal.h" #include "HGString.h" diff --git a/app/scanner/dialog_aquireinto.cpp b/app/scanner/dialog_aquireinto.cpp index 9bf40f78..f2f3253c 100644 --- a/app/scanner/dialog_aquireinto.cpp +++ b/app/scanner/dialog_aquireinto.cpp @@ -23,7 +23,10 @@ Dialog_AquireInto::Dialog_AquireInto(QWidget* parent) : HGChar aquireIntoPath[512]; HGBase_GetDocumentsPath(aquireIntoPath, 512); - strcat(aquireIntoPath, "AquireInto/"); + HGChar procName[512]; + HGBase_GetProcessName(procName, 512); + strcat(aquireIntoPath, procName); + strcat(aquireIntoPath, "/AquireInto/"); QString filePath = getStdFileName(StdStringToUtf8(aquireIntoPath).c_str()); ui->lineEdit_directory->setText(getCfgValue("aquire", "aquireIntoPath", filePath)); diff --git a/app/scanner/dialog_clrcache.cpp b/app/scanner/dialog_clrcache.cpp index 37dfc452..11427e4c 100644 --- a/app/scanner/dialog_clrcache.cpp +++ b/app/scanner/dialog_clrcache.cpp @@ -27,7 +27,10 @@ QString Dialog_ClrCache::getCachePath() { HGChar cachePath[512]; HGBase_GetDocumentsPath(cachePath, 512); - strcat(cachePath, "Cache/"); + HGChar procName[512]; + HGBase_GetProcessName(procName, 512); + strcat(cachePath, procName); + strcat(cachePath, "/Cache/"); QString filePath = getStdFileName(StdStringToUtf8(cachePath).c_str()); return filePath; } diff --git a/build/windows/HGBase/HGBase.def b/build/windows/HGBase/HGBase.def index 4fb4f9c1..18489684 100644 --- a/build/windows/HGBase/HGBase.def +++ b/build/windows/HGBase/HGBase.def @@ -42,6 +42,8 @@ HGBase_EnableInfo HGBase_DisableInfo HGBase_WriteInfo +HGBase_GetLocalTime + HGBase_GetTmpPath HGBase_GetCurrentDir HGBase_SetCurrentDir @@ -57,11 +59,12 @@ HGBase_GetDocumentsPath HGBase_GetProcessName HGBase_GetFileName HGBase_GetFilePath +HGBase_StandardiseFileName + HGBase_SetProfileInt HGBase_SetProfileString HGBase_GetProfileInt HGBase_GetProfileString -HGBase_StandardiseFileName HGBase_CreateBuffer HGBase_CreateBufferFromData diff --git a/build/windows/HGBase/HGBase.vcxproj b/build/windows/HGBase/HGBase.vcxproj index 6e2863dc..316c22f5 100644 --- a/build/windows/HGBase/HGBase.vcxproj +++ b/build/windows/HGBase/HGBase.vcxproj @@ -32,12 +32,14 @@ + + @@ -54,12 +56,14 @@ + + diff --git a/build/windows/HGWebService/HGWebService.vcxproj b/build/windows/HGWebService/HGWebService.vcxproj index 05ea1961..5a0ced15 100644 --- a/build/windows/HGWebService/HGWebService.vcxproj +++ b/build/windows/HGWebService/HGWebService.vcxproj @@ -130,7 +130,7 @@ true MultiThreadedDebug ../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories) - 28251;%(DisableSpecificWarnings) + 28251;26812;%(DisableSpecificWarnings) Windows diff --git a/modules/base/HGBase.h b/modules/base/HGBase.h index af583b7d..59ed1659 100644 --- a/modules/base/HGBase.h +++ b/modules/base/HGBase.h @@ -16,5 +16,8 @@ #include "HGThread.h" #include "HGUtility.h" #include "HGInfo.h" +#include "HGIni.h" +#include "HGMsgPump.h" +#include "HGTime.h" #endif /* __HGBASE_H__ */ \ No newline at end of file diff --git a/modules/base/HGInfoImpl.cpp b/modules/base/HGInfoImpl.cpp index 46ff9617..0429bf8f 100644 --- a/modules/base/HGInfoImpl.cpp +++ b/modules/base/HGInfoImpl.cpp @@ -2,6 +2,7 @@ #include "HGInfo.h" #include "HGInc.h" #include "HGUtility.h" +#include "HGIni.h" #include #include diff --git a/modules/base/HGIni.cpp b/modules/base/HGIni.cpp new file mode 100644 index 00000000..3f3a87cc --- /dev/null +++ b/modules/base/HGIni.cpp @@ -0,0 +1,327 @@ +#include "HGIni.h" +#include +#include + +static int IniWriteValue(const char* section, const char* key, const char* val, const char* file) +{ + typedef std::vector > KeyList; + typedef std::vector > SectionList; + + SectionList sectList; + + FILE* fp = fopen(file, "r"); + if (fp != NULL) + { + KeyList* pCurKeyList = NULL; + + while (feof(fp) == 0) + { + char lineContent[256] = { 0 }; + if (NULL == fgets(lineContent, 256, fp)) + { + continue; + } + + if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) + { + continue; + } + + for (size_t i = 0; i < strlen(lineContent); ++i) + { + if (lineContent[i] == '\r' || lineContent[i] == '\n') + { + lineContent[i] = '\0'; + break; + } + } + + if (lineContent[0] == '[') + { + std::pair pr; + pr.first = lineContent; + sectList.push_back(pr); + pCurKeyList = §List[sectList.size() - 1].second; + } + else + { + int pos = -1; + for (int i = 0; i < (int)strlen(lineContent); ++i) + { + if (lineContent[i] == '=') + { + pos = i; + break; + } + } + + if (NULL != pCurKeyList) + { + std::pair pr; + if (-1 != pos) + { + pr.first.assign(lineContent, pos); + pr.second.assign(lineContent + pos + 1); + } + else + { + pr.first = lineContent; + } + + pCurKeyList->push_back(pr); + } + } + } + + fclose(fp); + } + + bool bFindSect = false; + for (size_t i = 0; i < sectList.size(); ++i) + { + if (strcmp(sectList[i].first.c_str(), section) == 0) + { + bool bFindKey = false; + for (size_t j = 0; j < sectList[i].second.size(); ++j) + { + if (strcmp(sectList[i].second[j].first.c_str(), key) == 0) + { + sectList[i].second[j].second = val; + bFindKey = true; + break; + } + } + + if (!bFindKey) + { + std::pair pr; + pr.first = key; + pr.second = val; + sectList[i].second.push_back(pr); + } + + bFindSect = true; + break; + } + } + + if (!bFindSect) + { + std::pair pr; + pr.first = section; + std::pair pr2; + pr2.first = key; + pr2.second = val; + pr.second.push_back(pr2); + sectList.push_back(pr); + } + + fp = fopen(file, "w"); + if (fp == NULL) + { + return -1; + } + + for (size_t i = 0; i < sectList.size(); ++i) + { + fputs(sectList[i].first.c_str(), fp); + fputs("\n", fp); + + for (size_t j = 0; j < sectList[i].second.size(); ++j) + { + fputs(sectList[i].second[j].first.c_str(), fp); + fputs("=", fp); + fputs(sectList[i].second[j].second.c_str(), fp); + fputs("\n", fp); + } + } + + fclose(fp); + return 0; +} + +static int writeStringValue(const char* section, const char* key, const char* val, const char* file) +{ + if (section == NULL || key == NULL || val == NULL || file == NULL) + { + return -1; + } + + char sect[256]; + sprintf(sect, "[%s]", section); + return IniWriteValue(sect, key, val, file); +} + +HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, HGInt value) +{ + if (NULL == fileName || NULL == appName || NULL == keyName) + { + return HGBASE_ERR_INVALIDARG; + } + + char str[8]; + sprintf(str, "%d", value); + if (0 != writeStringValue(appName, keyName, str, fileName)) + return HGBASE_ERR_FAIL; + return HGBASE_ERR_OK; +} + +HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, const HGChar* value) +{ + if (NULL == fileName || NULL == appName || NULL == keyName) + { + return HGBASE_ERR_INVALIDARG; + } + + if (0 != writeStringValue(appName, keyName, value, fileName)) + return HGBASE_ERR_FAIL; + return HGBASE_ERR_OK; +} + +static int IniReadValue(const char* section, const char* key, char* val, const char* def, const char* file) +{ + typedef std::vector > KeyList; + typedef std::vector > SectionList; + + SectionList sectList; + + FILE* fp = fopen(file, "r"); + if (fp != NULL) + { + KeyList* pCurKeyList = NULL; + + while (feof(fp) == 0) + { + char lineContent[256] = { 0 }; + if (NULL == fgets(lineContent, 256, fp)) + { + continue; + } + + if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) + { + continue; + } + + for (size_t i = 0; i < strlen(lineContent); ++i) + { + if (lineContent[i] == '\r' || lineContent[i] == '\n') + { + lineContent[i] = '\0'; + break; + } + } + + if (lineContent[0] == '[') + { + std::pair pr; + pr.first = lineContent; + sectList.push_back(pr); + pCurKeyList = §List[sectList.size() - 1].second; + } + else + { + int pos = -1; + for (int i = 0; i < (int)strlen(lineContent); ++i) + { + if (lineContent[i] == '=') + { + pos = i; + break; + } + } + + if (NULL != pCurKeyList) + { + std::pair pr; + if (-1 != pos) + { + pr.first.assign(lineContent, pos); + pr.second.assign(lineContent + pos + 1); + } + else + { + pr.first = lineContent; + } + + pCurKeyList->push_back(pr); + } + } + } + + fclose(fp); + } + else + { + strcpy(val, def); + return -1; + } + + bool bGetVal = false; + for (size_t i = 0; i < sectList.size(); ++i) + { + if (strcmp(sectList[i].first.c_str(), section) == 0) + { + for (size_t j = 0; j < sectList[i].second.size(); ++j) + { + if (strcmp(sectList[i].second[j].first.c_str(), key) == 0) + { + strcpy(val, sectList[i].second[j].second.c_str()); + bGetVal = true; + break; + } + } + + break; + } + } + + if (!bGetVal) + { + strcpy(val, def); + } + + return 0; +} + +static int readStringValue(const char* section, const char* key, char* val, const char* def, const char* file) +{ + if (section == NULL || key == NULL || val == NULL || def == NULL || file == NULL) + { + return -1; + } + + char sect[256]; + sprintf(sect, "[%s]", section); + return IniReadValue(sect, key, val, def, file); +} + +HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, HGInt def, HGInt* value) +{ + if (NULL == appName || NULL == keyName || NULL == value) + { + return HGBASE_ERR_INVALIDARG; + } + + char strRet[256] = { 0 }; + char strDef[32]; + sprintf(strDef, "%d", def); + readStringValue(appName, keyName, strRet, strDef, fileName); + *value = atoi(strRet); + return HGBASE_ERR_OK; +} + +HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen) +{ + if (NULL == appName || NULL == keyName || NULL == value) + { + return HGBASE_ERR_INVALIDARG; + } + + readStringValue(appName, keyName, value, def, fileName); + return HGBASE_ERR_OK; +} \ No newline at end of file diff --git a/modules/base/HGIni.h b/modules/base/HGIni.h new file mode 100644 index 00000000..05f84d62 --- /dev/null +++ b/modules/base/HGIni.h @@ -0,0 +1,27 @@ +#ifndef __HGINI_H__ +#define __HGINI_H__ + +#include "HGDef.h" +#include "HGBaseErr.h" + +/* 设置ini文件的值 +*/ +HGEXPORT HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, HGInt value); + +/* 设置ini文件的值 +*/ +HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, const HGChar* value); + +/* 获取ini文件的值 +*/ +HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, HGInt def, HGInt* value); + +/* 获取ini文件的值 +*/ +HGEXPORT HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName, + const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen); + +#endif /* __HGINI_H__ */ \ No newline at end of file diff --git a/modules/base/HGTime.cpp b/modules/base/HGTime.cpp new file mode 100644 index 00000000..aded4bf4 --- /dev/null +++ b/modules/base/HGTime.cpp @@ -0,0 +1,37 @@ +#include "HGTime.h" +#include "HGInc.h" + +HGResult HGAPI HGBase_GetLocalTime(HGTimeInfo* timeInfo) +{ + if (NULL == timeInfo) + { + return HGBASE_ERR_INVALIDARG; + } + +#if defined(HG_CMP_MSC) + SYSTEMTIME st; + GetLocalTime(&st); + timeInfo->year = st.wYear; + timeInfo->month = st.wMonth; + timeInfo->day = st.wDay; + timeInfo->dayOfWeek = st.wDayOfWeek; + timeInfo->hour = st.wHour; + timeInfo->minute = st.wMinute; + timeInfo->second = st.wSecond; + timeInfo->milliseconds = st.wMilliseconds; +#else + struct timeval time; + gettimeofday(&time, NULL); + struct tm* p = localtime(&time.tv_sec); + assert(NULL != p); + timeInfo->year = p->tm_year + 1900; + timeInfo->month = 1 + p->tm_mon; + timeInfo->day = p->tm_mday; + timeInfo->dayOfWeek = p->tm_wday; + timeInfo->hour = p->tm_hour; + timeInfo->minute = p->tm_min; + timeInfo->second = p->tm_sec; + timeInfo->milliseconds = time.tv_usec / 1000; +#endif + return HGBASE_ERR_OK; +} \ No newline at end of file diff --git a/modules/base/HGTime.h b/modules/base/HGTime.h new file mode 100644 index 00000000..e980e508 --- /dev/null +++ b/modules/base/HGTime.h @@ -0,0 +1,21 @@ +#ifndef __HGTIME_H__ +#define __HGTIME_H__ + +#include "HGDef.h" +#include "HGBaseErr.h" + +typedef struct +{ + HGUShort year; + HGUShort month; + HGUShort day; + HGUShort dayOfWeek; /* 0为星期天, 1-6表示星期一到星期六 */ + HGUShort hour; + HGUShort minute; + HGUShort second; + HGUShort milliseconds; +}HGTimeInfo; + +HGEXPORT HGResult HGAPI HGBase_GetLocalTime(HGTimeInfo *timeInfo); + +#endif /* __HGTIME_H__ */ \ No newline at end of file diff --git a/modules/base/HGUtility.cpp b/modules/base/HGUtility.cpp index 5a5f4971..616db11f 100644 --- a/modules/base/HGUtility.cpp +++ b/modules/base/HGUtility.cpp @@ -6,7 +6,6 @@ #else #include "uuid/uuid.h" #endif -#include #include HGResult HGAPI HGBase_GetTmpPath(HGChar* path, HGUInt maxLen) @@ -224,7 +223,7 @@ HGResult HGAPI HGBase_GetUuid(HGChar* uuid, HGUInt maxLen) return HGBASE_ERR_OK; } -HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen) +HGResult HGAPI HGBase_GetTmpFileName(const HGChar* suffix, HGChar* fileName, HGUInt maxLen) { if (NULL == fileName || 0 == maxLen) { @@ -235,6 +234,11 @@ HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen) HGBase_GetTmpPath(path, 256); HGBase_GetUuid(uuid, 128); strcat(path, uuid); + if (NULL != suffix && 0 != *suffix) + { + strcat(path, "."); + strcat(path, suffix); + } if (maxLen < strlen(path) + 1) return HGBASE_ERR_FAIL; @@ -332,22 +336,12 @@ HGResult HGAPI HGBase_GetDocumentsPath(HGChar* documentsPath, HGUInt maxLen) return HGBASE_ERR_FAIL; if (docPath[strlen(docPath) - 1] != '\\') strcat(docPath, "\\"); - - HGChar procName[64]; - HGBase_GetProcessName(procName, 64); - strcat(docPath, procName); - strcat(docPath, "\\"); #else char docPath[512] = { 0 }; strcpy(docPath, getenv("HOME")); if (docPath[strlen(docPath) - 1] != '/') strcat(docPath, "/"); strcat(docPath, "Documents/"); - - HGChar procName[64]; - HGBase_GetProcessName(procName, 64); - strcat(docPath, procName); - strcat(docPath, "/"); #endif if (maxLen < strlen(docPath) + 1) @@ -446,330 +440,6 @@ HGResult HGAPI HGBase_GetFilePath(const HGChar* filePath, HGChar* path, HGUInt m return HGBASE_ERR_OK; } -static int IniWriteValue(const char* section, const char* key, const char* val, const char* file) -{ - typedef std::vector > KeyList; - typedef std::vector > SectionList; - - SectionList sectList; - - FILE* fp = fopen(file, "r"); - if (fp != NULL) - { - KeyList* pCurKeyList = NULL; - - while (feof(fp) == 0) - { - char lineContent[256] = { 0 }; - if (NULL == fgets(lineContent, 256, fp)) - { - continue; - } - - if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) - { - continue; - } - - for (size_t i = 0; i < strlen(lineContent); ++i) - { - if (lineContent[i] == '\r' || lineContent[i] == '\n') - { - lineContent[i] = '\0'; - break; - } - } - - if (lineContent[0] == '[') - { - std::pair pr; - pr.first = lineContent; - sectList.push_back(pr); - pCurKeyList = §List[sectList.size() - 1].second; - } - else - { - int pos = -1; - for (int i = 0; i < (int)strlen(lineContent); ++i) - { - if (lineContent[i] == '=') - { - pos = i; - break; - } - } - - if (NULL != pCurKeyList) - { - std::pair pr; - if (-1 != pos) - { - pr.first.assign(lineContent, pos); - pr.second.assign(lineContent + pos + 1); - } - else - { - pr.first = lineContent; - } - - pCurKeyList->push_back(pr); - } - } - } - - fclose(fp); - } - - bool bFindSect = false; - for (size_t i = 0; i < sectList.size(); ++i) - { - if (strcmp(sectList[i].first.c_str(), section) == 0) - { - bool bFindKey = false; - for (size_t j = 0; j < sectList[i].second.size(); ++j) - { - if (strcmp(sectList[i].second[j].first.c_str(), key) == 0) - { - sectList[i].second[j].second = val; - bFindKey = true; - break; - } - } - - if (!bFindKey) - { - std::pair pr; - pr.first = key; - pr.second = val; - sectList[i].second.push_back(pr); - } - - bFindSect = true; - break; - } - } - - if (!bFindSect) - { - std::pair pr; - pr.first = section; - std::pair pr2; - pr2.first = key; - pr2.second = val; - pr.second.push_back(pr2); - sectList.push_back(pr); - } - - fp = fopen(file, "w"); - if (fp == NULL) - { - return -1; - } - - for (size_t i = 0; i < sectList.size(); ++i) - { - fputs(sectList[i].first.c_str(), fp); - fputs("\n", fp); - - for (size_t j = 0; j < sectList[i].second.size(); ++j) - { - fputs(sectList[i].second[j].first.c_str(), fp); - fputs("=", fp); - fputs(sectList[i].second[j].second.c_str(), fp); - fputs("\n", fp); - } - } - - fclose(fp); - return 0; -} - -static int writeStringValue(const char* section, const char* key, const char* val, const char* file) -{ - if (section == NULL || key == NULL || val == NULL || file == NULL) - { - return -1; - } - - char sect[256]; - sprintf(sect, "[%s]", section); - return IniWriteValue(sect, key, val, file); -} - -HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, HGInt value) -{ - if (NULL == fileName || NULL == appName || NULL == keyName) - { - return HGBASE_ERR_INVALIDARG; - } - - char str[8]; - sprintf(str, "%d", value); - if (0 != writeStringValue(appName, keyName, str, fileName)) - return HGBASE_ERR_FAIL; - return HGBASE_ERR_OK; -} - -HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, const HGChar* value) -{ - if (NULL == fileName || NULL == appName || NULL == keyName) - { - return HGBASE_ERR_INVALIDARG; - } - - if (0 != writeStringValue(appName, keyName, value, fileName)) - return HGBASE_ERR_FAIL; - return HGBASE_ERR_OK; -} - -static int IniReadValue(const char* section, const char* key, char* val, const char* def, const char* file) -{ - typedef std::vector > KeyList; - typedef std::vector > SectionList; - - SectionList sectList; - - FILE* fp = fopen(file, "r"); - if (fp != NULL) - { - KeyList* pCurKeyList = NULL; - - while (feof(fp) == 0) - { - char lineContent[256] = { 0 }; - if (NULL == fgets(lineContent, 256, fp)) - { - continue; - } - - if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n')) - { - continue; - } - - for (size_t i = 0; i < strlen(lineContent); ++i) - { - if (lineContent[i] == '\r' || lineContent[i] == '\n') - { - lineContent[i] = '\0'; - break; - } - } - - if (lineContent[0] == '[') - { - std::pair pr; - pr.first = lineContent; - sectList.push_back(pr); - pCurKeyList = §List[sectList.size() - 1].second; - } - else - { - int pos = -1; - for (int i = 0; i < (int)strlen(lineContent); ++i) - { - if (lineContent[i] == '=') - { - pos = i; - break; - } - } - - if (NULL != pCurKeyList) - { - std::pair pr; - if (-1 != pos) - { - pr.first.assign(lineContent, pos); - pr.second.assign(lineContent + pos + 1); - } - else - { - pr.first = lineContent; - } - - pCurKeyList->push_back(pr); - } - } - } - - fclose(fp); - } - else - { - strcpy(val, def); - return -1; - } - - bool bGetVal = false; - for (size_t i = 0; i < sectList.size(); ++i) - { - if (strcmp(sectList[i].first.c_str(), section) == 0) - { - for (size_t j = 0; j < sectList[i].second.size(); ++j) - { - if (strcmp(sectList[i].second[j].first.c_str(), key) == 0) - { - strcpy(val, sectList[i].second[j].second.c_str()); - bGetVal = true; - break; - } - } - - break; - } - } - - if (!bGetVal) - { - strcpy(val, def); - } - - return 0; -} - -static int readStringValue(const char* section, const char* key, char* val, const char* def, const char* file) -{ - if (section == NULL || key == NULL || val == NULL || def == NULL || file == NULL) - { - return -1; - } - - char sect[256]; - sprintf(sect, "[%s]", section); - return IniReadValue(sect, key, val, def, file); -} - -HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, HGInt def, HGInt* value) -{ - if (NULL == appName || NULL == keyName || NULL == value) - { - return HGBASE_ERR_INVALIDARG; - } - - char strRet[256] = { 0 }; - char strDef[32]; - sprintf(strDef, "%d", def); - readStringValue(appName, keyName, strRet, strDef, fileName); - *value = atoi(strRet); - return HGBASE_ERR_OK; -} - -HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen) -{ - if (NULL == appName || NULL == keyName || NULL == value) - { - return HGBASE_ERR_INVALIDARG; - } - - readStringValue(appName, keyName, value, def, fileName); - return HGBASE_ERR_OK; -} - HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result, HGUInt maxLen) { if (NULL == fileName || NULL == result || 0 == maxLen) diff --git a/modules/base/HGUtility.h b/modules/base/HGUtility.h index 185e8e45..e3f375e4 100644 --- a/modules/base/HGUtility.h +++ b/modules/base/HGUtility.h @@ -40,7 +40,7 @@ HGEXPORT HGResult HGAPI HGBase_GetUuid(HGChar* uuid, HGUInt maxLen); /* 鑾峰彇涓存椂鏂囦欢鍚 */ -HGEXPORT HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen); +HGEXPORT HGResult HGAPI HGBase_GetTmpFileName(const HGChar *suffix, HGChar* fileName, HGUInt maxLen); /* 鑾峰彇閰嶇疆鏂囦欢璺緞 */ @@ -66,26 +66,6 @@ HGEXPORT HGResult HGAPI HGBase_GetFileName(const HGChar *filePath, HGChar* name, */ HGEXPORT HGResult HGAPI HGBase_GetFilePath(const HGChar* filePath, HGChar* path, HGUInt maxLen); -/* 璁剧疆ini鏂囦欢鐨勫 -*/ -HGEXPORT HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, HGInt value); - -/* 璁剧疆ini鏂囦欢鐨勫 -*/ -HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, const HGChar *value); - -/* 鑾峰彇ini鏂囦欢鐨勫 -*/ -HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, HGInt def, HGInt* value); - -/* 鑾峰彇ini鏂囦欢鐨勫 -*/ -HGEXPORT HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName, - const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen); - /* 灏嗘枃浠跺悕鏍囧噯鍖 */ HGEXPORT HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar *result, HGUInt maxLen); diff --git a/modules/imgfmt/HGOfdImpl.cpp b/modules/imgfmt/HGOfdImpl.cpp index caabe489..2f4a035b 100644 --- a/modules/imgfmt/HGOfdImpl.cpp +++ b/modules/imgfmt/HGOfdImpl.cpp @@ -653,7 +653,7 @@ bool HGOfdImageWriterImpl::AddPublicResXml() bool HGOfdImageWriterImpl::AddXmlFile(tinyxml2::XMLDocument& xmlDoc, const HGChar* name) { HGChar tmpName[256]; - HGBase_GetTmpFileName(tmpName, 256); + HGBase_GetTmpFileName(NULL, tmpName, 256); if (tinyxml2::XML_SUCCESS != xmlDoc.SaveFile(tmpName)) { return false; @@ -681,7 +681,7 @@ bool HGOfdImageWriterImpl::AddXmlFile(tinyxml2::XMLDocument& xmlDoc, const HGCha bool HGOfdImageWriterImpl::AddJpegImageFile(HGImage image, const HGJpegSaveInfo* info, const HGChar* name) { HGChar tmpName[256]; - HGBase_GetTmpFileName(tmpName, 256); + HGBase_GetTmpFileName(NULL, tmpName, 256); if (HGBASE_ERR_OK != HGImgFmt_SaveJpegImage(image, info, tmpName)) { return false; diff --git a/modules/imgproc/HGOCRHanvon.cpp b/modules/imgproc/HGOCRHanvon.cpp index c7596272..e1997099 100644 --- a/modules/imgproc/HGOCRHanvon.cpp +++ b/modules/imgproc/HGOCRHanvon.cpp @@ -103,8 +103,7 @@ HGResult HGOCRHanvon::ImageOCR(HGImage image, class HGOCRRetImpl **ocrRet) } HGChar tmpFileName[256]; - HGBase_GetTmpFileName(tmpFileName, 256); - strcat(tmpFileName, ".bmp"); + HGBase_GetTmpFileName("bmp", tmpFileName, 256); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); char* rst = NULL; @@ -154,8 +153,7 @@ HGResult HGOCRHanvon::ImageOCRToFile(HGImage image, HGUInt outType, const HGChar } HGChar tmpFileName[256]; - HGBase_GetTmpFileName(tmpFileName, 256); - strcat(tmpFileName, ".bmp"); + HGBase_GetTmpFileName("bmp", tmpFileName, 256); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); HGChar tmpDir[256]; @@ -179,8 +177,7 @@ HGResult HGOCRHanvon::ImageTextDirectOCR(HGImage image, HGUInt* direct) } HGChar tmpFileName[256]; - HGBase_GetTmpFileName(tmpFileName, 256); - strcat(tmpFileName, ".bmp"); + HGBase_GetTmpFileName("bmp", tmpFileName, 256); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); int dire = -1; diff --git a/sdk/webservice/ManagerV1.cpp b/sdk/webservice/ManagerV1.cpp index 27bab8d1..ec800651 100644 --- a/sdk/webservice/ManagerV1.cpp +++ b/sdk/webservice/ManagerV1.cpp @@ -2,6 +2,7 @@ #include "base/HGBuffer.h" #include "base/HGBase64.h" #include "base/HGUtility.h" +#include "base/HGIni.h" #include "base/HGInfo.h" #include "imgfmt/HGJpeg.h" #include "imgfmt/HGOfd.h" @@ -144,8 +145,10 @@ namespace ver_1 HGBase_CreateLock(&m_lock); m_devName.clear(); m_devHandle = NULL; - m_scanEvent = NULL; - m_scanParam = NULL; + m_saneEvent = NULL; + m_saneParam = NULL; + m_saneImageCallback = NULL; + m_saneImageParam = NULL; m_scanInsertImgName.clear(); m_scanIsInsert = false; @@ -227,21 +230,39 @@ namespace ver_1 m_scanning = false; } - void ManagerV1::SetScanEvent(ScanEvent event, void* param) + void ManagerV1::SetSaneEvent(SaneEvent event, void* param) { assert(NULL != event && NULL != param); HGBase_EnterLock(m_lock); - m_scanEvent = event; - m_scanParam = param; + m_saneEvent = event; + m_saneParam = param; HGBase_LeaveLock(m_lock); } - void ManagerV1::ResetScanEvent() + void ManagerV1::SetSaneImageCallback(SaneImageCallback func, void* param) + { + assert(NULL != func && NULL != param); + + HGBase_EnterLock(m_lock); + m_saneImageCallback = func; + m_saneImageParam = param; + HGBase_LeaveLock(m_lock); + } + + void ManagerV1::ResetSaneEvent() { HGBase_EnterLock(m_lock); - m_scanEvent = NULL; - m_scanParam = NULL; + m_saneEvent = NULL; + m_saneParam = NULL; + HGBase_LeaveLock(m_lock); + } + + void ManagerV1::ResetSaneImageCallback() + { + HGBase_EnterLock(m_lock); + m_saneImageCallback = NULL; + m_saneImageParam = NULL; HGBase_LeaveLock(m_lock); } @@ -675,7 +696,7 @@ namespace ver_1 } HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); + HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportOfdFile(devId, isAuto, tmpFileName); *data = GetBuffer(tmpFileName, size); @@ -754,7 +775,7 @@ namespace ver_1 } HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); + HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportPdfFile(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); @@ -833,7 +854,7 @@ namespace ver_1 } HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); + HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportTiff(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); @@ -917,7 +938,7 @@ namespace ver_1 } HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); + HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportZipFile(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); @@ -936,7 +957,7 @@ namespace ver_1 GetCurDevId(devId); HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); + HGBase_GetTmpFileName(NULL, tmpFileName, 512); if (0 == uploadParam.format) { strcat(tmpFileName, ".ofd"); @@ -1364,8 +1385,11 @@ namespace ver_1 { HGChar docsPath[256]; HGBase_GetDocumentsPath(docsPath, 256); + HGChar procName[256]; + HGBase_GetProcessName(procName, 256); + HGChar imgPath[512]; - sprintf(imgPath, "%s%s/", docsPath, Utf8ToStdString(devId).c_str()); + sprintf(imgPath, "%s%s/%s/", docsPath, procName, Utf8ToStdString(devId).c_str()); HGChar stdImgPath[512]; HGBase_StandardiseFileName(imgPath, stdImgPath, 512); @@ -1893,11 +1917,6 @@ namespace ver_1 SANE_Device* sane_dev = (SANE_Device*)data; HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str()); - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_ARRIVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - OpenDevParam* openDevParam = new OpenDevParam; openDevParam->mgr = p; openDevParam->devName = sane_dev->name; @@ -1909,6 +1928,11 @@ namespace ver_1 { delete openDevParam; } + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_ARRIVE, sane_dev->name, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); } break; case SANE_EVENT_DEVICE_LEFT: @@ -1916,11 +1940,6 @@ namespace ver_1 SANE_Device* sane_dev = (SANE_Device*)data; HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str()); - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_REMOVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - CloseDevParam* closeDevParam = new CloseDevParam; closeDevParam->mgr = p; closeDevParam->devName = sane_dev->name; @@ -1932,6 +1951,11 @@ namespace ver_1 { delete closeDevParam; } + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_REMOVE, sane_dev->name, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); } break; case SANE_EVENT_STATUS: @@ -1939,8 +1963,8 @@ namespace ver_1 HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_STATUS, (void*)data, (void*)0, p->m_scanParam); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_STATUS, (const char*)data, false, p->m_saneParam); HGBase_LeaveLock(p->m_lock); } break; @@ -1949,8 +1973,8 @@ namespace ver_1 HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_ERROR, (void*)data, (void*)0, p->m_scanParam); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_ERROR, (const char*)data, (0 != *len), p->m_saneParam); HGBase_LeaveLock(p->m_lock); } break; @@ -1959,8 +1983,8 @@ namespace ver_1 HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_WORKING, (void*)data, (void*)0, p->m_scanParam); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_WORKING, (const char*)data, false, p->m_saneParam); HGBase_LeaveLock(p->m_lock); } break; @@ -2046,8 +2070,8 @@ namespace ver_1 imgBase64 += GetBase64(img); HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_IMAGE, (void*)imgName.c_str(), (void*)imgBase64.c_str(), p->m_scanParam); + if (NULL != p->m_saneImageCallback) + p->m_saneImageCallback(imgName.c_str(), imgBase64.c_str(), p->m_saneImageParam); HGBase_LeaveLock(p->m_lock); HGBase_DestroyImage(img); @@ -2058,11 +2082,6 @@ namespace ver_1 { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - ScanFinishParam* scanFinishParam = new ScanFinishParam; scanFinishParam->mgr = p; @@ -2073,6 +2092,11 @@ namespace ver_1 { delete scanFinishParam; } + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_FINISH, (const char*)data, (0 != *len), p->m_saneParam); + HGBase_LeaveLock(p->m_lock); } break; } diff --git a/sdk/webservice/ManagerV1.h b/sdk/webservice/ManagerV1.h index 05a7c18e..dd6fb301 100644 --- a/sdk/webservice/ManagerV1.h +++ b/sdk/webservice/ManagerV1.h @@ -16,13 +16,12 @@ namespace ver_1 { enum { - SCANEVENT_ARRIVE = 1L, - SCANEVENT_REMOVE, - SCANEVENT_STATUS, - SCANEVENT_WORKING, - SCANEVENT_IMAGE, - SCANEVENT_FINISH, - SCANEVENT_ERROR + SANEEVENT_ARRIVE = 1L, + SANEEVENT_REMOVE, + SANEEVENT_STATUS, + SANEEVENT_WORKING, + SANEEVENT_FINISH, + SANEEVENT_ERROR }; struct DevParam @@ -87,7 +86,8 @@ namespace ver_1 int format; // 涓婁紶鏍煎紡 0: ofd 1: pdf 2: zip, 榛樿2 }; - typedef void (*ScanEvent)(HGUInt event, void* value1, void* value2, void* param); + typedef void (*SaneEvent)(int code, const char* str, bool err, void* param); + typedef void (*SaneImageCallback)(const char* name, const char *base64, void* param); class ManagerV1 : public Manager { @@ -103,9 +103,11 @@ namespace ver_1 void ScanFinish(const ScanFinishParam* param); // 璁剧疆鍥炶皟 - void SetScanEvent(ScanEvent event, void* param); + void SetSaneEvent(SaneEvent event, void* param); + void SetSaneImageCallback(SaneImageCallback func, void* param); // 娓呯悊鍥炶皟 - void ResetScanEvent(); + void ResetSaneEvent(); + void ResetSaneImageCallback(); // 鎵弿 bool Scan(const std::string& insertImgName, bool isInsert); // 鍋滄鎵弿 @@ -185,8 +187,10 @@ namespace ver_1 std::string m_devName; SANE_Handle m_devHandle; DevParam m_devParam; - ScanEvent m_scanEvent; - void* m_scanParam; + SaneEvent m_saneEvent; + void* m_saneParam; + SaneImageCallback m_saneImageCallback; + void* m_saneImageParam; std::string m_scanInsertImgName; bool m_scanIsInsert; diff --git a/sdk/webservice/ManagerV2.cpp b/sdk/webservice/ManagerV2.cpp index 89782758..5809edcf 100644 --- a/sdk/webservice/ManagerV2.cpp +++ b/sdk/webservice/ManagerV2.cpp @@ -2,7 +2,9 @@ #include "base/HGBuffer.h" #include "base/HGBase64.h" #include "base/HGUtility.h" +#include "base/HGIni.h" #include "base/HGInfo.h" +#include "base/HGTime.h" #include "imgfmt/HGJpeg.h" #include "imgfmt/HGOfd.h" #include "imgfmt/HGPdf.h" @@ -15,21 +17,107 @@ namespace ver_2 ManagerV2::ManagerV2(HGMsgPump msgPump) : Manager(msgPump) { - + HGBase_CreateLock(&m_lock); + + HGChar docsPath[256]; + HGBase_GetDocumentsPath(docsPath, 256); + HGChar procName[256]; + HGBase_GetProcessName(procName, 256); + HGChar defSavePath[256]; + sprintf(defSavePath, "%s%s/", docsPath, procName); + + m_globalCfg.fileSavePath = GetCfgStringValue("global", "fileSavePath", defSavePath); + m_globalCfg.fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago"); + m_globalCfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time"); + m_globalCfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); + m_globalCfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80); + m_globalCfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw"); + m_globalCfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80); + m_globalCfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", ""); + m_globalCfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80); + m_globalCfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi"); + m_globalCfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", ""); + m_globalCfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", ""); + m_globalCfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", ""); + m_globalCfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21); + + m_globalCfg.fileSavePath.push_back('/'); + HGChar stdSavePath[256]; + HGBase_StandardiseFileName(m_globalCfg.fileSavePath.c_str(), stdSavePath, 256); + m_globalCfg.fileSavePath = stdSavePath; + + // 璇诲彇m_saveFilePathList + m_saveFilePathList.clear(); + m_saneEvent = NULL; + m_saneParam = NULL; + m_saneImageCallback = NULL; + m_saneImageParam = NULL; + + m_initDevice = false; + m_devNameList.clear(); + m_openDevice = false; + m_devName.clear(); + m_devHandle = NULL; + m_devParam.Reset(); + m_scanning = false; } ManagerV2::~ManagerV2() { + DeinitDevice(); + HGBase_DestroyLock(m_lock); + m_lock = NULL; + } + + void ManagerV2::SetSaneEvent(SaneEvent event, void* param) + { + assert(NULL != event && NULL != param); + + HGBase_EnterLock(m_lock); + m_saneEvent = event; + m_saneParam = param; + HGBase_LeaveLock(m_lock); + } + + void ManagerV2::SetSaneImageCallback(SaneImageCallback func, void* param) + { + assert(NULL != func && NULL != param); + + HGBase_EnterLock(m_lock); + m_saneImageCallback = func; + m_saneImageParam = param; + HGBase_LeaveLock(m_lock); + } + + void ManagerV2::ResetSaneEvent() + { + HGBase_EnterLock(m_lock); + m_saneEvent = NULL; + m_saneParam = NULL; + HGBase_LeaveLock(m_lock); + } + + void ManagerV2::ResetSaneImageCallback() + { + HGBase_EnterLock(m_lock); + m_saneImageCallback = NULL; + m_saneImageParam = NULL; + HGBase_LeaveLock(m_lock); } void ManagerV2::ScanFinish(const ScanFinishParam* param) { assert(NULL != param && this == param->mgr); + + m_scanning = false; } int ManagerV2::SetGlobalConfig(const GlobalConfig& cfg) { + if (m_scanning) + return -1; + if ("date_time" != cfg.fileNameMode && "random" != cfg.fileNameMode) return -1; if ("jpg" != cfg.imageFormat && "bmp" != cfg.imageFormat && "png" != cfg.imageFormat && "tif" != cfg.imageFormat @@ -37,46 +125,38 @@ namespace ver_2 return -1; if (cfg.imageJpegQuality < 0 || cfg.imageJpegQuality > 100) return -1; - if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression && "jpeg" != cfg.imageTiffCompression) + if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression + && "jpeg" != cfg.imageTiffCompression && "ccitt-g4" != cfg.imageTiffCompression) return -1; if (cfg.imageTiffJpegQuality < 0 || cfg.imageTiffJpegQuality > 100) return -1; - SetCfgStringValue("global", "fileSavePath", Utf8ToStdString(cfg.fileSavePath)); - SetCfgStringValue("global", "fileNamePrefix", Utf8ToStdString(cfg.fileNamePrefix)); - SetCfgStringValue("global", "fileNameMode", cfg.fileNameMode); - SetCfgStringValue("global", "imageFormat", cfg.imageFormat); - SetCfgIntValue("global", "imageJpegQuality", cfg.imageJpegQuality); - SetCfgStringValue("global", "imageTiffCompression", cfg.imageTiffCompression); - SetCfgIntValue("global", "imageTiffJpegQuality", cfg.imageTiffJpegQuality); - SetCfgStringValue("global", "uploadHttpHost", cfg.uploadHttpHost); - SetCfgIntValue("global", "uploadHttpPort", cfg.uploadHttpPort); - SetCfgStringValue("global", "uploadHttpPath", cfg.uploadHttpPath); - SetCfgStringValue("global", "uploadFtpUser", cfg.uploadFtpUser); - SetCfgStringValue("global", "uploadFtpPassword", cfg.uploadFtpPassword); - SetCfgStringValue("global", "uploadFtpHost", cfg.uploadFtpHost); - SetCfgIntValue("global", "uploadFtpPort", cfg.uploadFtpPort); + m_globalCfg = cfg; + m_globalCfg.fileSavePath.push_back('/'); + HGChar stdSavePath[256]; + HGBase_StandardiseFileName(m_globalCfg.fileSavePath.c_str(), stdSavePath, 256); + m_globalCfg.fileSavePath = stdSavePath; + + SetCfgStringValue("global", "fileSavePath", m_globalCfg.fileSavePath); + SetCfgStringValue("global", "fileNamePrefix", m_globalCfg.fileNamePrefix); + SetCfgStringValue("global", "fileNameMode", m_globalCfg.fileNameMode); + SetCfgStringValue("global", "imageFormat", m_globalCfg.imageFormat); + SetCfgIntValue("global", "imageJpegQuality", m_globalCfg.imageJpegQuality); + SetCfgStringValue("global", "imageTiffCompression", m_globalCfg.imageTiffCompression); + SetCfgIntValue("global", "imageTiffJpegQuality", m_globalCfg.imageTiffJpegQuality); + SetCfgStringValue("global", "uploadHttpHost", m_globalCfg.uploadHttpHost); + SetCfgIntValue("global", "uploadHttpPort", m_globalCfg.uploadHttpPort); + SetCfgStringValue("global", "uploadHttpPath", m_globalCfg.uploadHttpPath); + SetCfgStringValue("global", "uploadFtpUser", m_globalCfg.uploadFtpUser); + SetCfgStringValue("global", "uploadFtpPassword", m_globalCfg.uploadFtpPassword); + SetCfgStringValue("global", "uploadFtpHost", m_globalCfg.uploadFtpHost); + SetCfgIntValue("global", "uploadFtpPort", m_globalCfg.uploadFtpPort); return 0; } int ManagerV2::GetGlobalConfig(GlobalConfig& cfg) { - HGChar docPath[256]; - HGBase_GetDocumentsPath(docPath, 256); - cfg.fileSavePath = StdStringToUtf8(GetCfgStringValue("global", "fileSavePath", docPath)); - cfg.fileNamePrefix = StdStringToUtf8(GetCfgStringValue("global", "fileNamePrefix", "Huago")); - cfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time"); - cfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); - cfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80); - cfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw"); - cfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80); - cfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", ""); - cfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80); - cfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi"); - cfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", ""); - cfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", ""); - cfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", ""); - cfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21); + cfg = m_globalCfg; return 0; } @@ -85,11 +165,11 @@ namespace ver_2 imgBase64.clear(); HGUInt imgType = 0; - HGImgFmt_GetImgFmtType(Utf8ToStdString(imagePath).c_str(), &imgType); + HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType); if (0 == imgType) return -1; - int ret = LoadBase64(Utf8ToStdString(imagePath), imgBase64); + int ret = LoadBase64(imagePath, imgBase64); if (0 != ret) return ret; @@ -135,17 +215,55 @@ namespace ver_2 if (0 != ret) return ret; - imagePath = StdStringToUtf8(imagePath2); + imagePath = imagePath2; return 0; } int ManagerV2::DeleteLocalFile(const std::string& filePath) { - return 0; + int ret = -1; + for (int i = 0; i < (int)m_saveFilePathList.size(); ++i) + { + if (filePath == m_saveFilePathList[i]) + { + if (HGBASE_ERR_OK == HGBase_DeleteFile(filePath.c_str())) + { + m_saveFilePathList.erase(m_saveFilePathList.begin() + i); + ret = 0; + } + + break; + } + } + + return ret; } int ManagerV2::ClearGlobalFileSavePath() { + int i = 0; + while (i < (int)m_saveFilePathList.size()) + { + HGChar path[256]; + HGBase_GetFilePath(m_saveFilePathList[i].c_str(), path, 256); + if (0 == strcmp(path, m_globalCfg.fileSavePath.c_str())) + { + if (HGBASE_ERR_OK == HGBase_DeleteFile(m_saveFilePathList[i].c_str())) + { + m_saveFilePathList.erase(m_saveFilePathList.begin() + i); + } + else + { + ++i; + } + } + else + { + ++i; + } + } + + HGBase_DeleteDir(m_globalCfg.fileSavePath.c_str()); return 0; } @@ -182,6 +300,132 @@ namespace ver_2 return 0; } + int ManagerV2::InitDevice() + { + if (m_initDevice) + return -1; + + SANE_Int version_code = 0; + SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this); + assert(SANE_STATUS_GOOD == status); + m_initDevice = true; + return 0; + } + + int ManagerV2::DeinitDevice() + { + if (m_initDevice) + { + CloseDevice(); + sane_exit(); + m_devNameList.clear(); + m_initDevice = false; + } + + return 0; + } + + int ManagerV2::GetDeviceNameList(std::vector& deviceNameList) + { + deviceNameList.clear(); + + if (!m_initDevice) + return -1; + + HGBase_EnterLock(m_lock); + deviceNameList = m_devNameList; + HGBase_LeaveLock(m_lock); + return 0; + } + + int ManagerV2::OpenDevice(const std::string& deviceName) + { + if (!m_initDevice || m_openDevice) + return -1; + + std::string devName = deviceName; + HGBase_EnterLock(m_lock); + if (devName.empty() && m_devNameList.size() > 0) + devName = m_devNameList[0]; + HGBase_LeaveLock(m_lock); + + SANE_Handle dev = NULL; + SANE_Status status = sane_open(devName.c_str(), &dev); + if (SANE_STATUS_GOOD != status) + return -1; + + m_devHandle = dev; + m_devName = devName; + // 鍔犺浇m_devParam锛屽苟璁剧疆鍒拌澶 + m_openDevice = true; + return 0; + } + + int ManagerV2::CloseDevice() + { + if (m_openDevice) + { + StopScan(); + sane_close(m_devHandle); + m_devHandle = NULL; + m_devName.clear(); + m_devParam.Reset(); + m_openDevice = false; + } + + return 0; + } + + int ManagerV2::SetDeviceParam(const DeviceParam& param) + { + if (!m_openDevice || m_scanning) + return -1; + + m_devParam = param; + // 璁剧疆鍒拌澶 + return 0; + } + + int ManagerV2::GetDeviceParam(DeviceParam& param) + { + param.Reset(); + + if (!m_openDevice) + return -1; + + param = m_devParam; + return 0; + } + + int ManagerV2::GetCurrDeviceName(std::string& deviceName) + { + deviceName = m_devName; + return 0; + } + + int ManagerV2::StartScan() + { + if (!m_openDevice || m_scanning) + return -1; + + SANE_Status status = sane_start(m_devHandle); + if (SANE_STATUS_GOOD != status) + return -1; + + m_scanning = true; + return 0; + } + + int ManagerV2::StopScan() + { + if (m_scanning) + { + sane_cancel(m_devHandle); + } + + return 0; + } + std::string ManagerV2::GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def) { HGChar cfgPath[256]; @@ -312,26 +556,19 @@ namespace ver_2 std::string ManagerV2::GetFilePath(const std::string& suffix) { - HGChar docPath[256]; - HGBase_GetDocumentsPath(docPath, 256); - std::string fileSavePath = GetCfgStringValue("global", "fileSavePath", docPath); - std::string fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago"); - std::string fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time"); - char filePath[256] = { 0 }; - if ("random" == fileNameMode) + if ("random" == m_globalCfg.fileNameMode) { HGChar uuid[256]; HGBase_GetUuid(uuid, 256); - sprintf(filePath, "%s%s%s.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), uuid, suffix.c_str()); + sprintf(filePath, "%s%s%s.%s", m_globalCfg.fileSavePath.c_str(), m_globalCfg.fileNamePrefix.c_str(), uuid, suffix.c_str()); } else { - timeb tb; - ftime(&tb); - struct tm* p = localtime(&tb.time); - sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), (1900 + p->tm_year), - (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, suffix.c_str()); + HGTimeInfo timeInfo; + HGBase_GetLocalTime(&timeInfo); + sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d%03d.%s", m_globalCfg.fileSavePath.c_str(), m_globalCfg.fileNamePrefix.c_str(), timeInfo.year, + timeInfo.month, timeInfo.day, timeInfo.hour, timeInfo.minute, timeInfo.second, timeInfo.milliseconds, suffix.c_str()); } return filePath; @@ -339,7 +576,150 @@ namespace ver_2 std::string ManagerV2::GetImagePath() { - std::string imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); - return GetFilePath(imageFormat); + return GetFilePath(m_globalCfg.imageFormat); + } + + int ManagerV2::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param) + { + (void)hdev; + (void)len; + + ManagerV2* p = (ManagerV2*)param; + switch (code) + { + case SANE_EVENT_DEVICE_ARRIVED: + { + SANE_Device* sane_dev = (SANE_Device*)data; + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_DEVICE_ARRIVED, name=%s", Utf8ToStdString(sane_dev->name).c_str()); + + HGBase_EnterLock(p->m_lock); + p->m_devNameList.push_back(sane_dev->name); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_ARRIVE, sane_dev->name, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_DEVICE_LEFT: + { + SANE_Device* sane_dev = (SANE_Device*)data; + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_DEVICE_LEFT, name=%s", Utf8ToStdString(sane_dev->name).c_str()); + + HGBase_EnterLock(p->m_lock); + for (int i = 0; i < (int)p->m_devNameList.size(); ++i) + { + if (0 == strcmp(sane_dev->name, p->m_devNameList[i].c_str())) + { + p->m_devNameList.erase(p->m_devNameList.begin() + i); + break; + } + } + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_REMOVE, sane_dev->name, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_WORKING: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_WORKING, (const char*)data, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_SCAN_FINISHED: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); + + ScanFinishParam* scanFinishParam = new ScanFinishParam; + scanFinishParam->mgr = p; + + HGMsg msg; + msg.id = MSGID_SCAN_FINISH; + msg.data = scanFinishParam; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) + { + delete scanFinishParam; + } + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_FINISH, (const char*)data, (0 != *len), p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_STATUS: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_STATUS, (const char*)data, false, p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_ERROR: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneEvent) + p->m_saneEvent(SANEEVENT_ERROR, (const char*)data, (0 != *len), p->m_saneParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_IMAGE_OK: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_IMAGE_OK"); + + SANE_Image* sane_img = (SANE_Image*)data; + + HGUInt imgType = 0; + if (sane_img->header.format == SANE_FRAME_GRAY) + { + if (1 == sane_img->header.depth) + imgType = HGBASE_IMGTYPE_BINARY; + else if (8 == sane_img->header.depth) + imgType = HGBASE_IMGTYPE_GRAY; + } + else if (sane_img->header.format == SANE_FRAME_RGB) + imgType = HGBASE_IMGTYPE_RGB; + + HGByte* data = sane_img->data; + HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines, + imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP }; + + HGImage img = NULL; + HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + std::string imagePath = p->GetImagePath(); + + HGImgFmtSaveInfo saveInfo; + saveInfo.jpegQuality = p->m_globalCfg.imageJpegQuality; + saveInfo.tiffJpegQuality = p->m_globalCfg.imageTiffJpegQuality; + saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW; + if ("none" == p->m_globalCfg.imageTiffCompression) + saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE; + else if ("jpeg" == p->m_globalCfg.imageTiffCompression) + saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG; + else if ("ccitt-g4" == p->m_globalCfg.imageTiffCompression) + saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4; + if (HGBASE_ERR_OK == HGImgFmt_SaveImage(img, 0, &saveInfo, imagePath.c_str())) + { + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_saneImageCallback) + p->m_saneImageCallback(imagePath.c_str(), p->m_saneImageParam); + HGBase_LeaveLock(p->m_lock); + } + + HGBase_DestroyImage(img); + } + } + break; + } + + return 0; } } diff --git a/sdk/webservice/ManagerV2.h b/sdk/webservice/ManagerV2.h index 61c196bd..22c4aedd 100644 --- a/sdk/webservice/ManagerV2.h +++ b/sdk/webservice/ManagerV2.h @@ -14,8 +14,26 @@ namespace ver_2 { + enum + { + SANEEVENT_ARRIVE = 1L, + SANEEVENT_REMOVE, + SANEEVENT_STATUS, + SANEEVENT_WORKING, + SANEEVENT_FINISH, + SANEEVENT_ERROR + }; + struct GlobalConfig { + GlobalConfig() + { + imageJpegQuality = 0; + imageTiffJpegQuality = 0; + uploadHttpPort = 0; + uploadFtpPort = 0; + } + // 鏂囦欢淇濆瓨 std::string fileSavePath; std::string fileNamePrefix; @@ -35,12 +53,69 @@ namespace ver_2 unsigned short uploadFtpPort; }; + struct DeviceParam + { + DeviceParam() + { + Reset(); + } + + void Reset() + { + + } + + // 棰滆壊妯″紡 + std::vector colorModeList; + std::string colorMode; + // 鎵弿椤甸潰 + std::vector pageModeList; + std::string pageMode; + // 鍒嗚鲸鐜 + std::vector resolutionList; + int resolution; + // 浜害 + int brightnessMin; + int brightnessMax; + int brightness; + // 瀵规瘮搴 + int contrastMin; + int contrastMax; + int contrast; + // 浼介┈ + int gammaMin; + int gammaMax; + int gamma; + // 绾稿紶灏哄 + std::vector paperSizeList; + std::string pageSize; + int paperSizeWidth; + int paperSizeHeight; + // 鎵嬪姩瑁佸壀 + bool paperCutEnabled; + float paperCutLeft; + float paperCutTop; + float paperCutRight; + float paperCutBottom; + // 鏄惁绾犲亸 + bool autoCrop; + }; + + typedef void (*SaneEvent)(int code, const char *str, bool err, void* param); + typedef void (*SaneImageCallback)(const char* path, void* param); + class ManagerV2 : public Manager { public: ManagerV2(HGMsgPump msgPump); virtual ~ManagerV2(); + // 璁剧疆鍥炶皟 + void SetSaneEvent(SaneEvent event, void* param); + void SetSaneImageCallback(SaneImageCallback func, void* param); + // 娓呯悊鍥炶皟 + void ResetSaneEvent(); + void ResetSaneImageCallback(); // 鎵弿瀹屾垚 void ScanFinish(const ScanFinishParam* param); @@ -72,6 +147,27 @@ namespace ver_2 // 涓婁紶鏂囦欢 int UploadLocalFile(const std::string& filePath, const std::string& mode, const std::string& remoteFilePath); + // 璁惧鍒濆鍖 + int InitDevice(); + // 璁惧鍙嶅垵濮嬪寲 + int DeinitDevice(); + // 鑾峰彇璁惧鍒楄〃 + int GetDeviceNameList(std::vector &deviceNameList); + // 鎵撳紑璁惧 + int OpenDevice(const std::string& deviceName); + // 鍏抽棴璁惧 + int CloseDevice(); + // 璁剧疆璁惧鍙傛暟 + int SetDeviceParam(const DeviceParam& param); + // 鑾峰彇璁惧鍙傛暟 + int GetDeviceParam(DeviceParam& param); + // 鑾峰彇褰撳墠璁惧鍚 + int GetCurrDeviceName(std::string& deviceName); + // 寮濮嬫壂鎻 + int StartScan(); + // 鍋滄鎵弿 + int StopScan(); + private: static std::string GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def); static int GetCfgIntValue(const std::string& app, const std::string& key, int def); @@ -81,10 +177,25 @@ namespace ver_2 static bool SetCfgBoolValue(const std::string& app, const std::string& key, bool val); static int LoadBase64(const std::string& fileName, std::string& base64); static int SaveBase64(const std::string& base64, const std::string& fileName); - static std::string GetFilePath(const std::string& suffix); - static std::string GetImagePath(); + std::string GetFilePath(const std::string& suffix); + std::string GetImagePath(); + static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param); private: - + HGLock m_lock; + GlobalConfig m_globalCfg; + std::vector m_saveFilePathList; + SaneEvent m_saneEvent; + void* m_saneParam; + SaneImageCallback m_saneImageCallback; + void* m_saneImageParam; + + bool m_initDevice; + std::vector m_devNameList; + bool m_openDevice; + SANE_Handle m_devHandle; + std::string m_devName; + DeviceParam m_devParam; + bool m_scanning; }; } \ No newline at end of file diff --git a/sdk/webservice/SockIoUser.cpp b/sdk/webservice/SockIoUser.cpp index d5a01f54..27d7f89c 100644 --- a/sdk/webservice/SockIoUser.cpp +++ b/sdk/webservice/SockIoUser.cpp @@ -16,12 +16,14 @@ namespace ver_1 #endif : WebUser(server, id, ip, port, sockConn) { - GetManager()->SetScanEvent(ScanCallback, this); + GetManager()->SetSaneEvent(SaneEvent2, this); + GetManager()->SetSaneImageCallback(SaneImageCallback2, this); } SockIoUser::~SockIoUser() { - GetManager()->ResetScanEvent(); + GetManager()->ResetSaneImageCallback(); + GetManager()->ResetSaneEvent(); } ManagerV1* SockIoUser::GetManager() @@ -425,42 +427,53 @@ namespace ver_1 } } - void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* param) + void SockIoUser::SaneEvent2(int code, const char* str, bool err, void* param) { SockIoUser* p = (SockIoUser*)param; char* resp = NULL; - if (SCANEVENT_ARRIVE == event) + if (SANEEVENT_ARRIVE == code) { resp = new char[256]; - sprintf(resp, "42[\"success\", \"%s\"]", (const char*)value1); + sprintf(resp, "42[\"success\", \"%s\"]", str); } - else if (SCANEVENT_REMOVE == event) + else if (SANEEVENT_REMOVE == code) { } - else if (SCANEVENT_WORKING == event) + else if (SANEEVENT_WORKING == code) { resp = new char[256]; sprintf(resp, "42[\"event\", \"%s\"]", "......"); } - else if (SCANEVENT_FINISH == event) + else if (SANEEVENT_FINISH == code) { resp = new char[256]; - sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", (const char*)value1); + sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", str); } - else if (SCANEVENT_ERROR == event) + else if (SANEEVENT_ERROR == code) { resp = new char[256]; - sprintf(resp, "42[\"error\", \"%s\"]", (const char*)value1); + sprintf(resp, "42[\"error\", \"%s\"]", str); } - else if (SCANEVENT_IMAGE == event) + + if (NULL != resp) { - const char* imgName = (const char*)value1; - const char* imgBase64 = (const char*)value2; - resp = new char[256 + strlen(imgName) + strlen(imgBase64)]; - sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64); + p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp)); + delete[] resp; } + } + + void SockIoUser::SaneImageCallback2(const char* name, const char* base64, void* param) + { + SockIoUser* p = (SockIoUser*)param; + + char* resp = NULL; + + const char* imgName = (const char*)name; + const char* imgBase64 = (const char*)base64; + resp = new char[256 + strlen(imgName) + strlen(imgBase64)]; + sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64); if (NULL != resp) { diff --git a/sdk/webservice/SockIoUser.h b/sdk/webservice/SockIoUser.h index 1f447126..8c659504 100644 --- a/sdk/webservice/SockIoUser.h +++ b/sdk/webservice/SockIoUser.h @@ -25,7 +25,8 @@ namespace ver_1 virtual void ThreadFunc(); private: - static void ScanCallback(HGUInt event, void* value1, void* value2, void* param); + static void SaneEvent2(int code, const char* str, bool err, void* param); + static void SaneImageCallback2(const char* name, const char *base64, void* param); static void GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data); bool ShakeHand(const std::string& head); void Pong(); diff --git a/sdk/webservice/WSUser.cpp b/sdk/webservice/WSUser.cpp index 59a501a0..35475049 100644 --- a/sdk/webservice/WSUser.cpp +++ b/sdk/webservice/WSUser.cpp @@ -5,6 +5,7 @@ #include "cJSON.h" #include "sha1.h" #include "base64.h" +#include "HGString.h" namespace ver_2 { @@ -97,18 +98,244 @@ namespace ver_2 if (NULL != json) { std::string func = GetJsonStringValue(json, "func"); + std::string iden = GetJsonStringValue(json, "iden"); + if ("set_global_config" == func) { + GlobalConfig cfg; + cfg.fileSavePath = Utf8ToStdString(GetJsonStringValue(json, "file_save_path")); + cfg.fileNamePrefix = Utf8ToStdString(GetJsonStringValue(json, "file_name_prefix")); + cfg.fileNameMode = GetJsonStringValue(json, "file_name_mode"); + cfg.imageFormat = GetJsonStringValue(json, "image_format"); + cfg.imageJpegQuality = GetJsonIntValue(json, "image_jpeg_quality"); + cfg.imageTiffCompression = GetJsonStringValue(json, "image_tiff_compression"); + cfg.imageTiffJpegQuality = GetJsonIntValue(json, "image_tiff_jpeg_quality"); + cfg.uploadHttpHost = GetJsonStringValue(json, "upload_http_host"); + cfg.uploadHttpPort = GetJsonIntValue(json, "upload_http_port"); + cfg.uploadHttpPath = GetJsonStringValue(json, "upload_http_path"); + cfg.uploadFtpUser = GetJsonStringValue(json, "upload_ftp_user"); + cfg.uploadFtpPassword = GetJsonStringValue(json, "upload_ftp_password"); + cfg.uploadFtpHost = GetJsonStringValue(json, "upload_ftp_host"); + cfg.uploadFtpPort = GetJsonIntValue(json, "upload_ftp_port"); + int ret = GetManager()->SetGlobalConfig(cfg); + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte *)resp, (HGUInt)strlen(resp), HGTRUE); } else if ("get_global_config" == func) { + GlobalConfig cfg; + int ret = GetManager()->GetGlobalConfig(cfg); + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\", "; + 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, "; + fmt += "\"upload_http_host\":\"%s\", "; + fmt += "\"upload_http_port\":%d, "; + fmt += "\"upload_http_path\":\"%s\", "; + fmt += "\"upload_ftp_user\":\"%s\", "; + fmt += "\"upload_ftp_password\":\"%s\", "; + fmt += "\"upload_ftp_host\":\"%s\", "; + fmt += "\"upload_ftp_port\":%d}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), 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, cfg.uploadHttpHost.c_str(), cfg.uploadHttpPort, + strToJson(cfg.uploadHttpPath).c_str(), cfg.uploadFtpUser.c_str(), cfg.uploadFtpPassword.c_str(), cfg.uploadFtpHost.c_str(), + cfg.uploadFtpPort); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); } else if ("load_local_image" == func) { } + else if ("init_device" == func) + { + int ret = GetManager()->InitDevice(); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("deinit_device" == func) + { + int ret = GetManager()->DeinitDevice(); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("get_device_name_list" == func) + { + std::vector devNameList; + int ret = GetManager()->GetDeviceNameList(devNameList); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\", "; + 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]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), nameList.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("open_device" == func) + { + std::string devName = GetJsonStringValue(json, "device_name"); + int ret = GetManager()->OpenDevice(devName); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("close_device" == func) + { + int ret = GetManager()->CloseDevice(); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("get_curr_device_name" == func) + { + std::string devName; + int ret = GetManager()->GetCurrDeviceName(devName); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\", "; + fmt += "\"device_name\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), devName.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("start_scan" == func) + { + int ret = GetManager()->StartScan(); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } + else if ("stop_scan" == func) + { + int ret = GetManager()->StopScan(); + + std::string errInfo; + if (0 != ret) + errInfo = StdStringToUtf8("閿欒"); + + std::string fmt; + fmt += "{\"func\":\"%s\", "; + fmt += "\"iden\":\"%s\", "; + fmt += "\"ret\":%d, "; + fmt += "\"err_info\":\"%s\"}"; + + char resp[1024]; + sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str()); + SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE); + } cJSON_Delete(json); } @@ -452,6 +679,46 @@ namespace ver_2 } } + 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 '/': + 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; diff --git a/sdk/webservice/WSUser.h b/sdk/webservice/WSUser.h index 8bd78de6..79f24431 100644 --- a/sdk/webservice/WSUser.h +++ b/sdk/webservice/WSUser.h @@ -25,6 +25,7 @@ namespace ver_2 virtual void ThreadFunc(); private: + static std::string strToJson(const std::string str); bool ShakeHand(const std::string& head); void Pong(); bool SendResponse(const HGByte* data, HGUInt size, HGBool text); diff --git a/sdk/webservice/main.cpp b/sdk/webservice/main.cpp index 8c4eda95..faa6b42f 100644 --- a/sdk/webservice/main.cpp +++ b/sdk/webservice/main.cpp @@ -2,6 +2,7 @@ #include "base/HGInc.h" #include "base/HGThread.h" #include "base/HGUtility.h" +#include "base/HGIni.h" #include "base/HGMsgPump.h" #include "ManagerV1.h" #include "ManagerV2.h"