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

293 рядки
8.7KB

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