laatzen/Common/Ui/Common.UI/ViewModels/HomeVM.cs
2025-03-26 12:53:29 +01:00

86 lines
2.5 KiB
C#

namespace Common.UI.ViewModels
{
using Common.Hardware.SIRT;
using Common.UI.Infrastructure;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Timers;
using System.Windows.Input;
internal class HomeVM : VM<HomeVM>
{
private readonly HashSet<uint> addresses433;
private readonly HashSet<uint> addresses868;
private readonly SIRTHub sirtHub;
private readonly Timer timer;
public HomeVM()
{
this.addresses433 = new HashSet<uint>();
this.addresses868 = new HashSet<uint>();
this.timer = new Timer(3000);
this.timer.Elapsed += this.Timer_Elapsed;
this.sirtHub = new SIRTHub();
this.sirtHub.MessageProcessed += this.SirtHub_MessageProcessed;
this.StartScanning = new Command(this.StartScanning_Command);
this.Addresses433 = new ObservableCollection<uint>();
this.Addresses868 = new ObservableCollection<uint>();
}
public ICommand StartScanning { get; }
public ObservableCollection<uint> Addresses433 { get; }
public ObservableCollection<uint> Addresses868 { get; }
private void StartScanning_Command()
{
this.InvokeTaskAsync(() =>
{
this.timer.Start();
//this.sirtHub.Open("COM6");
//this.sirtHub.Open("COM8");
});
}
private void SirtHub_MessageProcessed(SIRTMessage message)
{
var address = message.Address;
if (address > 0)
{
if (message.Frequency == 433)
{
this.addresses433.Add(address);
}
else
{
this.addresses868.Add(address);
}
}
}
private void Timer_Elapsed(object sender, ElapsedEventArgs args)
{
this.InvokeAsync(() =>
{
this.Addresses433.Clear();
foreach (var address in this.addresses433.OrderBy(x => x))
{
this.Addresses433.Add(address);
}
this.Addresses868.Clear();
foreach (var address in this.addresses868.OrderBy(x => x))
{
this.Addresses868.Add(address);
}
});
}
}
}