PhaseG2: OwlAdapter — RSA登录+4接口实现 编译通过
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
<Solution>
|
<Solution>
|
||||||
|
<Folder Name="/src/">
|
||||||
|
<Project Path="src/IntegrationGateway.Adapters.Owl/IntegrationGateway.Adapters.Owl.csproj" />
|
||||||
|
</Folder>
|
||||||
<Project Path="src/IntegrationGateway.Core/IntegrationGateway.Core.csproj" />
|
<Project Path="src/IntegrationGateway.Core/IntegrationGateway.Core.csproj" />
|
||||||
<Project Path="src/IntegrationGateway.Host/IntegrationGateway.Host.csproj" />
|
<Project Path="src/IntegrationGateway.Host/IntegrationGateway.Host.csproj" />
|
||||||
</Solution>
|
</Solution>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\IntegrationGateway.Core\IntegrationGateway.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
153
gateway/src/IntegrationGateway.Adapters.Owl/OwlAdapter.cs
Normal file
153
gateway/src/IntegrationGateway.Adapters.Owl/OwlAdapter.cs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
using IntegrationGateway.Core.Abstractions;
|
||||||
|
using IntegrationGateway.Core.Infrastructure;
|
||||||
|
using IntegrationGateway.Core.Models;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
|
||||||
|
namespace IntegrationGateway.Adapters.Owl;
|
||||||
|
|
||||||
|
public class OwlAdapter : IHasFlatDevices, IHasStreams, IHasRecordings, IAcceptsMetadataPush
|
||||||
|
{
|
||||||
|
private readonly HttpClient _http;
|
||||||
|
private readonly OwlAuthHelper _auth;
|
||||||
|
private readonly RateLimiter _limiter = new(5);
|
||||||
|
|
||||||
|
public string AdapterCode { get; }
|
||||||
|
public string DisplayName => $"Owl ({AdapterCode})";
|
||||||
|
public AdapterCapabilities Capabilities => new()
|
||||||
|
{
|
||||||
|
HasFlatDevices = true, HasStreams = true, HasPtz = true, HasRecordings = true, AcceptsMetadataPush = true
|
||||||
|
};
|
||||||
|
|
||||||
|
public OwlAdapter(string adapterCode, HttpClient http, string baseUrl, string username, string password)
|
||||||
|
{
|
||||||
|
AdapterCode = adapterCode;
|
||||||
|
_http = http;
|
||||||
|
_auth = new OwlAuthHelper(http, baseUrl, username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitializeAsync() => await _auth.GetTokenAsync();
|
||||||
|
|
||||||
|
public async Task<bool> HealthCheckAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var resp = await client.GetAsync("/health");
|
||||||
|
return resp.IsSuccessStatusCode;
|
||||||
|
}
|
||||||
|
catch { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── IHasFlatDevices ───
|
||||||
|
public async Task<PagedResult<StandardDevice>> GetDevicesAsync(int page, int size, string? keyword = null)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var url = $"/devices?page={page}&size={size}";
|
||||||
|
if (!string.IsNullOrEmpty(keyword)) url += $"&key={Uri.EscapeDataString(keyword)}";
|
||||||
|
var json = await client.GetStringAsync(url);
|
||||||
|
var owl = JsonSerializer.Deserialize<OwlPagedResult<OwlDevice>>(json)!;
|
||||||
|
return new PagedResult<StandardDevice>
|
||||||
|
{
|
||||||
|
Items = owl.Items.Select(MapDevice).ToList(),
|
||||||
|
Total = owl.Total
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── IHasStreams ───
|
||||||
|
public async Task<StreamUrls> GetLiveUrlAsync(string channelId)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var resp = await client.PostAsync($"/channels/{channelId}/play", null);
|
||||||
|
resp.EnsureSuccessStatusCode();
|
||||||
|
var json = await resp.Content.ReadAsStringAsync();
|
||||||
|
var play = JsonSerializer.Deserialize<OwlPlayResponse>(json)!;
|
||||||
|
return MapStreamUrls(play);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StreamUrls> GetPlaybackUrlAsync(string channelId, DateTime start, DateTime end)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var startMs = new DateTimeOffset(start).ToUnixTimeMilliseconds();
|
||||||
|
var endMs = new DateTimeOffset(end).ToUnixTimeMilliseconds();
|
||||||
|
var token = await _auth.GetTokenAsync();
|
||||||
|
return new StreamUrls { Hls = $"{client.BaseAddress}recordings/channels/{channelId}/index.m3u8?start_ms={startMs}&end_ms={endMs}&token={token}" };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PtzControlAsync(string channelId, string direction, float speed)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
await client.PostAsJsonAsync($"/channels/{channelId}/ptz/control", new { action = "continuous", direction, speed });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PtzStopAsync(string channelId)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
await client.PostAsJsonAsync($"/channels/{channelId}/ptz/control", new { action = "stop" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StreamUrls> GetSnapshotAsync(string channelId)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var resp = await client.PostAsync($"/channels/{channelId}/snapshot",
|
||||||
|
new StringContent("{}", System.Text.Encoding.UTF8, "application/json"));
|
||||||
|
var json = await resp.Content.ReadAsStringAsync();
|
||||||
|
var snap = JsonSerializer.Deserialize<OwlSnapshotResponse>(json)!;
|
||||||
|
return new StreamUrls { Hls = snap.Link };
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── IHasRecordings ───
|
||||||
|
public async Task<PagedResult<StandardRecording>> GetRecordingsAsync(string channelId, DateTime start, DateTime end, int page, int size)
|
||||||
|
{
|
||||||
|
await _limiter.WaitAsync();
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var startMs = new DateTimeOffset(start).ToUnixTimeMilliseconds();
|
||||||
|
var endMs = new DateTimeOffset(end).ToUnixTimeMilliseconds();
|
||||||
|
var json = await client.GetStringAsync($"/recordings?cid={channelId}&start_ms={startMs}&end_ms={endMs}&page={page}&size={size}");
|
||||||
|
var owl = JsonSerializer.Deserialize<OwlPagedResult<OwlRecording>>(json)!;
|
||||||
|
return new PagedResult<StandardRecording>
|
||||||
|
{
|
||||||
|
Items = owl.Items.Select(r => new StandardRecording { Id = r.Id, ChannelId = r.Cid, StartedAt = r.StartedAt, EndedAt = r.EndedAt, Duration = r.Duration, FilePath = r.Path, Size = r.Size }).ToList(),
|
||||||
|
Total = owl.Total
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── IAcceptsMetadataPush ───
|
||||||
|
public async Task<MetadataPushResult> PushMetadataAsync(string sourceDeviceId, MetadataChangeSet changes)
|
||||||
|
{
|
||||||
|
var client = await _auth.GetAuthenticatedClientAsync();
|
||||||
|
var body = new Dictionary<string, object>();
|
||||||
|
if (changes.Name != null) body["name"] = changes.Name;
|
||||||
|
await client.PutAsJsonAsync($"/devices/{sourceDeviceId}", body);
|
||||||
|
return new MetadataPushResult { Success = true };
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Mapping ───
|
||||||
|
private static StandardDevice MapDevice(OwlDevice d) => new()
|
||||||
|
{
|
||||||
|
SourceId = d.Id ?? "", Name = d.Name ?? d.Id ?? "", Category = "硬盘录像机", Group = "视频设备",
|
||||||
|
IsOnline = d.IsOnline == "1", IsParent = true, IpAddress = d.Address,
|
||||||
|
Port = int.TryParse(d.Port, out var port) ? port : null,
|
||||||
|
Extra = new Dictionary<string, object?> { ["owlDeviceId"] = d.Id, ["protocol"] = d.Protocol ?? "GB28181", ["transport"] = d.Transport }
|
||||||
|
};
|
||||||
|
|
||||||
|
private static StreamUrls MapStreamUrls(OwlPlayResponse play)
|
||||||
|
{
|
||||||
|
var item = play.Items?.FirstOrDefault();
|
||||||
|
return new StreamUrls { WsFlv = item?.WsFlv, HttpFlv = item?.HttpFlv, Hls = item?.Hls, WebRtc = item?.WebRtc, Rtmp = item?.Rtmp, Rtsp = item?.Rtsp };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Owl JSON Models ───
|
||||||
|
public class OwlPagedResult<T> { public List<T> Items { get; set; } = new(); public int Total { get; set; } }
|
||||||
|
public class OwlDevice { public string? Id { get; set; } public string? Name { get; set; } public string? IsOnline { get; set; } public string? Protocol { get; set; } public string? Address { get; set; } public string? Port { get; set; } public string? Transport { get; set; } }
|
||||||
|
public class OwlPlayResponse { public List<OwlPlayItem>? Items { get; set; } }
|
||||||
|
public class OwlPlayItem { public string? WsFlv { get; set; } public string? HttpFlv { get; set; } public string? Hls { get; set; } public string? WebRtc { get; set; } public string? Rtmp { get; set; } public string? Rtsp { get; set; } }
|
||||||
|
public class OwlSnapshotResponse { public string? Link { get; set; } }
|
||||||
|
public class OwlRecording { public int Id { get; set; } public string? Cid { get; set; } public DateTime StartedAt { get; set; } public DateTime EndedAt { get; set; } public double Duration { get; set; } public string? Path { get; set; } public long Size { get; set; } }
|
||||||
58
gateway/src/IntegrationGateway.Adapters.Owl/OwlAuthHelper.cs
Normal file
58
gateway/src/IntegrationGateway.Adapters.Owl/OwlAuthHelper.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
|
||||||
|
namespace IntegrationGateway.Adapters.Owl;
|
||||||
|
|
||||||
|
public class OwlAuthHelper
|
||||||
|
{
|
||||||
|
private readonly HttpClient _http;
|
||||||
|
private readonly string _baseUrl;
|
||||||
|
private readonly string _username;
|
||||||
|
private readonly string _password;
|
||||||
|
private string? _token;
|
||||||
|
private DateTime _tokenExpiry = DateTime.MinValue;
|
||||||
|
|
||||||
|
public OwlAuthHelper(HttpClient http, string baseUrl, string username, string password)
|
||||||
|
{
|
||||||
|
_http = http; _baseUrl = baseUrl.TrimEnd('/');
|
||||||
|
_username = username; _password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetTokenAsync()
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(_token) && DateTime.UtcNow < _tokenExpiry) return _token;
|
||||||
|
|
||||||
|
var keyResp = await _http.GetStringAsync($"{_baseUrl}/login/key");
|
||||||
|
var keyData = JsonSerializer.Deserialize<LoginKeyResponse>(keyResp);
|
||||||
|
var publicKey = Encoding.UTF8.GetString(Convert.FromBase64String(keyData!.Key!));
|
||||||
|
|
||||||
|
using var rsa = RSA.Create();
|
||||||
|
rsa.ImportFromPem(publicKey);
|
||||||
|
var plain = JsonSerializer.Serialize(new { username = _username, password = _password });
|
||||||
|
var encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(plain), RSAEncryptionPadding.Pkcs1);
|
||||||
|
var payload = JsonSerializer.Serialize(new { data = Convert.ToBase64String(encrypted) });
|
||||||
|
|
||||||
|
var resp = await _http.PostAsync($"{_baseUrl}/login",
|
||||||
|
new StringContent(payload, Encoding.UTF8, "application/json"));
|
||||||
|
resp.EnsureSuccessStatusCode();
|
||||||
|
var loginResult = await resp.Content.ReadFromJsonAsync<LoginResponse>();
|
||||||
|
_token = loginResult!.Token;
|
||||||
|
_tokenExpiry = DateTime.UtcNow.AddDays(2.5);
|
||||||
|
return _token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Invalidate() => _token = null;
|
||||||
|
|
||||||
|
public async Task<HttpClient> GetAuthenticatedClientAsync()
|
||||||
|
{
|
||||||
|
var token = await GetTokenAsync();
|
||||||
|
var client = new HttpClient { BaseAddress = new Uri(_baseUrl) };
|
||||||
|
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoginKeyResponse { public string? Key { get; set; } }
|
||||||
|
public class LoginResponse { public string Token { get; set; } = ""; public string? User { get; set; } }
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\IntegrationGateway.Core\IntegrationGateway.Core.csproj" />
|
<ProjectReference Include="..\IntegrationGateway.Core\IntegrationGateway.Core.csproj" />
|
||||||
|
<ProjectReference Include="..\IntegrationGateway.Adapters.Owl\IntegrationGateway.Adapters.Owl.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user