-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigPage.cs
372 lines (318 loc) · 16.4 KB
/
ConfigPage.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using HomeSeerAPI;
using NullGuard;
using Scheduler;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Speech.Synthesis;
using static System.FormattableString;
namespace Hspi.Pages
{
/// <summary>
/// Helper class to generate configuration page for plugin
/// </summary>
/// <seealso cref="Scheduler.PageBuilderAndMenu.clsPageBuilder" />
[NullGuard(ValidationFlags.Arguments | ValidationFlags.NonPublic)]
internal class ConfigPage : PageHelper
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigPage" /> class.
/// </summary>
/// <param name="HS">The hs.</param>
/// <param name="pluginConfig">The plugin configuration.</param>
public ConfigPage(IHSApplication HS, PluginConfig pluginConfig) : base(HS, pluginConfig, Name)
{
}
/// <summary>
/// Gets the name of the web page.
/// </summary>
public static string Name { get; } = Invariant($"{PluginData.PlugInName} Configuration").Replace(' ', '_');
/// <summary>
/// Get the web page string for the configuration page.
/// </summary>
/// <returns>
/// System.String.
/// </returns>
public string GetWebPage(string queryString)
{
try
{
NameValueCollection parts = HttpUtility.ParseQueryString(queryString);
string pageType = parts[PageTypeId];
reset();
System.Text.StringBuilder stb = new System.Text.StringBuilder();
stb.Append(HS.GetPageHeader(Name, "Configuration", string.Empty, string.Empty, false, false));
stb.Append(DivStart("pluginpage", string.Empty));
switch (pageType)
{
case EditDevicePageType:
pluginConfig.Devices.TryGetValue(parts[DeviceIdId], out var device);
stb.Append(BuildAddNewWebPageBody(device)); break;
case MainPageType:
case null:
default:
stb.Append(BuildMainWebPageBody());
break;
}
stb.Append(DivEnd());
AddBody(stb.ToString());
AddFooter(HS.GetPageFooter());
suppressDefaultFooter = true;
return BuildPage();
}
catch (Exception)
{
return "error";
}
}
/// <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, [AllowNull]string user, int userRights)
{
NameValueCollection parts = HttpUtility.ParseQueryString(data);
string form = parts["id"];
if (form == NameToIdWithPrefix(SaveDeviceName))
{
StringBuilder results = new StringBuilder();
// Validate
IPAddress ipAddress = null;
if (string.IsNullOrWhiteSpace(parts[DeviceIPId]) ||
!IPAddress.TryParse(parts[DeviceIPId], out ipAddress))
{
results.AppendLine("IP Address is not Valid.<br>");
}
string name = parts[NameId];
if (string.IsNullOrWhiteSpace(name))
{
results.AppendLine("Name is not Valid.<br>");
}
if (results.Length > 0)
{
divToUpdate.Add(SaveErrorDivId, results.ToString());
}
else
{
string deviceId = parts[DeviceIdId];
if (string.IsNullOrWhiteSpace(deviceId))
{
deviceId = name.Replace(' ', '_').Replace('.', '_');
}
string volumeString = parts[VolumeId];
short volume;
if (!short.TryParse(volumeString, NumberStyles.Any, CultureInfo.InvariantCulture, out volume))
{
volume = -1;
}
var device = new ChromecastDevice(deviceId, parts[NameId], ipAddress, volume == -1 ? null : (ushort?)volume);
pluginConfig.AddDevice(device);
pluginConfig.FireConfigChanged();
divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}")));
}
}
else if (form == NameToIdWithPrefix(CancelDeviceName))
{
divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}")));
}
else if (form == NameToIdWithPrefix(DeleteDeviceName))
{
pluginConfig.RemoveDevice(parts[DeviceIdId]);
pluginConfig.FireConfigChanged();
divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}")));
}
else if (form == NameToIdWithPrefix(SaveSettingName))
{
StringBuilder results = new StringBuilder();
// Validate
IPAddress ipAddress = null;
if (!string.IsNullOrWhiteSpace(parts[ServerIPAddressId]))
{
if (!IPAddress.TryParse(parts[ServerIPAddressId], out ipAddress))
{
results.AppendLine("Server IP Address is not Valid.<br>");
}
}
ushort port = 0;
if (string.IsNullOrWhiteSpace(parts[ServerPortId]) ||
!ushort.TryParse(parts[ServerPortId], NumberStyles.Any, CultureInfo.InvariantCulture, out port))
{
results.AppendLine("Port is not Valid.<br>");
}
if (results.Length > 0)
{
divToUpdate.Add(SaveErrorDivId, results.ToString());
}
else
{
string sapiVoice = parts[SapiVoiceId];
sapiVoice = (sapiVoice == null) || (sapiVoice == NoValueForVoice) ? null : sapiVoice;
pluginConfig.WebServerPort = port;
pluginConfig.WebServerIPAddress = ipAddress;
pluginConfig.DebugLogging = parts[NameToId(DebugLoggingId)] == "checked";
pluginConfig.ForwardSpeach = parts[NameToId(FormatSpeechId)] == "checked";
pluginConfig.SAPIVoice = sapiVoice;
pluginConfig.FireConfigChanged();
divToUpdate.Add(SaveErrorDivId, string.Empty);
}
}
return base.postBackProc(Name, data, user, userRights);
}
private string BuildAddNewWebPageBody([AllowNull]ChromecastDevice device)
{
string name = device != null ? device.Name.ToString() : string.Empty;
string ip = device != null ? device.DeviceIP.ToString() : string.Empty;
string id = device != null ? device.Id : string.Empty;
string buttonLabel = device != null ? "Save" : "Add";
string header = device != null ? "Edit" : "Add New";
int volume = device != null ? (device.Volume ?? -1) : -1;
StringBuilder stb = new StringBuilder();
stb.Append(FormStart("ftmDeviceChange", "IdChange", "Post"));
stb.Append(@"<div>");
stb.Append(@"<table class='full_width_table'>");
stb.Append("<tr height='5'><td style='width:25%'></td><td style='width:20%'></td><td style='width:55%'></td></tr>");
stb.Append(Invariant($"<tr><td class='tableheader' colspan=3>{header}</td></tr>"));
stb.Append(Invariant($"<tr><td class='tablecell'>Name:</td><td class='tablecell' colspan=2>{HtmlTextBox(NameId, name, @readonly: !string.IsNullOrEmpty(id))}</td></tr>"));
stb.Append(Invariant($"<tr><td class='tablecell'>DeviceIP:</td><td class='tablecell' colspan=2>{HtmlTextBox(DeviceIPId, ip)}</td></tr>"));
stb.Append("<tr><td class='tablecell'>Volume:</td><td class='tablecell' colspan=2>");
var volumeDropDown = new clsJQuery.jqDropList(NameToId(VolumeId), PageName, false)
{
toolTip = "Select Volume of device when Spoken",
autoPostBack = false,
};
volumeDropDown.AddItem("Don't Change", NoValueForVoice, volume == -1);
foreach (var value in Enumerable.Range(1, 100))
{
var stringValue = value.ToString(CultureInfo.InvariantCulture);
volumeDropDown.AddItem(stringValue, stringValue, volume == value);
}
stb.Append(volumeDropDown.Build());
stb.Append("</td></tr>");
stb.Append(Invariant($"<tr><td colspan=3>{HtmlTextBox(DeviceIdId, id, type: "hidden")}<div id='{SaveErrorDivId}' style='color:Red'></div></td><td></td></tr>"));
stb.Append(Invariant($"<tr><td colspan=3>{FormPageButton(SaveDeviceName, buttonLabel)}"));
if (device != null)
{
stb.Append(FormPageButton(DeleteDeviceName, "Delete"));
}
stb.Append(FormPageButton(CancelDeviceName, "Cancel"));
stb.Append(Invariant($"</td><td></td></tr>"));
stb.Append("<tr height='5'><td colspan=3></td></tr>");
stb.Append(@"</table>");
stb.Append(@"</div>");
stb.Append(FormEnd());
return stb.ToString();
}
private string BuildMainWebPageBody()
{
StringBuilder stb = new StringBuilder();
stb.Append(@"<div>");
// Setting
stb.Append(CreateSettingTable());
//Device List
stb.Append(CreateDeviceListTable());
return stb.ToString();
}
private string CreateDeviceListTable()
{
StringBuilder stb = new StringBuilder();
stb.Append(@"<table class='full_width_table'>");
stb.Append("<tr height='5'><td colspan=4></td></tr>");
stb.Append("<tr><td class='tableheader' colspan=4>Devices</td></tr>");
stb.Append(@"<tr>" +
"<td class='tablecolumn'>Name</td>" +
"<td class='tablecolumn'>Device IP Address</td>" +
"<td class='tablecolumn'>Volume</td>" +
"<td class='tablecolumn'></td>" +
"</tr>");
foreach (var device in pluginConfig.Devices)
{
stb.Append(@"<tr>");
stb.Append(Invariant($"<td class='tablecell'>{HtmlEncode(device.Value.Name)}</td>"));
stb.Append(Invariant($"<td class='tablecell'>{HtmlEncode(device.Value.DeviceIP)}</td>"));
string volumeString = device.Value.Volume != null ?
device.Value.Volume.Value.ToString(CultureInfo.InvariantCulture) : "Don't Change";
stb.Append(Invariant($"<td class='tablecell'>{HtmlEncode(volumeString)}</td>"));
stb.Append(Invariant($"<td class='tablecell'>{PageTypeButton(Invariant($"Edit{device.Key}"), "Edit", EditDevicePageType, deviceId: device.Key)}</td></tr> "));
}
stb.Append(Invariant($"<tr><td colspan=4>{PageTypeButton("Add New Device", AddNewName, EditDevicePageType)}</td><td></td></tr>"));
stb.Append("<tr height='5'><td colspan=4></td></tr>");
stb.Append("<tr><td colspan=4></td></tr>");
stb.Append(@"<tr height='5'><td colspan=4></td></tr>");
stb.Append(@" </table>");
stb.Append(@"</div>");
return stb.ToString();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "System.Speech.Synthesis.SpeechSynthesizer.GetInstalledVoices")]
private string CreateSettingTable()
{
StringBuilder stb = new StringBuilder();
stb.Append(FormStart("ftmSettings", "Id", "Post"));
stb.Append(@"<table class='full_width_table'>");
stb.Append("<tr height='2'><td width=25%></td><td></td></tr>");
stb.Append("<tr><td class='tableheader' colspan=2>Settings</td></tr>");
stb.Append(Invariant($@"<tr class='tablecell'><td>Override Server IP Address:</td><td>{
HtmlTextBox(ServerIPAddressId,
pluginConfig.WebServerIPAddress != null ? pluginConfig.WebServerIPAddress.ToString() : string.Empty)
}</td ></tr>"));
stb.Append(Invariant($@"<tr class='tablecell'><td>Server Port:</td><td>{
HtmlTextBox(ServerPortId, pluginConfig.WebServerPort.ToString(CultureInfo.InvariantCulture))
}</td></tr>"));
stb.Append(Invariant($@"<tr class='tablecell'><td>Foward Speech To HomeSeer:</td><td>{
FormCheckBox(FormatSpeechId, string.Empty, pluginConfig.ForwardSpeach, false)
}</td></tr>"));
stb.Append("<tr class='tablecell'><td>SAPI Voice:</td><td>");
string sapiVoice = pluginConfig.SAPIVoice;
var volumeDropDown = new clsJQuery.jqDropList(NameToId(SapiVoiceId), PageName, false)
{
toolTip = "Select SAPI Voice",
autoPostBack = false,
};
volumeDropDown.AddItem("Don't Set", NoValueForVoice, string.IsNullOrWhiteSpace(sapiVoice));
using (var speechSynthesizer = new SpeechSynthesizer())
{
foreach (var value in speechSynthesizer.GetInstalledVoices())
{
var stringValue = value.VoiceInfo.Name;
volumeDropDown.AddItem(stringValue, stringValue, sapiVoice == stringValue);
}
}
stb.Append(volumeDropDown.Build());
stb.Append("</td></tr>");
stb.Append(Invariant($@"<tr class='tablecell'><td>Debug Logging Enabled:</td><td>{
FormCheckBox(DebugLoggingId, string.Empty, pluginConfig.DebugLogging, false)
}</td></tr>"));
stb.Append(Invariant($"<tr class='tablecell'><td colspan=2><div id='{SaveErrorDivId}' style='color:Red'></div></td></tr>"));
stb.Append(Invariant($@"<tr class='tablecell'><td colspan=2>{FormPageButton(SaveSettingName, "Save")}</td></tr>"));
stb.Append(@"<tr height='5'><td colspan=2></td></tr>");
stb.Append(@"</table>");
stb.Append(FormEnd());
return stb.ToString();
}
private const string AddNewName = "Add New";
private const string CancelDeviceName = "CancelDeviceName";
private const string DebugLoggingId = "DebugLoggingId";
private const string DeleteDeviceName = "DeleteDeviceName";
private const string DeviceIPId = "DeviceIPId";
private const string EditDevicePageType = "addNew";
private const string FormatSpeechId = "FormatSpeechId";
private const string ImageDivId = "image_id";
private const string MainPageType = "main";
private const string NameId = "NameId";
private const string NoValueForVoice = "-1";
private const string SapiVoiceId = "SapiVoiceId";
private const string SaveDeviceName = "SaveDeviceButton";
private const string SaveErrorDivId = "message_id";
private const string SaveSettingName = "SaveSettings";
private const string ServerIPAddressId = "ServerIPAddressId";
private const string ServerPortId = "ServerPortId";
private const string VolumeId = "VolumeId";
}
}