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(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'>{HtmlEncode(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(HtmlEncode(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(FormEnd()); return(stb.ToString()); }
public override string ConfigDevice(int deviceId, [AllowNull] string user, int userRights, bool newDevice) { if (newDevice) { return(string.Empty); } try { DeviceClass deviceClass = (DeviceClass)HS.GetDeviceByRef(deviceId); var deviceIdentifier = DeviceIdentifier.Identify(deviceClass); if (deviceIdentifier != null) { foreach (var device in pluginConfig.Devices) { if (device.Key == deviceIdentifier.DeviceId) { StringBuilder stb = new StringBuilder(); stb.Append(@"<table style='width:100%;border-spacing:0px;'"); 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='tablecell'>Name:</td><td class='tablecell' colspan=2>{ConfigPage.HtmlEncode(device.Value.Name)}</td></tr>")); stb.Append(Invariant($"<tr><td class='tablecell'>Device IP:</td><td class='tablecell' colspan=2>{ConfigPage.HtmlEncode(device.Value.DeviceIP)}</td></tr>")); stb.Append(Invariant($"<tr><td class='tablecell'>Port:</td><td class='tablecell' colspan=2>{ConfigPage.HtmlEncode(deviceIdentifier.Port)}</td></tr>")); stb.Append(Invariant($"<tr><td class='tablecell'>Type:</td><td class='tablecell' colspan=2>{EnumHelper.GetDescription(deviceIdentifier.DeviceType)}</td></tr>")); stb.Append(Invariant($"</td><td></td></tr>")); stb.Append("<tr height='5'><td colspan=3></td></tr>"); stb.Append(@" </table>"); return(stb.ToString()); } } } return(string.Empty); } catch (Exception ex) { LogError(Invariant($"ConfigDevice for {deviceId} With {ex.Message}")); return(string.Empty); } }
/// <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, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } } else if (form == NameToIdWithPrefix(CancelDeviceName)) { this.divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } else if (form == NameToIdWithPrefix(DeleteDeviceName)) { this.pluginConfig.RemoveDevice(parts[DeviceIdId]); this.pluginConfig.FireConfigChanged(); this.divToUpdate.Add(SaveErrorDivId, RedirectPageJS(Invariant($"/{HttpUtility.UrlEncode(ConfigPage.Name)}"))); } else if (form == NameToIdWithPrefix(DebugLoggingId)) { this.pluginConfig.DebugLogging = parts[NameToId(DebugLoggingId)] == "checked"; } return(base.postBackProc(Name, data, user, userRights)); }