Fix merge bugs

This commit is contained in:
Stefan
2023-05-03 09:30:19 +02:00
parent 0bc8454f86
commit 358bbdacce
2 changed files with 97 additions and 60 deletions
@@ -6,12 +6,12 @@
@using Elektrifikatsiya.Services.Implementations @using Elektrifikatsiya.Services.Implementations
@using Elektrifikatsiya.Utilities @using Elektrifikatsiya.Utilities
@using FluentResults; @using FluentResults;
@using Blazorise.LoadingIndicator;
@inject IVersionProvider VersionProvider @inject IVersionProvider VersionProvider
@inject IDeviceManagmentService DeviceManagmentService @inject IDeviceManagmentService DeviceManagmentService
@inject IAuthenticationService AuthenticationService @inject IAuthenticationService AuthenticationService
@inject IMessageService MessageService @inject IMessageService MessageService
@inject IAuthenticationService AuthenticationService
<Row Style="height:50%; width:100%" Padding="Padding.Is3.OnDesktop.Is5.FromStart.OnMobile"> <Row Style="height:50%; width:100%" Padding="Padding.Is3.OnDesktop.Is5.FromStart.OnMobile">
<Row Style="height:90%; width:100%"> <Row Style="height:90%; width:100%">
@@ -27,8 +27,8 @@
Add a plug via wifi Add a plug via wifi
</Column> </Column>
<Column ColumnSize="ColumnSize.Is3.Is6.WithOffset"> <Column ColumnSize="ColumnSize.Is3.Is6.WithOffset">
<LoadingIndicator @bind-Visible="@visible"> <LoadingIndicator @bind-Visible="@visible"/>
</LoadingIndicator>
</Column> </Column>
</BarBrand> </BarBrand>
</Bar> </Bar>
@@ -99,34 +99,59 @@
@code { @code {
List<IPAddress> ipAddresses = new List<IPAddress>(); List<IPAddress> ipAddresses = new List<IPAddress>();
List<IPAddress> ipAddresses = new List<IPAddress>(); Validation? ipValidation;
bool visible = false; bool visible = true;
DateTime time = DateTime.UtcNow; string ipText = null!;
protected override void OnInitialized() protected override void OnInitialized()
{ {
ipAddresses.AddRange(MdnsDiscovery.GetChachedDevices()); MdnsDiscovery.OnDeviceFound += async ipAddress =>
System.Timers.Timer aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += (_, _) => {
DateTime dateTime = DateTime.UtcNow;
if(dateTime >= time.AddSeconds(3))
{ {
visible = false; visible = false;
InvokeAsync(StateHasChanged); if (!ipAddresses.Contains(ipAddress))
}
};
aTimer.Start();
MdnsDiscovery.OnDeviceFound += address =>
{ {
visible = true; ipAddresses.Add(ipAddress);
await InvokeAsync(StateHasChanged);
}
};
MdnsDiscovery.FetchChachedDevices();
}
private async Task RegisterDevice(IPAddress ipAddress)
{
Result<Device> 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!");
}
}
ipAddresses.Add(address); public void ValidateIPv4(ValidatorEventArgs e)
InvokeAsync(StateHasChanged); {
string? input = Convert.ToString(e.Value);
time = DateTime.UtcNow; 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;
}
} }
@@ -2,8 +2,13 @@
@using Blazorise.Components; @using Blazorise.Components;
@using Elektrifikatsiya.Models; @using Elektrifikatsiya.Models;
@using System.Net @using System.Net
@using Elektrifikatsiya.Services
@using Elektrifikatsiya.Utilities
@using System.Diagnostics
@inject IVersionProvider VersionProvider @inject IVersionProvider VersionProvider
@inject IDeviceStatusService DeviceStatusService;
<Div> <Div>
<Row Padding="Padding.Is3"> <Row Padding="Padding.Is3">
<Div Display="Display.Flex.Row.OnDesktop" Margin=" Margin.Is3.FromBottom" Width="Width.Is100"> <Div Display="Display.Flex.Row.OnDesktop" Margin=" Margin.Is3.FromBottom" Width="Width.Is100">
@@ -34,7 +39,7 @@
</Row> </Row>
<Row> <Row>
<Column> <Column>
<Small>User: @context.Item.User</Small> <Small>User: @context.Item.User.Name</Small>
<br /> <br />
<Small>Room: @context.Item.Room</Small> <Small>Room: @context.Item.Room</Small>
</Column> </Column>
@@ -44,7 +49,7 @@
</Row> </Row>
</Column> </Column>
<Column ColumnSize="ColumnSize.Is4"> <Column ColumnSize="ColumnSize.Is4">
<Button Clicked="@ChangeButtonColor" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Primary"Primary Outline> <Button Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Primary"Primary Outline>
<!--TODO: This button should change color--> <!--TODO: This button should change color-->
<BarIcon IconName="IconName.Bolt" /> <BarIcon IconName="IconName.Bolt" />
</Button> </Button>
@@ -92,12 +97,22 @@
@code { @code {
//TODO: insert new event here if plug produces one //TODO: insert new event here if plug produces one
List<Event> events = new List<Event>() { new Event("Placeholder", "Event", DateTime.Now), new Event("Placeholder", "Event", DateTime.Now), new Event("Placeholder", "Event", DateTime.Now) }; List<Event> events = new List<Event>() { 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 //TODO: insert new Device here if user adds one
List<Device> plugs = new List<Device>() { 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<Device> plugs = new List<Device>();
//code for graph //code for graph
LineChart<double> lineChart; LineChart<double> lineChart;
protected override void OnInitialized()
{
plugs = DeviceStatusService.GetDevices().ValueOrDefault ?? new List<Device>();
DeviceStatusService.OnDeviceStatusChanged += (_, e) =>
{
InvokeAsync(StateHasChanged);
};
}
protected override async Task OnAfterRenderAsync(bool firstRender) protected override async Task OnAfterRenderAsync(bool firstRender)
{ {
if (firstRender) if (firstRender)
@@ -108,50 +123,47 @@
async Task HandleRedraw() async Task HandleRedraw()
{ {
labels.Clear();
await lineChart.Clear(); await lineChart.Clear();
await lineChart.AddLabelsDatasetsAndUpdate(labels, await GetLineChartDataset());
await lineChart.AddLabelsDatasetsAndUpdate(Labels, GetLineChartDataset());
} }
//TODO: insert dataset of current and last voltage usages //TODO: insert dataset of current and last voltage usages
LineChartDataset<double> GetLineChartDataset() async Task<LineChartDataset<double>> GetLineChartDataset()
{ {
return new LineChartDataset<double> return new LineChartDataset<double>
{ {
Label = "# of random number in thousands", Label = "Wattage",
Data = RandomizeData(), Data = await RandomizeData(),
BackgroundColor = backgroundColors,
BorderColor = borderColors,
Fill = true, Fill = true,
PointRadius = 3, PointRadius = 3,
CubicInterpolationMode = "monotone", CubicInterpolationMode = "monotone",
}; };
} }
string[] Labels = { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" }; List<string> labels = new List<string>();
List<string> backgroundColors = new List<string> { 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<string> borderColors = new List<string> { 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<double> RandomizeData() async Task<List<double>> 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); var r = new Random(DateTime.Now.Millisecond);
return new List<double> { return deviceData?.MatrixTypeToTimestampFloatTuple().ValueOrDefault?.Select(x =>
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() };
}
//private Background buttonColor = Background.Primary;
private void ChangeButtonColor()
{ {
//device.on = !device.on; labels.Add(DateTimeOffset.FromUnixTimeSeconds(long.Parse($"1{x.Item1}0")).UtcDateTime.ToLocalTime().ToShortTimeString());
return x.Item2;
}).ToList() ?? new List<double>();
} }
} }