Camera Information System
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.

91 lines
2.5 KiB

using Cis.Application.Core.Component.PtzServer;
using EC.Helper.CameraSDK;
using FreeRedis;
using System.Collections.Concurrent;
namespace Cis.Application.Core.Component.CameraSDK;
/// <summary>
/// 相机 sdk 服务类
/// </summary>
public class CameraSdkServer : ICameraSdkServer, ISingleton
{
#region Attr
private readonly RedisClient _cache;
/// <summary>
/// {ip, ICameraSDK}
/// </summary>
private ConcurrentDictionary<string, ICameraSDK> CameraSdkDict { get; set; } = new();
#endregion Attr
public CameraSdkServer(RedisClient cache)
{
_cache = cache;
}
#region Base Method
public bool Register(CameraInfo cameraInfo)
{
bool ret = CameraSdkDict.ContainsKey(cameraInfo.Ip);
if (ret) return false;
ICameraSDK cameraSDK = ((CameraManufactor)cameraInfo.Manufactor).CreateCameraSDK(cameraInfo);
if (cameraSDK == null) return false;
ret = cameraSDK.Init();
if (!ret) return false;
ret = CameraSdkDict.TryAdd(cameraInfo.Ip, cameraSDK);
return ret;
}
public bool Delete(string ip)
{
return CameraSdkDict.TryRemove(ip, out _);
}
public bool IsExists(string ip)
{
return CameraSdkDict.ContainsKey(ip);
}
#endregion Base Method
#region Main Method
public PtzInfo GetPtzInfoByIp(string ip)
{
bool ret = CameraSdkDict.TryGetValue(ip, out ICameraSDK cameraSDK);
if (!ret) return PtzInfo.Default;
return cameraSDK.GetPtzInfo();
}
public async Task<PtzInfo> GetPtzInfoByIpAsync(string ip)
{
return await Task.Run(() => GetPtzInfoByIp(ip));
}
public bool TryGetPtzInfoByIp(string ip, out PtzInfo ptzInfo)
{
bool ret = CameraSdkDict.TryGetValue(ip, out ICameraSDK cameraSDK);
if (!ret) { ptzInfo = PtzInfo.Default; return false; }
return cameraSDK.TryGetPtzInfo(out ptzInfo);
}
public async Task<PtzInfo> GetPtzInfoByIdAsync(string cameraId)
{
string ip = await _cache.HGetAsync(CacheInfo.CameraId2Ip, cameraId);
if (string.IsNullOrEmpty(ip)) return PtzInfo.Default;
return GetPtzInfoByIp(ip);
}
public bool TryGetPtzInfoById(string cameraId, out PtzInfo ptzInfo)
{
string ip = _cache.HGet(CacheInfo.CameraId2Ip, cameraId);
if (string.IsNullOrEmpty(ip)) { ptzInfo = PtzInfo.Default; return false; }
return TryGetPtzInfoByIp(ip, out ptzInfo);
}
#endregion Main Method
}