Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

263 рядки
7.6KB

  1. package runtime
  2. import (
  3. "errors"
  4. "math"
  5. "strings"
  6. "sync"
  7. "sdr-visual-suite/internal/config"
  8. )
  9. type ConfigUpdate struct {
  10. CenterHz *float64 `json:"center_hz"`
  11. SampleRate *int `json:"sample_rate"`
  12. FFTSize *int `json:"fft_size"`
  13. GainDb *float64 `json:"gain_db"`
  14. TunerBwKHz *int `json:"tuner_bw_khz"`
  15. UseGPUFFT *bool `json:"use_gpu_fft"`
  16. Detector *DetectorUpdate `json:"detector"`
  17. Recorder *RecorderUpdate `json:"recorder"`
  18. }
  19. type DetectorUpdate struct {
  20. ThresholdDb *float64 `json:"threshold_db"`
  21. MinDuration *int `json:"min_duration_ms"`
  22. HoldMs *int `json:"hold_ms"`
  23. EmaAlpha *float64 `json:"ema_alpha"`
  24. HysteresisDb *float64 `json:"hysteresis_db"`
  25. MinStableFrames *int `json:"min_stable_frames"`
  26. GapToleranceMs *int `json:"gap_tolerance_ms"`
  27. CFARMode *string `json:"cfar_mode"`
  28. CFARGuardCells *int `json:"cfar_guard_cells"`
  29. CFARTrainCells *int `json:"cfar_train_cells"`
  30. CFARRank *int `json:"cfar_rank"`
  31. CFARScaleDb *float64 `json:"cfar_scale_db"`
  32. CFARWrapAround *bool `json:"cfar_wrap_around"`
  33. }
  34. type SettingsUpdate struct {
  35. AGC *bool `json:"agc"`
  36. DCBlock *bool `json:"dc_block"`
  37. IQBalance *bool `json:"iq_balance"`
  38. }
  39. type RecorderUpdate struct {
  40. Enabled *bool `json:"enabled"`
  41. MinSNRDb *float64 `json:"min_snr_db"`
  42. MinDuration *string `json:"min_duration"`
  43. MaxDuration *string `json:"max_duration"`
  44. PrerollMs *int `json:"preroll_ms"`
  45. RecordIQ *bool `json:"record_iq"`
  46. RecordAudio *bool `json:"record_audio"`
  47. AutoDemod *bool `json:"auto_demod"`
  48. AutoDecode *bool `json:"auto_decode"`
  49. MaxDiskMB *int `json:"max_disk_mb"`
  50. OutputDir *string `json:"output_dir"`
  51. ClassFilter *[]string `json:"class_filter"`
  52. RingSeconds *int `json:"ring_seconds"`
  53. }
  54. type Manager struct {
  55. mu sync.RWMutex
  56. cfg config.Config
  57. }
  58. func New(cfg config.Config) *Manager {
  59. return &Manager{cfg: cfg}
  60. }
  61. func (m *Manager) Snapshot() config.Config {
  62. m.mu.RLock()
  63. defer m.mu.RUnlock()
  64. return m.cfg
  65. }
  66. func (m *Manager) Replace(cfg config.Config) {
  67. m.mu.Lock()
  68. defer m.mu.Unlock()
  69. m.cfg = cfg
  70. }
  71. func (m *Manager) ApplyConfig(update ConfigUpdate) (config.Config, error) {
  72. m.mu.Lock()
  73. defer m.mu.Unlock()
  74. next := m.cfg
  75. if update.CenterHz != nil {
  76. if *update.CenterHz < 1e3 || *update.CenterHz > 2e9 {
  77. return m.cfg, errors.New("center_hz out of range")
  78. }
  79. next.CenterHz = *update.CenterHz
  80. }
  81. if update.SampleRate != nil {
  82. if *update.SampleRate <= 0 {
  83. return m.cfg, errors.New("sample_rate must be > 0")
  84. }
  85. next.SampleRate = *update.SampleRate
  86. }
  87. if update.FFTSize != nil {
  88. if *update.FFTSize <= 0 {
  89. return m.cfg, errors.New("fft_size must be > 0")
  90. }
  91. if *update.FFTSize&(*update.FFTSize-1) != 0 {
  92. return m.cfg, errors.New("fft_size must be a power of 2")
  93. }
  94. next.FFTSize = *update.FFTSize
  95. }
  96. if update.GainDb != nil {
  97. next.GainDb = *update.GainDb
  98. }
  99. if update.TunerBwKHz != nil {
  100. if *update.TunerBwKHz <= 0 {
  101. return m.cfg, errors.New("tuner_bw_khz must be > 0")
  102. }
  103. next.TunerBwKHz = *update.TunerBwKHz
  104. }
  105. if update.UseGPUFFT != nil {
  106. next.UseGPUFFT = *update.UseGPUFFT
  107. }
  108. if update.Detector != nil {
  109. if update.Detector.ThresholdDb != nil {
  110. next.Detector.ThresholdDb = *update.Detector.ThresholdDb
  111. }
  112. if update.Detector.MinDuration != nil {
  113. if *update.Detector.MinDuration <= 0 {
  114. return m.cfg, errors.New("min_duration_ms must be > 0")
  115. }
  116. next.Detector.MinDurationMs = *update.Detector.MinDuration
  117. }
  118. if update.Detector.HoldMs != nil {
  119. if *update.Detector.HoldMs <= 0 {
  120. return m.cfg, errors.New("hold_ms must be > 0")
  121. }
  122. next.Detector.HoldMs = *update.Detector.HoldMs
  123. }
  124. if update.Detector.EmaAlpha != nil {
  125. v := *update.Detector.EmaAlpha
  126. if math.IsNaN(v) || math.IsInf(v, 0) || v < 0 || v > 1 {
  127. return m.cfg, errors.New("ema_alpha must be between 0 and 1")
  128. }
  129. next.Detector.EmaAlpha = v
  130. }
  131. if update.Detector.HysteresisDb != nil {
  132. v := *update.Detector.HysteresisDb
  133. if math.IsNaN(v) || math.IsInf(v, 0) || v < 0 {
  134. return m.cfg, errors.New("hysteresis_db must be >= 0")
  135. }
  136. next.Detector.HysteresisDb = v
  137. }
  138. if update.Detector.MinStableFrames != nil {
  139. if *update.Detector.MinStableFrames < 1 {
  140. return m.cfg, errors.New("min_stable_frames must be >= 1")
  141. }
  142. next.Detector.MinStableFrames = *update.Detector.MinStableFrames
  143. }
  144. if update.Detector.GapToleranceMs != nil {
  145. if *update.Detector.GapToleranceMs < 0 {
  146. return m.cfg, errors.New("gap_tolerance_ms must be >= 0")
  147. }
  148. next.Detector.GapToleranceMs = *update.Detector.GapToleranceMs
  149. }
  150. if update.Detector.CFARMode != nil {
  151. mode := strings.ToUpper(strings.TrimSpace(*update.Detector.CFARMode))
  152. switch mode {
  153. case "OFF", "CA", "OS", "GOSCA", "CASO":
  154. next.Detector.CFARMode = mode
  155. default:
  156. return m.cfg, errors.New("cfar_mode must be OFF, CA, OS, GOSCA, or CASO")
  157. }
  158. }
  159. if update.Detector.CFARWrapAround != nil {
  160. next.Detector.CFARWrapAround = *update.Detector.CFARWrapAround
  161. }
  162. if update.Detector.CFARGuardCells != nil {
  163. if *update.Detector.CFARGuardCells < 0 {
  164. return m.cfg, errors.New("cfar_guard_cells must be >= 0")
  165. }
  166. next.Detector.CFARGuardCells = *update.Detector.CFARGuardCells
  167. }
  168. if update.Detector.CFARTrainCells != nil {
  169. if *update.Detector.CFARTrainCells <= 0 {
  170. return m.cfg, errors.New("cfar_train_cells must be > 0")
  171. }
  172. next.Detector.CFARTrainCells = *update.Detector.CFARTrainCells
  173. }
  174. if update.Detector.CFARRank != nil {
  175. if *update.Detector.CFARRank <= 0 {
  176. return m.cfg, errors.New("cfar_rank must be > 0")
  177. }
  178. if next.Detector.CFARTrainCells > 0 && *update.Detector.CFARRank > 2*next.Detector.CFARTrainCells {
  179. return m.cfg, errors.New("cfar_rank must be <= 2 * cfar_train_cells")
  180. }
  181. next.Detector.CFARRank = *update.Detector.CFARRank
  182. }
  183. if update.Detector.CFARScaleDb != nil {
  184. next.Detector.CFARScaleDb = *update.Detector.CFARScaleDb
  185. }
  186. }
  187. if update.Recorder != nil {
  188. if update.Recorder.Enabled != nil {
  189. next.Recorder.Enabled = *update.Recorder.Enabled
  190. }
  191. if update.Recorder.MinSNRDb != nil {
  192. next.Recorder.MinSNRDb = *update.Recorder.MinSNRDb
  193. }
  194. if update.Recorder.MinDuration != nil {
  195. next.Recorder.MinDuration = *update.Recorder.MinDuration
  196. }
  197. if update.Recorder.MaxDuration != nil {
  198. next.Recorder.MaxDuration = *update.Recorder.MaxDuration
  199. }
  200. if update.Recorder.PrerollMs != nil {
  201. next.Recorder.PrerollMs = *update.Recorder.PrerollMs
  202. }
  203. if update.Recorder.RecordIQ != nil {
  204. next.Recorder.RecordIQ = *update.Recorder.RecordIQ
  205. }
  206. if update.Recorder.RecordAudio != nil {
  207. next.Recorder.RecordAudio = *update.Recorder.RecordAudio
  208. }
  209. if update.Recorder.AutoDemod != nil {
  210. next.Recorder.AutoDemod = *update.Recorder.AutoDemod
  211. }
  212. if update.Recorder.AutoDecode != nil {
  213. next.Recorder.AutoDecode = *update.Recorder.AutoDecode
  214. }
  215. if update.Recorder.MaxDiskMB != nil {
  216. next.Recorder.MaxDiskMB = *update.Recorder.MaxDiskMB
  217. }
  218. if update.Recorder.OutputDir != nil {
  219. next.Recorder.OutputDir = *update.Recorder.OutputDir
  220. }
  221. if update.Recorder.ClassFilter != nil {
  222. next.Recorder.ClassFilter = *update.Recorder.ClassFilter
  223. }
  224. if update.Recorder.RingSeconds != nil {
  225. next.Recorder.RingSeconds = *update.Recorder.RingSeconds
  226. }
  227. }
  228. m.cfg = next
  229. return m.cfg, nil
  230. }
  231. func (m *Manager) ApplySettings(update SettingsUpdate) (config.Config, error) {
  232. m.mu.Lock()
  233. defer m.mu.Unlock()
  234. next := m.cfg
  235. if update.AGC != nil {
  236. next.AGC = *update.AGC
  237. }
  238. if update.DCBlock != nil {
  239. next.DCBlock = *update.DCBlock
  240. }
  241. if update.IQBalance != nil {
  242. next.IQBalance = *update.IQBalance
  243. }
  244. m.cfg = next
  245. return m.cfg, nil
  246. }