123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Avalonia.Media;
- using Avalonia.Threading;
- using AvaloniaDialogs.Views;
- using Comal.Classes;
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using InABox.Avalonia;
- using InABox.Avalonia.Converters;
- using InABox.Avalonia.Platform;
- using InABox.Core;
- using PRS.Avalonia.Components;
- namespace PRS.Avalonia.Modules;
- public class EquipmentInRangeVisibilityConverter : AbstractConverter<EquipmentShell, bool>
- {
- public static IBluetoothDevice[] AvailableDevices = [];
-
- protected override bool Convert(EquipmentShell? value, object? parameter = null)
- {
- if (value != null)
- {
- var device = AvailableDevices.FirstOrDefault(x => x.AvailableServices.Contains(value.ID));
- return (device != null);
- }
- return false;
- }
- }
- public partial class EquipmentListViewModel : ModuleViewModel
- {
- public override string Title => "Equipment List";
-
- [ObservableProperty]
- private EquipmentModel _equipment;
- [ObservableProperty]
- private IBluetoothDevice[] _devices = [];
- public EquipmentListViewModel()
- {
- Equipment = new EquipmentModel(
- DataAccess,
- () => new Filter<Equipment>().All(),
- () => Repositories.DefaultCacheFileName<EquipmentShell>()
- );
-
- }
- protected override async Task OnActivated()
- {
- PlatformTools.Bluetooth.Changed += BTChanged;
- await PlatformTools.Bluetooth.StartScanningAsync(PlatformTools.DigitalKeyServiceId);
- await base.OnActivated();
- }
- protected override async Task OnDeactivated()
- {
- PlatformTools.Bluetooth.Changed -= BTChanged;
- await PlatformTools.Bluetooth.StopScanningAsync();
- await base.OnDeactivated();
- }
- private void BTChanged(object sender, EventArgs args)
- {
- UpdateDevices(PlatformTools.Bluetooth.Devices);
- }
- private bool bActive = false;
- private void UpdateDevices(IEnumerable<IBluetoothDevice> devices)
- {
- if (bActive)
- return;
- Devices = devices.ToArray();
- EquipmentInRangeVisibilityConverter.AvailableDevices = Devices;
- Dispatcher.UIThread.Invoke(() => Equipment.Refresh(false));
- }
- protected override async Task<TimeSpan> OnRefresh()
- {
- await Equipment.RefreshAsync(false);
- StartScanning();
- return TimeSpan.Zero;
- }
- private void StartScanning()
- {
-
- }
- [RelayCommand]
- private void SelectEquipment(EquipmentShell shell)
- {
- Navigation.Navigate<EquipmentDetailsViewModel>((model) =>
- {
- model.Shell = shell;
- });
- }
-
- IConnectedBluetoothDevice? _connectedDevice = null;
-
- [RelayCommand]
- private async Task StartEquipment(EquipmentShell shell)
- {
- var device = Devices.FirstOrDefault(x => x.AvailableServices.Contains(shell.ID));
- if (device == null)
- return;
-
- bActive = true;
- _connectedDevice = await PlatformTools.Bluetooth.Connect(device);
- if (_connectedDevice != null)
- {
- var cmd = new Dictionary<string, long>() { { "Relay00", 15000 }, { "Relay01", 0 } , { "Relay02", 0 } , { "Relay03", 0 } , { "Relay4", 0 }, { "Relay05", 0 } };
- var json = Serialization.Serialize(cmd);
- await _connectedDevice.WriteStringAsync(shell.ID, PlatformTools.DigitalKeyControlId, json);
- }
-
- }
-
- [RelayCommand]
- private async Task EquipmentStopped(EquipmentShell shell)
- {
- if (_connectedDevice != null)
- await PlatformTools.Bluetooth.Disconnect(_connectedDevice);
- bActive = false;
- shell.Tag = null;
- }
-
-
- }
|