Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

109 Zeilen
2.8KB

  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 Config struct {
  18. Bands []Band `yaml:"bands" json:"bands"`
  19. CenterHz float64 `yaml:"center_hz" json:"center_hz"`
  20. SampleRate int `yaml:"sample_rate" json:"sample_rate"`
  21. FFTSize int `yaml:"fft_size" json:"fft_size"`
  22. GainDb float64 `yaml:"gain_db" json:"gain_db"`
  23. AGC bool `yaml:"agc" json:"agc"`
  24. DCBlock bool `yaml:"dc_block" json:"dc_block"`
  25. IQBalance bool `yaml:"iq_balance" json:"iq_balance"`
  26. Detector DetectorConfig `yaml:"detector" json:"detector"`
  27. WebAddr string `yaml:"web_addr" json:"web_addr"`
  28. EventPath string `yaml:"event_path" json:"event_path"`
  29. FrameRate int `yaml:"frame_rate" json:"frame_rate"`
  30. WaterfallLines int `yaml:"waterfall_lines" json:"waterfall_lines"`
  31. WebRoot string `yaml:"web_root" json:"web_root"`
  32. }
  33. func Default() Config {
  34. return Config{
  35. Bands: []Band{
  36. {Name: "example", StartHz: 99.5e6, EndHz: 100.5e6},
  37. },
  38. CenterHz: 100.0e6,
  39. SampleRate: 2_048_000,
  40. FFTSize: 2048,
  41. GainDb: 30,
  42. AGC: false,
  43. DCBlock: false,
  44. IQBalance: false,
  45. Detector: DetectorConfig{ThresholdDb: -20, MinDurationMs: 250, HoldMs: 500},
  46. WebAddr: ":8080",
  47. EventPath: "data/events.jsonl",
  48. FrameRate: 15,
  49. WaterfallLines: 200,
  50. WebRoot: "web",
  51. }
  52. }
  53. func Load(path string) (Config, error) {
  54. cfg := Default()
  55. b, err := os.ReadFile(path)
  56. if err != nil {
  57. return cfg, err
  58. }
  59. if err := yaml.Unmarshal(b, &cfg); err != nil {
  60. return cfg, err
  61. }
  62. if cfg.Detector.MinDurationMs <= 0 {
  63. cfg.Detector.MinDurationMs = 250
  64. }
  65. if cfg.Detector.HoldMs <= 0 {
  66. cfg.Detector.HoldMs = 500
  67. }
  68. if cfg.FrameRate <= 0 {
  69. cfg.FrameRate = 15
  70. }
  71. if cfg.WaterfallLines <= 0 {
  72. cfg.WaterfallLines = 200
  73. }
  74. if cfg.WebRoot == "" {
  75. cfg.WebRoot = "web"
  76. }
  77. if cfg.WebAddr == "" {
  78. cfg.WebAddr = ":8080"
  79. }
  80. if cfg.EventPath == "" {
  81. cfg.EventPath = "data/events.jsonl"
  82. }
  83. if cfg.SampleRate <= 0 {
  84. cfg.SampleRate = 2_048_000
  85. }
  86. if cfg.FFTSize <= 0 {
  87. cfg.FFTSize = 2048
  88. }
  89. if cfg.CenterHz == 0 {
  90. cfg.CenterHz = 100.0e6
  91. }
  92. return cfg, nil
  93. }
  94. func (c Config) FrameInterval() time.Duration {
  95. fps := c.FrameRate
  96. if fps <= 0 {
  97. fps = 15
  98. }
  99. return time.Second / time.Duration(fps)
  100. }