25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

145 satır
4.0KB

  1. package config
  2. import (
  3. "os"
  4. "time"
  5. "gopkg.in/yaml.v3"
  6. )
  7. type Band struct {
  8. Name string `yaml:"name" json:"name"`
  9. StartHz float64 `yaml:"start_hz" json:"start_hz"`
  10. EndHz float64 `yaml:"end_hz" json:"end_hz"`
  11. }
  12. type DetectorConfig struct {
  13. ThresholdDb float64 `yaml:"threshold_db" json:"threshold_db"`
  14. MinDurationMs int `yaml:"min_duration_ms" json:"min_duration_ms"`
  15. HoldMs int `yaml:"hold_ms" json:"hold_ms"`
  16. }
  17. type RecorderConfig struct {
  18. Enabled bool `yaml:"enabled" json:"enabled"`
  19. MinSNRDb float64 `yaml:"min_snr_db" json:"min_snr_db"`
  20. MinDuration string `yaml:"min_duration" json:"min_duration"`
  21. MaxDuration string `yaml:"max_duration" json:"max_duration"`
  22. PrerollMs int `yaml:"preroll_ms" json:"preroll_ms"`
  23. RecordIQ bool `yaml:"record_iq" json:"record_iq"`
  24. OutputDir string `yaml:"output_dir" json:"output_dir"`
  25. ClassFilter []string `yaml:"class_filter" json:"class_filter"`
  26. RingSeconds int `yaml:"ring_seconds" json:"ring_seconds"`
  27. }
  28. type Config struct {
  29. Bands []Band `yaml:"bands" json:"bands"`
  30. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  31. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  32. FFTSize int `yaml:"fft_size" json:"fft_size"`
  33. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  34. TunerBwKHz int `yaml:"tuner_bw_khz" json:"tuner_bw_khz"`
  35. UseGPUFFT bool `yaml:"use_gpu_fft" json:"use_gpu_fft"`
  36. AGC bool `yaml:"agc" json:"agc"`
  37. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  38. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  39. Detector DetectorConfig `yaml:"detector" json:"detector"`
  40. Recorder RecorderConfig `yaml:"recorder" json:"recorder"`
  41. WebAddr string `yaml:"web_addr" json:"web_addr"`
  42. EventPath string `yaml:"event_path" json:"event_path"`
  43. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  44. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  45. WebRoot string `yaml:"web_root" json:"web_root"`
  46. }
  47. func Default() Config {
  48. return Config{
  49. Bands: []Band{
  50. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  51. },
  52. CenterHz: 100.0e6,
  53. SampleRate: 2_048_000,
  54. FFTSize: 2048,
  55. GainDb: 30,
  56. TunerBwKHz: 1536,
  57. UseGPUFFT: false,
  58. AGC: false,
  59. DCBlock: false,
  60. IQBalance: false,
  61. Detector: DetectorConfig{ThresholdDb: -20, MinDurationMs: 250, HoldMs: 500},
  62. Recorder: RecorderConfig{
  63. Enabled: false,
  64. MinSNRDb: 10,
  65. MinDuration: "1s",
  66. MaxDuration: "300s",
  67. PrerollMs: 500,
  68. RecordIQ: true,
  69. OutputDir: "data/recordings",
  70. RingSeconds: 8,
  71. },
  72. WebAddr: ":8080",
  73. EventPath: "data/events.jsonl",
  74. FrameRate: 15,
  75. WaterfallLines: 200,
  76. WebRoot: "web",
  77. }
  78. }
  79. func Load(path string) (Config, error) {
  80. cfg := Default()
  81. b, err := os.ReadFile(path)
  82. if err != nil {
  83. return cfg, err
  84. }
  85. if err := yaml.Unmarshal(b, &cfg); err != nil {
  86. return cfg, err
  87. }
  88. if cfg.Detector.MinDurationMs <= 0 {
  89. cfg.Detector.MinDurationMs = 250
  90. }
  91. if cfg.Detector.HoldMs <= 0 {
  92. cfg.Detector.HoldMs = 500
  93. }
  94. if cfg.FrameRate <= 0 {
  95. cfg.FrameRate = 15
  96. }
  97. if cfg.WaterfallLines <= 0 {
  98. cfg.WaterfallLines = 200
  99. }
  100. if cfg.WebRoot == "" {
  101. cfg.WebRoot = "web"
  102. }
  103. if cfg.WebAddr == "" {
  104. cfg.WebAddr = ":8080"
  105. }
  106. if cfg.EventPath == "" {
  107. cfg.EventPath = "data/events.jsonl"
  108. }
  109. if cfg.SampleRate <= 0 {
  110. cfg.SampleRate = 2_048_000
  111. }
  112. if cfg.FFTSize <= 0 {
  113. cfg.FFTSize = 2048
  114. }
  115. if cfg.TunerBwKHz <= 0 {
  116. cfg.TunerBwKHz = 1536
  117. }
  118. if cfg.CenterHz == 0 {
  119. cfg.CenterHz = 100.0e6
  120. }
  121. if cfg.Recorder.OutputDir == "" {
  122. cfg.Recorder.OutputDir = "data/recordings"
  123. }
  124. if cfg.Recorder.RingSeconds <= 0 {
  125. cfg.Recorder.RingSeconds = 8
  126. }
  127. return cfg, nil
  128. }
  129. func (c Config) FrameInterval() time.Duration {
  130. fps := c.FrameRate
  131. if fps <= 0 {
  132. fps = 15
  133. }
  134. return time.Second / time.Duration(fps)
  135. }