using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.RegularExpressions; 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 Tokens { get; set; } public string StreamUriXml { get; set; } public string StreamUri { get; set; } public string SnapshotUriXml { get; set; } public string SnapshotUri { get; set; } public void ParseDiscovery() { this.DeviceUrl = this.RegexMatch(this.DiscoveryXml, @"[^<>\s]*", @"", @"(\s+|)"); this.Id = this.RegexMatch(this.DiscoveryXml, @"[^<>\s]*", @"", @"(\s+|)").Replace("urn:uuid:", "").Replace("-", ""); } public void ParseCapabilities() { this.MediaUrl = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>\s]*", @"\s*", @"(\s+|)"); this.PTZAddress = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>]*", @"\s*", @""); var ptz3DZoomValue = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>]*", @"", @""); this.Ptz3DZoomSupport = bool.TryParse(ptz3DZoomValue, out bool ptz3DZoomSupportBool) ? ptz3DZoomSupportBool : false; } public void ParseProfiles() { this.Tokens = Regex.Matches(this.GetProfilesXml, @"trt:Profiles[^<>]+token\s*=\s*""([^""]*)""").Select(o => o.Groups[1].Value).Where(o => !string.IsNullOrEmpty(o)).ToList(); } public void ParseStreamUri() { this.StreamUri = WebUtility.HtmlDecode(this.RegexMatch(this.StreamUriXml, @"[^<>]*", @"", @"")); } public void ParseSnapshotUri() { this.SnapshotUri = WebUtility.HtmlDecode(this.RegexMatch(this.SnapshotUriXml, @"[^<>]*", @"", @"")); } private string RegexMatch(string input, string pattern, string prefix, string suffix) { return Regex.Match(input, $"(?<={prefix}){pattern}(?={suffix})").Value; } } }