-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdcdbridge.go
122 lines (91 loc) · 2.79 KB
/
vdcdbridge.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"sync"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"github.com/splattner/vdcd-bridge/pkg/discovery"
"github.com/splattner/vdcd-bridge/pkg/vdcdapi"
)
type VcdcBridgeConfig struct {
host string
port int
modelName string
vendorName string
mqttHost string
mqttUsername string
mqttPassword string
deconzHost string
deconzPort int
deconcWebSockerPort int
deconzApi string
deconzEnableGroups bool
mqttDiscoveryEnabled bool
tasmotaDisabled bool
shellyDisabled bool
deconzDisabled bool
}
type VcdcBridge struct {
vdcdClient *vdcdapi.Client
mqttClient mqtt.Client
wg sync.WaitGroup
config VcdcBridgeConfig
}
func (e *VcdcBridge) NewVcdcBrige(config VcdcBridgeConfig) {
e.config = config
e.vdcdClient = new(vdcdapi.Client)
e.vdcdClient.NewCient(e.config.host, e.config.port, e.config.modelName, e.config.vendorName)
e.vdcdClient.Connect()
defer e.vdcdClient.Close()
// Configure MQTT Client if enabled
if config.mqttDiscoveryEnabled {
log.Infof("Connecting to MQTT Host: %s\n", config.mqttHost)
mqttBroker := fmt.Sprintf("tcp://%s", config.mqttHost)
opts := mqtt.NewClientOptions().AddBroker(mqttBroker).SetClientID("vdcd_cient")
opts.SetKeepAlive(60 * time.Second)
opts.SetPingTimeout(1 * time.Second)
opts.SetProtocolVersion(3)
opts.SetOrderMatters(false)
opts.SetUsername(config.mqttUsername)
opts.SetPassword(config.mqttPassword)
e.mqttClient = mqtt.NewClient(opts)
}
e.wg.Add(2)
go e.startDiscovery()
go e.loopVcdcClient()
log.Debugln("Waiting for Waitgroup")
e.wg.Wait()
log.Debugln("Waitgroup finished")
}
func (e *VcdcBridge) startDiscovery() {
log.Debugln("Start startDiscovery")
if e.config.mqttDiscoveryEnabled {
// Connect to MQTT Broker
if token := e.mqttClient.Connect(); token.Wait() && token.Error() != nil {
log.Error("MQTT connect failed: ", token.Error())
}
// Tasmota Device Discovery
if !e.config.tasmotaDisabled {
tasmotaDiscovery := new(discovery.TasmotaDevice)
tasmotaDiscovery.StartDiscovery(e.vdcdClient, e.mqttClient)
}
// Shelly Device Discovery
if !e.config.shellyDisabled {
shellyDiscovery := new(discovery.ShellyDevice)
shellyDiscovery.StartDiscovery(e.vdcdClient, e.mqttClient)
}
}
if !e.config.deconzDisabled {
deconzDiscovery := new(discovery.DeconzDevice)
deconzDiscovery.StartDiscovery(e.vdcdClient, e.config.deconzHost, e.config.deconzPort, e.config.deconcWebSockerPort, e.config.deconzApi, e.config.deconzEnableGroups)
}
log.Debugln("Calling Waitgroup done for startDiscovery")
e.wg.Done()
}
func (e *VcdcBridge) loopVcdcClient() {
log.Debugln("Start loopVcdcClient")
e.vdcdClient.Listen()
log.Debugln("Calling Waitgroup done for loopVcdcClient")
e.wg.Done()
}