Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

125 lignes
2.7KB

  1. package runtime
  2. import (
  3. "errors"
  4. "sync"
  5. "sdr-visual-suite/internal/config"
  6. )
  7. type ConfigUpdate struct {
  8. CenterHz *float64 `json:"center_hz"`
  9. SampleRate *int `json:"sample_rate"`
  10. FFTSize *int `json:"fft_size"`
  11. GainDb *float64 `json:"gain_db"`
  12. TunerBwKHz *int `json:"tuner_bw_khz"`
  13. UseGPUFFT *bool `json:"use_gpu_fft"`
  14. Detector *DetectorUpdate `json:"detector"`
  15. }
  16. type DetectorUpdate struct {
  17. ThresholdDb *float64 `json:"threshold_db"`
  18. MinDuration *int `json:"min_duration_ms"`
  19. HoldMs *int `json:"hold_ms"`
  20. }
  21. type SettingsUpdate struct {
  22. AGC *bool `json:"agc"`
  23. DCBlock *bool `json:"dc_block"`
  24. IQBalance *bool `json:"iq_balance"`
  25. }
  26. type Manager struct {
  27. mu sync.RWMutex
  28. cfg config.Config
  29. }
  30. func New(cfg config.Config) *Manager {
  31. return &Manager{cfg: cfg}
  32. }
  33. func (m *Manager) Snapshot() config.Config {
  34. m.mu.RLock()
  35. defer m.mu.RUnlock()
  36. return m.cfg
  37. }
  38. func (m *Manager) Replace(cfg config.Config) {
  39. m.mu.Lock()
  40. defer m.mu.Unlock()
  41. m.cfg = cfg
  42. }
  43. func (m *Manager) ApplyConfig(update ConfigUpdate) (config.Config, error) {
  44. m.mu.Lock()
  45. defer m.mu.Unlock()
  46. next := m.cfg
  47. if update.CenterHz != nil {
  48. next.CenterHz = *update.CenterHz
  49. }
  50. if update.SampleRate != nil {
  51. if *update.SampleRate <= 0 {
  52. return m.cfg, errors.New("sample_rate must be > 0")
  53. }
  54. next.SampleRate = *update.SampleRate
  55. }
  56. if update.FFTSize != nil {
  57. if *update.FFTSize <= 0 {
  58. return m.cfg, errors.New("fft_size must be > 0")
  59. }
  60. next.FFTSize = *update.FFTSize
  61. }
  62. if update.GainDb != nil {
  63. next.GainDb = *update.GainDb
  64. }
  65. if update.TunerBwKHz != nil {
  66. if *update.TunerBwKHz <= 0 {
  67. return m.cfg, errors.New("tuner_bw_khz must be > 0")
  68. }
  69. next.TunerBwKHz = *update.TunerBwKHz
  70. }
  71. if update.UseGPUFFT != nil {
  72. next.UseGPUFFT = *update.UseGPUFFT
  73. }
  74. if update.Detector != nil {
  75. if update.Detector.ThresholdDb != nil {
  76. next.Detector.ThresholdDb = *update.Detector.ThresholdDb
  77. }
  78. if update.Detector.MinDuration != nil {
  79. if *update.Detector.MinDuration <= 0 {
  80. return m.cfg, errors.New("min_duration_ms must be > 0")
  81. }
  82. next.Detector.MinDurationMs = *update.Detector.MinDuration
  83. }
  84. if update.Detector.HoldMs != nil {
  85. if *update.Detector.HoldMs <= 0 {
  86. return m.cfg, errors.New("hold_ms must be > 0")
  87. }
  88. next.Detector.HoldMs = *update.Detector.HoldMs
  89. }
  90. }
  91. m.cfg = next
  92. return m.cfg, nil
  93. }
  94. func (m *Manager) ApplySettings(update SettingsUpdate) (config.Config, error) {
  95. m.mu.Lock()
  96. defer m.mu.Unlock()
  97. next := m.cfg
  98. if update.AGC != nil {
  99. next.AGC = *update.AGC
  100. }
  101. if update.DCBlock != nil {
  102. next.DCBlock = *update.DCBlock
  103. }
  104. if update.IQBalance != nil {
  105. next.IQBalance = *update.IQBalance
  106. }
  107. m.cfg = next
  108. return m.cfg, nil
  109. }