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

package main
import (
"strings"
"time"
"github.com/BurntSushi/toml"
"maze.io/x/esphome"
)
type Config struct {
// Listen is the server listen address.
Listen string `toml:"listen"`
// Static files path.
Static string `toml:"static"`
// Template files path.
Template string `toml:"template"`
// Scan configuration.
Scan ScanConfig `toml:"scan"`
// Secret is a map of device name to native API secret.
Secret map[string]string `toml:"secret"`
// DefaultSecret is the default native API secret.
DefaultSecret string `toml:"default_secret"`
}
type ScanConfig struct {
Domain string
Service string
Interval time.Duration
Timeout time.Duration
}
func DefaultConfig() *Config {
return &Config{
Listen: DefaultServerListen,
Static: DefaultServerStatic,
Template: DefaultServerTemplate,
Scan: ScanConfig{
Domain: esphome.DefaultMDNSDomain,
Service: esphome.DefaultMDNSService,
Interval: 15 * time.Second,
Timeout: 10 * time.Second,
},
Secret: make(map[string]string),
}
}
func (c *Config) Load(name string) error {
if _, err := toml.DecodeFile(name, c); err != nil {
return err
}
/*
c.Listen = pick(c.Listen, DefaultServerListen)
c.Static = pick(c.Static, DefaultServerStatic)
c.Template = pick(c.Template, DefaultServerTemplate)
c.Scan.Domain = pick(c.Scan.Domain, esphome.DefaultMDNSDomain)
c.Scan.Service = pick(c.Scan.Service, esphome.DefaultMDNSService)
c.Scan.Timeout = pickDuration(c.Scan.Timeout, 10*time.Second)
c.Scan.Interval = pickDuration(c.Scan.Interval, 15*time.Second)
*/
if c.Scan.Timeout >= c.Scan.Interval {
c.Scan.Timeout = c.Scan.Interval - time.Second
}
return nil
}
// GetSecret returns the native API secret for a device.
func (c *Config) GetSecret(hostname string) string {
return pick(c.Secret[strings.ToLower(hostname)], c.DefaultSecret)
}
func pick(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}
func pickDuration(values ...time.Duration) time.Duration {
for _, v := range values {
if v != 0 {
return v
}
}
return 0
}