feat: MySQL direct sync, IP tracking, and SiteId normalization
- Replace API sync with direct MySQL database insert - Add IP address capture and storage (SQLite + MySQL) - Normalize SiteId to 2-digit format (e.g., "02" instead of "SITE : 2") - Mark records as synced instead of deleting after sync - Add day-end cleanup for old synced rows - Rename table to lunch_order_transactions - Add MySqlConnector package and connection string configpull/1/head
parent
306e03e71a
commit
35161336d0
|
|
@ -122,7 +122,7 @@ public class AppDbContext : DbContext
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lightweight schema upgrade: add SiteId and DeviceId to lunch_order_transactions if missing (no EF migrations).
|
/// Lightweight schema upgrade: add SiteId, DeviceId and IpAddress to lunch_order_transactions if missing (no EF migrations).
|
||||||
/// Does not delete any data.
|
/// Does not delete any data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void UpgradeLunchOrderTransactionsSchemaIfNeeded()
|
private void UpgradeLunchOrderTransactionsSchemaIfNeeded()
|
||||||
|
|
@ -155,6 +155,13 @@ public class AppDbContext : DbContext
|
||||||
cmd.CommandText = "ALTER TABLE lunch_order_transactions ADD COLUMN DeviceId TEXT DEFAULT ''";
|
cmd.CommandText = "ALTER TABLE lunch_order_transactions ADD COLUMN DeviceId TEXT DEFAULT ''";
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (columns.Count > 0 && !columns.Contains("IpAddress", StringComparer.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
using var cmd = conn.CreateCommand();
|
||||||
|
cmd.CommandText = "ALTER TABLE lunch_order_transactions ADD COLUMN IpAddress TEXT DEFAULT ''";
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
namespace UtopiaCanteenSystem.Models;
|
namespace UtopiaCanteenSystem.Models;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a single RFID scan event. Used for local storage and UIND sync.
|
/// Represents a single RFID scan event. Used for local storage and sync.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ScanRecord
|
public class ScanRecord
|
||||||
{
|
{
|
||||||
|
|
@ -10,10 +10,12 @@ public class ScanRecord
|
||||||
public string CardId { get; set; } = string.Empty;
|
public string CardId { get; set; } = string.Empty;
|
||||||
/// <summary>UTC time when the scan occurred.</summary>
|
/// <summary>UTC time when the scan occurred.</summary>
|
||||||
public DateTime ScanTime { get; set; }
|
public DateTime ScanTime { get; set; }
|
||||||
/// <summary>True after record has been successfully sent to the sync API.</summary>
|
/// <summary>True after record has been successfully sent / synced.</summary>
|
||||||
public bool IsSynced { get; set; }
|
public bool IsSynced { get; set; }
|
||||||
/// <summary>Site identifier where the scan occurred (from config).</summary>
|
/// <summary>Site identifier where the scan occurred (2-digit string, e.g. "02").</summary>
|
||||||
public string SiteId { get; set; } = string.Empty;
|
public string SiteId { get; set; } = string.Empty;
|
||||||
/// <summary>Stable device identifier that generated the scan (from config).</summary>
|
/// <summary>Stable device identifier that generated the scan (from config).</summary>
|
||||||
public string DeviceId { get; set; } = string.Empty;
|
public string DeviceId { get; set; } = string.Empty;
|
||||||
|
/// <summary>IP address of the device at scan time.</summary>
|
||||||
|
public string IpAddress { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
@ -51,11 +52,46 @@ public class ConfigService : IConfigService
|
||||||
SaveConfig();
|
SaveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetSiteId() => _config.SiteId ?? "SITE : 1";
|
public string GetSiteId()
|
||||||
|
{
|
||||||
|
// Normalize to a 2-digit string (e.g. "02", "07", "12").
|
||||||
|
// Accepts legacy values like "SITE : 2" or "2" and converts them.
|
||||||
|
var raw = _config.SiteId;
|
||||||
|
if (string.IsNullOrWhiteSpace(raw))
|
||||||
|
return "01";
|
||||||
|
|
||||||
|
var digits = new string(raw.Where(char.IsDigit).ToArray());
|
||||||
|
if (string.IsNullOrWhiteSpace(digits))
|
||||||
|
return "01";
|
||||||
|
|
||||||
|
if (!int.TryParse(digits, out var numeric))
|
||||||
|
return "01";
|
||||||
|
|
||||||
|
return Math.Clamp(numeric, 0, 99).ToString("D2");
|
||||||
|
}
|
||||||
|
|
||||||
public void SetSiteId(string siteId)
|
public void SetSiteId(string siteId)
|
||||||
{
|
{
|
||||||
_config.SiteId = string.IsNullOrWhiteSpace(siteId) ? "SITE : 1" : (siteId ?? string.Empty);
|
// Store the normalized 2-digit site code.
|
||||||
|
var normalized = siteId;
|
||||||
|
if (string.IsNullOrWhiteSpace(normalized))
|
||||||
|
{
|
||||||
|
normalized = "01";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var digits = new string(normalized.Where(char.IsDigit).ToArray());
|
||||||
|
if (string.IsNullOrWhiteSpace(digits) || !int.TryParse(digits, out var numeric))
|
||||||
|
{
|
||||||
|
normalized = "01";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
normalized = Math.Clamp(numeric, 0, 99).ToString("D2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_config.SiteId = normalized;
|
||||||
SaveConfig();
|
SaveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,6 +174,14 @@ public class ConfigService : IConfigService
|
||||||
SaveConfig();
|
SaveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetMySqlConnectionString() => _config.MySqlConnectionString ?? string.Empty;
|
||||||
|
|
||||||
|
public void SetMySqlConnectionString(string connectionString)
|
||||||
|
{
|
||||||
|
_config.MySqlConnectionString = connectionString ?? string.Empty;
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
private AppConfig LoadConfig()
|
private AppConfig LoadConfig()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -193,5 +237,8 @@ public class ConfigService : IConfigService
|
||||||
public bool RememberAdminCredentials { get; set; } = false;
|
public bool RememberAdminCredentials { get; set; } = false;
|
||||||
public string SavedAdminUsername { get; set; } = string.Empty;
|
public string SavedAdminUsername { get; set; } = string.Empty;
|
||||||
public string SavedAdminPasswordProtected { get; set; } = string.Empty;
|
public string SavedAdminPasswordProtected { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
// MySQL connection string for direct DB sync (local testing).
|
||||||
|
public string MySqlConnectionString { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,4 +25,7 @@ public interface IConfigService
|
||||||
void SetSavedAdminUsername(string username);
|
void SetSavedAdminUsername(string username);
|
||||||
string GetSavedAdminPassword();
|
string GetSavedAdminPassword();
|
||||||
void SetSavedAdminPassword(string password);
|
void SetSavedAdminPassword(string password);
|
||||||
|
|
||||||
|
string GetMySqlConnectionString();
|
||||||
|
void SetMySqlConnectionString(string connectionString);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using UtopiaCanteenSystem.Data;
|
using UtopiaCanteenSystem.Data;
|
||||||
|
|
@ -81,7 +83,8 @@ public class RfidService : IRfidService
|
||||||
ScanTime = nowUtc,
|
ScanTime = nowUtc,
|
||||||
IsSynced = false,
|
IsSynced = false,
|
||||||
SiteId = _configService.GetSiteId(),
|
SiteId = _configService.GetSiteId(),
|
||||||
DeviceId = _configService.GetDeviceId()
|
DeviceId = _configService.GetDeviceId(),
|
||||||
|
IpAddress = GetLocalIpAddress()
|
||||||
};
|
};
|
||||||
db.LunchOrderTransactions.Add(record);
|
db.LunchOrderTransactions.Add(record);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
|
|
@ -214,4 +217,18 @@ public class RfidService : IRfidService
|
||||||
var remaining = (int)Math.Ceiling((endUtc - nowUtc).TotalSeconds);
|
var remaining = (int)Math.Ceiling((endUtc - nowUtc).TotalSeconds);
|
||||||
return Math.Max(0, remaining);
|
return Math.Max(0, remaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string GetLocalIpAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
var ip = host.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
|
||||||
|
return ip?.ToString() ?? string.Empty;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,18 @@
|
||||||
using System.Net.Http.Json;
|
|
||||||
using System.Text.Json;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MySqlConnector;
|
||||||
using UtopiaCanteenSystem.Data;
|
using UtopiaCanteenSystem.Data;
|
||||||
using UtopiaCanteenSystem.Models;
|
using UtopiaCanteenSystem.Models;
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
namespace UtopiaCanteenSystem.Services;
|
namespace UtopiaCanteenSystem.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scheduled sync: fetches unsynced lunch_order_transactions, POSTs them to the configured API,
|
/// Scheduled sync: fetches unsynced lunch_order_transactions, inserts them into MySQL database,
|
||||||
/// and deletes uploaded records from local SQLite on success.
|
/// marks them as synced in SQLite, and periodically cleans up old synced rows.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SyncService : ISyncService
|
public class SyncService : ISyncService
|
||||||
{
|
{
|
||||||
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
||||||
private readonly IConfigService _configService;
|
private readonly IConfigService _configService;
|
||||||
private static readonly HttpClient HttpClient = new();
|
|
||||||
|
|
||||||
public SyncService(IDbContextFactory<AppDbContext> dbFactory, IConfigService configService)
|
public SyncService(IDbContextFactory<AppDbContext> dbFactory, IConfigService configService)
|
||||||
{
|
{
|
||||||
|
|
@ -25,9 +22,10 @@ public class SyncService : ISyncService
|
||||||
|
|
||||||
public async Task SyncNowAsync(CancellationToken cancellationToken = default)
|
public async Task SyncNowAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var endpoint = _configService.GetSyncApiEndpoint();
|
var connectionString = _configService.GetMySqlConnectionString();
|
||||||
if (string.IsNullOrWhiteSpace(endpoint))
|
// TODO: For local testing only. Set MySqlConnectionString in appsettings.json or via config to avoid hardcoding.
|
||||||
return;
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||||||
|
connectionString = "Server=localhost;Database=canteen_prod;User=root;Password=Root@12345_;Port=3306;";
|
||||||
|
|
||||||
List<ScanRecord> toSync;
|
List<ScanRecord> toSync;
|
||||||
using (var db = _dbFactory.CreateDbContext())
|
using (var db = _dbFactory.CreateDbContext())
|
||||||
|
|
@ -42,24 +40,44 @@ public class SyncService : ISyncService
|
||||||
if (toSync.Count == 0)
|
if (toSync.Count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var payload = toSync.Select(r => new
|
try
|
||||||
{
|
{
|
||||||
DeviceLocalRowId = r.Id,
|
// Insert into MySQL using transaction for atomicity
|
||||||
ScanTimeUtc = r.ScanTime,
|
await using var mysqlConn = new MySqlConnection(connectionString);
|
||||||
SiteId = r.SiteId ?? string.Empty,
|
await mysqlConn.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||||
DeviceId = r.DeviceId ?? string.Empty,
|
|
||||||
r.CardId
|
await using var transaction = await mysqlConn.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await HttpClient
|
// Use INSERT IGNORE to handle duplicates (unique key on DeviceId, DeviceLocalRowId)
|
||||||
.PostAsJsonAsync(endpoint, payload, cancellationToken: cancellationToken)
|
// This ensures no duplicates even if sync runs multiple times
|
||||||
.ConfigureAwait(false);
|
var insertSql = @"
|
||||||
|
INSERT IGNORE INTO lunch_order_transactions
|
||||||
|
(DeviceLocalRowId, ScanTimeUtc, SiteId, DeviceId, CardId, IpAddress, ReceivedAtUtc)
|
||||||
|
VALUES
|
||||||
|
(@DeviceLocalRowId, @ScanTimeUtc, @SiteId, @DeviceId, @CardId, @IpAddress, UTC_TIMESTAMP(3))";
|
||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
await using var cmd = new MySqlCommand(insertSql, mysqlConn, transaction);
|
||||||
return;
|
|
||||||
|
|
||||||
|
foreach (var record in toSync)
|
||||||
|
{
|
||||||
|
cmd.Parameters.Clear();
|
||||||
|
cmd.Parameters.AddWithValue("@DeviceLocalRowId", record.Id);
|
||||||
|
cmd.Parameters.AddWithValue("@ScanTimeUtc", record.ScanTime);
|
||||||
|
cmd.Parameters.AddWithValue("@SiteId", record.SiteId ?? string.Empty);
|
||||||
|
cmd.Parameters.AddWithValue("@DeviceId", record.DeviceId ?? string.Empty);
|
||||||
|
cmd.Parameters.AddWithValue("@CardId", record.CardId ?? string.Empty);
|
||||||
|
cmd.Parameters.AddWithValue("@IpAddress", record.IpAddress ?? string.Empty);
|
||||||
|
|
||||||
|
var rowsAffected = await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
// Note: INSERT IGNORE returns 0 if row already exists (duplicate), 1 if inserted
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit MySQL transaction
|
||||||
|
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Only mark as synced in SQLite after successful MySQL insert
|
||||||
var ids = toSync.Select(r => r.Id).ToList();
|
var ids = toSync.Select(r => r.Id).ToList();
|
||||||
using (var db = _dbFactory.CreateDbContext())
|
using (var db = _dbFactory.CreateDbContext())
|
||||||
{
|
{
|
||||||
|
|
@ -67,14 +85,63 @@ public class SyncService : ISyncService
|
||||||
.Where(r => ids.Contains(r.Id))
|
.Where(r => ids.Contains(r.Id))
|
||||||
.ToListAsync(cancellationToken)
|
.ToListAsync(cancellationToken)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
// On successful upload, delete uploaded scan events from local SQLite.
|
foreach (var record in records)
|
||||||
db.LunchOrderTransactions.RemoveRange(records);
|
record.IsSynced = true;
|
||||||
|
|
||||||
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Day-end cleanup: delete only synced rows from previous days.
|
||||||
|
await CleanupOldSyncedRowsAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Rollback MySQL transaction on error
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
// Ignore rollback errors
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log error for debugging (you can replace with proper logging)
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Sync error: {ex.Message}");
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
|
||||||
|
|
||||||
|
throw; // Re-throw to be caught by outer catch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log error for debugging (you can replace with proper logging)
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Sync failed: {ex.Message}");
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
|
||||||
|
|
||||||
// Leave records intact; will retry on next run
|
// Leave records intact; will retry on next run
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes only synced rows (IsSynced = 1) where ScanTime is before the start of today (local day).
|
||||||
|
/// </summary>
|
||||||
|
private async Task CleanupOldSyncedRowsAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// Define "today" by the local calendar day, but ScanTime is stored as UTC.
|
||||||
|
var startOfTodayLocal = DateTime.Today;
|
||||||
|
var startUtc = startOfTodayLocal.ToUniversalTime();
|
||||||
|
|
||||||
|
using var db = _dbFactory.CreateDbContext();
|
||||||
|
var oldSynced = await db.LunchOrderTransactions
|
||||||
|
.Where(r => r.IsSynced && r.ScanTime < startUtc)
|
||||||
|
.ToListAsync(cancellationToken)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (oldSynced.Count == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
db.LunchOrderTransactions.RemoveRange(oldSynced);
|
||||||
|
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="MySqlConnector" Version="2.3.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue