-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetalarm.cpp
More file actions
184 lines (164 loc) · 4.09 KB
/
netalarm.cpp
File metadata and controls
184 lines (164 loc) · 4.09 KB
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
//============================================================================
// Name : main.cpp
// Author : Huy Do (huydo1@gmail.com)
// Version : 2017-04-22
// Description : Net Alarm Test for DAHUA NVR
//============================================================================
#include <iostream>
#include <unistd.h> // for sleep
#include <string>
#include "dhnetsdk.h"
using namespace std;
LLONG m_lLoginHandle;
void CALLBACK DisConnectFunc(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
cout << "Callback DisConnect" << endl;
return;
}
void init()
{
cout << "init" << endl;
bool success = CLIENT_Init(DisConnectFunc, (LDWORD) 0);
if (success)
{
cout << "init ok" << endl;
}
else
{
cout << "init failed" << endl;
}
}
bool login(string destip, long port, string username, string password)
{
cout << "login" << endl;
int error = 0;
NET_DEVICEINFO deviceInfo = {0};
m_lLoginHandle = CLIENT_Login(destip.c_str(), port, username.c_str(), password.c_str(), &deviceInfo, &error);
if(m_lLoginHandle == 0)
{
if(error != 255)
{
cout << "login failed with code " << error << endl;
}
else
{
cout << "login also failed" << endl;
}
return false;
}
else
{
cout << "login ok" << endl;
return true;
}
}
void logout()
{
cout << "logout" << endl;
bool success = CLIENT_Logout(m_lLoginHandle);
if(success)
{
cout << "logout ok" << endl;
}
else
{
cout << "logout failed" << endl;
}
}
void triggerAlarm(int alarmIndex, int action)
{
if (action > 1)
{
cout << "invalid action" << endl;
return;
}
cout << "trigger alarm: " << alarmIndex << " -> " << action << endl;
if(0 != m_lLoginHandle)
{
ALARMCTRL_PARAM alarmParam = {0};
alarmParam.dwSize = sizeof(ALARMCTRL_PARAM);
alarmParam.nAlarmNo = alarmIndex;
alarmParam.nAction = action;
BOOL bSuccess = CLIENT_ControlDevice(m_lLoginHandle, DH_TRIGGER_ALARM_IN, &alarmParam);
if(bSuccess)
{
cout << "ok" << endl;
}
else
{
cout << "failed" << endl;
}
}
else
{
cout << "invalid login" << endl;
}
}
void alarmInStart(int alarmIndex)
{
cout << "alarm start" << endl;
if(0 != m_lLoginHandle)
{
ALARMCTRL_PARAM alarmParam = {0};
alarmParam.dwSize = sizeof(ALARMCTRL_PARAM);
alarmParam.nAction = 1;
alarmParam.nAlarmNo = alarmIndex;
BOOL bSuccess = CLIENT_ControlDevice(m_lLoginHandle, DH_TRIGGER_ALARM_IN, &alarmParam);
if(bSuccess)
{
cout << "ok" << endl;
}
else
{
cout << "failed" << endl;
}
}
else
{
cout << "invalid login" << endl;
}
}
void alarmInStop()
{
cout << "alarm stop" << endl;
if(0 != m_lLoginHandle)
{
ALARMCTRL_PARAM alarmParam = {0};
alarmParam.dwSize = sizeof(ALARMCTRL_PARAM);
alarmParam.nAction = 0;
alarmParam.nAlarmNo = 0;
BOOL bSuccess = CLIENT_ControlDevice(m_lLoginHandle, DH_TRIGGER_ALARM_IN, &alarmParam);
if(bSuccess)
{
cout << "ok" << endl;
}
else
{
cout << "failed" << endl;
}
}
else
{
cout << "invalid login" << endl;
}
}
int main(int argc, char* argv[]) {
cout << "Dahua Net Alarm Test" << endl;
if (argc != 6)
{
cout << "Usage: netalarm <host ip> <port> <username> <password> <alarm channel>" << endl;
return 1;
}
init();
bool log_ok = login(argv[1], std::stol(argv[2]), argv[3], argv[4]);
if (!log_ok)
{
return 2;
}
int alarmIndex = std::stoi(argv[5]);
triggerAlarm(alarmIndex, 1);
sleep(2);
triggerAlarm(alarmIndex, 0);
logout();
return 0;
}