public void AddDevice(AirVisualNode device) { configLock.EnterWriteLock(); try { devices[device.Id] = device; SetValue(NameKey, device.Name, device.Id); SetValue(IPAddressKey, device.DeviceIP.ToString(), device.Id); SetValue(UserNameKey, device.Username, device.Id); SetValue(PasswordKey, HS.EncryptString(device.Password, EncryptPassword), device.Id); SetValue(DeviceIds, devices.Keys.Aggregate((x, y) => x + DeviceIdsSeparator + y)); } finally { configLock.ExitWriteLock(); } }
private string BuildAddNewWebPageBody([AllowNull] AirVisualNode device) { string name = device != null ? device.Name : string.Empty; string ip = device != null?device.DeviceIP.ToString() : string.Empty; string userName = device != null ? device.Username : string.Empty; string password = device != null ? device.Password : string.Empty; string id = device != null ? device.Id : string.Empty; string buttonLabel = device != null ? "Save" : "Add"; string header = device != null ? "Edit" : "Add New"; StringBuilder stb = new StringBuilder(); stb.Append(PageBuilderAndMenu.clsPageBuilder.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(Invariant($"<tr><td class='tablecell'>Username:</td><td class='tablecell' colspan=2>{HtmlTextBox(UserNameId, userName)}</td></tr>")); stb.Append(Invariant($"<tr><td class='tablecell'>Password:</td><td class='tablecell' colspan=2>{HtmlTextBox(PasswordId, password, type: "password")}</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(PageBuilderAndMenu.clsPageBuilder.FormEnd()); return(stb.ToString()); }
/// <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 (string.IsNullOrWhiteSpace(parts[UserNameId])) { results.AppendLine("User name is not Valid.<br>"); } if (results.Length > 0) { this.divToUpdate.Add(SaveErrorDivId, results.ToString()); } else { string deviceId = parts[DeviceIdId]; if (string.IsNullOrWhiteSpace(deviceId)) { deviceId = name.Replace(' ', '_').Replace('.', '_'); } var device = new AirVisualNode(deviceId, parts[NameId], ipAddress, parts[UserNameId], parts[PasswordId]); this.pluginConfig.AddDevice(device); this.pluginConfig.FireConfigChanged(); this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } } else if (form == NameToIdWithPrefix(CancelDeviceName)) { this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } else if (form == NameToIdWithPrefix(DeleteDeviceName)) { this.pluginConfig.RemoveDevice(parts[DeviceIdId]); this.pluginConfig.FireConfigChanged(); this.divToUpdate.Add(SaveErrorDivId, RedirectPage(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } else if (form == NameToIdWithPrefix(DebugLoggingId)) { this.pluginConfig.DebugLogging = parts[NameToId(DebugLoggingId)] == "checked"; } return(base.postBackProc(Name, data, user, userRights)); }