API er færdigt
This commit is contained in:
2026-07-27 14:40:27 +02:00
parent 589a8c477d
commit 489d574789
12 changed files with 195 additions and 80 deletions

View File

@@ -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 });
}
}
}
}

View File

@@ -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);
}
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;
Tag? currentTag = dbContext.Tags
.SingleOrDefault(c => c.TagUid == openPrintTag.tag.tagUid);
dbContext.SaveChanges();
return Ok();
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 });
}
}
[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"
});
}
}

View File

@@ -5,5 +5,7 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"ConnectionStrings": {
"FilamentGuardDatabase": "Server=mssql2022,1433;Database=filamentguard;User Id=filamentguard;Password=rlsBN2TJMp5WWGW0lm;TrustServerCertificate=True;"
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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; }

View File

@@ -1,5 +1,5 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#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<Esp32> Esp32s { get; set; }
public virtual DbSet<Log> Logs { get; set; }
public virtual DbSet<Reader> Readers { get; set; }
public virtual DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Tag>(entity =>
{
entity.Property(e => e.Time).HasDefaultValueSql("(getdate())", "DF_Tag_Time");
});
OnModelCreatingPartial(modelBuilder);
}

View File

@@ -0,0 +1,24 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#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; }
}

View File

@@ -1,5 +1,5 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#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; }

View File

@@ -0,0 +1,20 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#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!;
}

View File

@@ -1,5 +1,5 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#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; }
}

View File

@@ -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,