DigitalKeysViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using InABox.Avalonia.Platform;
  4. using InABox.Core;
  5. namespace PRS.DigitalKey;
  6. public partial class DigitalKeyDisplay : ObservableObject
  7. {
  8. [ObservableProperty] private IBluetoothDevice _device;
  9. [ObservableProperty] private Guid _id;
  10. [ObservableProperty] private string _code;
  11. [ObservableProperty] private string _description;
  12. [ObservableProperty] private byte[] _image;
  13. [ObservableProperty] private bool _active;
  14. [ObservableProperty] private bool _controlServiceID;
  15. [ObservableProperty] private DateTime _unlockedUntil;
  16. }
  17. public partial class DigitalKeysViewModel : ViewModelBase
  18. {
  19. [ObservableProperty]
  20. private CoreObservableCollection<DigitalKeyDisplay> _keys = new();
  21. private Dictionary<Guid, DateTime> _unlockedKeys = new();
  22. public DigitalKeysViewModel()
  23. {
  24. BackButtonVisible = false;
  25. }
  26. protected override async Task OnActivated()
  27. {
  28. PlatformTools.Bluetooth.Changed += BTChanged;
  29. if (await PlatformTools.Bluetooth.IsAvailable())
  30. await PlatformTools.Bluetooth.StartScanningAsync(PlatformTools.DigitalKeyServiceId);
  31. await base.OnActivated();
  32. }
  33. protected override async Task OnDeactivated()
  34. {
  35. PlatformTools.Bluetooth.Changed -= BTChanged;
  36. await PlatformTools.Bluetooth.StopScanningAsync();
  37. await base.OnDeactivated();
  38. }
  39. private void BTChanged(object sender, EventArgs args)
  40. {
  41. UpdateKeys();
  42. }
  43. private void UpdateKeys()
  44. {
  45. if (bActive)
  46. return;
  47. var keys = new List<DigitalKeyDisplay>();
  48. {
  49. foreach (var device in PlatformTools.Bluetooth.Devices.ToArray())
  50. {
  51. if (device.AvailableServices.Any())
  52. {
  53. var id = device.AvailableServices.First();
  54. _unlockedKeys.TryGetValue(id, out DateTime _unlocktime);
  55. keys.Add(
  56. new DigitalKeyDisplay()
  57. {
  58. Device = device,
  59. Id = id,
  60. Code = device.Name,
  61. UnlockedUntil = _unlocktime
  62. }
  63. );
  64. }
  65. }
  66. }
  67. Keys.ReplaceRange(keys.OrderBy(x=>x.Code));
  68. }
  69. private bool bActive;
  70. private IConnectedBluetoothDevice? _connectedBluetoothDevice = null;
  71. [RelayCommand]
  72. private void Start(DigitalKeyDisplay shell)
  73. {
  74. if (shell.Id == Guid.Empty)
  75. return;
  76. _unlockedKeys[shell.Id] = DateTime.Now.AddMinutes(5);
  77. bActive = true;
  78. _ = Task.Run(async () =>
  79. {
  80. shell.Active = true;
  81. _connectedBluetoothDevice = await PlatformTools.Bluetooth.Connect(shell.Device);
  82. if (_connectedBluetoothDevice != null)
  83. {
  84. var cmd = new Dictionary<string, long>() { { "Relay00", 15000 }, { "Relay01", 0 } , { "Relay02", 0 } , { "Relay03", 0 } , { "Relay4", 0 }, { "Relay05", 0 } };
  85. var json = Serialization.Serialize(cmd);
  86. await _connectedBluetoothDevice.WriteStringAsync(shell.Id, PlatformTools.DigitalKeyControlId, json);
  87. }
  88. ;
  89. });
  90. }
  91. [RelayCommand]
  92. private async Task Stopped(DigitalKeyDisplay shell)
  93. {
  94. shell.Active = false;
  95. if (_connectedBluetoothDevice != null)
  96. await PlatformTools.Bluetooth.Disconnect(_connectedBluetoothDevice);
  97. bActive = false;
  98. }
  99. }