Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

114 řádky
2.4KB

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