Dispenser/DispenserUI/ViewModels/Components/CameraParamsVM.cs

114 lines
3.8 KiB
C#

using System;
using DispenserCommon.Aop;
using DispenserCommon.Ioc;
using DispenserHal.Camera.DTO;
using DispenserHal.Camera.Factory;
using DispenserUI.ViewModels.DTO;
using Newtonsoft.Json;
using ReactiveUI;
namespace DispenserUI.ViewModels.Components;
[Component, GlobalTry]
public class CameraParamsVM : ViewModelBase
{
private CameraParams _params = new();
public CameraParams Params
{
get => _params;
set => this.RaiseAndSetIfChanged(ref _params, value);
}
/// <summary>
/// 重新刷新相机参数信息
/// </summary>
public void ReloadCameraParams()
{
var camera = CameraManager.GetCamera()!;
var properties = Params.GetType().GetProperties();
foreach (var property in properties)
{
var propertyType = property.PropertyType;
var propertyName = property.Name;
switch (propertyType.Name)
{
case nameof(StringValue):
property.SetValue(Params, camera.GetStringValue(propertyName));
break;
case nameof(IntValue):
property.SetValue(Params, camera.GetIntValue(propertyName));
break;
case nameof(Boolean):
property.SetValue(Params, camera.GetBoolValue(propertyName));
break;
case nameof(FloatValue):
property.SetValue(Params, camera.GetFloatValue(propertyName));
break;
case nameof(EnumValue):
property.SetValue(Params, camera.GetEnumValue(propertyName));
// 如果是枚举类型,需要特殊处理,去获取每个枚举项的值
if (property.GetValue(Params) is EnumValue enumValue)
{
for (uint i = 0; i < enumValue.SupportedNum; i++)
{
// 获取枚举项
var item = camera.GetEnumEntrySymbolic(propertyName, enumValue.SupportValue[i]);
enumValue.Items.Add(item);
enumValue.Labels.Add(item.Symbolic);
}
for (uint i = 0; i < enumValue.SupportedNum; i++)
{
if (enumValue.CurValue != enumValue.SupportValue[i]) continue;
enumValue.CurIndex = (int)i;
break;
}
}
break;
}
}
Console.WriteLine(JsonConvert.SerializeObject(Params));
}
/// <summary>
/// 更新相机配置
/// </summary>
/// <param name="property"></param>
/// <param name="value"></param>
[Operation("更新相机配置参数")]
public void UpdateCameraParams(string property, object value)
{
var camera = CameraManager.GetCamera()!;
var properties = Params.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
if (propertyInfo.Name != property) continue;
switch (propertyInfo.PropertyType.Name)
{
case nameof(IntValue):
camera.SetIntValue(property, (long)value);
break;
case nameof(Boolean):
camera.SetBoolValue(property, (bool)value);
break;
case nameof(FloatValue):
var @decimal = (float)Convert.ToDecimal(value);
camera.SetFloatValue(property, @decimal);
break;
case nameof(EnumValue):
var val = Convert.ToUInt32(value);
camera.SetEnumValue(property, val);
break;
}
}
}
}