| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | using CommunityToolkit.Mvvm.ComponentModel;using CommunityToolkit.Mvvm.Input;using InABox.Avalonia.Platform;using InABox.Core;namespace PRS.DigitalKey;public partial class DigitalKeyDisplay : ObservableObject{    [ObservableProperty] private IBluetoothDevice _device;    [ObservableProperty] private Guid _id;    [ObservableProperty] private string _code;    [ObservableProperty] private string _description;    [ObservableProperty] private byte[] _image;    [ObservableProperty] private bool _active;    [ObservableProperty] private bool _controlServiceID;    [ObservableProperty] private DateTime _unlockedUntil;}public partial class DigitalKeysViewModel : ViewModelBase{        [ObservableProperty]     private CoreObservableCollection<DigitalKeyDisplay> _keys = new();    private Dictionary<Guid, DateTime> _unlockedKeys = new();    public DigitalKeysViewModel()    {        BackButtonVisible = false;    }        protected override async Task OnActivated()    {        PlatformTools.Bluetooth.Changed += BTChanged;        if (await PlatformTools.Bluetooth.IsAvailable())            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)    {        UpdateKeys();    }    private void UpdateKeys()    {        if (bActive)            return;                var keys = new List<DigitalKeyDisplay>();        {            foreach (var device in PlatformTools.Bluetooth.Devices.ToArray())            {                if (device.AvailableServices.Any())                {                    var id = device.AvailableServices.First();                    _unlockedKeys.TryGetValue(id, out DateTime _unlocktime);                                        keys.Add(                        new DigitalKeyDisplay()                        {                            Device = device,                            Id = id,                            Code = device.Name,                            UnlockedUntil = _unlocktime                        }                    );                }            }        }        Keys.ReplaceRange(keys.OrderBy(x=>x.Code));    }        private bool bActive;    private IConnectedBluetoothDevice? _connectedBluetoothDevice = null;        [RelayCommand]    private void Start(DigitalKeyDisplay shell)    {        if (shell.Id == Guid.Empty)            return;        _unlockedKeys[shell.Id] = DateTime.Now.AddMinutes(5);                bActive = true;        _ = Task.Run(async () =>        {            shell.Active = true;            _connectedBluetoothDevice = await PlatformTools.Bluetooth.Connect(shell.Device);            if (_connectedBluetoothDevice != 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 _connectedBluetoothDevice.WriteStringAsync(shell.Id, PlatformTools.DigitalKeyControlId, json);            }            ;        });    }        [RelayCommand]    private async Task Stopped(DigitalKeyDisplay shell)    {        shell.Active = false;        if (_connectedBluetoothDevice != null)            await PlatformTools.Bluetooth.Disconnect(_connectedBluetoothDevice);        bActive = false;    }}
 |