wangzhengquan
2020-06-16 794acdd7e45b2305f25fdddc6fd43e7648216807
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "netdisk.h"
 
bool HCNetdisk::envInited = false;
 
/**
 * 设备初始化
 */
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文件夹所在的路径
      NET_DVR_SetSDKInitCfg(NET_SDK_INIT_CFG_SDK_PATH, &struComPath);
    }
 
    //---------------------------------------
    // 初始化
    NET_DVR_Init();
    //设置连接时间与重连时间
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);
    envInited = true;
}
 
void HCNetdisk::netdisk_deinit() {
    if (!envInited) {
        return;
    }
    NET_DVR_Cleanup();
    envInited = false;
}
 
 
 
 
HCNetdisk::HCNetdisk(): Netdisk(){
  deviceType="HC";
}
 
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);
  printf("lUserID = %d\n", lUserID);
  if (lUserID < 0)
  {
    printf("Login error, %d\n", NET_DVR_GetLastError());
    NET_DVR_Cleanup();
    return -1;
  }
 
  
  loginInfo.userid = std::to_string(lUserID);
  userid = lUserID;
  username = loginInfo.username;
  password = loginInfo.password;
  host = loginInfo.host;
  port = loginInfo.port;
 
 
  deviceInfo.startChannel = struDeviceInfo.struDeviceV30.byStartDChan;
  deviceInfo.maxChannels = struDeviceInfo.struDeviceV30.byIPChanNum + struDeviceInfo.struDeviceV30.byHighDChanNum * 256;
 
  return 0;
}
 
int HCNetdisk::login(Netdisk_LoginInfo &loginInfo) {
 
   
  return _login(loginInfo);
  //Netdisk::addLoginInfo(loginInfo);
 
}
 
int HCNetdisk::logout() {
    //注销用户
    NET_DVR_Logout(userid);
    return 0;
}
 
int HCNetdisk::downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files) {
    struct tm start, end;
    char destfile[1024];
    int i = 0;
 
    // Netdisk_LoginInfo loginInfo = Netdisk::getLoginInfo(request.loginUUID);
    // _login(loginInfo);
    
    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(), 
            deviceType.c_str(), 
             request.channel, 
             mktime(&start), 
             i);
    
        if(downloadByTime_wrapper(userid, request.channel, &start, &end, destfile) != -1) {
      if(files != NULL) {
        files->push_back(destfile);
      }
    }
        
        start = end;
        i++;
    }
    
    return 0;
}
 
HCNetdisk::~HCNetdisk() { }
 
/**
 * @return success: 0, failture : -1
 */
int HCNetdisk::downloadByTime_wrapper(long userid, int channel, struct tm *start, struct tm *end, char *destfile) {
  NET_DVR_FILECOND_V40 struFileCond = {0};
  struFileCond.dwFileType = 0xFF;
  
//通道号
  struFileCond.lChannel = channel;
  struFileCond.dwIsLocked = 0xFF;
  struFileCond.dwUseCardNo = 0;
  struFileCond.struStartTime.dwYear = start->tm_year + 1900; //开始时间
  struFileCond.struStartTime.dwMonth =  start->tm_mon + 1;
  struFileCond.struStartTime.dwDay = start->tm_mday;
  struFileCond.struStartTime.dwHour = start->tm_hour;
  struFileCond.struStartTime.dwMinute = start->tm_min;
  struFileCond.struStartTime.dwSecond = start->tm_sec;
 
  struFileCond.struStopTime.dwYear = end->tm_year + 1900; //结束时间
  struFileCond.struStopTime.dwMonth = end->tm_mon + 1;
  struFileCond.struStopTime.dwDay = end->tm_mday;
  struFileCond.struStopTime.dwHour = end->tm_hour;
  struFileCond.struStopTime.dwMinute = end->tm_min;
  struFileCond.struStopTime.dwSecond = end->tm_sec;
 
 
//---------------------------------------
//查找录像文件
  int lFindHandle = NET_DVR_FindFile_V40(userid, &struFileCond);
  if (lFindHandle < 0)
  {
    printf("find file fail,last error %d\n", NET_DVR_GetLastError());
    return -1;
  }
  NET_DVR_FINDDATA_V40 struFileData;
  while (true)
  {
//逐个获取查找到的文件信息
    int result = NET_DVR_FindNextFile_V40(lFindHandle, &struFileData);
    if (result == NET_DVR_ISFINDING)
    {
 
      continue;
    }
    else if (result == NET_DVR_FILE_SUCCESS) //获取文件信息成功
    {
      char strFileName[256] = {0};
      if (destfile != NULL) {
        sprintf(strFileName, "%s", destfile);
        char *dir = dirname(strdup(destfile));
        printf("dir === %s\n", dir);
        if ((mkdir(dir, DIR_MODE) == -1) && (errno != EEXIST)) {
            err_msg(errno, "hc_downloadByTime mkdir error ");
            return -1;
        }
      } else {
        sprintf(strFileName, "./%s", struFileData.sFileName);
      }
      
      return saveRecordFile(userid, struFileData.sFileName, strFileName);
    }
    else if (result == NET_DVR_FILE_NOFIND || result == NET_DVR_NOMOREFILE) //未查找到文件或者查找结束
    {
      err_msg(0, "can not finding! destfile=%s\n", destfile);
      break;
    }
    else
    {
      printf("find file fail for illegal get file state");
      break;
    }
  }
//停止查找
  if (lFindHandle >= 0)
  {
    NET_DVR_FindClose_V30(lFindHandle);
  }
  return -1;
}
 
 
 
 
 
int HCNetdisk::saveRecordFile(int userId, char * srcfile, char * destfile)
{
  printf("destfile=%s\n", destfile);
  int bRes = 1;
  int hPlayback = 0;
//按文件名下载录像
  if ( (hPlayback = NET_DVR_GetFileByName(userId, srcfile, destfile)) < 0 )
  {
    printf( "GetFileByName failed. error[%d]\n", NET_DVR_GetLastError());
    bRes = -1;
    return bRes;
  }
//开始下载
  if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART, NULL, 0, NULL, NULL))
  {
    printf("play back control failed [%d]\n", NET_DVR_GetLastError());
    bRes = -1;
    return bRes;
  }
  int nPos = 0;
  for (nPos = 0; nPos < 100 && nPos >= 0; nPos = NET_DVR_GetDownloadPos(hPlayback))
  {
    printf("Be downloading...%d %%\n", nPos);
    sleep(1);
//下载进度
//millisecond
  }
  printf("have got %d\n", nPos);
//停止下载
  if (!NET_DVR_StopGetFile(hPlayback))
  {
    printf("failed to stop get file [%d]\n", NET_DVR_GetLastError());
    bRes = -1;
    return bRes;
  }
  printf("%s\n", srcfile);
  if (nPos < 0 || nPos > 100)
  {
    printf("download err [%d]\n", NET_DVR_GetLastError());
    bRes = -1;
    return bRes;
  }
  else
  {
    return 0;
  }
}