您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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