-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (118 loc) · 4.64 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"crypto/tls"
"flag"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/feranydev/gree_havc_mqtt_bridge_go/app"
"github.com/feranydev/gree_havc_mqtt_bridge_go/config"
"github.com/labstack/gommon/log"
"net"
"os"
)
func create() *config.Config {
c := &config.Config{}
c.Gree = make([]config.Gree, 1)
var ipStr string
flag.StringVar(&ipStr, "hvac-host", "", "Device IP Address")
flag.IntVar(&c.Gree[0].Port, "hvac-port", 7000, "Device Port")
flag.StringVar(&c.Mqtt.Havc.Host, "mqtt-broker-url", "", "MQTT Broker URL")
flag.IntVar(&c.Mqtt.Havc.Port, "mqtt-broker-port", 1883, "MQTT Broker Port")
flag.StringVar(&c.Mqtt.Havc.Username, "mqtt-username", "admin", "MQTT User")
flag.StringVar(&c.Mqtt.Havc.Password, "mqtt-password", "admin", "MQTT Password")
flag.BoolVar(&c.Mqtt.Havc.Retain, "mqtt-retain", false, "MQTT Retain")
flag.StringVar(&c.Gree[0].HavcTopic, "mqtt-topic-prefix", "home/greehvac", "MQTT Topic Prefix")
flag.StringVar(&c.Mqtt.Bemfa.Host, "bemfa-broker-url", "bemfa.com", "BEMFA Broker URL")
flag.IntVar(&c.Mqtt.Bemfa.Port, "bemfa-broker-port", 9503, "BEMFA Broker Port")
flag.StringVar(&c.Mqtt.Bemfa.ClientID, "bemfa-client-id", "", "BEMFA Client ID")
flag.BoolVar(&c.Mqtt.Bemfa.Retain, "bemfa-retain", false, "BEMFA Retain")
flag.BoolVar(&c.Mqtt.Bemfa.Tls, "bemfa-tls", true, "BEMFA TLS")
flag.StringVar(&c.Gree[0].BemfaTopic, "bemfa-topic", "", "BEMFA Topic")
var configPath string
flag.StringVar(&configPath, "c", "", "Config Path")
flag.Parse()
if (ipStr == "" && (c.Mqtt.Havc.Host == "" || (c.Mqtt.Bemfa.ClientID == "" && c.Gree[0].BemfaTopic == ""))) && configPath == "" {
flag.PrintDefaults()
os.Exit(1)
}
if configPath != "" {
c = config.GetConfig(configPath)
} else {
c.Gree[0].Host = net.ParseIP(ipStr)
}
return c
}
func main() {
log.SetLevel(log.DEBUG)
c := create()
var mqttClient mqtt.Client
var bemfaClient mqtt.Client
var clients []*app.AppOptions
reconnect := func(client mqtt.Client, options *mqtt.ClientOptions) {
log.Debugf("[MQTT] Reconnecting to %s:%d", c.Mqtt.Havc.Host, c.Mqtt.Havc.Port)
for _, client := range clients {
go client.OnConnected()
}
}
if c.Mqtt.Havc.Host != "" {
mqttOpts := mqtt.NewClientOptions()
mqttOpts.SetUsername(c.Mqtt.Havc.Username)
mqttOpts.SetPassword(c.Mqtt.Havc.Password)
mqttOpts.SetCleanSession(true)
if c.Mqtt.Havc.ClientID != "" {
mqttOpts.ClientID = c.Mqtt.Havc.ClientID
}
mqttOpts.SetDefaultPublishHandler(func(client mqtt.Client, message mqtt.Message) {
log.Infof("[MQTT]Received message on topic %s: %s", message.Topic(), message.Payload())
})
mqttOpts.SetReconnectingHandler(reconnect)
if c.Mqtt.Havc.Tls {
mqttOpts.SetTLSConfig(&tls.Config{InsecureSkipVerify: false})
mqttOpts.AddBroker(fmt.Sprintf("mqtts://%s:%d", c.Mqtt.Havc.Host, c.Mqtt.Havc.Port))
} else {
mqttOpts.AddBroker(fmt.Sprintf("mqtt://%s:%d", c.Mqtt.Havc.Host, c.Mqtt.Havc.Port))
}
mqttOpts.OnConnect = func(client mqtt.Client) {
log.Infof("[MQTT] Connected to %s:%d", c.Mqtt.Havc.Host, c.Mqtt.Havc.Port)
}
mqttOpts.OnConnectionLost = func(client mqtt.Client, err error) {
log.Infof("[MQTT] Connection lost to %s:%d", c.Mqtt.Havc.Host, c.Mqtt.Havc.Port)
}
mqttClient = mqtt.NewClient(mqttOpts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
if c.Mqtt.Bemfa.ClientID != "" {
bemfaOpts := mqtt.NewClientOptions()
if c.Mqtt.Bemfa.Tls {
bemfaOpts.SetTLSConfig(&tls.Config{InsecureSkipVerify: false})
bemfaOpts.AddBroker(fmt.Sprintf("mqtts://%s:%d", c.Mqtt.Bemfa.Host, c.Mqtt.Bemfa.Port))
} else {
bemfaOpts.AddBroker(fmt.Sprintf("mqtt://%s:%d", c.Mqtt.Bemfa.Host, c.Mqtt.Bemfa.Port))
}
bemfaOpts.SetCleanSession(true)
bemfaOpts.SetDefaultPublishHandler(func(client mqtt.Client, message mqtt.Message) {
log.Infof("[BEMFA]Received message on topic %s: %s", message.Topic(), message.Payload())
})
bemfaOpts.SetClientID(c.Mqtt.Bemfa.ClientID)
bemfaOpts.SetReconnectingHandler(reconnect)
bemfaOpts.OnConnect = func(client mqtt.Client) {
log.Infof("[BEMFA] Connected to %s:%d", c.Mqtt.Bemfa.Host, c.Mqtt.Bemfa.Port)
}
bemfaOpts.OnConnectionLost = func(client mqtt.Client, err error) {
log.Infof("[BEMFA] Connection lost to %s:%d", c.Mqtt.Bemfa.Host, c.Mqtt.Bemfa.Port)
}
bemfaClient = mqtt.NewClient(bemfaOpts)
if token := bemfaClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
for i, gree := range c.Gree {
log.Infof("[DEVICE:%d] Connecting to %s:%d", i+1, gree.Host, gree.Port)
client := app.Create()
client.Start(mqttClient, bemfaClient, &gree)
clients = append(clients, client)
}
select {}
}