using EC.App.ThatBLL.Onvif.Intf; using EC.App.ThatService.Onvif; using EC.Onvif; using System; using System.IO; using System.Net; using System.Threading.Tasks; namespace EC.App.ThatBLL.Onvif { public class MediaBLL : IMediaBLL { private readonly OnvifClientService _ocService; public MediaBLL(OnvifClientService onvifClientService) { _ocService = onvifClientService; } public bool IsConnected(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); bool ret = flag && onvifClient.IsMediaContected(); return ret; } public async Task GetStreamUrl(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); if (!flag) return string.Empty; string url = await onvifClient.GetStreamUri(); return url; } public async Task GetSnapshotUrl(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); if (!flag) return string.Empty; string url = await onvifClient.GetSnapshotUri(); return url; } public async Task GetSnapshot(string ip) { bool flag = _ocService.TryGet(ip, out OnvifClient onvifClient); if (!flag) return string.Empty; string url = await onvifClient.GetSnapshotUri(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.PreAuthenticate = true; request.Credentials = new NetworkCredential(onvifClient.Username, onvifClient.Password); using HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using Stream stream = response.GetResponseStream(); using MemoryStream mStream = new(); byte[] buffer = new byte[1024]; int byteCount; do { byteCount = stream.Read(buffer, 0, buffer.Length); mStream.Write(buffer, 0, byteCount); } while (byteCount > 0); mStream.Position = 0; return Convert.ToBase64String(mStream.ToArray()); } } }