Simple MJPEG streamer for ESPHome cameras.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.1 KiB

2 years ago
  1. package main
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/BurntSushi/toml"
  6. "maze.io/x/esphome"
  7. )
  8. type Config struct {
  9. // Listen is the server listen address.
  10. Listen string `toml:"listen"`
  11. // Static files path.
  12. Static string `toml:"static"`
  13. // Template files path.
  14. Template string `toml:"template"`
  15. // Scan configuration.
  16. Scan ScanConfig `toml:"scan"`
  17. // Secret is a map of device name to native API secret.
  18. Secret map[string]string `toml:"secret"`
  19. // DefaultSecret is the default native API secret.
  20. DefaultSecret string `toml:"default_secret"`
  21. }
  22. type ScanConfig struct {
  23. Domain string
  24. Service string
  25. Interval time.Duration
  26. Timeout time.Duration
  27. }
  28. func DefaultConfig() *Config {
  29. return &Config{
  30. Listen: DefaultServerListen,
  31. Static: DefaultServerStatic,
  32. Template: DefaultServerTemplate,
  33. Scan: ScanConfig{
  34. Domain: esphome.DefaultMDNSDomain,
  35. Service: esphome.DefaultMDNSService,
  36. Interval: 15 * time.Second,
  37. Timeout: 10 * time.Second,
  38. },
  39. Secret: make(map[string]string),
  40. }
  41. }
  42. func (c *Config) Load(name string) error {
  43. if _, err := toml.DecodeFile(name, c); err != nil {
  44. return err
  45. }
  46. /*
  47. c.Listen = pick(c.Listen, DefaultServerListen)
  48. c.Static = pick(c.Static, DefaultServerStatic)
  49. c.Template = pick(c.Template, DefaultServerTemplate)
  50. c.Scan.Domain = pick(c.Scan.Domain, esphome.DefaultMDNSDomain)
  51. c.Scan.Service = pick(c.Scan.Service, esphome.DefaultMDNSService)
  52. c.Scan.Timeout = pickDuration(c.Scan.Timeout, 10*time.Second)
  53. c.Scan.Interval = pickDuration(c.Scan.Interval, 15*time.Second)
  54. */
  55. if c.Scan.Timeout >= c.Scan.Interval {
  56. c.Scan.Timeout = c.Scan.Interval - time.Second
  57. }
  58. return nil
  59. }
  60. // GetSecret returns the native API secret for a device.
  61. func (c *Config) GetSecret(hostname string) string {
  62. return pick(c.Secret[strings.ToLower(hostname)], c.DefaultSecret)
  63. }
  64. func pick(values ...string) string {
  65. for _, v := range values {
  66. if v != "" {
  67. return v
  68. }
  69. }
  70. return ""
  71. }
  72. func pickDuration(values ...time.Duration) time.Duration {
  73. for _, v := range values {
  74. if v != 0 {
  75. return v
  76. }
  77. }
  78. return 0
  79. }