using System;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenNest.Data;
using OpenNest.Data.MaterialLedger;
using OpenNest.Desktop;
using OpenNest.Engine.Jobs;
namespace OpenNest.Mcp.Database;
///
/// The nest server the Database-mode tools talk to, and what they need to make nests as the
/// desktop does: File > New nests (), the saved Auto Nest engine and
/// this PC's MaterialLedger connection. reads the desktop's own settings
/// (storage.json, defaults, machines, user-settings.json); tests build one around an
/// in-process server.
///
public sealed class NestServerContext : IDisposable
{
private static readonly HttpClient LedgerHttp = new();
// Distinguishes OpenNest's ledger key from other DPAPI blobs of the same user; matches the desktop.
private static readonly byte[] LedgerEntropy = Encoding.UTF8.GetBytes("OpenNest.MaterialLedger.ApiKey");
private readonly Func> _createNest;
private readonly Func _engineName;
private readonly Func _createLedger;
private readonly IDisposable[] _owned;
/// Makes a group's empty nest as File > New would.
/// The Auto Nest engine to use when a call names none.
///
/// MaterialLedger stock for one run, leaving out codes on every open nest but the given one;
/// null when the ledger is not set up, so nests use sheet sizes only.
///
public NestServerContext(string serverUrl, RemoteDrawingLibrary library, IDrawingSetStore sets,
INestRepository nests, Func> createNest, Func engineName,
Func createLedger, string userName, params IDisposable[] owned)
{
ServerUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl));
Library = library ?? throw new ArgumentNullException(nameof(library));
Sets = sets ?? throw new ArgumentNullException(nameof(sets));
Nests = nests ?? throw new ArgumentNullException(nameof(nests));
_createNest = createNest ?? throw new ArgumentNullException(nameof(createNest));
_engineName = engineName ?? throw new ArgumentNullException(nameof(engineName));
_createLedger = createLedger;
UserName = string.IsNullOrWhiteSpace(userName) ? Environment.UserName : userName.Trim();
_owned = owned ?? Array.Empty();
}
private NestServerContext(string error)
{
Error = error;
_owned = Array.Empty();
}
/// Why the tools cannot reach a nest server (File mode, no URL); null when they can.
public string Error { get; }
public string ServerUrl { get; }
public RemoteDrawingLibrary Library { get; }
public IDrawingSetStore Sets { get; }
public INestRepository Nests { get; }
/// Recorded as SavedBy, ReleasedBy and UndoneBy.
public string UserName { get; }
/// True when MaterialLedger stock is offered before the sheet sizes.
public bool HasLedger => _createLedger != null;
public Task CreateNestAsync(SetReleaseGroup group) => _createNest(group);
/// The ledger stock for one run, or null when the ledger is not set up.
public ReleaseLedgerStock CreateLedger(Guid excludeNestId) => _createLedger?.Invoke(excludeNestId);
///
/// as the registry spells it, else the engine the desktop last
/// used (engine-selection.json), else Default.
///
public string ResolveEngine(string requested, out string error)
{
error = null;
if (string.IsNullOrWhiteSpace(requested))
return _engineName();
var name = NestingEngineRegistry.ResolveName(requested.Trim());
if (name != null)
return name;
error = $"Unknown engine '{requested}'. Available: "
+ string.Join(", ", NestingEngineRegistry.AvailableEngines.Select(e => e.Name)) + ".";
return null;
}
///
/// The nest server from storage.json in Database mode, with File > New nests, the engine
/// and MaterialLedger set up from the desktop's settings files.
///
public static NestServerContext Load() => FromStorage(NestStorageSettings.Load(NestStorageSettings.DefaultPath));
/// for the given storage settings; File mode gives a context with .
public static NestServerContext FromStorage(NestStorageSettings storage)
{
ArgumentNullException.ThrowIfNull(storage);
if (!storage.IsDatabaseMode || string.IsNullOrWhiteSpace(storage.ServerUrl))
return new NestServerContext(
"OpenNest is not in Database mode on this PC (storage.json has no nest server), so the nest server tools are unavailable.");
var url = storage.ServerUrl;
RemoteDrawingLibrary library;
RemoteDrawingSetStore sets;
RemoteNestRepository nests;
try
{
library = new RemoteDrawingLibrary(url);
sets = new RemoteDrawingSetStore(url);
nests = new RemoteNestRepository(url);
}
catch (ArgumentException ex)
{
return new NestServerContext($"The nest server URL '{url}' in storage.json is not valid: {ex.Message}");
}
var shared = new SharedSettingsSource(() => storage);
var machines = new MachineSettingsSource(shared, new MachineUserSettingsFile(UserSettingsFile.DefaultPath));
var newNests = new NewNestSource(machines, new NewNestUserSettingsFile(UserSettingsFile.DefaultPath), () => nests);
var ledgerSettings = MaterialLedgerSettings.Load(MaterialLedgerSettings.DefaultPath, UnprotectLedgerKey);
Func ledger = !ledgerSettings.IsConfigured
? null
: exclude => new ReleaseLedgerStock(
(material, grade, thicknessIn, token) => new MaterialLedgerClient(LedgerHttp, ledgerSettings)
.GetStockAsync(material, grade, thicknessIn, token),
token => LedgerCodesInUse.ReadAsync(nests, exclude, token));
return new NestServerContext(url, library, sets, nests, group => newNests.CreateAsync(group),
SavedEngine, ledger, Environment.UserName, library, sets, nests);
}
/// The engine the desktop last used, resolved against the loaded engines.
public static string SavedEngine() =>
EngineSelectionSettings.Load(EngineSelectionSettings.DefaultPath)
.Resolve(NestingEngineRegistry.AvailableEngines.Select(e => e.Name), out _, NestingEngineRegistry.ResolveName);
private static string UnprotectLedgerKey(string protectedText)
{
if (!OperatingSystem.IsWindows())
throw new PlatformNotSupportedException("The MaterialLedger key is protected for Windows.");
return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(protectedText), LedgerEntropy,
DataProtectionScope.CurrentUser));
}
public void Dispose()
{
foreach (var owned in _owned)
owned?.Dispose();
}
}