From afa30961c9949ffae299635d37ce818ca0a6309a Mon Sep 17 00:00:00 2001 From: Calvin McLean Date: Tue, 16 Jul 2024 16:43:46 -0700 Subject: [PATCH] Refactor watering notification handler - Rename file so it is easier to find --- garden-app/server/api.go | 3 +-- ...andler.go => water_notification_handler.go} | 18 ++++++++++-------- ...t.go => water_notification_handler_test.go} | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) rename garden-app/server/{mqtt_handler.go => water_notification_handler.go} (81%) rename garden-app/server/{mqtt_handler_test.go => water_notification_handler_test.go} (95%) diff --git a/garden-app/server/api.go b/garden-app/server/api.go index df64469a..3c7d4d89 100644 --- a/garden-app/server/api.go +++ b/garden-app/server/api.go @@ -13,7 +13,6 @@ import ( "github.com/calvinmclean/automated-garden/garden-app/worker" "github.com/calvinmclean/babyapi" "github.com/calvinmclean/babyapi/html" - paho "github.com/eclipse/paho.mqtt.golang" "github.com/prometheus/client_golang/prometheus/promhttp" prommetrics "github.com/slok/go-http-metrics/metrics/prometheus" metrics_middleware "github.com/slok/go-http-metrics/middleware" @@ -86,7 +85,7 @@ func (api *API) Setup(cfg Config, validateData bool) error { ).Info("initializing MQTT client") mqttClient, err := mqtt.NewClient(cfg.MQTTConfig, mqtt.DefaultHandler(logger), mqtt.TopicHandler{ Topic: "+/data/water", - Handler: paho.MessageHandler(NewMQTTHandler(storageClient, logger).Handle), + Handler: NewWaterNotificationHandler(storageClient, logger).HandleMessage, }) if err != nil { return fmt.Errorf("unable to initialize MQTT client: %v", err) diff --git a/garden-app/server/mqtt_handler.go b/garden-app/server/water_notification_handler.go similarity index 81% rename from garden-app/server/mqtt_handler.go rename to garden-app/server/water_notification_handler.go index 3f0251b2..2108a7d2 100644 --- a/garden-app/server/mqtt_handler.go +++ b/garden-app/server/water_notification_handler.go @@ -14,16 +14,16 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" ) -type MQTTHandler struct { +type WaterNotificationHandler struct { storageClient *storage.Client logger *slog.Logger } -func NewMQTTHandler(storageClient *storage.Client, logger *slog.Logger) *MQTTHandler { - return &MQTTHandler{storageClient, logger} +func NewWaterNotificationHandler(storageClient *storage.Client, logger *slog.Logger) *WaterNotificationHandler { + return &WaterNotificationHandler{storageClient, logger} } -func (h *MQTTHandler) getGarden(topicPrefix string) (*pkg.Garden, error) { +func (h *WaterNotificationHandler) getGarden(topicPrefix string) (*pkg.Garden, error) { gardens, err := h.storageClient.Gardens.GetAll(context.Background(), nil) if err != nil { return nil, fmt.Errorf("error getting all gardens: %w", err) @@ -42,7 +42,7 @@ func (h *MQTTHandler) getGarden(topicPrefix string) (*pkg.Garden, error) { return garden, nil } -func (h *MQTTHandler) getZone(gardenID string, zonePosition int) (*pkg.Zone, error) { +func (h *WaterNotificationHandler) getZone(gardenID string, zonePosition int) (*pkg.Zone, error) { zones, err := h.storageClient.Zones.GetAll(context.Background(), nil) if err != nil { return nil, fmt.Errorf("error getting all zones: %w", err) @@ -63,14 +63,14 @@ func (h *MQTTHandler) getZone(gardenID string, zonePosition int) (*pkg.Zone, err return zone, nil } -func (h *MQTTHandler) Handle(_ mqtt.Client, msg mqtt.Message) { +func (h *WaterNotificationHandler) HandleMessage(_ mqtt.Client, msg mqtt.Message) { err := h.handle(msg.Topic(), msg.Payload()) if err != nil { h.logger.With("topic", msg.Topic(), "error", err).Error("error handling message") } } -func (h *MQTTHandler) handle(topic string, payload []byte) error { +func (h *WaterNotificationHandler) handle(topic string, payload []byte) error { logger := h.logger.With("topic", topic) logger.Info("received message", "message", string(payload)) @@ -96,7 +96,9 @@ func (h *MQTTHandler) handle(topic string, payload []byte) error { } logger.Info("found zone with position", "zone_position", zonePosition, "zone_id", zone.GetID()) - // TODO: this might end up getting client from garden or zone config instead of using all + // TODO: Use Garden notification client here? However, Garden Notifications only work if a lightschedule exists. + // Instead, I could move the NotificationClientID from a WaterSched + // TODO: rename this file to notification_handler or something since it's hard to find notificationClients, err := h.storageClient.NotificationClientConfigs.GetAll(context.Background(), nil) if err != nil { return fmt.Errorf("error getting all notification clients: %w", err) diff --git a/garden-app/server/mqtt_handler_test.go b/garden-app/server/water_notification_handler_test.go similarity index 95% rename from garden-app/server/mqtt_handler_test.go rename to garden-app/server/water_notification_handler_test.go index 1b87a4f7..309b9b4a 100644 --- a/garden-app/server/mqtt_handler_test.go +++ b/garden-app/server/water_notification_handler_test.go @@ -42,13 +42,13 @@ func TestParseWaterMessage(t *testing.T) { } } -func TestHandle(t *testing.T) { +func TestHandleMessage(t *testing.T) { storageClient, err := storage.NewClient(storage.Config{ Driver: "hashmap", }) require.NoError(t, err) - handler := NewMQTTHandler(storageClient, slog.Default()) + handler := NewWaterNotificationHandler(storageClient, slog.Default()) t.Run("ErrorParsingMessage", func(t *testing.T) { err = handler.handle("garden/data/water", []byte{})