Skip to content

Commit

Permalink
Changes Monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
valentintintin committed May 26, 2023
1 parent 98f3878 commit 3543a3b
Show file tree
Hide file tree
Showing 69 changed files with 1,550 additions and 64 deletions.
6 changes: 3 additions & 3 deletions Monitor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Web/storage
Web/bin
Web/obj
Monitor/storage
Monitor/bin
Monitor/obj
2 changes: 1 addition & 1 deletion Monitor/Monitor.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Web\Web.csproj", "{2B342B16-F6DA-4874-B35F-A9B6A63CA86B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Monitor", "Monitor\Monitor.csproj", "{2B342B16-F6DA-4874-B35F-A9B6A63CA86B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
40 changes: 40 additions & 0 deletions Monitor/Monitor/Context/DataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Monitor.Context.Entities;

namespace Monitor.Context;

public class DataContext : DbContext
{
public required DbSet<Weather> Weathers { get; set; }

public required DbSet<Mppt> Mppts { get; set; }

public required DbSet<Entities.System> Systems { get; set; }

public DataContext(DbContextOptions<DataContext> options) : base(options) {}

public override int SaveChanges()
{
ComputeEntitiesBeforeSaveChanges();

return base.SaveChanges();
}

private void ComputeEntitiesBeforeSaveChanges()
{
foreach (EntityEntry entityEntry in ChangeTracker.Entries())
{
if (entityEntry.Entity is IEntity entity)
{
switch (entityEntry.State)
{
case EntityState.Added:
entity.CreatedAt = DateTime.UtcNow;
break;

}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Web.Context.Entities;
namespace Monitor.Context.Entities;

public interface IEntity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace Web.Context.Entities;
namespace Monitor.Context.Entities;

public class Mppt : IEntity
{
Expand Down
17 changes: 17 additions & 0 deletions Monitor/Monitor/Context/Entities/System.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Monitor.Context.Entities;

public class System : IEntity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }

public bool Npr { get; set; }

public bool Wifi { get; set; }

public bool BoxOpened { get; set; }

public long Uptime { get; set; }

public long McuUptime { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace Web.Context.Entities;
namespace Monitor.Context.Entities;

public class Weather : IEntity
{
Expand Down
13 changes: 13 additions & 0 deletions Monitor/Monitor/Controllers/AController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;

namespace Monitor.Controllers;

public abstract class AController : Controller
{
protected readonly ILogger<AController> Logger;

protected AController(ILogger<AController> logger)
{
Logger = logger;
}
}
31 changes: 31 additions & 0 deletions Monitor/Monitor/Controllers/GpioController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Monitor.Services;

namespace Monitor.Controllers;

[Route("gpio")]
public class GpioController : AController
{
private readonly SerialMessageService _serialMessageService;

public GpioController(ILogger<GpioController> logger, SerialMessageService serialMessageService) : base(logger)
{
_serialMessageService = serialMessageService;
}

[Route("wifi/{enabled}")]
public void Wifi(bool enabled)
{
Logger.LogInformation("From web, set wifi to {enabled}", enabled);

_serialMessageService.SetWifi(enabled);
}

[Route("npr/{enabled}")]
public void Npr(bool enabled)
{
Logger.LogInformation("From web, set npr to {enabled}", enabled);

_serialMessageService.SetNpr(enabled);
}
}
24 changes: 24 additions & 0 deletions Monitor/Monitor/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using Monitor.Models.Views;
using Monitor.Services;

namespace Monitor.Controllers;

public class HomeController : AController
{
private readonly CameraService _cameraService;

public HomeController(ILogger<HomeController> logger, CameraService cameraService) : base(logger)
{
_cameraService = cameraService;
}

public IActionResult Index()
{
return View(new HomeModel
{
State = MonitorService.State,
LastPhoto = _cameraService.GetLast()
});
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Web.Exceptions;
namespace Monitor.Exceptions;

public class HttpRequestException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Web.Exceptions;
namespace Monitor.Exceptions;

public class MessageParseException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Web.Exceptions;
namespace Monitor.Exceptions;

public class MissingConfigurationException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using Web.Exceptions;
using Monitor.Exceptions;

namespace Web.Extensions;
namespace Monitor.Extensions;

public static class ConfigurationExtensions
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions Monitor/Monitor/Migrations/20230525192341_AddSystem.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions Monitor/Monitor/Migrations/20230525192341_AddSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Monitor.Migrations
{
/// <inheritdoc />
public partial class AddSystem : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Systems",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
Npr = table.Column<bool>(type: "INTEGER", nullable: false),
Wifi = table.Column<bool>(type: "INTEGER", nullable: false),
BoxOpened = table.Column<bool>(type: "INTEGER", nullable: false),
Uptime = table.Column<TimeSpan>(type: "TEXT", nullable: false),
McuUptime = table.Column<TimeSpan>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Systems", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Systems");
}
}
}
Loading

0 comments on commit 3543a3b

Please sign in to comment.