using System.Collections.ObjectModel; using System.Linq; using UnifiedFarmLauncher.Models; using UnifiedFarmLauncher.Services; namespace UnifiedFarmLauncher.ViewModels { public class WorkerEditViewModel : ViewModelBase { private readonly ConfigService _configService; private readonly bool _isNew; private int _id; private string _name = string.Empty; private bool _enabled = true; private string _sshHost = string.Empty; private int _sshPort = 22; private string _sshArgs = string.Empty; private string _sheepItGpu = "OPTIX_0"; private string _sheepItUsername = string.Empty; private string _sheepItRenderKey = string.Empty; private string _flamencoWorkerPath = string.Empty; private bool _hasSheepIt; private bool _hasFlamenco; public WorkerEditViewModel(ConfigService configService, WorkerConfig? worker = null) { _configService = configService; _isNew = worker == null; NetworkDrives = new ObservableCollection(); NetworkPaths = new ObservableCollection(); if (worker != null) { LoadWorker(worker); } else { _id = _configService.GetNextWorkerId(); } } public int Id { get => _id; set => SetAndRaise(ref _id, value); } public string Name { get => _name; set => SetAndRaise(ref _name, value); } public bool Enabled { get => _enabled; set => SetAndRaise(ref _enabled, value); } public string SshHost { get => _sshHost; set => SetAndRaise(ref _sshHost, value); } public int SshPort { get => _sshPort; set => SetAndRaise(ref _sshPort, value); } public string SshArgs { get => _sshArgs; set => SetAndRaise(ref _sshArgs, value); } public bool HasSheepIt { get => _hasSheepIt; set => SetAndRaise(ref _hasSheepIt, value); } public bool HasFlamenco { get => _hasFlamenco; set => SetAndRaise(ref _hasFlamenco, value); } public string SheepItGpu { get => _sheepItGpu; set => SetAndRaise(ref _sheepItGpu, value); } public string SheepItUsername { get => _sheepItUsername; set => SetAndRaise(ref _sheepItUsername, value); } public string SheepItRenderKey { get => _sheepItRenderKey; set => SetAndRaise(ref _sheepItRenderKey, value); } public string FlamencoWorkerPath { get => _flamencoWorkerPath; set => SetAndRaise(ref _flamencoWorkerPath, value); } public ObservableCollection NetworkDrives { get; } public ObservableCollection NetworkPaths { get; } private void LoadWorker(WorkerConfig worker) { Id = worker.Id; Name = worker.Name; Enabled = worker.Enabled; SshHost = worker.Ssh.Host; SshPort = worker.Ssh.Port; SshArgs = worker.Ssh.Args; if (worker.WorkerTypes.SheepIt != null) { HasSheepIt = true; SheepItGpu = worker.WorkerTypes.SheepIt.Gpu; SheepItUsername = worker.WorkerTypes.SheepIt.Username; SheepItRenderKey = worker.WorkerTypes.SheepIt.RenderKey; } if (worker.WorkerTypes.Flamenco != null) { HasFlamenco = true; FlamencoWorkerPath = worker.WorkerTypes.Flamenco.WorkerPath; NetworkDrives.Clear(); foreach (var drive in worker.WorkerTypes.Flamenco.NetworkDrives) { NetworkDrives.Add(drive); } NetworkPaths.Clear(); foreach (var path in worker.WorkerTypes.Flamenco.NetworkPaths) { NetworkPaths.Add(path); } } } public WorkerConfig ToWorkerConfig() { var worker = new WorkerConfig { Id = Id, Name = Name, Enabled = Enabled, Ssh = new SshConfig { Host = SshHost, Port = SshPort, Args = SshArgs }, WorkerTypes = new WorkerTypeConfig() }; if (HasSheepIt) { worker.WorkerTypes.SheepIt = new SheepItConfig { Gpu = SheepItGpu, Username = SheepItUsername, RenderKey = SheepItRenderKey }; } if (HasFlamenco) { worker.WorkerTypes.Flamenco = new FlamencoConfig { WorkerPath = FlamencoWorkerPath, NetworkDrives = NetworkDrives.ToList(), NetworkPaths = NetworkPaths.ToList() }; } return worker; } public void Save() { var worker = ToWorkerConfig(); if (_isNew) { _configService.AddWorker(worker); } else { _configService.UpdateWorker(worker); } } } }