-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (89 loc) · 2.91 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
package main
import (
"fmt"
"github.com/eclipse/paho.mqtt.golang"
"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"mqttbridge/handlers"
"mqttbridge/models"
"mqttbridge/utils"
"regexp"
"time"
)
func main() {
dbConfig := utils.GetConnectionString()
db, err := gorm.Open(postgres.Open(dbConfig), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
mqttCfg := utils.GetMqttUtils()
clientID := uuid.New().String()
opts := mqtt.NewClientOptions().
AddBroker(fmt.Sprintf("tcp://%s:%d", mqttCfg.Host, mqttCfg.Port)).
SetClientID(clientID).
SetUsername(mqttCfg.Username).
SetPassword(mqttCfg.Password)
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Failed to connect to the MQTT broker: %v", token.Error())
}
regex := regexp.MustCompile(`ERROR|OK`)
client.Subscribe("register", 0, func(client mqtt.Client, msg mqtt.Message) {
log.Printf("Received message: %s from topic: %s", msg.Payload(), msg.Topic())
message := string(msg.Payload())
if !regex.MatchString(message) {
result := handlers.RegisterHandler(db, msg.Topic(), message)
client.Publish(msg.Topic(), 0, false, result)
}
})
client.Subscribe("data", 0, func(client mqtt.Client, msg mqtt.Message) {
log.Printf("Received message: %s from topic: %s", msg.Payload(), msg.Topic())
message := string(msg.Payload())
if !regex.MatchString(message) {
result := handlers.DataHandler(db, msg.Topic(), message)
client.Publish(msg.Topic(), 0, false, result)
}
})
var timeoutDevicesArray []models.TimeoutDevices
client.Subscribe("live", 0, func(client mqtt.Client, msg mqtt.Message) {
log.Printf("Received message: %s from topic: %s", msg.Payload(), msg.Topic())
message := string(msg.Payload())
if !regex.MatchString(message) {
resultMessage, _ := handlers.LiveHandler(db, msg.Topic(), message, timeoutDevicesArray)
client.Publish(msg.Topic(), 0, false, resultMessage)
}
})
ticker := time.NewTicker(1 * time.Minute)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
timeoutDevices := handlers.TimeoutLiveHandler(db)
for _, device := range timeoutDevices {
if len(timeoutDevicesArray) == 0 {
timeoutDevicesArray = append(timeoutDevicesArray, models.TimeoutDevices{SerialNumber: device.SerialNumber, Counter: 0})
}
for i, timeoutDevice := range timeoutDevicesArray {
if timeoutDevice.SerialNumber == device.SerialNumber {
if timeoutDevicesArray[i].Counter < 3 {
timeoutDevicesArray[i].Counter++
}
if timeoutDevicesArray[i].Counter >= 3 {
handlers.OfflineHandler(db, device)
}
} else {
timeoutDevicesArray = append(timeoutDevicesArray, models.TimeoutDevices{SerialNumber: device.SerialNumber, Counter: 0})
}
}
}
case <-quit:
ticker.Stop()
return
}
}
}()
select {}
}