You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.6 KiB

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<string> 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]*", @"<d:XAddrs>", @"(\s+|</d:XAddrs>)");
this.Id = this.RegexMatch(this.DiscoveryXml, @"[^<>\s]*", @"<wsa(dis)?:Address>", @"(\s+|</wsa(dis)?:Address>)").Replace("urn:uuid:", "").Replace("-", "");
}
public void ParseCapabilities()
{
this.MediaUrl = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>\s]*", @"<tt:Media>\s*<tt:XAddr>", @"(\s+|</tt:XAddr>)");
this.PTZAddress = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>]*", @"<tt:PTZ>\s*<tt:XAddr>", @"</tt:XAddr>");
var ptz3DZoomValue = this.RegexMatch(this.GetCapabilitiesXml, @"[^<>]*", @"<hikxsd:PTZ3DZoomSupport>", @"</hikxsd:PTZ3DZoomSupport>");
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, @"[^<>]*", @"<tt:Uri>", @"</tt:Uri>"));
}
public void ParseSnapshotUri()
{
this.SnapshotUri = WebUtility.HtmlDecode(this.RegexMatch(this.SnapshotUriXml, @"[^<>]*", @"<tt:Uri>", @"</tt:Uri>"));
}
private string RegexMatch(string input, string pattern, string prefix, string suffix)
{
return Regex.Match(input, $"(?<={prefix}){pattern}(?={suffix})").Value;
}
}
}