Files
Ayay/SHH.CameraDashboard/Services/WebApis/CameraReps/CameraRepositoryImgProc.cs
2026-01-01 22:40:32 +08:00

126 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text.Json;
namespace SHH.CameraDashboard
{
public partial class CameraRepository
{
public async Task<bool> UpdateImageProcessingAsync(CameraEditInfo dto)
{
if (dto == null) return false;
var serviceNode = AppGlobal.UseServiceNode;
if (serviceNode == null) return false;
// 1. 使用专用的 Processing 路由
string requestUrl = $"http://{serviceNode.ServiceNodeIp}:{serviceNode.ServiceNodePort}{WebApiRoutes.Cameras.Processing(dto.Id.ToString())}";
// 2. 解析分辨率字符串 (例如 "1920x1080" -> 1920, 1080)
int width = 1280; // 默认值
int height = 720;
if (!string.IsNullOrWhiteSpace(dto.TargetResolution))
{
var parts = dto.TargetResolution.ToLower().Split('x');
if (parts.Length == 2)
{
int.TryParse(parts[0], out width);
int.TryParse(parts[1], out height);
}
}
try
{
// 2. 构建精简的专用 JSON Payload
// 只包含图像处理相关的字段,不发送其他干扰信息
var payload = new
{
// --- 分辨率 (拆分成 int) ---
targetWidth = width,
targetHeight = height,
// 确保这些字段名与后端 Processing 接口的定义一致
useGrayscale = dto.UseGrayscale,
enhanceImage = dto.EnhanceImage,
// 缩放控制
allowShrink = dto.AllowShrink,
allowEnlarge = dto.AllowEnlarge, // 这里使用你确认过的 AllowEnlarge
// 目标分辨率
targetResolution = dto.TargetResolution ?? ""
};
string jsonBody = JsonHelper.Serialize(payload);
// 3. 发送请求
// 通常专用功能接口使用 POST 或 PUT。根据你的路由命名(Processing 是名词/动词)
// 且是对资源的部分修改PUT 的可能性较大;但如果是“执行处理”,也可能是 POST。
// 建议先试 PUT (因为是 Update 操作),如果报 405 则改 POST。
await WebApiService.Instance.PostAsync(requestUrl, jsonBody, "图像处理配置");
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"更新图像处理配置失败: {ex.Message}");
return false;
}
}
/// <summary>
/// [新增] 获取指定设备的图像处理配置
/// 对应后端: GET /api/cameras/{id}/processing
/// </summary>
public async Task<CameraEditInfo> GetImageProcessingAsync(long id)
{
var serviceNode = AppGlobal.UseServiceNode;
if (serviceNode == null) return null;
// 1. 拼接 URL: http://.../api/Cameras/1001/processing
string requestUrl = $"http://{serviceNode.ServiceNodeIp}:{serviceNode.ServiceNodePort}{WebApiRoutes.Cameras.Root}/{id}/processing";
try
{
// 2. 发送 GET 请求
string jsonResponse = await WebApiService.Instance.GetAsync(requestUrl, "获取图像处理配置");
if (string.IsNullOrEmpty(jsonResponse)) return null;
// 3. 解析后端返回的 ProcessingOptions JSON
// 注意:这里定义一个临时类来匹配后端 JSON 结构,确保字段名一致
var options = JsonHelper.Deserialize<ProcessingOptionsDto>(jsonResponse);
if (options == null) return null;
// 4. 映射回 CameraEditInfo 给 ViewModel 使用
return new CameraEditInfo
{
Id = id,
// 将宽高拼回 "1920x1080" 格式
TargetResolution = $"{options.targetWidth}x{options.targetHeight}",
// 映射开关 (注意字段名对应关系)
AllowShrink = options.enableShrink,
AllowEnlarge = options.enableExpand, // 后端叫 EnableExpand
EnhanceImage = options.enableBrightness
};
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Repository] 获取图像配置失败: {ex.Message}");
return null;
}
}
// [内部类] 用于接收后端 JSON 的 DTO
// 字段名需与后端 ProcessingOptions 序列化出来的 JSON 字段一致 (通常是 camelCase)
private class ProcessingOptionsDto
{
public int targetWidth { get; set; }
public int targetHeight { get; set; }
public bool enableShrink { get; set; }
public bool enableExpand { get; set; } // 对应后端的 EnableExpand
public bool enableBrightness { get; set; } // 对应后端的 EnableBrightness
public int brightness { get; set; }
}
}
}