From f48e2e378bacffb96cb5575876a7b2f0081de0be Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 08 六月 2020 15:19:01 +0800 Subject: [PATCH] update --- netdisk/include/netdisk.h | 34 +++-- netdisk/hcnetdisk.c | 183 +++++++++++++++++------------- netdisk/include/hcnetdisk.h | 29 ++-- netdisk/test.c | 48 ++++++- test/test_static.c | 40 ++++++ netdisk/test | 0 6 files changed, 216 insertions(+), 118 deletions(-) diff --git a/netdisk/hcnetdisk.c b/netdisk/hcnetdisk.c index 155ee7a..5bac513 100644 --- a/netdisk/hcnetdisk.c +++ b/netdisk/hcnetdisk.c @@ -1,12 +1,18 @@ #include "netdisk.h" #include "hcnetdisk.h" +bool HCNetdisk::envInited = false; -void HCNetdisk::netdisk_init(HCNetdisk_EnvConfig &config){ - - if (&config != NULL && config.libpath.length() != 0) { +/** + * 璁惧鍒濆鍖� + */ +void HCNetdisk::netdisk_init(Netdisk_EnvConfig *config){ + if (envInited) { + return; + } + if (config != NULL && config->libpath.length() != 0) { NET_DVR_LOCAL_SDK_PATH struComPath = {0}; - sprintf(struComPath.sPath, "%s", config.libpath.c_str()); //HCNetSDKCom鏂囦欢澶规墍鍦ㄧ殑璺緞 + sprintf(struComPath.sPath, "%s", config->libpath.c_str()); //HCNetSDKCom鏂囦欢澶规墍鍦ㄧ殑璺緞 NET_DVR_SetSDKInitCfg(NET_SDK_INIT_CFG_SDK_PATH, &struComPath); } @@ -16,7 +22,102 @@ //璁剧疆杩炴帴鏃堕棿涓庨噸杩炴椂闂� NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); + envInited = true; } + +void HCNetdisk::netdisk_deinit() { + if (!envInited) { + return; + } + NET_DVR_Cleanup(); + envInited = false; +} + + + + + + +int HCNetdisk::login(Netdisk_LoginInfo &loginInfo) { + + //鐧诲綍鍙傛暟锛屽寘鎷澶囧湴鍧�銆佺櫥褰曠敤鎴枫�佸瘑鐮佺瓑 + NET_DVR_USER_LOGIN_INFO struLoginInfo = {0}; + struLoginInfo.bUseAsynLogin = 0; //鍚屾鐧诲綍鏂瑰紡 + strcpy(struLoginInfo.sDeviceAddress, loginInfo.host.c_str()); //璁惧IP鍦板潃 + struLoginInfo.wPort = loginInfo.port; //璁惧鏈嶅姟绔彛 + strcpy(struLoginInfo.sUserName, loginInfo.username.c_str()); //璁惧鐧诲綍鐢ㄦ埛鍚� + strcpy(struLoginInfo.sPassword, loginInfo.password.c_str()); //璁惧鐧诲綍瀵嗙爜 + +// 娉ㄥ唽璁惧 + LONG lUserID; + NET_DVR_DEVICEINFO_V40 struDeviceInfo = {0}; + //lUserID = 0; + lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfo); + if (lUserID < 0) + { + printf("Login error, %d\n", NET_DVR_GetLastError()); + NET_DVR_Cleanup(); + return -1; + } + + printf("lUserID = %ld\n", lUserID); + userid = lUserID; + username = loginInfo.username; + password = loginInfo.password; + host = loginInfo.host; + port = loginInfo.port; + + + startChannel = struDeviceInfo.struDeviceV30.byStartDChan; + maxChannels = struDeviceInfo.struDeviceV30.byIPChanNum + struDeviceInfo.struDeviceV30.byHighDChanNum * 256; + + + return 0; +} + +int HCNetdisk::logout() { + //娉ㄩ攢鐢ㄦ埛 + NET_DVR_Logout(userid); + userid = -1; + return 0; +} + +int HCNetdisk::downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files) { + struct tm start, end; + char destfile[1024]; + int i = 0; + start = request.start; + + while(mktime(&start) < mktime(&(request.end))) { + if (difftime(mktime(&(request.end)), mktime(&start)) > 10 * 60) { + end = start; + end.tm_min += 10; + mktime(&end); + } else { + end = request.end; + } + + // printf("start = %s", asctime(&start)); + // printf("end = %s", asctime(&end)); + sprintf(destfile, "%s/%s-%d-%ld-%d", + request.destpath.c_str(), + devicename.c_str(), + request.channel, + mktime(&start), + i); + // printf("%s\n", destfile); + downloadByTime_wrapper(userid, request.channel, &start, &end, destfile); + if(files != NULL) { + files->push_back(destfile); + } + start = end; + i++; + } + + return 0; +} + +HCNetdisk::~HCNetdisk() { } /** * @return success: 0, failture : -1 @@ -150,77 +251,3 @@ } } - - - -int HCNetdisk::login(Netdisk_LoginInfo &loginInfo) { - - //鐧诲綍鍙傛暟锛屽寘鎷澶囧湴鍧�銆佺櫥褰曠敤鎴枫�佸瘑鐮佺瓑 - NET_DVR_USER_LOGIN_INFO struLoginInfo = {0}; - struLoginInfo.bUseAsynLogin = 0; //鍚屾鐧诲綍鏂瑰紡 - strcpy(struLoginInfo.sDeviceAddress, loginInfo.host.c_str()); //璁惧IP鍦板潃 - struLoginInfo.wPort = loginInfo.port; //璁惧鏈嶅姟绔彛 - strcpy(struLoginInfo.sUserName, loginInfo.username.c_str()); //璁惧鐧诲綍鐢ㄦ埛鍚� - strcpy(struLoginInfo.sPassword, loginInfo.password.c_str()); //璁惧鐧诲綍瀵嗙爜 - -// 娉ㄥ唽璁惧 - LONG lUserID; - NET_DVR_DEVICEINFO_V40 struDeviceInfo = {0}; - //lUserID = 0; - lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfo); - if (lUserID < 0) - { - printf("Login error, %d\n", NET_DVR_GetLastError()); - NET_DVR_Cleanup(); - return -1; - } - // printf("lUserID = %ld\n", lUserID); - userid = lUserID; - username = loginInfo.username; - password = loginInfo.password; - host = loginInfo.host; - port = loginInfo.port; - return 0; - -} - -int HCNetdisk::logout() { - //娉ㄩ攢鐢ㄦ埛 - NET_DVR_Logout(userid); - //閲婃斁 SDK 璧勬簮 - NET_DVR_Cleanup(); - userid = -1; - return 0; -} - -int HCNetdisk::downloadByTime(Netdisk_DownloadRequest &request) { - struct tm start, end; - char destfile[1024]; - int i = 0; - start = request.start; - - while(mktime(&start) < mktime(&(request.end))) { - if (difftime(mktime(&(request.end)), mktime(&start)) > 10 * 60) { - end = start; - end.tm_min += 10; - mktime(&end); - } else { - end = request.end; - } - - // printf("start = %s", asctime(&start)); - // printf("end = %s", asctime(&end)); - sprintf(destfile, "%s/%s-%d-%ld-%d", - request.destpath.c_str(), - devicename.c_str(), - request.channel, - mktime(&start), - i); - // printf("%s\n", destfile); - downloadByTime_wrapper(userid, request.channel, &start, &end, destfile); - start = end; - i++; - } - - return 0; -} diff --git a/netdisk/include/hcnetdisk.h b/netdisk/include/hcnetdisk.h index bbd8773..47e2e56 100644 --- a/netdisk/include/hcnetdisk.h +++ b/netdisk/include/hcnetdisk.h @@ -6,34 +6,29 @@ #include "HCNetSDK.h" #include "netdisk.h" +//娴峰悍缃戠粶纭洏 +class HCNetdisk : public Netdisk{ - - -class HCNetdisk { - - std::string devicename; - - long userid; - std::string username; - std::string password; - std::string host; - int port; + +private: int downloadByTime_wrapper(long userid, int channel, struct tm *start, struct tm *end, char *destfile); int saveRecordFile(int userId, char * srcfile, char * destfile); public: - HCNetdisk(const std::string &_devicename): devicename(_devicename) {} + static bool envInited ; + HCNetdisk(const std::string &_devicename = "HC"): Netdisk(_devicename) {} - ~HCNetdisk() {} + virtual ~HCNetdisk(); - static void netdisk_init(HCNetdisk_EnvConfig &config); + virtual int login(Netdisk_LoginInfo &loginInfo); - int login(Netdisk_LoginInfo &loginInfo); + virtual int logout(); - int logout(); + virtual int downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files); - int downloadByTime(Netdisk_DownloadRequest &request); + static void netdisk_init(Netdisk_EnvConfig *config); + static void netdisk_deinit(); }; diff --git a/netdisk/include/netdisk.h b/netdisk/include/netdisk.h index 248a445..5ac652c 100644 --- a/netdisk/include/netdisk.h +++ b/netdisk/include/netdisk.h @@ -25,30 +25,36 @@ - +// 缃戠粶纭洏 鎶借薄绫� class Netdisk { + protected: - std::string devicename; + std::string devicename; //璁惧鍚嶇О + int startChannel; //璧峰閫氶亾鍙� + int maxChannels; //鏈�澶ч�氶亾鏁� - long userid; - std::string username; - std::string password; - std::string host; - int port; + long userid; //鐢ㄦ埛id + std::string username; //鐢ㄦ埛鍚� + std::string password; //瀵嗙爜 + std::string host; //ip + int port; //绔彛 - public: - HCNetdisk(const std::string &_devicename): devicename(_devicename) {} + Netdisk(const std::string &_devicename): devicename(_devicename) {} - ~HCNetdisk() {} + virtual ~Netdisk() {} + - static void netdisk_init(HCNetdisk_EnvConfig &config); + virtual int login(Netdisk_LoginInfo &loginInfo) = 0; - int login(Netdisk_LoginInfo &loginInfo); + virtual int logout() = 0; - int logout(); + virtual int downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files) = 0; - int downloadByTime(Netdisk_DownloadRequest &request); + int getStartChannel() {return startChannel;} + + int getMaxChannels() {return maxChannels;} + }; #endif \ No newline at end of file diff --git a/netdisk/test b/netdisk/test index 4cb10d7..a0193a0 100755 --- a/netdisk/test +++ b/netdisk/test Binary files differ diff --git a/netdisk/test.c b/netdisk/test.c index 0233e84..c44aaab 100644 --- a/netdisk/test.c +++ b/netdisk/test.c @@ -2,14 +2,12 @@ #include "netdisk.h" #include "hcnetdisk.h" -int main() { - HCNetdisk_EnvConfig config; - config.libpath = "../hclib/"; - HCNetdisk::netdisk_init(config); - HCNetdisk *netdisk = new HCNetdisk("娴峰悍"); - //Netdisk *netdisk = dhnetdisk_new(); +void test1() { + + + Netdisk *netdisk = new HCNetdisk("娴峰悍"); //鐧诲綍 Netdisk_LoginInfo loginInfo; @@ -19,6 +17,7 @@ loginInfo.password = "a1234567"; netdisk->login(loginInfo); + std::cout << "璧峰閫氶亾:" << netdisk->getStartChannel() << ", 鏈�澶ч�氶亾鍙凤細" << netdisk->getMaxChannels() << std::endl; //涓嬭浇 Netdisk_DownloadRequest request; request.start.tm_year = 2020-1900; // 杩欎釜鏃堕棿绫诲瀷浠�1900寮�濮嬬畻浣滅涓�骞� @@ -38,15 +37,46 @@ request.channel = 1; request.destpath = "/home/basic/data/Downloads/test"; - - if (netdisk->downloadByTime(request) != 0) { + std::vector<std::string> files; + if (netdisk->downloadByTime(request, &files) != 0) { printf("涓嬭浇澶辫触\n"); + } else { + for(auto f : files) { + std::cout << f << '\n'; + } } netdisk->logout(); +} - +void test2() { + + HCNetdisk netdisk("娴峰悍"); + //Netdisk *netdisk = dhnetdisk_new(); + + + //鐧诲綍 + Netdisk_LoginInfo loginInfo; + loginInfo.host = "192.168.20.11"; + loginInfo.port = 8000; + loginInfo.username = "admin"; + loginInfo.password = "a1234567"; + netdisk.login(loginInfo); + + std::cout << "璧峰閫氶亾:" << netdisk.getStartChannel() << ", 鏈�澶ч�氶亾鍙凤細" << netdisk.getMaxChannels() << std::endl; +} + +int main() { + + Netdisk_EnvConfig config; + config.libpath = "../hclib/"; + HCNetdisk::netdisk_init(&config); + + test1(); + // test2(); + + HCNetdisk::netdisk_deinit(); } \ No newline at end of file diff --git a/test/test_static.c b/test/test_static.c new file mode 100644 index 0000000..68fe065 --- /dev/null +++ b/test/test_static.c @@ -0,0 +1,40 @@ +#include <iostream> + +using namespace std; + +class Box { + public: + static int objectCount; + + // Constructor definition + Box(double l = 2.0, double b = 2.0, double h = 2.0) { + cout <<"Constructor called." << endl; + length = l; + breadth = b; + height = h; + + // Increase every time object is created + objectCount++; + } + double Volume() { + return length * breadth * height; + } + + private: + double length; // Length of a box + double breadth; // Breadth of a box + double height; // Height of a box +}; + +// Initialize static member of class Box +int Box::objectCount = 0; + +int main(void) { + Box Box1(3.3, 1.2, 1.5); // Declare box1 + Box Box2(8.5, 6.0, 2.0); // Declare box2 + + // Print total number of objects. + cout << "Total objects: " << Box::objectCount << endl; + + return 0; +} \ No newline at end of file -- Gitblit v1.8.0