123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #include "stdafx.h"
- #include "FingerPrint_impl.h"
- #include "log4vendor.h"
- #include "path.h"
- #ifdef RVC_OS_WIN
- #include <direct.h>
- #define GetCurrentDir _getcwd
- #else
- #include <unistd.h>
- #include<cstring>
- #include <cstdio>
- #include <netdb.h> //gethostbyname
- #include <arpa/inet.h> //ntohl
- #include <unistd.h> // Linux系统中
- #include <netdb.h>
- #include <net/if.h>
- #include <arpa/inet.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #define GetCurrentDir getcwd
- #endif
- #ifdef RVC_OS_WIN
- BOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- #endif
- string get_current_directory() {
- char buff[256];
- GetCurrentDir(buff, 256);
- string current_working_directory(buff);
- return current_working_directory;
- }
- void writeFile(const char* filename)
- {
- FILE* fp = fopen(filename, "wb");
- fprintf(fp, "fingerImage");
- string content = "fingerImage";
- fwrite(content.c_str(), content.length(), 1, fp);
- fclose(fp);
- }
- DEVICEBASE_API ErrorCodeEnum CreateDevComponent(DeviceBaseClass*& pBaseObj)
- {
- pBaseObj = new FingerPrintImpl();
- if (pBaseObj == NULL) {
- return Error_Resource;
- }
- cmb::log_init_config config;
- config.dev_name = "FingerPrint";
- #ifdef RVC_OS_WIN
- config.log_dir = ("C:\\rvc\\dbg\\");
- #else
- config.log_dir = ("/opt/rvc/dbg/");
- #endif
- std::string str;
- cmb::log4vendor::init(config, str);
- printf("init after: %s\n", str.c_str());
- return Error_Succeed;
- }
- DEVICEBASE_API ErrorCodeEnum ReleaseDevComponent(DeviceBaseClass*& pBaseObj)
- {
- if (pBaseObj == NULL) {
- return Error_Param;
- }
- if (FingerPrintImpl* pTmp = dynamic_cast<FingerPrintImpl*>(pBaseObj))
- {
- delete pTmp;
- pTmp = NULL;
- return Error_Succeed;
- }
- return Error_Param;
- }
- FingerPrintImpl::FingerPrintImpl()
- {
- }
- FingerPrintImpl::~FingerPrintImpl()
- {
- }
- ErrorCodeEnum FingerPrintImpl::GetDevCategory(DevCategoryInfo& devCategory)
- {
- if (GetDevCategoryHttp(devCategory)) {
- strcpy(devCategory.szModel, "CM=2.0#CID=00000000");
- strcpy(devCategory.szType, "PVER=ACT#MID=ACT-F5-5540-0SH");
- strcpy(devCategory.szVendor, "cmbszSimulator");
- return Error_Succeed;
- }
- else {
- return Error_Exception;
- }
- }
- ErrorCodeEnum FingerPrintImpl::Reset()
- {
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::DevClose()
- {
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::GetLastErr(DevErrorInfo& devErrInfo)
- {
- static int times = 0;
- char szMessage[1024];
- sprintf(szMessage, "这是第 %d 条错误信息", ++times);
- strcpy(devErrInfo.szErrMsg, szMessage);
- devErrInfo.dwErrMsgLen = strlen(szMessage);
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::DevOpen(DWORD dwPort, DWORD dwBaudRate)
- {
- LOG4VTM(INFO, "当前目录:" << get_current_directory());
- depCfgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + "cmbsz.ini";
- bool ret = iniRead.ReadConfig(depCfgPath);
- if (ret == false)
- {
- LOG4VTM(INFO, "dep ReadConfig is Error,cfg=" << depCfgPath);
- }
- return Error_Succeed;
- }
- ErrorCodeEnum FingerPrintImpl::Image2Feature(const char* imageName, LPBYTE lpbFeature, int& iLength)
- {
- //imageName写入的文件,lpbFeature 特征码,iLength特征码长度
- string imgName(imageName);
- string imgPath = get_current_directory() + SPLIT_SLASH_STR + "dep" + SPLIT_SLASH_STR + imgName;
- writeFile(imgPath.c_str());
- //获取特征码
- if (Image2FeatureHttp(lpbFeature, iLength)) {
- return Error_Succeed;
- }
- else {
- return Error_Exception;
- }
- }
- ErrorCodeEnum FingerPrintImpl::Image2Template(const char* imagePath1, const char* imagePath2, const char* imagePath3, LPBYTE lpbTemplate, int& iLength)
- {
- //获取模板特征码
- LOG4VTM(INFO, "imagePath1 path = " << imagePath1);
- LOG4VTM(INFO, "imagePath2 path = " << imagePath2);
- LOG4VTM(INFO, "imagePath3 path = " << imagePath3);
- if (Image2TemplateHttp(lpbTemplate, iLength)) {
- return Error_Succeed;
- }
- else {
- return Error_Exception;
- }
- }
- ErrorCodeEnum FingerPrintImpl::Match(LPBYTE lpbTemplate[], int iTemplateLen[], int templateNum, LPBYTE lbpFeature, int& iFeatureLen, int level /*= 3*/)
- {
- return Error_NotImpl;
- }
- ErrorCodeEnum FingerPrintImpl::Cancel()
- {
- return Error_NotImpl;
- }
- bool FingerPrintImpl::Image2FeatureHttp(LPBYTE lpbFeature, int& iLength)
- {
- Image2FeatureReq fReq;
- Image2FeatureRet fRet;
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["ip"] = GetLocalIP();
- rootReq["entityName"] = "FingerPrint";
- rootReq["method"] = "getFingerPrint";//实体对外接口名
- rootReq["adapterInterName"] = "Image2Feature";//适配器接口名
- //fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- string urlStr = iniRead.ReadString("server", "url", "");
- if (urlStr.empty()) {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
- fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- }
- else {
- fReq.m_url = urlStr;
- }
- fReq.m_timeOut = 10;
- string jsonReq = writer.write(rootReq);
- fReq.m_reqStr = jsonReq;//请求参数
- bool ret = client->Post(fReq, fRet);
- if (ret) {
- LOG4VTM(INFO, "Image2FeatureHttp fRet.m_retStr = " << fRet.m_retStr);
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(fRet.m_retStr, rootRet, false))
- {
- LOG4VTM(INFO, "Image2FeatureHttp parse resp is fail,url=" << fReq.m_url.c_str());
- return false;//失败
- }
- bool isSucc = rootRet["success"].asBool();
- if (isSucc) {
- //解析数据
- if (rootRet.isMember("data")) {
- string strFeature = rootRet["data"]["feature"].asString();
- if (strFeature.length() != 684) {
- LOG4VTM(INFO, "Image2FeatureHttp return feature length is not right,len = " << strFeature.length());
- return false;
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
- }
- #ifdef RVC_OS_WIN
- strcpy_s((char*)lpbFeature, 1024, strFeature.c_str());
- #else
- strncpy((char*)lpbFeature, strFeature.c_str(), 684);
- #endif
- iLength = strFeature.length();
- LOG4VTM(INFO, "Image2FeatureHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbFeature);
- return true;
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp return [data] is null");
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp [success] is false,url= " << fReq.m_url.c_str());
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,err=%s" << fRet.m_errMsg.c_str());
- return false;
- }
- }
- bool FingerPrintImpl::Image2TemplateHttp(LPBYTE lpbTemplate, int& iLength)
- {
- Image2TemplateReq fReq;
- Image2TemplateRet fRet;
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["ip"] = GetLocalIP();
- rootReq["entityName"] = "FingerPrint";
- rootReq["method"] = "generateTemplate";
- rootReq["adapterInterName"] = "Image2Template";//???
- string urlStr = iniRead.ReadString("server", "url", "");
- if (urlStr.empty()) {
- LOG4VTM(INFO, "Image2FeatureHttp req fail,url is empty, use default url");
- fReq.m_url = "http://localhost:8080/avs/imitate/simulateData";
- }
- else {
- fReq.m_url = urlStr;
- }
- fReq.m_timeOut = 10;
- string jsonReq = writer.write(rootReq);
- fReq.m_reqStr = jsonReq;//请求参数
- bool ret = client->Post(fReq, fRet);
- if (ret) {
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(fRet.m_retStr, rootRet, false))
- {
- LOG4VTM(INFO, "Image2TemplateHttp parse resp is fail,url=" << fReq.m_url.c_str());
- return false;//失败
- }
- bool isSucc = rootRet["success"].asBool();
- if (isSucc) {
- //解析数据
- if (rootRet.isMember("data")) {
- string strFeature = rootRet["data"]["template"].asString();
- if (strFeature.length() != 684) {
- LOG4VTM(INFO, "Image2TemplateHttp return feature length is not right,len = " << strFeature.length());
- return false;
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp return feature len = " << strFeature.length() << " feature = " << strFeature.c_str());
- }
- #ifdef RVC_OS_WIN
- strcpy_s((char*)lpbTemplate, 1024, strFeature.c_str());
- #else
- strncpy((char*)lpbTemplate, strFeature.c_str(), 684);
- #endif
- iLength = strFeature.length();
- LOG4VTM(INFO, "Image2TemplateHttp return feature len =" << strFeature.length() << ", lpbFeature = " << lpbTemplate);
- return true;
- }
- else {
- LOG4VTM(INFO, ("Image2TemplateHttp return [data] is null"));
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp [success] is false,url= " << fReq.m_url.c_str());
- return false;//查询失败
- }
- }
- else {
- LOG4VTM(INFO, "Image2TemplateHttp req fail,err=%s" << fRet.m_errMsg.c_str());
- return false;
- }
- }
- bool FingerPrintImpl::GetDevCategoryHttp(DevCategoryInfo& devCategory)
- {
- LOG4VTM(INFO, "GetDevCategoryHttp");
- return commonSimpleHttp("GetDevCategory");
- }
- bool FingerPrintImpl::commonSimpleHttp(string adapterInterName)
- {
- CommonReq cReq;
- CommonRet cRet;
- IHttpFunc* client;
- client = create_http(HttpsLogCallBack);
- Json::Value rootReq;
- Json::FastWriter writer;
- rootReq["ip"] = GetLocalIP();
- rootReq["entityName"] = "FingerPrint";
- rootReq["adapterInterName"] = adapterInterName;//适配器接口名
- string strErrPrefix = adapterInterName + "http";
- cReq.m_url = getUrl();
- cReq.m_timeOut = 30;
- string jsonReq = writer.write(rootReq);
- cReq.m_reqStr = jsonReq;//请求参数
- bool ret = client->Post(cReq, cRet);
- if (ret) {
- Json::Reader reader;
- Json::Value rootRet;
- if (!reader.parse(cRet.m_retStr, rootRet, false))
- {
- LOG4VTM(INFO, strErrPrefix << " parse resp is fail,url=" << cReq.m_url.c_str());
- return false;//失败
- }
- bool isSucc = rootRet["success"].asBool();
- if (isSucc) {
- //解析数据
- if (rootRet.isMember("data")) {
- bool isResult = rootRet["data"]["result"].asBool();
- if (isResult) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- LOG4VTM(INFO, strErrPrefix << " return [data] is null");
- return false;
- }
- }
- else {
- LOG4VTM(INFO, strErrPrefix << " [success] is false,");
- return false;
- }
- }
- else {
- LOG4VTM(INFO, strErrPrefix << " req fail,err=%s" << cRet.m_errMsg.c_str());
- return false;
- }
- }
- string FingerPrintImpl::getUrl()
- {
- string urlStr = iniRead.ReadString("server", "url", "");
- if (urlStr.empty()) {
- LOG4VTM(INFO, "url is empty, use default url");
- return "http://localhost:8080/avs/imitate/simulateDataN";
- }
- else {
- return urlStr;
- }
- }
- #ifdef NEWER_COMPILER_WORKAROUNDS
- DEVICEBASE_API ErrorCodeEnum GetDevAdapterVersion(DevSoftVersion& retVesion)
- {
- retVesion.wMajor = retVesion.wMinor = retVesion.wBuild = retVesion.wRevision = 0;
- return Error_Succeed;
- }
- #endif // NEWER_COMPILER_WORKAROUNDS
|