Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

237 строки
6.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. EmaAlpha *float64 `json:"ema_alpha"`
  22. HysteresisDb *float64 `json:"hysteresis_db"`
  23. MinStableFrames *int `json:"min_stable_frames"`
  24. GapToleranceMs *int `json:"gap_tolerance_ms"`
  25. CFAREnabled *bool `json:"cfar_enabled"`
  26. CFARGuardCells *int `json:"cfar_guard_cells"`
  27. CFARTrainCells *int `json:"cfar_train_cells"`
  28. CFARRank *int `json:"cfar_rank"`
  29. CFARScaleDb *float64 `json:"cfar_scale_db"`
  30. }
  31. type SettingsUpdate struct {
  32. AGC *bool `json:"agc"`
  33. DCBlock *bool `json:"dc_block"`
  34. IQBalance *bool `json:"iq_balance"`
  35. }
  36. type RecorderUpdate struct {
  37. Enabled *bool `json:"enabled"`
  38. MinSNRDb *float64 `json:"min_snr_db"`
  39. MinDuration *string `json:"min_duration"`
  40. MaxDuration *string `json:"max_duration"`
  41. PrerollMs *int `json:"preroll_ms"`
  42. RecordIQ *bool `json:"record_iq"`
  43. RecordAudio *bool `json:"record_audio"`
  44. AutoDemod *bool `json:"auto_demod"`
  45. AutoDecode *bool `json:"auto_decode"`
  46. MaxDiskMB *int `json:"max_disk_mb"`
  47. OutputDir *string `json:"output_dir"`
  48. ClassFilter *[]string `json:"class_filter"`
  49. RingSeconds *int `json:"ring_seconds"`
  50. }
  51. type Manager struct {
  52. mu sync.RWMutex
  53. cfg config.Config
  54. }
  55. func New(cfg config.Config) *Manager {
  56. return &Manager{cfg: cfg}
  57. }
  58. func (m *Manager) Snapshot() config.Config {
  59. m.mu.RLock()
  60. defer m.mu.RUnlock()
  61. return m.cfg
  62. }
  63. func (m *Manager) Replace(cfg config.Config) {
  64. m.mu.Lock()
  65. defer m.mu.Unlock()
  66. m.cfg = cfg
  67. }
  68. func (m *Manager) ApplyConfig(update ConfigUpdate) (config.Config, error) {
  69. m.mu.Lock()
  70. defer m.mu.Unlock()
  71. next := m.cfg
  72. if update.CenterHz != nil {
  73. if *update.CenterHz < 1e3 || *update.CenterHz > 2e9 {
  74. return m.cfg, errors.New("center_hz out of range")
  75. }
  76. next.CenterHz = *update.CenterHz
  77. }
  78. if update.SampleRate != nil {
  79. if *update.SampleRate <= 0 {
  80. return m.cfg, errors.New("sample_rate must be > 0")
  81. }
  82. next.SampleRate = *update.SampleRate
  83. }
  84. if update.FFTSize != nil {
  85. if *update.FFTSize <= 0 {
  86. return m.cfg, errors.New("fft_size must be > 0")
  87. }
  88. if *update.FFTSize&(*update.FFTSize-1) != 0 {
  89. return m.cfg, errors.New("fft_size must be a power of 2")
  90. }
  91. next.FFTSize = *update.FFTSize
  92. }
  93. if update.GainDb != nil {
  94. next.GainDb = *update.GainDb
  95. }
  96. if update.TunerBwKHz != nil {
  97. if *update.TunerBwKHz <= 0 {
  98. return m.cfg, errors.New("tuner_bw_khz must be > 0")
  99. }
  100. next.TunerBwKHz = *update.TunerBwKHz
  101. }
  102. if update.UseGPUFFT != nil {
  103. next.UseGPUFFT = *update.UseGPUFFT
  104. }
  105. if update.Detector != nil {
  106. if update.Detector.ThresholdDb != nil {
  107. next.Detector.ThresholdDb = *update.Detector.ThresholdDb
  108. }
  109. if update.Detector.MinDuration != nil {
  110. if *update.Detector.MinDuration <= 0 {
  111. return m.cfg, errors.New("min_duration_ms must be > 0")
  112. }
  113. next.Detector.MinDurationMs = *update.Detector.MinDuration
  114. }
  115. if update.Detector.HoldMs != nil {
  116. if *update.Detector.HoldMs <= 0 {
  117. return m.cfg, errors.New("hold_ms must be > 0")
  118. }
  119. next.Detector.HoldMs = *update.Detector.HoldMs
  120. }
  121. if update.Detector.EmaAlpha != nil {
  122. next.Detector.EmaAlpha = *update.Detector.EmaAlpha
  123. }
  124. if update.Detector.HysteresisDb != nil {
  125. next.Detector.HysteresisDb = *update.Detector.HysteresisDb
  126. }
  127. if update.Detector.MinStableFrames != nil {
  128. next.Detector.MinStableFrames = *update.Detector.MinStableFrames
  129. }
  130. if update.Detector.GapToleranceMs != nil {
  131. next.Detector.GapToleranceMs = *update.Detector.GapToleranceMs
  132. }
  133. if update.Detector.CFAREnabled != nil {
  134. next.Detector.CFAREnabled = *update.Detector.CFAREnabled
  135. }
  136. if update.Detector.CFARGuardCells != nil {
  137. if *update.Detector.CFARGuardCells < 0 {
  138. return m.cfg, errors.New("cfar_guard_cells must be >= 0")
  139. }
  140. next.Detector.CFARGuardCells = *update.Detector.CFARGuardCells
  141. }
  142. if update.Detector.CFARTrainCells != nil {
  143. if *update.Detector.CFARTrainCells <= 0 {
  144. return m.cfg, errors.New("cfar_train_cells must be > 0")
  145. }
  146. next.Detector.CFARTrainCells = *update.Detector.CFARTrainCells
  147. }
  148. if update.Detector.CFARRank != nil {
  149. if *update.Detector.CFARRank <= 0 {
  150. return m.cfg, errors.New("cfar_rank must be > 0")
  151. }
  152. if next.Detector.CFARTrainCells > 0 && *update.Detector.CFARRank > 2*next.Detector.CFARTrainCells {
  153. return m.cfg, errors.New("cfar_rank must be <= 2 * cfar_train_cells")
  154. }
  155. next.Detector.CFARRank = *update.Detector.CFARRank
  156. }
  157. if update.Detector.CFARScaleDb != nil {
  158. next.Detector.CFARScaleDb = *update.Detector.CFARScaleDb
  159. }
  160. }
  161. if update.Recorder != nil {
  162. if update.Recorder.Enabled != nil {
  163. next.Recorder.Enabled = *update.Recorder.Enabled
  164. }
  165. if update.Recorder.MinSNRDb != nil {
  166. next.Recorder.MinSNRDb = *update.Recorder.MinSNRDb
  167. }
  168. if update.Recorder.MinDuration != nil {
  169. next.Recorder.MinDuration = *update.Recorder.MinDuration
  170. }
  171. if update.Recorder.MaxDuration != nil {
  172. next.Recorder.MaxDuration = *update.Recorder.MaxDuration
  173. }
  174. if update.Recorder.PrerollMs != nil {
  175. next.Recorder.PrerollMs = *update.Recorder.PrerollMs
  176. }
  177. if update.Recorder.RecordIQ != nil {
  178. next.Recorder.RecordIQ = *update.Recorder.RecordIQ
  179. }
  180. if update.Recorder.RecordAudio != nil {
  181. next.Recorder.RecordAudio = *update.Recorder.RecordAudio
  182. }
  183. if update.Recorder.AutoDemod != nil {
  184. next.Recorder.AutoDemod = *update.Recorder.AutoDemod
  185. }
  186. if update.Recorder.AutoDecode != nil {
  187. next.Recorder.AutoDecode = *update.Recorder.AutoDecode
  188. }
  189. if update.Recorder.MaxDiskMB != nil {
  190. next.Recorder.MaxDiskMB = *update.Recorder.MaxDiskMB
  191. }
  192. if update.Recorder.OutputDir != nil {
  193. next.Recorder.OutputDir = *update.Recorder.OutputDir
  194. }
  195. if update.Recorder.ClassFilter != nil {
  196. next.Recorder.ClassFilter = *update.Recorder.ClassFilter
  197. }
  198. if update.Recorder.RingSeconds != nil {
  199. next.Recorder.RingSeconds = *update.Recorder.RingSeconds
  200. }
  201. }
  202. m.cfg = next
  203. return m.cfg, nil
  204. }
  205. func (m *Manager) ApplySettings(update SettingsUpdate) (config.Config, error) {
  206. m.mu.Lock()
  207. defer m.mu.Unlock()
  208. next := m.cfg
  209. if update.AGC != nil {
  210. next.AGC = *update.AGC
  211. }
  212. if update.DCBlock != nil {
  213. next.DCBlock = *update.DCBlock
  214. }
  215. if update.IQBalance != nil {
  216. next.IQBalance = *update.IQBalance
  217. }
  218. m.cfg = next
  219. return m.cfg, nil
  220. }