Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

185 rindas
4.8KB

  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. Recorder *RecorderUpdate `json:"recorder"`
  16. }
  17. type DetectorUpdate struct {
  18. ThresholdDb *float64 `json:"threshold_db"`
  19. MinDuration *int `json:"min_duration_ms"`
  20. HoldMs *int `json:"hold_ms"`
  21. }
  22. type SettingsUpdate struct {
  23. AGC *bool `json:"agc"`
  24. DCBlock *bool `json:"dc_block"`
  25. IQBalance *bool `json:"iq_balance"`
  26. }
  27. type RecorderUpdate struct {
  28. Enabled *bool `json:"enabled"`
  29. MinSNRDb *float64 `json:"min_snr_db"`
  30. MinDuration *string `json:"min_duration"`
  31. MaxDuration *string `json:"max_duration"`
  32. PrerollMs *int `json:"preroll_ms"`
  33. RecordIQ *bool `json:"record_iq"`
  34. RecordAudio *bool `json:"record_audio"`
  35. AutoDemod *bool `json:"auto_demod"`
  36. AutoDecode *bool `json:"auto_decode"`
  37. OutputDir *string `json:"output_dir"`
  38. ClassFilter *[]string `json:"class_filter"`
  39. RingSeconds *int `json:"ring_seconds"`
  40. }
  41. type Manager struct {
  42. mu sync.RWMutex
  43. cfg config.Config
  44. }
  45. func New(cfg config.Config) *Manager {
  46. return &Manager{cfg: cfg}
  47. }
  48. func (m *Manager) Snapshot() config.Config {
  49. m.mu.RLock()
  50. defer m.mu.RUnlock()
  51. return m.cfg
  52. }
  53. func (m *Manager) Replace(cfg config.Config) {
  54. m.mu.Lock()
  55. defer m.mu.Unlock()
  56. m.cfg = cfg
  57. }
  58. func (m *Manager) ApplyConfig(update ConfigUpdate) (config.Config, error) {
  59. m.mu.Lock()
  60. defer m.mu.Unlock()
  61. next := m.cfg
  62. if update.CenterHz != nil {
  63. if *update.CenterHz < 1e3 || *update.CenterHz > 2e9 {
  64. return m.cfg, errors.New("center_hz out of range")
  65. }
  66. next.CenterHz = *update.CenterHz
  67. }
  68. if update.SampleRate != nil {
  69. if *update.SampleRate <= 0 {
  70. return m.cfg, errors.New("sample_rate must be > 0")
  71. }
  72. next.SampleRate = *update.SampleRate
  73. }
  74. if update.FFTSize != nil {
  75. if *update.FFTSize <= 0 {
  76. return m.cfg, errors.New("fft_size must be > 0")
  77. }
  78. if *update.FFTSize&(*update.FFTSize-1) != 0 {
  79. return m.cfg, errors.New("fft_size must be a power of 2")
  80. }
  81. next.FFTSize = *update.FFTSize
  82. }
  83. if update.GainDb != nil {
  84. next.GainDb = *update.GainDb
  85. }
  86. if update.TunerBwKHz != nil {
  87. if *update.TunerBwKHz <= 0 {
  88. return m.cfg, errors.New("tuner_bw_khz must be > 0")
  89. }
  90. next.TunerBwKHz = *update.TunerBwKHz
  91. }
  92. if update.UseGPUFFT != nil {
  93. next.UseGPUFFT = *update.UseGPUFFT
  94. }
  95. if update.Detector != nil {
  96. if update.Detector.ThresholdDb != nil {
  97. next.Detector.ThresholdDb = *update.Detector.ThresholdDb
  98. }
  99. if update.Detector.MinDuration != nil {
  100. if *update.Detector.MinDuration <= 0 {
  101. return m.cfg, errors.New("min_duration_ms must be > 0")
  102. }
  103. next.Detector.MinDurationMs = *update.Detector.MinDuration
  104. }
  105. if update.Detector.HoldMs != nil {
  106. if *update.Detector.HoldMs <= 0 {
  107. return m.cfg, errors.New("hold_ms must be > 0")
  108. }
  109. next.Detector.HoldMs = *update.Detector.HoldMs
  110. }
  111. }
  112. if update.Recorder != nil {
  113. if update.Recorder.Enabled != nil {
  114. next.Recorder.Enabled = *update.Recorder.Enabled
  115. }
  116. if update.Recorder.MinSNRDb != nil {
  117. next.Recorder.MinSNRDb = *update.Recorder.MinSNRDb
  118. }
  119. if update.Recorder.MinDuration != nil {
  120. next.Recorder.MinDuration = *update.Recorder.MinDuration
  121. }
  122. if update.Recorder.MaxDuration != nil {
  123. next.Recorder.MaxDuration = *update.Recorder.MaxDuration
  124. }
  125. if update.Recorder.PrerollMs != nil {
  126. next.Recorder.PrerollMs = *update.Recorder.PrerollMs
  127. }
  128. if update.Recorder.RecordIQ != nil {
  129. next.Recorder.RecordIQ = *update.Recorder.RecordIQ
  130. }
  131. if update.Recorder.RecordAudio != nil {
  132. next.Recorder.RecordAudio = *update.Recorder.RecordAudio
  133. }
  134. if update.Recorder.AutoDemod != nil {
  135. next.Recorder.AutoDemod = *update.Recorder.AutoDemod
  136. }
  137. if update.Recorder.AutoDecode != nil {
  138. next.Recorder.AutoDecode = *update.Recorder.AutoDecode
  139. }
  140. if update.Recorder.OutputDir != nil {
  141. next.Recorder.OutputDir = *update.Recorder.OutputDir
  142. }
  143. if update.Recorder.ClassFilter != nil {
  144. next.Recorder.ClassFilter = *update.Recorder.ClassFilter
  145. }
  146. if update.Recorder.RingSeconds != nil {
  147. next.Recorder.RingSeconds = *update.Recorder.RingSeconds
  148. }
  149. }
  150. m.cfg = next
  151. return m.cfg, nil
  152. }
  153. func (m *Manager) ApplySettings(update SettingsUpdate) (config.Config, error) {
  154. m.mu.Lock()
  155. defer m.mu.Unlock()
  156. next := m.cfg
  157. if update.AGC != nil {
  158. next.AGC = *update.AGC
  159. }
  160. if update.DCBlock != nil {
  161. next.DCBlock = *update.DCBlock
  162. }
  163. if update.IQBalance != nil {
  164. next.IQBalance = *update.IQBalance
  165. }
  166. m.cfg = next
  167. return m.cfg, nil
  168. }