修改微服务中的配置

dev_lx
刘鑫 1 week ago
parent 5071e8c310
commit d0b8aa44a7
  1. 1
      WeiCloud.Fusion/VideoService/Video.API/Program.cs
  2. 14
      WeiCloud.Fusion/VideoService/Video.API/appsettings.json
  3. 95
      WeiCloud.Fusion/VideoService/Video.DomainService/Dahvision/DahuaGeneralCtlService.cs
  4. 28
      WeiCloud.Fusion/VideoService/Video.DomainService/Dahvision/UrlHostReplacer.cs

@ -1,6 +1,5 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Common.Shared.Application.DaHua;
using Common.Shared.DomainService; using Common.Shared.DomainService;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using NLog; using NLog;

@ -28,18 +28,18 @@
"Username": "system", "Username": "system",
"Password": "Admin123", "Password": "Admin123",
"Real": { "Real": {
"DefaultRoot": "192.168.21.18:9100", "DefaultRoot": "192.168.21.18:9102",
"DefaultReplace": "demo.weienergy.cn:15210", "DefaultReplace": "demo.weienergy.cn:15235",
"Overrides": { "Overrides": {
"192.168.21.18:9100": "demo.weienergy.cn:15210", "192.168.21.18:9100": "demo.weienergy.cn:15235",
"10.20.30.40:9100": "demo.weienergy.cn:15210" "10.20.30.40:9100": "demo.weienergy.cn:15235"
} }
}, },
"Time": { "Time": {
"DefaultRoot": "192.168.21.18:9320", "DefaultRoot": "192.168.21.18:9322",
"DefaultReplace": "demo.weienergy.cn:15211", "DefaultReplace": "demo.weienergy.cn:15232",
"Overrides": { "Overrides": {
"192.168.21.18:9320": "demo.weienergy.cn:15211" "192.168.21.18:9320": "demo.weienergy.cn:15232"
} }
} }
} }

