EquipmentListViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Avalonia.Media;
  7. using Avalonia.Threading;
  8. using AvaloniaDialogs.Views;
  9. using Comal.Classes;
  10. using CommunityToolkit.Mvvm.ComponentModel;
  11. using CommunityToolkit.Mvvm.Input;
  12. using InABox.Avalonia;
  13. using InABox.Avalonia.Converters;
  14. using InABox.Avalonia.Platform;
  15. using InABox.Core;
  16. using PRS.Avalonia.Components;
  17. namespace PRS.Avalonia.Modules;
  18. public class EquipmentInRangeVisibilityConverter : AbstractConverter<EquipmentShell, bool>
  19. {
  20. public static IBluetoothDevice[] AvailableDevices = [];
  21. protected override bool Convert(EquipmentShell? value, object? parameter = null)
  22. {
  23. if (value != null)
  24. {
  25. var device = AvailableDevices.FirstOrDefault(x => x.AvailableServices.Contains(value.ID));
  26. return (device != null);
  27. }
  28. return false;
  29. }
  30. }
  31. public partial class EquipmentListViewModel : ModuleViewModel
  32. {
  33. public override string Title => "Equipment List";
  34. [ObservableProperty]
  35. private EquipmentModel _equipment;
  36. [ObservableProperty]
  37. private IBluetoothDevice[] _devices = [];
  38. public EquipmentListViewModel()
  39. {
  40. Equipment = new EquipmentModel(
  41. DataAccess,
  42. () => new Filter<Equipment>().All(),
  43. () => Repositories.DefaultCacheFileName<EquipmentShell>()
  44. );
  45. }
  46. protected override async Task OnActivated()
  47. {
  48. PlatformTools.Bluetooth.Changed += BTChanged;
  49. await PlatformTools.Bluetooth.StartScanningAsync(PlatformTools.DigitalKeyServiceId);
  50. await base.OnActivated();
  51. }
  52. protected override async Task OnDeactivated()
  53. {
  54. PlatformTools.Bluetooth.Changed -= BTChanged;
  55. await PlatformTools.Bluetooth.StopScanningAsync();
  56. await base.OnDeactivated();
  57. }
  58. private void BTChanged(object sender, EventArgs args)
  59. {
  60. UpdateDevices(PlatformTools.Bluetooth.Devices);
  61. }
  62. private bool bActive = false;
  63. private void UpdateDevices(IEnumerable<IBluetoothDevice> devices)
  64. {
  65. if (bActive)
  66. return;
  67. Devices = devices.ToArray();
  68. EquipmentInRangeVisibilityConverter.AvailableDevices = Devices;
  69. Dispatcher.UIThread.Invoke(() => Equipment.Refresh(false));
  70. }
  71. protected override async Task<TimeSpan> OnRefresh()
  72. {
  73. await Equipment.RefreshAsync(false);
  74. StartScanning();
  75. return TimeSpan.Zero;
  76. }
  77. private void StartScanning()
  78. {
  79. }
  80. [RelayCommand]
  81. private void SelectEquipment(EquipmentShell shell)
  82. {
  83. Navigation.Navigate<EquipmentDetailsViewModel>((model) =>
  84. {
  85. model.Shell = shell;
  86. });
  87. }
  88. IConnectedBluetoothDevice? _connectedDevice = null;
  89. [RelayCommand]
  90. private async Task StartEquipment(EquipmentShell shell)
  91. {
  92. var device = Devices.FirstOrDefault(x => x.AvailableServices.Contains(shell.ID));
  93. if (device == null)
  94. return;
  95. bActive = true;
  96. _connectedDevice = await PlatformTools.Bluetooth.Connect(device);
  97. if (_connectedDevice != null)
  98. {
  99. var cmd = new Dictionary<string, long>() { { "Relay00", 15000 }, { "Relay01", 0 } , { "Relay02", 0 } , { "Relay03", 0 } , { "Relay4", 0 }, { "Relay05", 0 } };
  100. var json = Serialization.Serialize(cmd);
  101. await _connectedDevice.WriteStringAsync(shell.ID, PlatformTools.DigitalKeyControlId, json);
  102. }
  103. }
  104. [RelayCommand]
  105. private async Task EquipmentStopped(EquipmentShell shell)
  106. {
  107. if (_connectedDevice != null)
  108. await PlatformTools.Bluetooth.Disconnect(_connectedDevice);
  109. bActive = false;
  110. shell.Tag = null;
  111. }
  112. }