updated architecture

This commit is contained in:
Oliver Wallisch 2026-01-26 14:59:37 +01:00
parent 9068a66051
commit 4aa10b7746
10 changed files with 53 additions and 27 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
backend/.DS_Store vendored

Binary file not shown.

BIN
backend/cmd/.DS_Store vendored

Binary file not shown.

View file

@ -10,29 +10,29 @@ import (
)
func main() {
cfg := mqttclient.MQTTBroker{
MQTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
MQTTUser: "oliver3",
MQTTPW: "tESTUSER1234",
MQTTTopic: "esp32/oliver1/metrics",
MQTTPort: 8883,
ClientID: "GoClient-12345",
}
adapter := mqttclient.NewAdapter(cfg)
if err := adapter.Connect(); err != nil {
slog.Error("Konnte MQTT nicht verbinden", err)
}
go func() {
config := mqttclient.MQTTBroker{
MQTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
MQTTUser: "oliver3",
MQTTPW: "tESTUSER1234",
MQTTTopic: "esp32/oliver1/metrics",
MQTTPort: 8883,
ClientID: "GoClient-12345",
}
client := mqttclient.EstablishESPConnection(config)
defer client.Disconnect(250)
subscribeToken := mqttclient.RecieveTopics(client, "esp32/oliver1/metrics", byte(1))
defer adapter.Disconnect(250)
subscribeToken := adapter.RecieveTopics("esp32/oliver1/metrics", 1)
if subscribeToken.Wait() && subscribeToken.Error() != nil {
slog.Error("Failed to subscribe to topic!", "error", subscribeToken.Error)
}
fmt.Println("Programm läuft und hält die Verbindung. Beenden mit Ctrl+C.")
print(subscribeToken)
select {}
}()
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(202, "request sucesfully")

View file

@ -1,2 +1 @@
package main

Binary file not shown.

Binary file not shown.

View file

@ -6,14 +6,14 @@ import (
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func RecieveTopics(c mqtt.Client, topic string, qos byte) mqtt.Token {
token := c.Subscribe(topic, qos, RevieveMessage)
func (a *MQTTAdapter) RecieveTopics(topic string, qos byte) mqtt.Token {
token := a.client.Subscribe(topic, qos, a.RevieveMessage)
token.Wait()
fmt.Println("Subscription to the topic succesfully", topic)
return token
}
func RevieveMessage(c mqtt.Client, msg mqtt.Message) {
func (a *MQTTAdapter) RevieveMessage(c mqtt.Client, msg mqtt.Message) {
fmt.Printf("Nachricht auf Topic %s: %s\n", msg.Topic(), msg.Payload())
msg.Ack()
}

View file

@ -16,6 +16,11 @@ type MQTTBroker struct {
MQTTPort int
}
type MQTTAdapter struct {
client mqtt.Client
config MQTTBroker
}
// Todo: .env variables instead of clear
var config = MQTTBroker{
MQTTBroker: "8b758ea22b9f4c0f94ac43c9b09a254f.s1.eu.hivemq.cloud",
@ -23,28 +28,42 @@ var config = MQTTBroker{
MQTTPW: "tESTUSER1234",
MQTTTopic: "esp32/oliver1/metrics",
MQTTPort: 8883,
ClientID: "GoClient-12345",
}
func EstablishESPConnection(config MQTTBroker) mqtt.Client {
func NewAdapter(cfg MQTTBroker) *MQTTAdapter {
return &MQTTAdapter{
config: cfg,
}
}
func (a *MQTTAdapter) Connect() error {
opts := mqtt.NewClientOptions()
// opts.AddBroker(config.MQTTBroker)
brokerURL := fmt.Sprintf("tls://%s:%d", config.MQTTBroker, config.MQTTPort)
brokerURL := fmt.Sprintf("tls://%s:%d", a.config.MQTTBroker, a.config.MQTTPort)
opts.AddBroker(brokerURL)
opts.SetClientID(config.ClientID)
opts.SetUsername(config.MQTTUser)
opts.SetPassword(config.MQTTPW)
opts.SetClientID(a.config.ClientID)
opts.SetUsername(a.config.MQTTUser)
opts.SetPassword(a.config.MQTTPW)
opts.OnConnect = func(c mqtt.Client) {
fmt.Println("✅ Verbunden mit MQTT-Broker:", config.MQTTBroker)
}
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
a.client = mqtt.NewClient(opts)
if token := a.client.Connect(); token.Wait() && token.Error() != nil {
slog.Error(
"cannot establish connection to ESP",
slog.Any("reason", token.Error()),
slog.String("broker_url", brokerURL),
)
panic(error.Error)
return token.Error()
}
return nil
}
func (a *MQTTAdapter) Disconnect(quiesce uint) {
if a.client != nil && a.client.IsConnected() {
a.client.Disconnect(quiesce)
fmt.Println("MQTT Verbindung erfolgreich geschlossen")
}
return client
}

View file

@ -0,0 +1,8 @@
return {
"leoluz/nvim-dap-go",
opts = {
-- Dieser Befehl sagt dem Go-Plugin, wo es dlv finden soll,
-- und ignoriert das PATH-Problem.
dap_bin = vim.fn.stdpath("data") .. "/mason/bin/dlv",
},
}