From 489d574789c05a340cd98874b41f61b2de96aaec Mon Sep 17 00:00:00 2001 From: Bjarne Pedersen Date: Mon, 27 Jul 2026 14:40:27 +0200 Subject: [PATCH] API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API er færdigt --- fgs.api/Controllers/LogController.cs | 34 +++--- fgs.api/Controllers/TagController.cs | 108 +++++++++++++------ fgs.api/appsettings.json | 4 +- fgs.api/fgs.api.http | 2 +- fgs.api/models/OpenPrintTag.cs | 6 ++ fgs.api/models/OpenPrintTagRemoved.cs | 5 + fgs.database/Context/FilamentguardContext.cs | 11 +- fgs.database/Models/Esp32.cs | 24 +++++ fgs.database/Models/Log.cs | 25 ++--- fgs.database/Models/Reader.cs | 20 ++++ fgs.database/Models/Tag.cs | 26 ++--- fgs.database/efpt.config.json | 10 +- 12 files changed, 195 insertions(+), 80 deletions(-) create mode 100644 fgs.database/Models/Esp32.cs create mode 100644 fgs.database/Models/Reader.cs diff --git a/fgs.api/Controllers/LogController.cs b/fgs.api/Controllers/LogController.cs index e9ecb9a..89e4552 100644 --- a/fgs.api/Controllers/LogController.cs +++ b/fgs.api/Controllers/LogController.cs @@ -13,20 +13,28 @@ namespace fgs.api.Controllers { [HttpPost] [Route("create")] - public void Logging(LogRequest logRequest) + public IActionResult Logging(LogRequest logRequest) { - Log log = new Log(); - log.Category = logRequest.Category; - log.Data = logRequest.Data; - log.DeviceId = logRequest.deviceId; - log.FirmwareVersion = logRequest.FirmwareVersion; - log.Hostname = logRequest.Hostname; - log.Level = logRequest.Level; - log.Message = logRequest.Message; - log.UptimeMilliseconds = (long)logRequest.UptimeMilliseconds; - log.TagUid = logRequest.tagUid; - dbContext.Logs.Add(log); - dbContext.SaveChanges(); + try + { + Log log = new Log(); + log.Category = logRequest.Category; + log.Data = logRequest.Data; + log.DeviceId = logRequest.deviceId; + log.FirmwareVersion = logRequest.FirmwareVersion; + log.Hostname = logRequest.Hostname; + log.Level = logRequest.Level; + log.Message = logRequest.Message; + log.UptimeMilliseconds = (long)logRequest.UptimeMilliseconds; + log.TagUid = logRequest.tagUid; + dbContext.Logs.Add(log); + dbContext.SaveChanges(); + return Ok(new { message = "Log created successfully", id = log.Id }); + } + catch (Exception ex) + { + return StatusCode(500, new { message = "Error creating log", error = ex.Message }); + } } } } diff --git a/fgs.api/Controllers/TagController.cs b/fgs.api/Controllers/TagController.cs index 30105e5..9e5c331 100644 --- a/fgs.api/Controllers/TagController.cs +++ b/fgs.api/Controllers/TagController.cs @@ -1,7 +1,10 @@ using fgs.api.models; using fgs.database.Context; +using fgs.database.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Reader = fgs.database.Models.Reader; using Tag = fgs.database.Models.Tag; namespace fgs.api.Controllers @@ -14,35 +17,69 @@ namespace fgs.api.Controllers [Route("detected")] public IActionResult Detected([FromBody] OpenPrintTag openPrintTag) { - Tag? currentTag = dbContext.Tags - .SingleOrDefault(c => c.DeviceId.Equals(openPrintTag.device.deviceId) && c.TagUid.Equals(openPrintTag.tag.tagUid)); - - if (currentTag == null) + try { - currentTag = new Tag(); - dbContext.Tags.Add(currentTag); + Tag? currentTag = dbContext.Tags + .SingleOrDefault(c => c.TagUid == openPrintTag.tag.tagUid); + + + Esp32? esp32 = dbContext.Esp32s.FirstOrDefault(c => c.DeviceId == openPrintTag.device.deviceId); + Reader? reader = dbContext.Readers.FirstOrDefault(c => c.ReaderId == openPrintTag.reader.id); + + + if (esp32 == null) + { + esp32 = new Esp32(); + dbContext.Esp32s.Add(esp32); + } + esp32.DeviceId = openPrintTag.device.deviceId; + esp32.HostName = openPrintTag.device.hostname; + + + if (reader == null) + { + reader = new Reader(); + dbContext.Readers.Add(reader); + } + reader.ReaderId = openPrintTag.reader.id; + + dbContext.SaveChanges(); + + if (currentTag == null) + { + currentTag = new Tag(); + dbContext.Tags.Add(currentTag); + + } + + currentTag.Esp32id = esp32.Id; + currentTag.ReaderId = reader.Id; + + currentTag.HostName = openPrintTag.device.hostname; + currentTag.TagUid = openPrintTag.tag.tagUid; + currentTag.InstanceUid = openPrintTag.tag.instanceUuid; + currentTag.MaterialName = openPrintTag.tag.materialName; + currentTag.MaterialType = openPrintTag.tag.materialType; + currentTag.Brand = openPrintTag.tag.brand; + currentTag.ColorHex = openPrintTag.tag.colorHex; + currentTag.ActualWeightGrams = openPrintTag.tag.actualWeightGrams; + currentTag.NominalWeightGrams = openPrintTag.tag.nominalWeightGrams; + currentTag.SpoolWeightGrams = openPrintTag.tag.spoolWeightGrams; + currentTag.ConsumedWeightGrams = openPrintTag.tag.consumedWeightGrams; + currentTag.RemainingWeightGrams = openPrintTag.tag.remainingWeightGrams; + currentTag.HasActualWeight = openPrintTag.tag.hasActualWeight; + currentTag.HasNominalWeight = openPrintTag.tag.hasNominalWeight; + currentTag.HasSpoolWeight = openPrintTag.tag.hasSpoolWeight; + currentTag.HasConsumedWeight = openPrintTag.tag.hasConsumedWeight; + currentTag.IsActive = true; + currentTag.Time = DateTime.Now; + dbContext.SaveChanges(); + return Ok(new { message = "Tag detected successfully", deviceId = esp32.DeviceId, tagUid = currentTag.TagUid}); + } + catch (Exception ex) + { + return StatusCode(500, new { message = "Error processing tag detection", error = ex.Message }); } - currentTag.DeviceId = openPrintTag.device?.deviceId; - currentTag.HostName = openPrintTag.device?.hostname; - currentTag.TagUid = openPrintTag.tag.tagUid; - currentTag.InstanceUid = openPrintTag.tag.instanceUuid; - currentTag.MaterialName = openPrintTag.tag.materialName; - currentTag.MaterialType = openPrintTag.tag.materialType; - currentTag.Brand = openPrintTag.tag.brand; - currentTag.ColorHex = openPrintTag.tag.colorHex; - currentTag.ActualWeightGrams = openPrintTag.tag.actualWeightGrams; - currentTag.NominalWeightGrams = openPrintTag.tag.nominalWeightGrams; - currentTag.SpoolWeightGrams = openPrintTag.tag.spoolWeightGrams; - currentTag.ConsumedWeightGrams = openPrintTag.tag.consumedWeightGrams; - currentTag.RemainingWeightGrams = openPrintTag.tag.remainingWeightGrams; - currentTag.HasActualWeight = openPrintTag.tag.hasActualWeight; - currentTag.HasNominalWeight = openPrintTag.tag.hasNominalWeight; - currentTag.HasSpoolWeight = openPrintTag.tag.hasSpoolWeight; - currentTag.HasConsumedWeight = openPrintTag.tag.hasConsumedWeight; - currentTag.IsActive = true; - - dbContext.SaveChanges(); - return Ok(); } [HttpPost] @@ -50,26 +87,27 @@ namespace fgs.api.Controllers public IActionResult Removed([FromBody] OpenPrintTagRemoved openPrintTagRemoved) { Tag? currentTag = dbContext.Tags - .SingleOrDefault(c => c.DeviceId.Equals(openPrintTagRemoved.device.deviceId) && c.TagUid.Equals(openPrintTagRemoved.tag.tagUid)); + .SingleOrDefault(c => c.TagUid == openPrintTagRemoved.tag.tagUid); + if (currentTag != null) { currentTag.IsActive = false; + currentTag.Time = DateTime.Now; + dbContext.SaveChanges(); return Ok(new { message = "Tag removal received, tag removed", tagUid = openPrintTagRemoved.tag.tagUid, - deviceId = openPrintTagRemoved.device?.deviceId + deviceId = openPrintTagRemoved.device.deviceId }); } - else + + return Ok(new { - return Ok(new - { - message = "Tag removal received, but no tag to remove" - }); - } + message = "Tag removal received, but no tag to remove" + }); } } diff --git a/fgs.api/appsettings.json b/fgs.api/appsettings.json index 10f68b8..a437e01 100644 --- a/fgs.api/appsettings.json +++ b/fgs.api/appsettings.json @@ -5,5 +5,7 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "ConnectionStrings": { + "FilamentGuardDatabase": "Server=mssql2022,1433;Database=filamentguard;User Id=filamentguard;Password=rlsBN2TJMp5WWGW0lm;TrustServerCertificate=True;" + } } diff --git a/fgs.api/fgs.api.http b/fgs.api/fgs.api.http index 7469e53..5b7b7b1 100644 --- a/fgs.api/fgs.api.http +++ b/fgs.api/fgs.api.http @@ -1,4 +1,4 @@ -@fgs.api_HostAddress = http://localhost:5011 +@fgs.api_HostAddress = http://192.168.3.2:9090/ GET {{fgs.api_HostAddress}}/weatherforecast/ Accept: application/json diff --git a/fgs.api/models/OpenPrintTag.cs b/fgs.api/models/OpenPrintTag.cs index 4e29005..fd4403e 100644 --- a/fgs.api/models/OpenPrintTag.cs +++ b/fgs.api/models/OpenPrintTag.cs @@ -4,6 +4,12 @@ { public Device device { get; set; } public Tag tag { get; set; } + public Reader reader { get; set; } + } + + public class Reader + { + public string id { get; set; } } public class Device diff --git a/fgs.api/models/OpenPrintTagRemoved.cs b/fgs.api/models/OpenPrintTagRemoved.cs index c7246a4..3d77a45 100644 --- a/fgs.api/models/OpenPrintTagRemoved.cs +++ b/fgs.api/models/OpenPrintTagRemoved.cs @@ -4,8 +4,13 @@ { public DeviceRemoved device { get; set; } public TagRemoved tag { get; set; } + public ReaderRemoved reader { get; set; } } + public class ReaderRemoved + { + public string id { get; set; } + } public class DeviceRemoved { public string deviceId { get; set; } diff --git a/fgs.database/Context/FilamentguardContext.cs b/fgs.database/Context/FilamentguardContext.cs index d51dfc6..52c6567 100644 --- a/fgs.database/Context/FilamentguardContext.cs +++ b/fgs.database/Context/FilamentguardContext.cs @@ -1,5 +1,5 @@ // This file has been auto generated by EF Core Power Tools. -#nullable disable +#nullable enable using System; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; @@ -14,12 +14,21 @@ public partial class FilamentguardContext : DbContext { } + public virtual DbSet Esp32s { get; set; } + public virtual DbSet Logs { get; set; } + public virtual DbSet Readers { get; set; } + public virtual DbSet Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity(entity => + { + entity.Property(e => e.Time).HasDefaultValueSql("(getdate())", "DF_Tag_Time"); + }); + OnModelCreatingPartial(modelBuilder); } diff --git a/fgs.database/Models/Esp32.cs b/fgs.database/Models/Esp32.cs new file mode 100644 index 0000000..b75dd9c --- /dev/null +++ b/fgs.database/Models/Esp32.cs @@ -0,0 +1,24 @@ +// This file has been auto generated by EF Core Power Tools. +#nullable enable +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +namespace fgs.database.Models; + +[Table("ESP32")] +public partial class Esp32 +{ + [Key] + public int Id { get; set; } + + [StringLength(100)] + public string DeviceId { get; set; } = null!; + + [StringLength(100)] + public string HostName { get; set; } = null!; + + public string? FriendlyName { get; set; } +} \ No newline at end of file diff --git a/fgs.database/Models/Log.cs b/fgs.database/Models/Log.cs index 07f1a95..8416740 100644 --- a/fgs.database/Models/Log.cs +++ b/fgs.database/Models/Log.cs @@ -1,5 +1,5 @@ // This file has been auto generated by EF Core Power Tools. -#nullable disable +#nullable enable using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -14,36 +14,29 @@ public partial class Log [Key] public int Id { get; set; } - [Required] [Column("deviceId", TypeName = "text")] - public string DeviceId { get; set; } + public string DeviceId { get; set; } = null!; - [Required] [Column("tagUid", TypeName = "text")] - public string TagUid { get; set; } + public string TagUid { get; set; } = null!; - [Required] [Column("hostname", TypeName = "text")] - public string Hostname { get; set; } + public string Hostname { get; set; } = null!; - [Required] [Column("firmwareVersion", TypeName = "text")] - public string FirmwareVersion { get; set; } + public string FirmwareVersion { get; set; } = null!; - [Required] [Column("level", TypeName = "text")] - public string Level { get; set; } + public string Level { get; set; } = null!; - [Required] [Column("category", TypeName = "text")] - public string Category { get; set; } + public string Category { get; set; } = null!; - [Required] [Column("message", TypeName = "text")] - public string Message { get; set; } + public string Message { get; set; } = null!; [Column("data", TypeName = "text")] - public string Data { get; set; } + public string? Data { get; set; } [Column("uptimeMilliseconds")] public long UptimeMilliseconds { get; set; } diff --git a/fgs.database/Models/Reader.cs b/fgs.database/Models/Reader.cs new file mode 100644 index 0000000..d8bff59 --- /dev/null +++ b/fgs.database/Models/Reader.cs @@ -0,0 +1,20 @@ +// This file has been auto generated by EF Core Power Tools. +#nullable enable +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; + +namespace fgs.database.Models; + +[Table("Reader")] +public partial class Reader +{ + [Key] + public int Id { get; set; } + + [StringLength(150)] + [Unicode(false)] + public string ReaderId { get; set; } = null!; +} \ No newline at end of file diff --git a/fgs.database/Models/Tag.cs b/fgs.database/Models/Tag.cs index c408871..904a7f6 100644 --- a/fgs.database/Models/Tag.cs +++ b/fgs.database/Models/Tag.cs @@ -1,5 +1,5 @@ // This file has been auto generated by EF Core Power Tools. -#nullable disable +#nullable enable using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -14,26 +14,26 @@ public partial class Tag [Key] public int Id { get; set; } - [Required] - public string DeviceId { get; set; } + [Column("ESP32Id")] + public int? Esp32id { get; set; } + + public int? ReaderId { get; set; } public bool IsActive { get; set; } - [Required] - public string TagUid { get; set; } + public string TagUid { get; set; } = null!; - [Required] - public string HostName { get; set; } + public string HostName { get; set; } = null!; - public string InstanceUid { get; set; } + public string? InstanceUid { get; set; } - public string MaterialName { get; set; } + public string? MaterialName { get; set; } - public string MaterialType { get; set; } + public string? MaterialType { get; set; } - public string Brand { get; set; } + public string? Brand { get; set; } - public string ColorHex { get; set; } + public string? ColorHex { get; set; } public int? ActualWeightGrams { get; set; } @@ -52,4 +52,6 @@ public partial class Tag public bool HasSpoolWeight { get; set; } public bool HasConsumedWeight { get; set; } + + public DateTime Time { get; set; } } \ No newline at end of file diff --git a/fgs.database/efpt.config.json b/fgs.database/efpt.config.json index 16c8192..b0c8608 100644 --- a/fgs.database/efpt.config.json +++ b/fgs.database/efpt.config.json @@ -18,10 +18,18 @@ "SingularRules": null, "T4TemplatePath": null, "Tables": [ + { + "Name": "[dbo].[ESP32]", + "ObjectType": 0 + }, { "Name": "[dbo].[Log]", "ObjectType": 0 }, + { + "Name": "[dbo].[Reader]", + "ObjectType": 0 + }, { "Name": "[dbo].[Tag]", "ObjectType": 0 @@ -47,7 +55,7 @@ "UseNoNavigations": false, "UseNoObjectFilter": false, "UseNodaTime": false, - "UseNullableReferences": false, + "UseNullableReferences": true, "UsePrefixNavigationNaming": false, "UseSchemaFolders": false, "UseSchemaNamespaces": false,