@ -72,8 +72,10 @@ namespace Video.DomainService
req.Headers.TryAddWithoutValidation("Authorization", token); req.Headers.TryAddWithoutValidation("Authorization", token);
using var resp = await _http.SendAsync(req); using var resp = await _http.SendAsync(req);
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("录像请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("录像请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
@ -134,7 +136,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发送 req,才能带上头 using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发送 req,才能带上头
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
@ -192,7 +198,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发送 req using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发送 req
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
@ -257,8 +267,11 @@ namespace Video.DomainService
req.Headers.TryAddWithoutValidation("Authorization", token); req.Headers.TryAddWithoutValidation("Authorization", token);
using var resp = await _http.SendAsync(req); using var resp = await _http.SendAsync(req);
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
@ -298,41 +311,57 @@ namespace Video.DomainService
{ {
var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/logout"; var url = $"https://{_configuration["DahuaAuth:Host"]}/evo-apigw/evo-oauth/1.0.0/oauth/logout";
try
{
using var req = new HttpRequestMessage(HttpMethod.Get, url); using var req = new HttpRequestMessage(HttpMethod.Get, url);
req.Headers.TryAddWithoutValidation("Authorization", authorization); req.Headers.TryAddWithoutValidation("Authorization", authorization);
if (!string.IsNullOrWhiteSpace(openId)) if (!string.IsNullOrWhiteSpace(openId))
{
req.Headers.Add("openId", openId); req.Headers.Add("openId", openId);
}
if (userClient != null) if (userClient != null)
{
req.Headers.Add("User-Client", userClient.ToString()); req.Headers.Add("User-Client", userClient.ToString());
}
using var resp = await _http.SendAsync(req); try
resp.EnsureSuccessStatusCode(); {
using var resp = await _http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
var body = await resp.Content.ReadAsStringAsync();
var result = await resp.Content.ReadFromJsonAsync<DaHApiResult<object>>(); // 不抛异常,让业务逻辑决定是否失败
if (!resp.IsSuccessStatusCode)
{
_logger.LogWarning("注销登录 HTTP失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
return new DaHApiResult<object>
{
Success = false,
Code = ((int)resp.StatusCode).ToString(),
Msg = $"HTTP错误 {(int)resp.StatusCode}",
};
}
// 尝试反序列化
var result = JsonSerializer.Deserialize<DaHApiResult<object>>(body);
if (result == null)
{
_logger.LogWarning("注销登录反序列化失败: {Body}", body);
return new DaHApiResult<object> { Success = false, Code = "1011", Msg = "注销登录返回无效" };
}
if (result != null && (result.Success || result.Code != "0")) // 只有 Success==false 或 Code != "0" 才视为失败
if (!result.Success || result.Code != "0")
{ {
_logger.LogWarning("注销登录失败: {Body}", body);
result.Success = false; result.Success = false;
result.Code = "1011"; result.Code = result.Code ?? "1011";
result.Msg = "注销登录失败"; result.Msg ??= "注销登录失败";
_logger.LogWarning("注销登录失败,返回结果:{Result}", result);
} }
return result; return result;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "大华注销登录出错"); _logger.LogError(ex, "大华注销登录异常");
return new DaHApiResult<object> return new DaHApiResult<object>
{ {
Success = false, Success = false,
Code = "1011", Code = "1011",
Msg = "注销登录失败" Msg = $"注销登录异常:{ex.Message}"
}; };
} }
} }
@ -387,7 +416,11 @@ namespace Video.DomainService
reqs.Headers.TryAddWithoutValidation("Authorization", token); reqs.Headers.TryAddWithoutValidation("Authorization", token);
using var resp = await _http.SendAsync(reqs); using var resp = await _http.SendAsync(reqs);
var bodys = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var bodys = await reader.ReadToEndAsync();
var resultRecord = JsonSerializer.Deserialize<DaHApiResult<RecordDataResDto>>(bodys); var resultRecord = JsonSerializer.Deserialize<DaHApiResult<RecordDataResDto>>(bodys);
if (resultRecord == null || !resultRecord.Success) if (resultRecord == null || !resultRecord.Success)
{ {
@ -482,8 +515,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发 req using var resp = await _http.SendAsync(req); // 关键:用 SendAsync 发 req
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("实时流请求 HTTP 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
@ -567,8 +603,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); using var resp = await _http.SendAsync(req);
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("抓拍 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("抓拍 失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
@ -630,8 +669,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); using var resp = await _http.SendAsync(req);
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("控制云台镜头失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("控制云台镜头失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);
@ -690,8 +732,11 @@ namespace Video.DomainService
try try
{ {
using var resp = await _http.SendAsync(req); using var resp = await _http.SendAsync(req);
var body = await resp.Content.ReadAsStringAsync(); resp.EnsureSuccessStatusCode();
await using var stream = await resp.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
var body = await reader.ReadToEndAsync();
if (!resp.IsSuccessStatusCode) if (!resp.IsSuccessStatusCode)
{ {
_logger.LogWarning("云台方向控制失败: {Status}, Body: {Body}", (int)resp.StatusCode, body); _logger.LogWarning("云台方向控制失败: {Status}, Body: {Body}", (int)resp.StatusCode, body);

@ -7,20 +7,28 @@ namespace Video.DomainService.Dahvision
public static string ReplaceHost(string url, string? ipAddress, IConfiguration config, string sectionName) public static string ReplaceHost(string url, string? ipAddress, IConfiguration config, string sectionName)
{ {
var section = config.GetSection($"DahuaAuth:{sectionName}"); var section = config.GetSection($"DahuaAuth:{sectionName}");
var defaultRoot = section["DefaultRoot"]!;
var defaultReplace = section["DefaultReplace"]!; var defaultReplace = section["DefaultReplace"]!;
var overrides = section.GetSection("Overrides").Get<Dictionary<string, string>>() ?? new();
// 1) 确定 fromHost // 自动提取 host:port
var fromHost = string.IsNullOrWhiteSpace(ipAddress) ? defaultRoot : ipAddress; string hostAndPort;
if (url.Contains("://"))
{
var uri = new Uri(url);
hostAndPort = $"{uri.Host}:{uri.Port}";
}
else
{
// url 是纯 IP:Port,比如 "192.168.21.18:9100"
hostAndPort = url;
}
// 2) 尝试在 Overrides 找对应的 value // 优先使用 Overrides 中的映射
var overrides = section.GetSection("Overrides").Get<Dictionary<string, string>>() ?? new(); if (!overrides.TryGetValue(hostAndPort, out var toHost))
var toHost = overrides.TryGetValue(fromHost, out var mapped) toHost = defaultReplace;
? mapped
: defaultReplace;
// 3) 字符串替换 // 替换
return url.Replace(fromHost, toHost, StringComparison.OrdinalIgnoreCase); return url.Replace(hostAndPort, toHost, StringComparison.OrdinalIgnoreCase);
} }
} }
} }
Loading…
Cancel
Save