選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

121 行
2.6KB

  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. Detector *DetectorUpdate `json:"detector"`
  14. }
  15. type DetectorUpdate struct {
  16. ThresholdDb *float64 `json:"threshold_db"`
  17. MinDuration *int `json:"min_duration_ms"`
  18. HoldMs *int `json:"hold_ms"`
  19. }
  20. type SettingsUpdate struct {
  21. AGC *bool `json:"agc"`
  22. DCBlock *bool `json:"dc_block"`
  23. IQBalance *bool `json:"iq_balance"`
  24. }
  25. type Manager struct {
  26. mu sync.RWMutex
  27. cfg config.Config
  28. }
  29. func New(cfg config.Config) *Manager {
  30. return &Manager{cfg: cfg}
  31. }
  32. func (m *Manager) Snapshot() config.Config {
  33. m.mu.RLock()
  34. defer m.mu.RUnlock()
  35. return m.cfg
  36. }
  37. func (m *Manager) Replace(cfg config.Config) {
  38. m.mu.Lock()
  39. defer m.mu.Unlock()
  40. m.cfg = cfg
  41. }
  42. func (m *Manager) ApplyConfig(update ConfigUpdate) (config.Config, error) {
  43. m.mu.Lock()
  44. defer m.mu.Unlock()
  45. next := m.cfg
  46. if update.CenterHz != nil {
  47. next.CenterHz = *update.CenterHz
  48. }
  49. if update.SampleRate != nil {
  50. if *update.SampleRate <= 0 {
  51. return m.cfg, errors.New("sample_rate must be > 0")
  52. }
  53. next.SampleRate = *update.SampleRate
  54. }
  55. if update.FFTSize != nil {
  56. if *update.FFTSize <= 0 {
  57. return m.cfg, errors.New("fft_size must be > 0")
  58. }
  59. next.FFTSize = *update.FFTSize
  60. }
  61. if update.GainDb != nil {
  62. next.GainDb = *update.GainDb
  63. }
  64. if update.TunerBwKHz != nil {
  65. if *update.TunerBwKHz <= 0 {
  66. return m.cfg, errors.New("tuner_bw_khz must be > 0")
  67. }
  68. next.TunerBwKHz = *update.TunerBwKHz
  69. }
  70. if update.Detector != nil {
  71. if update.Detector.ThresholdDb != nil {
  72. next.Detector.ThresholdDb = *update.Detector.ThresholdDb
  73. }
  74. if update.Detector.MinDuration != nil {
  75. if *update.Detector.MinDuration <= 0 {
  76. return m.cfg, errors.New("min_duration_ms must be > 0")
  77. }
  78. next.Detector.MinDurationMs = *update.Detector.MinDuration
  79. }
  80. if update.Detector.HoldMs != nil {
  81. if *update.Detector.HoldMs <= 0 {
  82. return m.cfg, errors.New("hold_ms must be > 0")
  83. }
  84. next.Detector.HoldMs = *update.Detector.HoldMs
  85. }
  86. }
  87. m.cfg = next
  88. return m.cfg, nil
  89. }
  90. func (m *Manager) ApplySettings(update SettingsUpdate) (config.Config, error) {
  91. m.mu.Lock()
  92. defer m.mu.Unlock()
  93. next := m.cfg
  94. if update.AGC != nil {
  95. next.AGC = *update.AGC
  96. }
  97. if update.DCBlock != nil {
  98. next.DCBlock = *update.DCBlock
  99. }
  100. if update.IQBalance != nil {
  101. next.IQBalance = *update.IQBalance
  102. }
  103. m.cfg = next
  104. return m.cfg, nil
  105. }