Files
OpenNest/OpenNest.Data/SharedMachineProvider.cs
T
ajandClaude Opus 5.5 90e2fd2e6e feat(server): share machines, post settings, clamps and nest defaults
Schema v13 adds shared_settings and shared_setting_revisions behind
/api/settings: one revision-guarded JSON document per kind and key, with
every revision (deletions included) kept with who saved it. Recreating a
deleted document continues its revision count.

OpenNest.Data gains the client (RemoteSharedSettings), a synchronous store
that keeps the last copy read for offline use and refuses writes while the
server is unreachable (SharedSettingsStore), a machine provider over it, and
JSON parse/serialize entry points for clamp settings and nest defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 20:02:08 -04:00

42 lines
1.6 KiB
C#

using System.Text.Json;
namespace OpenNest.Data;
/// <summary>
/// The machine configurations shared through the nest server, in the same JSON as
/// <see cref="LocalJsonProvider"/>'s files. Reads come from the store's last-read copy; saves
/// and deletes go to the server and throw <see cref="IOException"/> when it cannot take them.
/// </summary>
public sealed class SharedMachineProvider(SharedSettingsStore store) : IDataProvider
{
public IReadOnlyList<MachineSummary> GetMachines() =>
store.List(SharedSettingKinds.Machine)
.Select(Read)
.OfType<MachineConfig>()
.Select(machine => new MachineSummary(machine.Id, machine.Name))
.ToList();
public MachineConfig? GetMachine(Guid id) =>
store.Get(SharedSettingKinds.Machine, id.ToString("D")) is { } record ? Read(record) : null;
public void SaveMachine(MachineConfig machine) =>
store.Save(SharedSettingKinds.Machine, machine.Id.ToString("D"), Serialize(machine));
public void DeleteMachine(Guid id) => store.Delete(SharedSettingKinds.Machine, id.ToString("D"));
/// <summary>A machine's shared document, the same JSON its local file holds.</summary>
public static string Serialize(MachineConfig machine) => JsonSerializer.Serialize(machine, LocalJsonProvider.JsonOptions);
private static MachineConfig? Read(SharedSettingRecord record)
{
try
{
return JsonSerializer.Deserialize<MachineConfig>(record.Json, LocalJsonProvider.JsonOptions);
}
catch (JsonException)
{
return null;
}
}
}