From 72a5927f1e07ffb631e6dc4dd1c9981f34c47b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sat, 24 Aug 2024 11:56:32 +0200 Subject: [PATCH] Initial MQTT-related first steps --- config.go | 8 ++++++++ go.mod | 11 +++++++++-- go.sum | 19 +++++++++---------- mqtt_worker.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 mqtt_worker.go diff --git a/config.go b/config.go index fbb2f3f..902db1f 100644 --- a/config.go +++ b/config.go @@ -25,6 +25,13 @@ type SatelChangeType struct { changeType satel.ChangeType } +type MqttConfig struct { + Enabled bool `yaml:"mqtt-enabled"` + BrokerUri string `yaml:"mqtt-broker-uri"` + Username string `yaml:"mqtt-username"` + Password string `yaml:"mqtt-password"` +} + type AppConfig struct { SatelAddr string `yaml:"satel-addr"` ChatIds []int64 `yaml:"tg-chat-ids"` @@ -35,6 +42,7 @@ type AppConfig struct { DisarmCallbackUrls []string `yaml:"disarm-callback-urls"` AlarmCallbackUrls []string `yaml:"alarm-callback-urls"` WriteMemoryProfile bool `yaml:"write-memory-profile"` + MqttConfiguration MqttConfig `yaml:"mqtt"` } func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error { diff --git a/go.mod b/go.mod index 5cb33e2..a44b8c8 100644 --- a/go.mod +++ b/go.mod @@ -6,13 +6,20 @@ require ( git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/stretchr/testify v1.8.4 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/gorilla/websocket v1.5.3 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/sync v0.7.0 // indirect ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/text v0.14.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + golang.org/x/text v0.16.0 // indirect ) replace github.com/probakowski/go-satel => git.sr.ht/~michalr/go-satel v0.0.0-20211120120346-bed9818777ce diff --git a/go.sum b/go.sum index 3cc4e40..d77e520 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,14 @@ -git.sr.ht/~michalr/go-satel v0.0.0-20240303214713-fcbf3bd29f6f h1:+jaUKR+4mKlJASOlLBfHxFr4qXfhubYbBM37fepIJug= -git.sr.ht/~michalr/go-satel v0.0.0-20240303214713-fcbf3bd29f6f/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= -git.sr.ht/~michalr/go-satel v0.0.0-20240304192450-999413ca9994 h1:uIDVu4eIJKT4AE0cckgId143xR8vlKFKSVBfQFKPHRg= -git.sr.ht/~michalr/go-satel v0.0.0-20240304192450-999413ca9994/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= -git.sr.ht/~michalr/go-satel v0.0.0-20240304210948-c93d9db530fe h1:huAwK9SgnCqgrbEn5p2nBvkoAAfl4icoGj2zXWl1ItQ= -git.sr.ht/~michalr/go-satel v0.0.0-20240304210948-c93d9db530fe/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= -git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0 h1:E5gMgMCgaZltNC9nM5s8gouzbO/X51zymT7+S4LNvvM= -git.sr.ht/~michalr/go-satel v0.0.0-20240305205259-053187c5d1a0/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= -git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8 h1:edwd27GRcof9fC93rBv0yTolaEcs5AlFmWyVquYKRK0= -git.sr.ht/~michalr/go-satel v0.0.0-20240306065228-8979c2dd1ed8/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733 h1:pvnT5ZT9LlY2/s8PBPA0PJz3Sw63nGLU18o6MQMzJUM= git.sr.ht/~michalr/go-satel v0.0.0-20240306182245-7ac13d8e4733/go.mod h1:J/Bnb8xBRmuEq03dvJKLf3eCwizIhGuomUY4lVGa/6U= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o= +github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -30,9 +24,13 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -48,6 +46,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/mqtt_worker.go b/mqtt_worker.go new file mode 100644 index 0000000..b2fc0b8 --- /dev/null +++ b/mqtt_worker.go @@ -0,0 +1,43 @@ +package main + +import ( + mqtt "github.com/eclipse/paho.mqtt.golang" + "log" + "sync" +) + +func configure_mqtt(config *MqttConfig) mqtt.Client { + opts := mqtt.NewClientOptions() + opts.AddBroker(config.BrokerUri) + opts.SetClientID("hswro-alarm-bot") + opts.SetUsername(config.Username) + opts.SetPassword(config.Password) + // opts.SetDefaultPublishHandler(messagePubHandler) + // opts.OnConnect = func() {} + // opts.OnConnectionLost = connectLostHandler // TODO: reconnect? + client := mqtt.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + return client +} + +func HandleMqtt(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger *log.Logger, config *MqttConfig) <-chan GenericMessage { + returnEvents := make(chan GenericMessage) + + go func() { + wg.Add(1) + defer wg.Done() + + mqttClient := configure_mqtt(config) + + for e := range events { + returnEvents <- e + + } + close(returnEvents) + mqttClient.Disconnect(250) + }() + + return returnEvents +}