using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using NAudio.Wave; using Resgrid.Providers.ApiClient.V4; using Resgrid.Relay.Engine; using Resgrid.Relay.Engine.Configuration; using Serilog; namespace Resgrid.Audio.Relay.Services { /// /// Starts, stops and pre-flight-checks relay modes for every part of the desktop UI (Operations cards, the shell's /// Start/Stop buttons, the tray menu and auto-start at launch), so they all apply the same rules. Each start runs on a /// deep copy of the current settings with Mode forced, so the shared settings object is never mutated. /// public sealed class RelayModeLauncher { public static readonly IReadOnlyList AllModes = new[] { "smtp", "audio", "radio", "record", "dispatch" }; private readonly RelayController _controller; private readonly ConfigurationService _configuration; private readonly DesktopSettingsService _desktop; private readonly ILogger _logger; public RelayModeLauncher(RelayController controller, ConfigurationService configuration, DesktopSettingsService desktop, ILogger logger) { _controller = controller; _configuration = configuration; _desktop = desktop; _logger = logger; } public RelayController Controller => _controller; public static string FriendlyName(string mode) { switch (mode) { case "smtp": return "SMTP Dispatch"; case "audio": return "Audio Tone-Detect"; case "radio": return "Radio Bridge"; case "record": return "Recorder"; case "dispatch": return "Dispatch Tone-Out"; default: return string.IsNullOrEmpty(mode) ? "Service" : char.ToUpperInvariant(mode[0]) + mode.Substring(1); } } /// True when a service for is tracked and not faulted/stopped. public bool IsActive(string mode) { var service = _controller.Find(mode); return service != null && service.State != RelayServiceState.Faulted && service.State != RelayServiceState.Stopped; } /// /// Pre-flight problems for against the current settings; empty means ready to start. Mirrors the /// checks the engine would otherwise fail on at start (credentials, grant type, devices, required paths). /// public IReadOnlyList Validate(string mode) { var o = _configuration.Current; var errors = new List(); if (!string.IsNullOrEmpty(_configuration.LoadError)) errors.Add("Settings could not be read: " + _configuration.LoadError); // Audio mode may take its credentials from its watcher file (written by the console 'setup'); the engine merges both // and validates the result, so only the other modes are checked here. if (mode != "audio") { try { (o.Resgrid ?? new ResgridApiClientOptions()).Validate(); } catch (InvalidOperationException ex) { errors.Add(ex.Message); } } var grant = o.Resgrid?.GrantType ?? ResgridAuthGrantType.RefreshToken; bool isVoiceMode = mode == "radio" || mode == "record" || mode == "dispatch"; if (isVoiceMode && grant != ResgridAuthGrantType.RefreshToken) errors.Add("Voice modes need the RefreshToken grant (a user's refresh token); Resgrid's Voice API refuses " + grant + "."); switch (mode) { case "smtp": { var smtp = o.Smtp ?? new SmtpRelayOptions(); var hasDomains = (smtp.DepartmentAddressDomains?.Length ?? 0) > 0 || (smtp.GroupAddressDomains?.Length ?? 0) > 0 || (smtp.GroupMessageAddressDomains?.Length ?? 0) > 0 || (smtp.ListAddressDomains?.Length ?? 0) > 0; if (!hasDomains) errors.Add("At least one SMTP dispatch domain must be configured."); if (smtp.Port < 1 || smtp.Port > 65535) errors.Add("The SMTP port must be between 1 and 65535."); if (smtp.RedisCache is { Enabled: true } && string.IsNullOrWhiteSpace(smtp.RedisCache.ConnectionString)) errors.Add("Redis cache is enabled but the connection string is empty."); break; } case "audio": { if (string.IsNullOrWhiteSpace(o.AudioConfigPath) || !File.Exists(o.AudioConfigPath)) errors.Add($"The tone-detect watcher file was not found ({o.AudioConfigPath}). Create it with the console 'setup' command."); break; } case "radio": { var radio = o.Radio ?? new RadioModeOptions(); int inputs = SafeCount(() => WaveIn.DeviceCount); int outputs = SafeCount(() => WaveOut.DeviceCount); if (inputs == 0) errors.Add("No audio input device was found for radio receive audio."); else if (radio.InputDevice < 0 || radio.InputDevice >= inputs) errors.Add($"Radio input device {radio.InputDevice} does not exist (0–{inputs - 1} available)."); if (radio.OutputDevice < -1 || radio.OutputDevice >= outputs) errors.Add($"Radio output device {radio.OutputDevice} does not exist (-1 = default, 0–{Math.Max(0, outputs - 1)} available)."); var ptt = radio.PttMethod ?? "Vox"; var carrier = radio.CarrierDetect ?? "None"; bool serialPtt = ptt.Equals("SerialRts", StringComparison.OrdinalIgnoreCase) || ptt.Equals("SerialDtr", StringComparison.OrdinalIgnoreCase); bool serialCarrier = carrier.Equals("SerialCts", StringComparison.OrdinalIgnoreCase) || carrier.Equals("SerialDsr", StringComparison.OrdinalIgnoreCase); if ((serialPtt || serialCarrier) && string.IsNullOrWhiteSpace(radio.SerialPort)) errors.Add("Serial PTT or carrier detect is selected but no serial port is set."); var profile = RadioProfiles.Find(radio.Profile); if (profile.IsPushToTalkOverCellular && ptt.Equals("Vox", StringComparison.OrdinalIgnoreCase)) errors.Add($"{profile.DisplayName} needs a hard PTT line (Cm108, SerialRts or SerialDtr), not Vox."); break; } case "record": { var store = (o.Recorder?.Store ?? "local").Trim().ToLowerInvariant(); if ((store == "s3" || store == "both") && string.IsNullOrWhiteSpace(o.Recorder?.S3?.Bucket)) errors.Add("Recorder S3 storage is selected but no S3 bucket is configured."); break; } case "dispatch": { if (string.IsNullOrWhiteSpace(o.Tts?.ServiceBaseUrl)) errors.Add("Dispatch tone-out needs a TTS service URL."); break; } } return errors; } private static int SafeCount(Func count) { try { return count(); } catch { return 0; } } /// /// Starts . A faulted instance of the same mode is cleared first. Returns null on success, or the /// reason it was not started. /// public async Task StartAsync(string mode) { var existing = _controller.Find(mode); if (existing != null) { if (existing.State != RelayServiceState.Faulted && existing.State != RelayServiceState.Stopped) return $"{FriendlyName(mode)} is already running."; await _controller.StopAsync(existing).ConfigureAwait(true); } var errors = Validate(mode); if (errors.Count > 0) return $"{FriendlyName(mode)} not started: {errors[0]}"; try { var options = CloneOptions(_configuration.Current); options.Mode = mode; _controller.Start(options); _logger?.Information("Started {Mode}", FriendlyName(mode)); return null; } catch (Exception ex) { _logger?.Error(ex, "Could not start {Mode}", FriendlyName(mode)); return $"{FriendlyName(mode)} not started: {ex.Message}"; } } public async Task StopAsync(string mode) { var service = _controller.Find(mode); if (service != null) await _controller.StopAsync(service).ConfigureAwait(true); } public async Task RestartAsync(string mode) { await StopAsync(mode).ConfigureAwait(true); return await StartAsync(mode).ConfigureAwait(true); } /// The modes the master Start runs: the auto-start set, or the configured default mode when none is chosen. public IReadOnlyList DefaultModes() { var auto = _desktop.Current.AutoStartModes .Where(m => AllModes.Contains(m, StringComparer.OrdinalIgnoreCase)) .Select(m => m.ToLowerInvariant()) .Distinct() .ToList(); if (auto.Count > 0) return auto; var configured = (_configuration.Current.Mode ?? "smtp").Trim().ToLowerInvariant(); return AllModes.Contains(configured) ? new[] { configured } : new[] { "smtp" }; } /// Starts every default mode that isn't already running; returns the problems (empty when all started). public async Task> StartDefaultModesAsync() { var problems = new List(); foreach (var mode in DefaultModes()) { if (IsActive(mode)) continue; var error = await StartAsync(mode).ConfigureAwait(true); if (error != null) problems.Add(error); } return problems; } /// Starts the modes chosen for auto-start at launch; returns the problems. public async Task> StartAutoStartModesAsync() { var problems = new List(); foreach (var mode in AllModes.Where(_desktop.IsAutoStart)) { var error = await StartAsync(mode).ConfigureAwait(true); if (error != null) problems.Add(error); } return problems; } private static RelayHostOptions CloneOptions(RelayHostOptions source) { var json = JsonSerializer.Serialize(source); return JsonSerializer.Deserialize(json) ?? new RelayHostOptions(); } } }