mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-06 13:00:55 +00:00
srfedgdfgh
This commit is contained in:
1 parent
2883956264
commit
4a0ac97c24
9 files changed
+225
-5
No files matched your search
@@ -0,0 +1,12 @@
|
||||
using CartriX.Share;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CartriX.Server.Data;
|
||||
|
||||
public class CartriXDbContext(DbContextOptions<CartriXDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Printer> Printers => Set<Printer>();
|
||||
public DbSet<Cartridge> Cartridges => Set<Cartridge>();
|
||||
public DbSet<CartridgeInstallation> CartridgeInstallations => Set<CartridgeInstallation>();
|
||||
public DbSet<PageCounterLog> PageCounterLogs => Set<PageCounterLog>();
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using CartriX.Server.Data;
|
||||
using CartriX.Server.Services;
|
||||
using CartriX.Share;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddDbContext<CartriXDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("CartriX") ?? "Data Source=cartrix.db"));
|
||||
builder.Services.AddGrpc();
|
||||
builder.Services.AddScoped<CartridgeLifecycleService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<CartriXDbContext>();
|
||||
db.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
app.MapGrpcService<PrinterMonitorGrpcService>();
|
||||
app.MapGet("/", () => "CartriX.Server is running.");
|
||||
|
||||
app.MapGet("/api/printers", async (CartriXDbContext db) =>
|
||||
await db.Printers.OrderBy(p => p.Name).ToListAsync());
|
||||
|
||||
app.MapPost("/api/printers", async (CreatePrinterRequest request, CartriXDbContext db) =>
|
||||
{
|
||||
var printer = new Printer
|
||||
{
|
||||
Name = request.Name,
|
||||
Model = request.Model,
|
||||
ConnectionType = request.ConnectionType,
|
||||
Location = request.Location
|
||||
};
|
||||
|
||||
db.Printers.Add(printer);
|
||||
await db.SaveChangesAsync();
|
||||
return Results.Created($"/api/printers/{printer.Id}", printer);
|
||||
});
|
||||
|
||||
app.MapGet("/api/cartridges", async (CartriXDbContext db) =>
|
||||
await db.Cartridges.OrderBy(c => c.Model).ToListAsync());
|
||||
|
||||
app.MapPost("/api/installations", async (CreateCartridgeInstallationRequest request, CartridgeLifecycleService lifecycle) =>
|
||||
{
|
||||
var installation = await lifecycle.StartInstallationAsync(request);
|
||||
return Results.Created($"/api/installations/{installation.Id}", installation);
|
||||
});
|
||||
|
||||
app.MapPost("/api/installations/{id:int}/remove", async (int id, int pageCounter, CartridgeLifecycleService lifecycle) =>
|
||||
{
|
||||
var closed = await lifecycle.CloseInstallationAsync(id, pageCounter);
|
||||
return closed is null ? Results.NotFound() : Results.Ok(closed);
|
||||
});
|
||||
|
||||
app.MapGet("/api/installations/active", async (CartriXDbContext db) =>
|
||||
await db.CartridgeInstallations
|
||||
.Where(x => x.RemovedAtUtc == null)
|
||||
.OrderByDescending(x => x.InstalledAtUtc)
|
||||
.ToListAsync());
|
||||
|
||||
app.Run();
|
||||
@@ -0,0 +1,46 @@
|
||||
using CartriX.Server.Data;
|
||||
using CartriX.Share;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CartriX.Server.Services;
|
||||
|
||||
public class CartridgeLifecycleService(CartriXDbContext db)
|
||||
{
|
||||
public async Task<CartridgeInstallation> StartInstallationAsync(CreateCartridgeInstallationRequest request)
|
||||
{
|
||||
var installation = new CartridgeInstallation
|
||||
{
|
||||
PrinterId = request.PrinterId,
|
||||
CartridgeId = request.CartridgeId,
|
||||
StartPageCounter = request.StartPageCounter,
|
||||
InstalledAtUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
db.CartridgeInstallations.Add(installation);
|
||||
await db.SaveChangesAsync();
|
||||
return installation;
|
||||
}
|
||||
|
||||
public async Task<CartridgeInstallation?> CloseInstallationAsync(int installationId, int endPageCounter)
|
||||
{
|
||||
var installation = await db.CartridgeInstallations.FirstOrDefaultAsync(x => x.Id == installationId);
|
||||
if (installation is null || installation.RemovedAtUtc is not null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var cartridge = await db.Cartridges.FirstOrDefaultAsync(x => x.Id == installation.CartridgeId);
|
||||
|
||||
installation.EndPageCounter = endPageCounter;
|
||||
installation.RemovedAtUtc = DateTime.UtcNow;
|
||||
installation.PrintedPages = Math.Max(0, endPageCounter - installation.StartPageCounter);
|
||||
|
||||
if (cartridge is not null && installation.PrintedPages > 0)
|
||||
{
|
||||
installation.CostPerPage = Math.Round(cartridge.Price / installation.PrintedPages.Value, 4);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
return installation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using CartriX.Server.Data;
|
||||
using CartriX.Share;
|
||||
using CartriX.Share.Grpc;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace CartriX.Server.Services;
|
||||
|
||||
public class PrinterMonitorGrpcService(CartriXDbContext db, ILogger<PrinterMonitorGrpcService> logger) : PrinterMonitor.PrinterMonitorBase
|
||||
{
|
||||
public override async Task<PrinterStatsResponse> SendStats(PrinterStatsRequest request, ServerCallContext context)
|
||||
{
|
||||
var log = new PageCounterLog
|
||||
{
|
||||
PrinterId = request.PrinterId,
|
||||
TotalPages = request.PageCount,
|
||||
TonerPercent = request.TonerPercent <= 0 ? null : request.TonerPercent,
|
||||
CapturedAtUtc = DateTime.UtcNow,
|
||||
Source = string.IsNullOrWhiteSpace(request.Source) ? "agent" : request.Source
|
||||
};
|
||||
|
||||
db.PageCounterLogs.Add(log);
|
||||
await db.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
logger.LogInformation("Stats received for printer {PrinterId}: pages={PageCount}", request.PrinterId, request.PageCount);
|
||||
|
||||
return new PrinterStatsResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "ok"
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user