-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.go
More file actions
201 lines (177 loc) · 5.17 KB
/
mqtt.go
File metadata and controls
201 lines (177 loc) · 5.17 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2023-2025 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package mqtt2db
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/eclipse/paho.golang/paho"
"github.com/tknie/log"
"github.com/tknie/services"
)
const layout = "2006-01-02T15:04:05"
var counter = uint64(0)
var mqttDone = make(chan bool, 1)
const DefaultLoopSeconds = 120
var OutLoopSeconds = DefaultLoopSeconds
var CloseIfStuck = false
// loop loop through receiving all messages from MQTT and store them into
// the database
func loopIncomingMessages(msgChan chan *paho.Publish, topicMap map[string]*Topic) {
if OutLoopSeconds == 0 {
return
}
go loopCounterAndCancelOutput()
for m := range msgChan {
log.Log.Debugf("%s: Message: %s", m.Topic, string(m.Payload))
if topic, ok := topicMap[m.Topic]; ok {
x := make(map[string]interface{})
log.Log.Debugf("EVENT....%s", string(m.Payload))
err := json.Unmarshal(m.Payload, &x)
if err != nil {
fmt.Println("JSON unmarshal fails:", err)
fmt.Println("JSON unmarshal fails for payload:", string(m.Payload))
continue
}
em := topic.ParseMessage(x)
if em != nil {
topic.storeEvent(em)
os.Stdout.Sync()
}
}
}
}
func loopCounterAndCancelOutput() {
lastCounter := uint64(0)
try := 0
for {
select {
case <-mqttDone:
services.ServerMessage("Ecoflow analyze loop is stopped")
return
case <-time.After(time.Second * time.Duration(OutLoopSeconds)):
services.ServerMessage("Received MQTT msgs: %04d", counter)
if counter == lastCounter && CloseIfStuck {
if try > 10 {
services.ServerMessage("Received MQTT msgs error still stuck")
os.Exit(10)
}
try++
} else {
try = 0
}
lastCounter = counter
}
}
}
func tryConnectMQTT(server string, tries int) net.Conn {
var err error
var conn net.Conn
for count := 0; count < tries; count++ {
conn, err = net.Dial("tcp", server)
if err == nil {
return conn
}
if count < tries {
services.ServerMessage("Error connecting MQTT retrying soon ... %v", err)
time.Sleep(10 * time.Second)
} else {
services.ServerMessage("Error connecting MQTT ... %v", err)
}
}
if err != nil {
log.Log.Fatalf("Failed to dial to %s: %s", server, err)
}
return nil
}
func (config *Config) ConnectMQTT() {
logger := &MQTTWrapperLogger{}
msgChan := make(chan *paho.Publish)
if c.Mqtt.LoopIntervalSeconds > 0 {
OutLoopSeconds = c.Mqtt.LoopIntervalSeconds
}
services.ServerMessage("Connect TCP/IP to %s", c.Mqtt.Server)
conn := tryConnectMQTT(c.Mqtt.Server, config.MaxTries)
pahoClient := paho.NewClient(paho.ClientConfig{PacketTimeout: 2 * time.Minute,
Router: paho.NewStandardRouterWithDefault(func(m *paho.Publish) {
msgChan <- m
}),
Conn: conn,
})
pahoClient.SetDebugLogger(logger)
pahoClient.SetErrorLogger(logger)
services.ServerMessage("Connecting paho services to %s", c.Mqtt.Server)
password := os.ExpandEnv(c.Mqtt.Password)
// connect to MQTT and listen and subscribe
cp := &paho.Connect{
KeepAlive: 30,
ClientID: config.Clientid,
CleanStart: true,
Username: c.Mqtt.Username,
Password: []byte(password),
}
if c.Mqtt.Username != "" {
cp.UsernameFlag = true
}
if password != "" {
cp.PasswordFlag = true
}
// connecting to MQTT server
ca, err := pahoClient.Connect(context.Background(), cp)
if err != nil {
services.ServerMessage("Error to connect paho services to %s with %s: %v",
c.Mqtt.Server, c.Mqtt.Username, err)
log.Log.Fatalf("Error to connect paho services to %s with %s: %v", c.Mqtt.Server, c.Mqtt.Username, err)
}
if ca.ReasonCode != 0 {
services.ServerMessage("Failed to connect paho services to %s with %s with reason code %d",
c.Mqtt.Server, c.Mqtt.Username, ca.ReasonCode)
log.Log.Fatalf("Failed to connect to %s : %d - %s", c.Mqtt.Server, ca.ReasonCode, ca.Properties.ReasonString)
}
services.ServerMessage("Connecting MQTT to %s", c.Mqtt.Server)
ic := make(chan os.Signal, 1)
signal.Notify(ic, os.Interrupt, syscall.SIGTERM)
go func() {
<-ic
fmt.Println("signal received, exiting")
if c != nil {
d := &paho.Disconnect{ReasonCode: 0}
pahoClient.Disconnect(d)
}
os.Exit(0)
}()
topicMap := make(map[string]*Topic)
// subscribe to a subscription MQTT topic
subscriptions := make([]paho.SubscribeOptions, 0)
for _, topic := range c.Topic {
topicMap[topic.Name] = topic
subscriptions = append(subscriptions, paho.SubscribeOptions{Topic: topic.Name,
QoS: byte(config.Qos)})
services.ServerMessage("Subscribed MQTT to %s", topic.Name)
services.ServerMessage("Storage of MQTT data to table '%s'", topic.StoreTablename)
}
sa, err := pahoClient.Subscribe(context.Background(), &paho.Subscribe{
Subscriptions: subscriptions,
})
if err != nil {
services.ServerMessage("Error subscribing MQTT ... %v", err)
log.Log.Fatalf("Error subscribing MQTT ... %v", err)
}
if sa.Reasons[0] != byte(config.Qos) {
log.Log.Fatalf("Failed to subscribe to %v : %d", subscriptions, sa.Reasons[0])
}
loopIncomingMessages(msgChan, topicMap)
}