-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (62 loc) · 1.58 KB
/
main.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"flag"
"log"
"github.com/cameliot/alpaca"
"github.com/cameliot/alpaca/meta"
)
var version = "unknown"
type Config struct {
Mqtt *MqttConfig
LichtCgiBase string
}
func parseFlags() *Config {
host := flag.String("host", "localhost:1883", "MQTT broker host")
user := flag.String("user", "", "MQTT broker host")
password := flag.String("password", "", "MQTT broker host")
baseTopic := flag.String("topic", "dali", "MQTT base topic")
lichtCgiBase := flag.String("lichtcgi", "http://dali", "licht.cgi server")
flag.Parse()
mqttConfig := &MqttConfig{
Host: *host,
User: *user,
Password: *password,
BaseTopic: *baseTopic,
}
config := &Config{
Mqtt: mqttConfig,
LichtCgiBase: *lichtCgiBase,
}
return config
}
func main() {
log.Println("Starting dali to mqtt bridge v.", version)
// Initialize configuration
config := parseFlags()
// Initialize MQTT connection
actions, dispatch := alpaca.DialMqtt(
config.Mqtt.BrokerUri(),
alpaca.Routes{
"lights": config.Mqtt.BaseTopic,
"meta": "v1/_meta",
},
)
lightsActions := make(alpaca.Actions)
metaActions := make(alpaca.Actions)
// So far so good. Let now the lights service
// handle actions
lightsSvc := NewLightsSvc(config.LichtCgiBase)
go lightsSvc.Handle(lightsActions, dispatch)
// Hanlde meta actions for service discovery
metaSvc := meta.NewMetaSvc(
"daliqtt@mainhall",
"daliqtt",
version,
"Bridge Dali / Lights to MQTT",
)
go metaSvc.Handle(metaActions, dispatch)
for action := range actions {
lightsActions <- action
metaActions <- action
}
}