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.
iot/projects/IoTNode/DeviceServices/Onvif/IPCamera.cs

103 lines
4.6 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace IoTNode.DeviceServices.Onvif
{
public class IPCamera
{
public string DiscoveryXml { get; set; }
public string GetCapabilitiesXml { get; set; }
public string DeviceUrl { get; set; }
public string Ip { 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<Profile> Profiles { get; set; } = new List<Profile>();
public string StreamUriXml { get; set; }
public string SnapshotUriXml { get; set; }
public string StreamUri { get; private set; }
public string SnapshotUri { get; private 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("-", "");
this.Ip = Regex.Match(this.DeviceUrl, "//([^/]*)/").Groups[1].Value;
}
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()
{
if (string.IsNullOrEmpty(this.GetProfilesXml))
{
return;
}
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 condition = false;
var videoEncoderConfiguration = item.Elements().FirstOrDefault(o => o.Name.LocalName == "VideoEncoderConfiguration");
XElement resolution = null;
if (videoEncoderConfiguration == null)
{
condition = true;
videoEncoderConfiguration = item.Elements().FirstOrDefault(o => o.Name.LocalName == "VideoSourceConfiguration");
resolution = videoEncoderConfiguration.Elements().FirstOrDefault(o => o.Name.LocalName == "Bounds");
}
else
{
resolution = videoEncoderConfiguration.Elements().FirstOrDefault(o => o.Name.LocalName == "Resolution");
}
if (condition)
{
profile.Width = Convert.ToInt32(resolution.Attribute("width").Value);
profile.Height = Convert.ToInt32(resolution.Attribute("height").Value);
}
else
{
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.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;
}
}
}