-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (48 loc) · 1.45 KB
/
main.go
File metadata and controls
57 lines (48 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"alpaca-switch/backend"
"alpaca-switch/backend/hikvision"
"alpaca-switch/backend/mi"
"alpaca-switch/server"
)
// Config is the unified configuration file format.
type Config struct {
AlpacaPort int `json:"alpaca_port"`
MiDevices []mi.Device `json:"mi_devices"`
HikvisionCameras []hikvision.CameraConfig `json:"hikvision_cameras"`
}
func loadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading %s: %w", path, err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing %s: %w", path, err)
}
if cfg.AlpacaPort == 0 {
cfg.AlpacaPort = 11111
}
return &cfg, nil
}
func main() {
cfg, err := loadConfig("config/settings.json")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Build backends
miBackend := mi.New(cfg.MiDevices, "")
hikBackend := hikvision.New(cfg.HikvisionCameras)
// Build router (Mi switches first, then Hikvision)
router := backend.NewRouter([]backend.SwitchBackend{miBackend, hikBackend})
log.Printf("alpaca-switch starting: %d total switches (%d Mi + %d Hikvision)",
router.NumSwitches(), miBackend.NumSwitches(), hikBackend.NumSwitches())
// Start discovery and API
go server.StartDiscovery(32227, cfg.AlpacaPort)
srv := server.New(router)
srv.Start(fmt.Sprintf(":%d", cfg.AlpacaPort))
}