using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Xml.Linq; namespace ONVIFService { public class IPCamera { public string DiscoveryXml { get; set; } public string GetCapabilitiesXml { get; set; } public string DeviceUrl { get; set; } public string Id { get; set; } public string MediaUrl { get; set; } public string GetProfilesXml { get; set; } public string UserName { get; set; } public string Password { get; set; } public string PTZAddress { get; set; } public bool Ptz3DZoomSupport { get; set; } public List Profiles { get; set; } = new List(); public string MainStreamUriXml { get; set; } public string MainSnapshotUriXml { get; set; } public string SubStreamUriXml { get; set; } public string SubSnapshotUriXml { get; set; } public string MainStreamUri { get; set; } public string SubStreamUri { get; set; } public string MainSnapshotUri { get; set; } public string SubSnapshotUri { get; set; } public void ParseDiscovery() { var doc = XDocument.Parse(this.DiscoveryXml); this.DeviceUrl = doc.Descendants().FirstOrDefault(o => o.Name.LocalName == "XAddrs").Value.Split(' ')[0]; this.Id = doc.Descendants() .FirstOrDefault(o => o.Name.LocalName == "EndpointReference").Elements() .FirstOrDefault(o => o.Name.LocalName == "Address").Value.Replace("urn:uuid:", "").Replace("-", ""); } public void ParseCapabilities() { var doc = XDocument.Parse(this.GetCapabilitiesXml); this.MediaUrl = doc.Descendants().FirstOrDefault(o => o.Name.LocalName == "Media").Elements().FirstOrDefault(o => o.Name.LocalName == "XAddr").Value; this.PTZAddress = doc.Descendants().FirstOrDefault(o => o.Name.LocalName == "PTZ")?.Elements().FirstOrDefault(o => o.Name.LocalName == "XAddr")?.Value; this.Ptz3DZoomSupport = string.IsNullOrWhiteSpace(this.PTZAddress) ? false : true; } public void ParseProfiles() { var doc = XDocument.Parse(this.GetProfilesXml); foreach (var item in doc.Descendants().Where(o => o.Name.LocalName == "Profiles")) { var profile = new Profile { Token = item.Attribute("token").Value, Name = item.Elements().FirstOrDefault(o => o.Name.LocalName == "Name").Value }; var videoEncoderConfiguration = item.Elements().FirstOrDefault(o => o.Name.LocalName == "VideoEncoderConfiguration"); profile.Encoding = videoEncoderConfiguration.Elements().FirstOrDefault(o => o.Name.LocalName == "Encoding").Value; var resolution = videoEncoderConfiguration.Elements().FirstOrDefault(o => o.Name.LocalName == "Resolution"); profile.Width = Convert.ToInt32(resolution.Elements().FirstOrDefault(o => o.Name.LocalName == "Width").Value); profile.Height = Convert.ToInt32(resolution.Elements().FirstOrDefault(o => o.Name.LocalName == "Height").Value); this.Profiles.Add(profile); } } public void ParseStreamUri() { this.MainStreamUri = WebUtility.HtmlDecode(this.RegexMatch(this.MainStreamUriXml, @"[^<>]*", @"", @"")); this.SubStreamUri = WebUtility.HtmlDecode(this.RegexMatch(this.SubStreamUriXml, @"[^<>]*", @"", @"")); } public void ParseSnapshotUri() { this.MainSnapshotUri = WebUtility.HtmlDecode(this.RegexMatch(this.MainSnapshotUriXml, @"[^<>]*", @"", @"")); this.SubSnapshotUri = WebUtility.HtmlDecode(this.RegexMatch(this.SubSnapshotUriXml, @"[^<>]*", @"", @"")); } private string RegexMatch(string input, string pattern, string prefix, string suffix) { return Regex.Match(input, $"(?<={prefix}){pattern}(?={suffix})").Value; } } }