public void AddDevice(MPowerDevice 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); foreach (var item in System.Enum.GetValues(typeof(DeviceType))) { SetValue(item.ToString(), device.EnabledTypes.Contains((DeviceType)item), device.Id); } foreach (var pair in device.Resolution) { SetValue(ResolutionKey(pair.Key), pair.Value, device.Id); } if (device.EnabledPorts.Count > 0) { SetValue(PortsEnabledKey, device.EnabledPorts .Select(x => x.ToString(CultureInfo.InvariantCulture)) .Aggregate((x, y) => x + PortsEnabledSeparator + y), device.Id); } else { SetValue(PortsEnabledKey, string.Empty, device.Id); } SetValue(DeviceIds, devices.Keys.Aggregate((x, y) => x + DeviceIdsSeparator + y)); } finally { configLock.ExitWriteLock(); } }
/// <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>"); } foreach (var item in System.Enum.GetValues(typeof(DeviceType))) { var deviceType = (DeviceType)item; string description = EnumHelper.GetDescription(deviceType); if (deviceType == DeviceType.Output) { continue; } string value = parts[GetResolutionId(deviceType)]; if (!double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double resolution) || resolution <= 0D) { results.AppendLine(Invariant($"{description} does not have valid resolution.<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 enabledTypes = new SortedSet <DeviceType>(); var resolution = new Dictionary <DeviceType, double>(); foreach (var item in System.Enum.GetValues(typeof(DeviceType))) { var deviceType = (DeviceType)item; if (parts[NameToId(item.ToString())] == "checked") { enabledTypes.Add(deviceType); } string value = parts[GetResolutionId(deviceType)]; if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double resolutionValue)) { resolution.Add(deviceType, resolutionValue); } } var enabledPorts = new SortedSet <int>(); foreach (var item in Enumerable.Range(1, PortsMax)) { if (parts[NameToId(item.ToString(CultureInfo.InvariantCulture))] == "checked") { enabledPorts.Add(item); } } var device = new MPowerDevice(deviceId, parts[NameId], ipAddress, parts[UserNameId], parts[PasswordId], enabledTypes, resolution, enabledPorts); 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)); }
private string BuildAddNewWebPageBody([AllowNull] MPowerDevice device) { string name = device != null?device.Name.ToString() : 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; var enabledTypes = device != null ? device.EnabledTypes : new SortedSet <DeviceType>(); var resolution = device != null ? device.Resolution : new ReadOnlyDictionary <DeviceType, double>(new Dictionary <DeviceType, double>()); var enabledPorts = device != null ? device.EnabledPorts : new SortedSet <int>(); 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(@"<tr><td class='tablecell'>Enabled Devices</td><td class='tablecell' colspan=2>"); foreach (var item in System.Enum.GetValues(typeof(DeviceType))) { var deviceType = (DeviceType)item; string description = EnumHelper.GetDescription((Enum)item); stb.Append(FormCheckBox(item.ToString(), description, enabledTypes.Contains(deviceType))); stb.Append("<br>"); } stb.Append(@"</td></tr>"); foreach (var item in System.Enum.GetValues(typeof(DeviceType))) { var deviceType = (DeviceType)item; string description = EnumHelper.GetDescription(deviceType); if (deviceType == DeviceType.Output) { continue; } stb.Append(Invariant($"<tr><td class='tablecell'>{description} Resolution:</td><td class='tablecell' colspan=2>")); stb.Append(HtmlTextBox(GetResolutionId(deviceType), enabledTypes.Contains(deviceType) ? resolution[deviceType].ToString(CultureInfo.InvariantCulture) : PluginConfig.GetDefaultResolution(deviceType).ToString(CultureInfo.InvariantCulture), 10)); stb.Append(" "); stb.Append(PluginConfig.GetUnits(deviceType)); stb.Append("</td></tr> "); } stb.Append(@"<tr><td class='tablecell'>Enabled Ports</td><td class='tablecell' colspan=2>"); foreach (var item in Enumerable.Range(1, PortsMax)) { string itemString = item.ToString(CultureInfo.InvariantCulture); stb.Append(FormCheckBox(itemString, itemString, enabledPorts.Contains(item))); stb.Append("<br>"); } 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(PageBuilderAndMenu.clsPageBuilder.FormEnd()); return(stb.ToString()); }