using Cis.Application.Cb;
using EC.Helper.Onvif;
using System.Collections.Concurrent;
namespace Cis.Application.Core.Component.Onvif;
public class OnvifServer : IOnvifServer, ISingleton
{
#region Attr
///
/// {cameraId, OnvifClient}
///
private ConcurrentDictionary OnvifClientDict { get; set; } = new();
#endregion Attr
public OnvifServer()
{
}
#region Base Method
public bool Register(CbCamera camera)
{
bool ret = OnvifClientDict.ContainsKey(camera.Id);
if (ret) return false;
OnvifClient client = new(camera.Ip, camera.UserName, camera.Password);
ret = client.Init().Result;
if (!ret) return false;
ret = OnvifClientDict.TryAdd(camera.Id, client);
return ret;
}
public async Task RegisterAsync(CbCamera camera)
{
bool ret = OnvifClientDict.ContainsKey(camera.Id);
if (ret) return false;
OnvifClient client = new(camera.Ip, camera.UserName, camera.Password);
ret = await client.Init();
if (!ret) return false;
ret = OnvifClientDict.TryAdd(camera.Id, client);
return ret;
}
public bool Delete(long cameraId)
{
return OnvifClientDict.TryRemove(cameraId, out _);
}
public bool IsExists(long cameraId)
{
return OnvifClientDict.ContainsKey(cameraId);
}
public OnvifClient Get(long cameraId)
{
OnvifClientDict.TryGetValue(cameraId, out OnvifClient client);
return client;
}
public bool TryGet(long cameraId, out OnvifClient client)
{
return OnvifClientDict.TryGetValue(cameraId, out client);
}
#endregion Base Method
}