diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/AddPlug.razor b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/AddPlug.razor index 37fee70..d78020d 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/AddPlug.razor +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/AddPlug.razor @@ -6,12 +6,12 @@ @using Elektrifikatsiya.Services.Implementations @using Elektrifikatsiya.Utilities @using FluentResults; +@using Blazorise.LoadingIndicator; @inject IVersionProvider VersionProvider @inject IDeviceManagmentService DeviceManagmentService @inject IAuthenticationService AuthenticationService @inject IMessageService MessageService -@inject IAuthenticationService AuthenticationService @@ -27,8 +27,8 @@ Add a plug via wifi - - + + @@ -97,36 +97,61 @@ @code { - List ipAddresses = new List(); - List ipAddresses = new List(); - bool visible = false; + Validation? ipValidation; - DateTime time = DateTime.UtcNow; + bool visible = true; + + string ipText = null!; protected override void OnInitialized() { - ipAddresses.AddRange(MdnsDiscovery.GetChachedDevices()); - - System.Timers.Timer aTimer = new System.Timers.Timer(1000); - aTimer.Elapsed += (_, _) => { - DateTime dateTime = DateTime.UtcNow; - if(dateTime >= time.AddSeconds(3)) - { - visible = false; - InvokeAsync(StateHasChanged); - } - }; - aTimer.Start(); - - MdnsDiscovery.OnDeviceFound += address => + MdnsDiscovery.OnDeviceFound += async ipAddress => { - visible = true; - - ipAddresses.Add(address); - InvokeAsync(StateHasChanged); - - time = DateTime.UtcNow; + visible = false; + if (!ipAddresses.Contains(ipAddress)) + { + ipAddresses.Add(ipAddress); + await InvokeAsync(StateHasChanged); + } }; + + MdnsDiscovery.FetchChachedDevices(); } + + private async Task RegisterDevice(IPAddress ipAddress) + { + Result result = await DeviceManagmentService.RegisterDevice(ipAddress, (await AuthenticationService.GetUserAsync()).ValueOrDefault); + + if (result.IsSuccess) + { + await MessageService.Success("Device added!"); + } + else + { + await MessageService.Error(string.Join(',', result.Errors.Select(e => e.Message)), "Adding device failed!"); + } + } + + public void ValidateIPv4(ValidatorEventArgs e) + { + string? input = Convert.ToString(e.Value); + + if (string.IsNullOrWhiteSpace(input)) + { + e.Status = ValidationStatus.None; + return; + } + + string[] splitValues = input.Split('.'); + if (splitValues.Length != 4) + { + e.Status = ValidationStatus.Error; + return; + } + + e.Status = splitValues.All(r => byte.TryParse(r, out _)) ? ValidationStatus.Success : ValidationStatus.Error; + } + +} \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Dashboard.razor b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Dashboard.razor index a2b1a5e..5b11650 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Dashboard.razor +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Dashboard.razor @@ -2,8 +2,13 @@ @using Blazorise.Components; @using Elektrifikatsiya.Models; @using System.Net +@using Elektrifikatsiya.Services +@using Elektrifikatsiya.Utilities +@using System.Diagnostics @inject IVersionProvider VersionProvider +@inject IDeviceStatusService DeviceStatusService; +
@@ -34,7 +39,7 @@ - User: @context.Item.User + User: @context.Item.User.Name
Room: @context.Item.Room
@@ -44,7 +49,7 @@
- @@ -92,12 +97,22 @@ @code { //TODO: insert new event here if plug produces one - List events = new List() { new Event("Placeholder", "Event", DateTime.Now), new Event("Placeholder", "Event", DateTime.Now), new Event("Placeholder", "Event", DateTime.Now) }; + List events = new List() { new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now) }; //TODO: insert new Device here if user adds one - List plugs = new List() { new Device("ffff:ffff:ffff:ffff", "Josne", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Josne", IPAddress.Broadcast, new User("Joe", "Josne", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Josne", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), }; + List plugs = new List(); //code for graph LineChart lineChart; + protected override void OnInitialized() + { + plugs = DeviceStatusService.GetDevices().ValueOrDefault ?? new List(); + + DeviceStatusService.OnDeviceStatusChanged += (_, e) => + { + InvokeAsync(StateHasChanged); + }; + } + protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) @@ -108,50 +123,47 @@ async Task HandleRedraw() { + labels.Clear(); await lineChart.Clear(); - - await lineChart.AddLabelsDatasetsAndUpdate(Labels, GetLineChartDataset()); + await lineChart.AddLabelsDatasetsAndUpdate(labels, await GetLineChartDataset()); } //TODO: insert dataset of current and last voltage usages - LineChartDataset GetLineChartDataset() + async Task> GetLineChartDataset() { return new LineChartDataset { - Label = "# of random number in thousands", - Data = RandomizeData(), - BackgroundColor = backgroundColors, - BorderColor = borderColors, + Label = "Wattage", + Data = await RandomizeData(), Fill = true, PointRadius = 3, CubicInterpolationMode = "monotone", }; } - string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" }; - List backgroundColors = new List { ChartColor.FromRgba(255, 99, 132, 0.2f), ChartColor.FromRgba(54, 162, 235, 0.2f), ChartColor.FromRgba(255, 206, 86, 0.2f), ChartColor.FromRgba(75, 192, 192, 0.2f), ChartColor.FromRgba(153, 102, 255, 0.2f), ChartColor.FromRgba(255, 159, 64, 0.2f) }; - List borderColors = new List { ChartColor.FromRgba(255, 99, 132, 1f), ChartColor.FromRgba(54, 162, 235, 1f), ChartColor.FromRgba(255, 206, 86, 1f), ChartColor.FromRgba(75, 192, 192, 1f), ChartColor.FromRgba(153, 102, 255, 1f), ChartColor.FromRgba(255, 159, 64, 1f) }; + List labels = new List(); - List RandomizeData() + async Task> RandomizeData() { + PrometheusQuery promQueryer = new PrometheusQuery("http://localhost:9090"); + + string plugnames = ""; + foreach (Device device in plugs) + { + plugnames += $"shellyplug-s-{device.MacAddress}/relay/0|"; + } + plugnames = plugnames[0..^1]; + + Debug.WriteLine($$"""sum(power{sensor=~"{{plugnames}}"})[1h:1m]"""); + + PrometheusDataWrapper? deviceData = (await promQueryer.Query($$"""sum(power{sensor=~"{{plugnames}}"})[1h:1m]"""))?.Data; + var r = new Random(DateTime.Now.Millisecond); - return new List { - r.Next( 3, 50 ) * r.NextDouble(), - r.Next( 3, 50 ) * r.NextDouble(), - r.Next( 3, 50 ) * r.NextDouble(), - r.Next( 3, 50 ) * r.NextDouble(), - r.Next( 3, 50 ) * r.NextDouble(), - r.Next( 3, 50 ) * r.NextDouble() }; + return deviceData?.MatrixTypeToTimestampFloatTuple().ValueOrDefault?.Select(x => + { + labels.Add(DateTimeOffset.FromUnixTimeSeconds(long.Parse($"1{x.Item1}0")).UtcDateTime.ToLocalTime().ToShortTimeString()); + return x.Item2; + }).ToList() ?? new List(); } - - //private Background buttonColor = Background.Primary; - - private void ChangeButtonColor() - { - //device.on = !device.on; - } - - -} - +} \ No newline at end of file