DeviceInfoViewModel.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using CommunityToolkit.Mvvm.Input;
  2. using Shaker.Models;
  3. using ShakerApp.Models;
  4. using ShakerApp.Tools;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Input;
  12. namespace ShakerApp.ViewModels
  13. {
  14. public class DeviceInfoViewModel : ViewModelBase<DeviceInfoModel>
  15. {
  16. public DeviceInfoViewModel()
  17. {
  18. }
  19. public DeviceInfoViewModel(DeviceInfoModel model):base(model)
  20. {
  21. }
  22. public void SaveTdmsConfig(Dictionary<string, string> config)
  23. {
  24. if (config == null) config = new Dictionary<string, string>();
  25. typeof(DeviceInfoModel).GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  26. .Select(x => (x, x.GetValue(Model)))
  27. .ToList()
  28. .ForEach(x =>
  29. {
  30. if (x.Item2 == null)
  31. {
  32. return;
  33. }
  34. if (x.x.FieldType.IsAssignableTo(typeof(IList)) || x.x.FieldType.IsArray)
  35. {
  36. config[$"{nameof(DeviceInfoModel)}_{x.x.Name}"] = string.Join("", x.Item2.GetBytes().Select(x => $"{x:X2}"));
  37. }
  38. else
  39. {
  40. config[$"{nameof(DeviceInfoModel)}_{x.x.Name}"] = x.Item2?.ToString() ?? string.Empty;
  41. }
  42. });
  43. }
  44. [PropertyAssociation(nameof(DeviceInfoModel.Name))]
  45. public string Name { get => Model.Name; set => SetProperty(ref Model.Name, value); }
  46. [PropertyAssociation(nameof(DeviceInfoModel.SN))]
  47. public string SN { get => Model.SN; set => SetProperty(ref Model.SN, value); }
  48. [PropertyAssociation(nameof(DeviceInfoModel.IP))]
  49. public string IP { get => Model.IP; set => SetProperty(ref Model.IP, value); }
  50. [PropertyAssociation(nameof(DeviceInfoModel.Port))]
  51. public int Port { get => Model.Port; set => SetProperty(ref Model.Port, value); }
  52. }
  53. }