Skip to content

Commit

Permalink
Refactor watering notification handler
Browse files Browse the repository at this point in the history
- Rename file so it is easier to find
  • Loading branch information
calvinmclean committed Jul 16, 2024
1 parent a702b6f commit afa3096
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 1 addition & 2 deletions garden-app/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down

0 comments on commit afa3096

Please sign in to comment.