mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 00:45:58 +02:00
Fix formatting and other stuff
whoopsie
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
using Blazorise;
|
||||
|
||||
using Elektrifikatsiya.Components.TodoApp;
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Elektrifikatsiya.Components.TodoApp;
|
||||
public abstract class BaseTodoItems : ComponentBase
|
||||
{
|
||||
protected Validations validations;
|
||||
|
||||
protected string description;
|
||||
|
||||
protected Filter filter = Filter.All;
|
||||
|
||||
protected List<Todo> todos = new()
|
||||
{
|
||||
new() { Description = "Buy milk" },
|
||||
new() { Description = "Call John regarding the meeting" },
|
||||
new() { Description = "Walk a dog" },
|
||||
};
|
||||
|
||||
protected IEnumerable<Todo> Todos
|
||||
{
|
||||
get
|
||||
{
|
||||
var query = from t in todos select t;
|
||||
|
||||
if (filter == Filter.Active)
|
||||
query = from q in query where !q.Completed select q;
|
||||
|
||||
if (filter == Filter.Completed)
|
||||
query = from q in query where q.Completed select q;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
protected void SetFilter(Filter filter)
|
||||
{
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
protected void OnCheckAll(bool isChecked)
|
||||
{
|
||||
todos.ForEach(x => x.Completed = isChecked);
|
||||
}
|
||||
|
||||
protected async Task OnAddTodo()
|
||||
{
|
||||
if (await validations.ValidateAll())
|
||||
{
|
||||
todos.Add(new() { Description = description });
|
||||
description = null;
|
||||
|
||||
await validations.ClearAll();
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnClearCompleted()
|
||||
{
|
||||
todos.RemoveAll(x => x.Completed);
|
||||
filter = Filter.All;
|
||||
}
|
||||
|
||||
protected Task OnTodoStatusChanged(bool isChecked)
|
||||
{
|
||||
return InvokeAsync(StateHasChanged);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Elektrifikatsiya.Components.TodoApp;
|
||||
|
||||
public enum Filter
|
||||
{
|
||||
All,
|
||||
Active,
|
||||
Completed,
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Elektrifikatsiya.Components.TodoApp;
|
||||
|
||||
public class Todo
|
||||
{
|
||||
public bool Completed { get; set; }
|
||||
|
||||
public string Description { get; set; }
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<ListGroupItem>
|
||||
<Field Horizontal Padding="Padding.IsAuto.OnAll">
|
||||
<FieldBody ColumnSize="ColumnSize.Is1">
|
||||
<Check TValue="bool" Checked="@Todo.Completed" CheckedChanged="@OnCheckedChanged"></Check>
|
||||
</FieldBody>
|
||||
<FieldBody ColumnSize="ColumnSize.Is11">
|
||||
@Todo.Description
|
||||
</FieldBody>
|
||||
</Field>
|
||||
</ListGroupItem>
|
||||
@code{
|
||||
Task OnCheckedChanged( bool isChecked )
|
||||
{
|
||||
Todo.Completed = isChecked;
|
||||
|
||||
return StatusChanged?.Invoke( isChecked );
|
||||
}
|
||||
|
||||
[Parameter] public Todo Todo { get; set; }
|
||||
|
||||
[Parameter] public Func<bool, Task> StatusChanged { get; set; }
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
@inherits BaseTodoItems
|
||||
<Container Fluid>
|
||||
<Row>
|
||||
<Column>
|
||||
<Card>
|
||||
<CardHeader Padding="Padding.Is1.FromBottom">
|
||||
<CardTitle Size="4">Todo List</CardTitle>
|
||||
</CardHeader>
|
||||
<CardBody Padding="Padding.Is0.FromBottom">
|
||||
<Fields>
|
||||
<Column ColumnSize="ColumnSize.Is1">
|
||||
<Check TValue="bool" Checked="@Todos.All(x=>x.Completed)" CheckedChanged="@OnCheckAll">All</Check>
|
||||
</Column>
|
||||
<Column ColumnSize="ColumnSize.Is11">
|
||||
<Addons>
|
||||
<Addon AddonType="AddonType.Body">
|
||||
<Validations @ref="validations" Mode="ValidationMode.Manual">
|
||||
<Validation Validator="@ValidationRule.IsNotEmpty">
|
||||
<TextEdit @bind-Text="@description" Placeholder="What needs to be done?"></TextEdit>
|
||||
</Validation>
|
||||
</Validations>
|
||||
</Addon>
|
||||
<Addon AddonType="AddonType.End">
|
||||
<Button Color="Color.Primary" Clicked="@OnAddTodo">
|
||||
<Icon Name="IconName.Add" />Add
|
||||
</Button>
|
||||
</Addon>
|
||||
</Addons>
|
||||
</Column>
|
||||
</Fields>
|
||||
</CardBody>
|
||||
<CardBody Padding="Padding.Is0.OnY">
|
||||
<ListGroup Flush>
|
||||
@foreach ( var todo in Todos )
|
||||
{
|
||||
<TodoItem Todo="@todo" StatusChanged="@OnTodoStatusChanged" />
|
||||
}
|
||||
</ListGroup>
|
||||
</CardBody>
|
||||
<CardFooter Padding="Padding.Is3.FromBottom">
|
||||
<Field Horizontal>
|
||||
<FieldBody ColumnSize="ColumnSize.Is10">
|
||||
<Buttons Role="ButtonsRole.Addons">
|
||||
<Button Color="Color.Info" Clicked="@(() => SetFilter( Filter.All ))" Active="@(filter == Filter.All)">All</Button>
|
||||
<Button Color="Color.Info" Clicked="@(() => SetFilter( Filter.Active ))" Active="@(filter == Filter.Active)">Active</Button>
|
||||
<Button Color="Color.Info" Clicked="@(() => SetFilter( Filter.Completed ))" Active="@(filter == Filter.Completed)">Completed</Button>
|
||||
</Buttons>
|
||||
</FieldBody>
|
||||
<FieldBody ColumnSize="ColumnSize.Is2">
|
||||
<Button Color="Color.Warning" Float="Float.End" Clicked="@OnClearCompleted" Display="@(todos.Any(x=>x.Completed) ? Display.Always : Display.None)">Clear Completed</Button>
|
||||
</FieldBody>
|
||||
</Field>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</Column>
|
||||
</Row>
|
||||
</Container>
|
||||
@@ -1,17 +1,42 @@
|
||||
@using System.Diagnostics;
|
||||
@using FluentResults;
|
||||
|
||||
@inject Elektrifikatsiya.Services.IAuthenticationService AuthenticationService
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
@inherits LayoutComponentBase
|
||||
<Image Source="logo.png" Width="Width.Is50" Height="Height.Is50"> </Image>
|
||||
<Card Width="Width.Is50" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Padding="Padding.Is5">
|
||||
<Column ColumnSize="ColumnSize.Is6.OnDesktop.Is12.Is3.WithOffset">
|
||||
<Field Position="Position.Relative.Top.Is50.Start.Is50.Translate.Middle">
|
||||
<TextEdit Placeholder="Username" />
|
||||
<TextEdit @bind-Text="username" Placeholder="Username" />
|
||||
</Field>
|
||||
<Field Position="Position.Relative.Top.Is50.Start.Is50.Translate.Middle">
|
||||
<TextEdit Role="TextRole.Password" Placeholder="Password" />
|
||||
<TextEdit @bind-Text="password" Role="TextRole.Password" Placeholder="Password" />
|
||||
</Field>
|
||||
<Button Color="Color.Primary" Position="Position.Relative.Bottom.Is50.Start.Is50.Translate.MiddleX">Login</Button>
|
||||
<Button Width="Width.Is25" Disabled="disabled" Color="color" Clicked="Clicked" Position="Position.Relative.Bottom.Is50.Start.Is50.Translate.MiddleX">Login</Button>
|
||||
</Column>
|
||||
</Card>
|
||||
@code {
|
||||
private string username = string.Empty;
|
||||
private string password = string.Empty;
|
||||
private Color color = Color.Primary;
|
||||
private bool disabled;
|
||||
private async Task Clicked()
|
||||
{
|
||||
color = Color.Primary;
|
||||
disabled = true;
|
||||
|
||||
Result loginResult = await AuthenticationService.LoginUserAsync(username, password);
|
||||
|
||||
if (loginResult.IsSuccess)
|
||||
{
|
||||
NavigationManager.NavigateTo("/", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
color = Color.Danger;
|
||||
disabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc.Diagnostics;
|
||||
|
||||
namespace Elektrifikatsiya.Models;
|
||||
|
||||
@@ -67,7 +67,7 @@ public class PrometheusDataWrapper
|
||||
|
||||
string[] segment = value.ToString()!.Split(",");
|
||||
|
||||
result.Add((Convert.ToDouble(segment[0][2..^1]), Convert.ToDouble(segment[1][1..^2])));
|
||||
result.Add((Convert.ToDouble(segment[0][2..^1], CultureInfo.InvariantCulture), Convert.ToDouble(segment[1][1..^2], CultureInfo.InvariantCulture)));
|
||||
}
|
||||
Debug.WriteLine(result);
|
||||
return result;
|
||||
@@ -80,18 +80,17 @@ public class PrometheusDataWrapper
|
||||
return FluentResults.Result.Fail("The response did not have the Vector Type");
|
||||
}
|
||||
|
||||
string[]? segment = Result.FirstOrDefault()?.Value.ToString()?.Split(",") ?? null;
|
||||
string[]? segment = Result.FirstOrDefault()?.Value?.ToString()?.Split(",");
|
||||
|
||||
if (segment is null)
|
||||
{
|
||||
return FluentResults.Result.Fail("There was no result in the Response Body");
|
||||
}
|
||||
|
||||
return((Convert.ToDouble(segment[0][2..^1]), Convert.ToDouble(segment[1][1..^2])));
|
||||
return (Convert.ToDouble(segment[0][2..^1]), Convert.ToDouble(segment[1][1..^2], CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class PrometheusDataMetric
|
||||
{
|
||||
[JsonPropertyName("__name__")] public string Name { get; set; }
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
</Row>
|
||||
</Column>
|
||||
<Column ColumnSize="ColumnSize.Is4">
|
||||
<Button Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Clicked="@(async () => await DeviceManagmentService.RegisterDevice(context.Item,(await AuthenticationService.GetUserAsync()).ValueOrDefault))" Outline>
|
||||
<Button Disabled="disabled" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Clicked="@(async () => await RegisterDevice(context.Item))" Outline>
|
||||
<BarIcon IconName="IconName.Add" />
|
||||
</Button>
|
||||
</Column>
|
||||
@@ -81,13 +81,21 @@
|
||||
<ListGroup Style="width:100%; border-radius: 0px 0px">
|
||||
<ListGroupItem Color="Color.Default">
|
||||
<Form>
|
||||
<Validation @ref="ipValidation" Validator="ValidateIPv4">
|
||||
<Field Horizontal>
|
||||
<FieldBody ColumnSize="ColumnSize.Is12">
|
||||
<TextEdit Placeholder="Enter the Device IP here" />
|
||||
<TextEdit @bind-Text="ipText" Placeholder="Enter the Device IP here">
|
||||
<Feedback>
|
||||
<ValidationNone>Please enter a ip address.</ValidationNone>
|
||||
<ValidationSuccess>Ip address is valid.</ValidationSuccess>
|
||||
<ValidationError>Ip address is not valid!</ValidationError>
|
||||
</Feedback>
|
||||
</TextEdit>
|
||||
<br>
|
||||
<Button style="background-color:#1a237e; color:white" Type="ButtonType.Submit" PreventDefaultOnSubmit>CONNECT</Button>
|
||||
<Button style="background-color:#1a237e; color:white" Type="ButtonType.Submit" Disabled="ipValidation?.Status != ValidationStatus.Success || disabled" Clicked="@(async (_)=> await RegisterDevice(IPAddress.Parse(ipText!)))" PreventDefaultOnSubmit>CONNECT</Button>
|
||||
</FieldBody>
|
||||
</Field>
|
||||
</Validation>
|
||||
</Form>
|
||||
</ListGroupItem>
|
||||
</ListGroup>
|
||||
@@ -102,6 +110,7 @@
|
||||
Validation? ipValidation;
|
||||
|
||||
bool visible = true;
|
||||
bool disabled = false;
|
||||
|
||||
string ipText = null!;
|
||||
|
||||
@@ -122,6 +131,7 @@
|
||||
|
||||
private async Task RegisterDevice(IPAddress ipAddress)
|
||||
{
|
||||
disabled = true;
|
||||
Result<Device> result = await DeviceManagmentService.RegisterDevice(ipAddress, (await AuthenticationService.GetUserAsync()).ValueOrDefault);
|
||||
|
||||
if (result.IsSuccess)
|
||||
@@ -132,6 +142,7 @@
|
||||
{
|
||||
await MessageService.Error(string.Join(',', result.Errors.Select(e => e.Message)), "Adding device failed!");
|
||||
}
|
||||
disabled = false;
|
||||
}
|
||||
|
||||
public void ValidateIPv4(ValidatorEventArgs e)
|
||||
|
||||
@@ -152,6 +152,12 @@
|
||||
{
|
||||
plugnames += $"shellyplug-s-{device.MacAddress}/relay/0|";
|
||||
}
|
||||
|
||||
if(string.IsNullOrEmpty(plugnames))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
plugnames = plugnames[0..^1];
|
||||
|
||||
Debug.WriteLine($$"""sum(power{sensor=~"{{plugnames}}"})[1h:1m]""");
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
@using Elektrifikatsiya.Components.TodoApp
|
||||
@page "/apps/todo"
|
||||
<TodoItems />
|
||||
@@ -60,7 +60,7 @@ IAuthenticationService authenticationService = serviceScope.ServiceProvider.GetR
|
||||
|
||||
if (!mainDatabase.Users.Any())
|
||||
{
|
||||
authenticationService.RegisterUserAsync("admin", "admin", Role.Admin);
|
||||
_ = authenticationService.RegisterUserAsync("admin", "admin", Role.Admin);
|
||||
}
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -33,12 +33,12 @@ public class UpdateService : IHostedService, IDisposable
|
||||
_ = deviceStatusService.TrackDevice(device);
|
||||
}
|
||||
|
||||
timer = new Timer((_) => Update(), null, TimeSpan.Zero, TimeSpan.FromSeconds(15));
|
||||
timer = new Timer(async (_) => await Update(), null, TimeSpan.Zero, TimeSpan.FromSeconds(15));
|
||||
|
||||
logger.LogInformation("Update service started.");
|
||||
}
|
||||
|
||||
private async void Update()
|
||||
private async Task Update()
|
||||
{
|
||||
Result<List<Device>> getDeviceStatusResult = deviceStatusService.GetDevices();
|
||||
|
||||
|
||||
Binary file not shown.
@@ -10,18 +10,15 @@ namespace Elektrifikatsiya.Utilities;
|
||||
|
||||
public class PrometheusQuery
|
||||
{
|
||||
private string connectionString;
|
||||
private readonly HttpClient client = new();
|
||||
|
||||
public PrometheusQuery(string connectionString)
|
||||
{
|
||||
this.connectionString = connectionString;
|
||||
client.BaseAddress = new Uri(connectionString);
|
||||
}
|
||||
|
||||
public Task<PrometheusQueryResult?> Query(string query)
|
||||
{
|
||||
var res = client.GetStringAsync($"/api/v1/query?query={UrlEncoder.Create().Encode(query)}").Result;
|
||||
return client.GetFromJsonAsync<PrometheusQueryResult>($"/api/v1/query?query={UrlEncoder.Create().Encode(query)}", new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
|
||||
Reference in New Issue
Block a user