-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHikAlarmConfig.cs
327 lines (284 loc) · 15.2 KB
/
HikAlarmConfig.cs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (C) 2016 SRG Technology, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Scheduler;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using HSPI_Template;
namespace HSPI_HikAlarmCheck
{
/// <summary>
/// Configuration web page for the HikAlarmCheck plugin.
/// This item shows up under the plugin menu option for HikAlarmCheck under the name Config.
/// It creates the web page using the controls available in the HomeSeer clsPageBuilder class.
/// Processing of the controls for this page are also performed but uses calls back to the plugin class to update HomeSeer information.
/// </summary>
/// <seealso cref="HSPI_HikAlarmCheck.HikAlarmPageBuilder" />
class HikAlarmConfig : PageBuilder
{
private static string pageName = "Config";
private HSPI plugin;
/// <summary>
/// Initializes a new instance of the <see cref="HikAlarmConfig"/> class.
/// </summary>
/// <param name="plugin">The HomeSeer plugin.</param>
public HikAlarmConfig(HSPI plugin) : base(pageName)
{
this.plugin = plugin;
}
/// <summary>
/// Gets the name of the web page.
/// </summary>
public string Name
{
get { return pageName; }
}
/// <summary>
/// The user has selected a control on the configuration web page.
/// The post data is provided to determine the control that initiated the post and the state of the other controls.
/// </summary>
/// <param name="data">The post data.</param>
/// <param name="user">The name of logged in user.</param>
/// <param name="userRights">The rights of the logged in user.</param>
/// <returns>Any serialized data that needs to be passed back to the web page, generated by the clsPageBuilder class.</returns>
public string PostBackProc(string data, string user, int userRights)
{
Console.WriteLine("PostBackProc: {0}", data);
NameValueCollection parts = HttpUtility.ParseQueryString(data);
switch (parts["id"])
{
case "oButAddCamera":
Console.WriteLine("Add Camera");
AddCameraButton(parts["TextBoxName"], parts["TextBoxIpAddress"], parts["TextBoxUsername"], parts["TextBoxPassword"]);
break;
case "oButSaveCamera":
Console.WriteLine("Save Camera: {0}", parts["ListBoxCameras"]);
SaveCameraButton(parts["ListBoxCameras"], parts["TextBoxIpAddressEdit"], parts["TextBoxUsernameEdit"], parts["TextBoxPasswordEdit"]);
break;
case "oButDeleteCamera":
Console.WriteLine("Delete Camera: {0}", parts["ListBoxCameras"]);
DeleteCameraButton(parts["ListBoxCameras"]);
break;
case "oListBoxCameras":
Console.WriteLine("Change Selected Camera");
ChangeCameraSelection(parts["ListBoxCameras"]);
break;
}
return base.postBackProc(Name, data, user, userRights);
}
/// <summary>
/// Get the web page string for the configuration page.
/// </summary>
/// <param name="user">The name of logged in user.</param>
/// <param name="userRights">The rights of the logged in user.</param>
/// <param name="queryString">The query string.</param>
/// <returns>System.String.</returns>
public string GetWebPage(string user, int userRights, string queryString)
{
System.Text.StringBuilder stb = new System.Text.StringBuilder();
try
{
reset();
// Add the title
AddHeader(plugin.HsApplication.GetPageHeader(Name, "Hik Alarm Check Configuration", "", "", false, false));
stb.Append(PageBuilderAndMenu.clsPageBuilder.DivStart("pluginpage", ""));
// a message area for error messages from jquery ajax postback (optional, only needed if using AJAX calls to get data)
//stb.Append(clsPageBuilder.DivStart("errormessage", "class='errormessage'"));
//stb.Append(clsPageBuilder.DivEnd());
//RefreshIntervalMilliSeconds = 5000; // # of seconds between callbacks
// add a callback post string, this is what will be posted when the page timer expires
//stb.Append(AddAjaxHandlerPost("id=timer", pageName));
// Add the
stb.Append(BuildWebPageBody());
stb.Append(PageBuilderAndMenu.clsPageBuilder.DivEnd());
// Add the body html to the page
AddBody(stb.ToString());
// The default footer has to be manually added for a plugin.
AddFooter(plugin.HsApplication.GetPageFooter());
suppressDefaultFooter = true;
// return the full page
return BuildPage();
}
catch (Exception)
{
// Log("Error - Building page: " & ex.Message, LogLevel.Normal)
return "error";
}
}
/// <summary>
/// Builds the web page body for the configuration page.
/// The page has separate forms so that only the data in the appropriate form is returned when a button is pressed.
/// </summary>
private string BuildWebPageBody()
{
StringBuilder stb = new StringBuilder();
NameValueCollection cameraList = HikAlarmThreadManager.CameraNameList();
bool allowEdit = (cameraList.Count > 0);
string editIpAddress = "";
string editUsername = "";
string editPassword = "";
if (allowEdit)
{
plugin.GetDeviceParameters(cameraList.Get(0), ref editIpAddress, ref editUsername, ref editPassword);
editPassword = "******";
}
try
{
stb.Append("<table width='1000' cellpadding='0' cellspacing='0' border='0'>");
// Help button
stb.Append(" <tr>");
stb.Append(" <td></td>");
stb.Append(" <td align='right'>");
//stb.Append(" <a href='HikAlaramCheck%20Help%20File\\HikAlarmCheck-Help.htm'>Help Page</a>");
stb.Append(" </td>");
stb.Append(" </tr>");
// Add new item section
stb.Append(" <tr>");
stb.Append(" <td colspan='2'>");
stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("frmAddCamera", "AddCamera", "Post"));
stb.Append(" <table width='1000' cellpadding='0' cellspacing='0' style='border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid;'>");
stb.Append(" <tr><td style='background-color: #Cdcdcd; text-align: center;'><br /></td></tr>");
stb.Append(" <tr>");
stb.Append(" <td align='center' style='background-color: #f5f5f5;'>");
stb.Append(" <table width='100%'>");
stb.Append(" <col width='100'><col width='100'>");
stb.Append(" <tr><td><strong>Add New Camera:</strong></td></tr>");
stb.Append(" <tr><td>Name:</td><td>" + BuildTextBox("TextBoxName") + "</td></tr>");
stb.Append(" <tr><td>Ip Address:</td><td>" + BuildTextBox("TextBoxIpAddress") + "</td></tr>");
stb.Append(" <tr><td>Username:</td><td>" + BuildTextBox("TextBoxUsername") + "</td></tr>");
stb.Append(" <tr><td>Password:</td><td>" + BuildTextBox("TextBoxPassword") + "</td></tr>");
stb.Append(" <tr><td></td><td>" + BuildButton("Add Camera", "ButAddCamera") + "</td></tr>");
stb.Append(" </table>");
stb.Append(" </td>");
stb.Append(" </tr>");
stb.Append(" </table>");
stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());
stb.Append(" </td>");
stb.Append(" </tr>");
// Select item to edit
stb.Append(" <tr>");
stb.Append(" <td colspan='2'>");
stb.Append(PageBuilderAndMenu.clsPageBuilder.FormStart("frmEditCamera", "EditCamera", "Post"));
stb.Append(" <table width='1000' cellpadding='0' cellspacing='0' style='border-right: black thin solid; border-top: none; border-left: black thin solid; border-bottom: black thin solid;'>");
stb.Append(" <tr><td style='background-color: #Cdcdcd; text-align: center;'><br /></td></tr>");
stb.Append(" <tr>");
stb.Append(" <td align='center' style='background-color: #f5f5f5;'>");
stb.Append(" <table width='100%'>");
stb.Append(" <col width='100'><col width='100'>");
stb.Append(" <tr><td><strong>Edit Camera:</strong></td><td>" + BuildDropList("ListBoxCameras", ref cameraList) + "</td></tr>");
stb.Append(" <tr><td>Ip Address:</td><td>" + BuildTextBox("TextBoxIpAddressEdit", editIpAddress, allowEdit) + "</td></tr>");
stb.Append(" <tr><td>Username:</td><td>" + BuildTextBox("TextBoxUsernameEdit", editUsername, allowEdit) + "</td></tr>");
stb.Append(" <tr><td>Password:</td><td>" + BuildTextBox("TextBoxPasswordEdit", editPassword, allowEdit) + "</td></tr>");
stb.Append(" <tr><td></td>");
stb.Append(" <td align='left'>");
stb.Append(" <table width='20%'>");
stb.Append(" <col width='30'><col width='30'>");
stb.Append(" <tr><td>" + BuildButton("Save", "ButSaveCamera", allowEdit) + "</td><td>" + BuildButton("Delete", "ButDeleteCamera", allowEdit) + "</td></tr>");
stb.Append(" </table>");
stb.Append(" </td>");
stb.Append(" </table>");
stb.Append(" </td>");
stb.Append(" </tr>");
stb.Append(" </table>");
stb.Append(PageBuilderAndMenu.clsPageBuilder.FormEnd());
stb.Append(" </td>");
stb.Append(" </tr>");
// End of table
stb.Append("</table>");
return stb.ToString();
}
catch (Exception)
{
}
return null;
}
/// <summary>
/// Add a new camera device to the HomeSeer configuration.
/// </summary>
/// <param name="name">The name for the HomeSeer device.</param>
/// <param name="ipAddress">The IP address for the camera.</param>
/// <param name="username">The username to use for the camera.</param>
/// <param name="password">The password to use for the camera.</param>
private void AddCameraButton(string name, string ipAddress, string username, string password)
{
if ((name == "") || (ipAddress == "") || (username == "") || (password == ""))
return;
int refId = plugin.CreateDeviceFromConfig(name, ipAddress, username, password);
NameValueCollection cameraList = HikAlarmThreadManager.CameraNameList();
UpdateDropList("ListBoxCameras", ref cameraList, SelectedValue: refId.ToString());
ChangeCameraSelection(refId.ToString());
}
/// <summary>
/// Update the information for the camera defined by the reference id.
/// </summary>
/// <param name="refIdStr">The device reference identifier (as string) for HomeSeer.</param>
/// <param name="ipAddress">The IP address string for the camera.</param>
/// <param name="username">The username to use for the camera.</param>
/// <param name="password">The password to use for the camera.</param>
private void SaveCameraButton(string refIdStr, string ipAddress, string username, string password)
{
if ((refIdStr == "") || (ipAddress == "") || (username == ""))
return;
if (password == "******")
password = "";
plugin.UpdateDevice(refIdStr, ipAddress, username, password);
ChangeCameraSelection(refIdStr);
}
/// <summary>
/// Delete the camera defined by the reference id.
/// </summary>
/// <param name="refIdStr">The device reference identifier (as string) for HomeSeer.</param>
private void DeleteCameraButton(string refIdStr)
{
plugin.DeleteDevice(refIdStr);
UpdateCameraList();
}
/// <summary>
/// Updates the camera list from the thread manager.
/// </summary>
private void UpdateCameraList()
{
string selectedValue = "";
NameValueCollection cameraList = HikAlarmThreadManager.CameraNameList();
if (cameraList.Count > 0)
selectedValue = cameraList.Get(0);
UpdateDropList("ListBoxCameras", ref cameraList, SelectedValue: selectedValue);
ChangeCameraSelection(selectedValue);
}
/// <summary>
/// Change the camera selection on the web page. Update the values in the other controls.
/// </summary>
/// <param name="refIdStr">The device reference identifier (as string) for HomeSeer.</param>
private void ChangeCameraSelection(string refIdStr)
{
string ipAddress = "";
string username = "";
string password = "";
bool allowEdit = (refIdStr != "");
plugin.GetDeviceParameters(refIdStr, ref ipAddress, ref username, ref password);
UpdateTextBox("TextBoxIpAddressEdit", ipAddress, allowEdit);
UpdateTextBox("TextBoxUsernameEdit", username, allowEdit);
UpdateTextBox("TextBoxPasswordEdit", "******", allowEdit);
UpdateButton("Save", "ButSaveCamera", allowEdit);
UpdateButton("Delete", "ButDeleteCamera", allowEdit);
}
}
}