From 14c7af02bfec87b3a6caac76c4031bedb015ca22 Mon Sep 17 00:00:00 2001 From: hkc320 Date: Fri, 6 Sep 2024 16:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E5=B8=A7=E6=A0=BC=E5=BC=8F=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E5=88=B0=E6=96=B0=E5=B8=A7=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/ems_datahubs/WinDataHubs.sln | 31 ++ applications/ems_datahubs/WinDataHubs.vcxproj | 167 ++++++++++ applications/ems_datahubs/eventhandler.cpp | 293 ++++++++++-------- applications/ems_datahubs/frame_define.h | 2 + applications/ems_datahubs/openjson.cpp | 13 +- .../ems_datahubs/test_data/test-data-03.txt | Bin 0 -> 331 bytes 6 files changed, 366 insertions(+), 140 deletions(-) create mode 100644 applications/ems_datahubs/WinDataHubs.sln create mode 100644 applications/ems_datahubs/WinDataHubs.vcxproj create mode 100644 applications/ems_datahubs/test_data/test-data-03.txt diff --git a/applications/ems_datahubs/WinDataHubs.sln b/applications/ems_datahubs/WinDataHubs.sln new file mode 100644 index 0000000..7b27e53 --- /dev/null +++ b/applications/ems_datahubs/WinDataHubs.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.35130.168 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinDataHubs", "WinDataHubs.vcxproj", "{2192CF4F-DE56-4896-9EA1-4C61B40590A5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Debug|x64.ActiveCfg = Debug|x64 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Debug|x64.Build.0 = Debug|x64 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Debug|x86.ActiveCfg = Debug|Win32 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Debug|x86.Build.0 = Debug|Win32 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Release|x64.ActiveCfg = Release|x64 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Release|x64.Build.0 = Release|x64 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Release|x86.ActiveCfg = Release|Win32 + {2192CF4F-DE56-4896-9EA1-4C61B40590A5}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {81B0EBA0-C45B-4F9A-B3E8-4780447CB231} + EndGlobalSection +EndGlobal diff --git a/applications/ems_datahubs/WinDataHubs.vcxproj b/applications/ems_datahubs/WinDataHubs.vcxproj new file mode 100644 index 0000000..a8a305c --- /dev/null +++ b/applications/ems_datahubs/WinDataHubs.vcxproj @@ -0,0 +1,167 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2192cf4f-de56-4896-9ea1-4c61b40590a5} + WinDataHubs + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\..\..\sdk\include;$(IncludePath) + $(SolutionDir)\..\..\sdk\lib;$(LibraryPath) + + + false + + + true + $(SolutionDir)\..\..\sdk\include;$(IncludePath) + $(SolutionDir)\..\..\sdk\Lib;$(LibraryPath) + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + hv.lib;zlibd.lib;mysqlcppconn.lib;libiconvD.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/applications/ems_datahubs/eventhandler.cpp b/applications/ems_datahubs/eventhandler.cpp index 0233ef6..6160708 100644 --- a/applications/ems_datahubs/eventhandler.cpp +++ b/applications/ems_datahubs/eventhandler.cpp @@ -56,148 +56,167 @@ void EventHandler::onRecvHandler(hio_t* io, void* buf, int readbytes) return; } - OpenJson json; - - CODING buf_code = GetCoding((unsigned char*)pReadFrame->frame_content, pReadFrame->frame_len); //ÅжÏÊÇ·ñÊÇutf-8 - - hlogi("<=== recieve buffer code is [%d]", buf_code); - - std::string msg((char*)pReadFrame->frame_content, pReadFrame->frame_len); - - if( buf_code == CODING::GBK - || buf_code == CODING::UNKOWN ) + if( Frame_DeviceData_Request == pReadFrame->frame_type ) { - std::string str_result; - //ת»»ÎªUTF8 - if( !GBKToUTF8(msg, str_result) ) + hlogi("<=== reveive device data request"); + + OpenJson json; + + CODING buf_code = GetCoding((unsigned char*)pReadFrame->frame_content, pReadFrame->frame_len); //ÅжÏÊÇ·ñÊÇutf-8 + + hlogi("<=== recieve buffer coding is [%s]", buf_code== GBK ? "GBK": (buf_code == UTF8 ? "UTF8" : "UNKNOWN CODING")); + + //ÕâÀォ֡ÄÚÖ¡µÄÄÚÈÝת»»Îª×Ö·û´®£¬·ûºÏjson¸ñʽµÄ×Ö·û´®£¬Ïê¼ûmqtt_msg.hµÄMessageData½á¹¹¶¨Òå + std::string msg((char*)pReadFrame->frame_content + 8, pReadFrame->frame_len - 8); + + if( buf_code == CODING::GBK + || buf_code == CODING::UNKOWN ) { - hloge("Failed to transfer code from GBK to UTF-8"); - respFrame.setErrorFrame(ERR_INVALID_UTF8); + std::string str_result; + //ת»»ÎªUTF8 + if( !GBKToUTF8(msg, str_result) ) + { + hloge("Failed to transfer code from GBK to UTF-8"); + respFrame.setErrorFrame(ERR_INVALID_UTF8); + hio_write(io, (void*)&respFrame, sizeof respFrame); + return; + } + + hlogi("Successfuly transfer code from GBK to UTF-8!"); + msg = str_result; + } + +#ifdef _DEBUG + hlogd("<=== recieve !!VALID!! mqtt pack len =[%d] data=[%s]", msg.length(), msg.c_str()); //ÕâÀﻹÊÇºÃµÄ +#endif + + unsigned int len = msg.length(); + char* pTmp = new char[len]; + memcpy(pTmp, msg.c_str(), len); + + std::shared_ptr ptr; //·Å¸öÖÇÄÜÖ¸ÕëÊ¡µÃÍü¼Çɾ³ý + ptr.reset(pTmp); + + //hlogi("<=== decode OK, msg=[%s]", msg.c_str()); + +#if 0 + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 17).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 18).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 19).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 20).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 21).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 22).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 23).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 24).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 25).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 26).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 27).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 28).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 29).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 30).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 31).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 32).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 33).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 34).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 61).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 62).c_str()); + hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 63).c_str()); +#endif + + //hlogd("<=== decode OK [\n%s\n]", printHex(pTmp, len).c_str()); + + try + { + if( !json.decode(msg) ) + { + hloge("Failed to decode json string pack , length=%d", readbytes); + respFrame.setErrorFrame(ERR_INVALID_JSON_FMT); + hio_write(io, (void*)&respFrame, sizeof respFrame); + return; + } + + std::string fsucode = json["FsuCode"].s(); + std::string msg_type = json["type"].s(); + std::string timestamp = get_current_timestamp(); // json["TimeStamp"].s(); + + if( fsucode.length() == 0 ) + { + //delete[] pTmp; + hlogw("!!empty fsucode recieved!"); + respFrame.setErrorFrame(ERR_INVALID_FSUCODE); + hio_write(io, (void*)&respFrame, sizeof respFrame); + return; + } + + hlogi("<=== decode OK, recieve fsucode=[%s] type=[%s] ts=[%s]", fsucode.c_str(), msg_type.c_str(), timestamp.c_str()); + + hio_write(io, (void*)&respFrame, sizeof respFrame); + +#ifdef _DEBUG + hlogd("<<<>>> \n[\n%s\n]\n", printHex(pTmp, len).c_str()); +#endif + + std::string out_compress; + + int zip_ret = 0; + if( (zip_ret = CompressString(pTmp, len, out_compress, Z_DEFAULT_COMPRESSION)) != Z_OK ) + { + hloge("Failed to compress source data, zip return value %d", zip_ret); + return; + } + + hlogd("<<<>>> \n[\n%s\n]\n", printHex(out_compress.c_str(), out_compress.size()).c_str()); +#endif + //std::string msg2(pTmp, len); + + if( msg_type == "gateway-data" + || msg_type == "gateway-alarmdata" + || msg_type == "gateway-writedata" + || msg_type == "gateway-readdata" + || msg_type == "web-write" + || msg_type == "web-alarm" ) + { + auto& IdCodeContent = json["IdCodeContent"]; + if( IdCodeContent.size() <= 0 ) + { + //delete[] pTmp; + hloge("invalid IdCodeContent's size: %d", IdCodeContent.size()); + return; + } + + auto& pNode = IdCodeContent[0]; //ÕâÊÇÖ»½âÎöµÚÒ»¸ö½Úµã + std::string oid = pNode["OID"].s(); + + //OpDatabase::getInstance()->InsertMessage(timestamp, msg_type, fsucode, out_compress, (int)pData->mqtt_topic, (int)pData->device_id); + } + + if( msg_type == "web-read" ) + { + auto& IdCodeContent = json["IdCodes"]; + if( IdCodeContent.size() <= 0 ) + { + hloge("invalid IdCodes's size: %d", IdCodeContent.size()); + //delete[] pTmp; + return; + } + + auto& pNode = IdCodeContent[0]; //ÕâÊÇÖ»½âÎöµÚÒ»¸ö½Úµã + std::string oid = pNode.s(); + + //OpDatabase::getInstance()->InsertMessage(timestamp, msg_type, fsucode, out_compress, (int)pData->mqtt_topic, (int)pData->device_id); + } + //delete[] pTmp; + } + catch( const char* errMsg ) + { + hloge("Failed to decode json string pack , catch error:%s", errMsg); + respFrame.setErrorFrame(ERR_INVALID_JSON_FMT); hio_write(io, (void*)&respFrame, sizeof respFrame); return; } - hlogi("Successfuly transfer code from GBK to UTF-8!"); - msg = str_result; } - -#ifdef _DEBUG - hlogd("<=== recieve !!VALID!! mqtt pack len =[%d] data=[%s]", msg.length(), msg.c_str()); //ÕâÀﻹÊÇºÃµÄ -#endif - - unsigned int len = msg.length(); - char* pTmp = new char[len]; - memcpy(pTmp, msg.c_str(), len); - - std::shared_ptr ptr; //·Å¸öÖÇÄÜÖ¸ÕëÊ¡µÃÍü¼Çɾ³ý - ptr.reset(pTmp); - - //hlogi("<=== decode OK, msg=[%s]", msg.c_str()); - -#if 0 - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 17).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 18).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 19).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 20).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 21).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 22).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 23).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 24).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 25).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 26).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 27).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 28).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 29).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 30).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 31).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 32).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 33).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 34).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 61).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 62).c_str()); - hlogi("<=== decode OK [\n%s\n]", printHex(pTmp, 63).c_str()); -#endif - - //hlogd("<=== decode OK [\n%s\n]", printHex(pTmp, len).c_str()); - - if( !json.decode(msg) ) - { - //delete[] pTmp; - hloge("Failed to decode json string pack , length=%d", readbytes); - hio_write(io, (void*)&respFrame, sizeof respFrame); - return; - } - - hio_write(io, (void*)&respFrame, sizeof respFrame); - - std::string fsucode = json["FsuCode"].s(); - std::string msg_type = json["type"].s(); - std::string timestamp = get_current_timestamp(); // json["TimeStamp"].s(); - - if( fsucode.length() == 0 ) - { - //delete[] pTmp; - hlogw("!!empty fsucode recieved!"); - return; - } - - hlogi("<=== decode OK, recieve fsucode=[%s] type=[%s] ts=[%s]", fsucode.c_str(), msg_type.c_str(), timestamp.c_str()); - -#ifdef _DEBUG - hlogd("<<<>>> \n[\n%s\n]\n", printHex(pTmp, len).c_str()); -#endif - - std::string out_compress; - - int zip_ret = 0; - if( (zip_ret = CompressString(pTmp, len, out_compress, Z_DEFAULT_COMPRESSION)) != Z_OK ) - { - hloge("Failed to compress source data, zip return value %d", zip_ret); - return; - } - - hlogd("<<<>>> \n[\n%s\n]\n", printHex(out_compress.c_str(), out_compress.size()).c_str()); -#endif - //std::string msg2(pTmp, len); - - if( msg_type == "gateway-data" - || msg_type == "gateway-alarmdata" - || msg_type == "gateway-writedata" - || msg_type == "gateway-readdata" - || msg_type == "web-write" - || msg_type == "web-alarm" ) - { - auto& IdCodeContent = json["IdCodeContent"]; - if( IdCodeContent.size() <= 0 ) - { - //delete[] pTmp; - hloge("invalid IdCodeContent's size: %d", IdCodeContent.size()); - return; - } - - auto& pNode = IdCodeContent[0]; //ÕâÊÇÖ»½âÎöµÚÒ»¸ö½Úµã - std::string oid = pNode["OID"].s(); - - //OpDatabase::getInstance()->InsertMessage(timestamp, msg_type, fsucode, out_compress, (int)pData->mqtt_topic, (int)pData->device_id); - } - - if( msg_type == "web-read" ) - { - auto& IdCodeContent = json["IdCodes"]; - if( IdCodeContent.size() <= 0 ) - { - hloge("invalid IdCodes's size: %d", IdCodeContent.size()); - //delete[] pTmp; - return; - } - - auto& pNode = IdCodeContent[0]; //ÕâÊÇÖ»½âÎöµÚÒ»¸ö½Úµã - std::string oid = pNode.s(); - - //OpDatabase::getInstance()->InsertMessage(timestamp, msg_type, fsucode, out_compress, (int)pData->mqtt_topic, (int)pData->device_id); - } - //delete[] pTmp; } \ No newline at end of file diff --git a/applications/ems_datahubs/frame_define.h b/applications/ems_datahubs/frame_define.h index 400553b..07e1292 100644 --- a/applications/ems_datahubs/frame_define.h +++ b/applications/ems_datahubs/frame_define.h @@ -11,6 +11,8 @@ typedef enum tagErrorCode : unsigned char ERR_INVALID_LEN = 0X01, ERR_INVALID_UTF8 = 0X02, ERR_INVALID_BUF_LEN = 0X03, + ERR_INVALID_JSON_FMT = 0X04, + ERR_INVALID_FSUCODE = 0X05, ERR_UNKOWN = 0XFF }ErrorCode; diff --git a/applications/ems_datahubs/openjson.cpp b/applications/ems_datahubs/openjson.cpp index 2b8002d..6d8f16f 100644 --- a/applications/ems_datahubs/openjson.cpp +++ b/applications/ems_datahubs/openjson.cpp @@ -20,10 +20,13 @@ #include #include +#include + #include "openjson.h" +//#define PRINTF printf +#define PRINTF hloge -#define PRINTF printf #if (defined(_MSC_VER) && (_MSC_VER >= 1400 )) inline int SNPRINTF(char* buffer, size_t size, const char* format, ...) { @@ -783,12 +786,16 @@ OpenJson& OpenJson::object(const char* key) { if (type_ == ARRAY) { - Log("JsonNode must be OBJECT, not ARRAY"); + PRINTF("JsonNode must be OBJECT, not ARRAY"); } type_ = OBJECT; } else { + if( !box_ ) + { + PRINTF("JsonNode must be OBJECT, not NOTHING! key:[%s]", key); + } assert(box_); } if (!box_) box_ = new Box; @@ -828,7 +835,7 @@ void OpenJson::addNode(OpenJson* node) if (!node) return; if (type_ != OBJECT && type_ != ARRAY) { - Log("JsonNode must be OBJECT or ARRAY"); + PRINTF("JsonNode must be OBJECT or ARRAY"); type_ = node->key_ ? OBJECT : ARRAY; } if (box_ == 0) box_ = new Box; diff --git a/applications/ems_datahubs/test_data/test-data-03.txt b/applications/ems_datahubs/test_data/test-data-03.txt new file mode 100644 index 0000000000000000000000000000000000000000..e31e2cca0a033d0fa21ebff4e9c42ffc8e5bfafe GIT binary patch literal 331 zcmZQ_Vq^eZT-?BtoS;Q$X6B^Ycnl^Gblaqj|YFfhKYSjr8|)0U4)fsAsHa1|&^D*wDn> z6v(o)0CIp3WC}=aSYl2oHp@YBZi&ey`9&b3lz@tBK{|-F!3c{DaF1az8)$Sa#LFR> oxv9Y=iMa(JdyNc?Omq!QbPdfF42`V}4XupLfljG?_y6600DV$M*Z=?k literal 0 HcmV?d00001