From b0859ed4f0a8a02cd315a3a6d4d33bd69c6d004d Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Mon, 27 Nov 2023 21:50:39 -0700 Subject: [PATCH] Turn subscribers into method --- pkg/nats/subscribers.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/nats/subscribers.go b/pkg/nats/subscribers.go index 52ea999..37c3c4b 100644 --- a/pkg/nats/subscribers.go +++ b/pkg/nats/subscribers.go @@ -9,14 +9,14 @@ import ( ha "code.jhot.me/jhot/hats/pkg/homeassistant" ) -func GenericStateSubscriber(logger *slog.Logger, natsClient *NatsConnection, entityId string, handler func(ha.StateData) error) { +func (n *NatsConnection) GenericStateSubscriber(logger *slog.Logger, entityId string, handler func(ha.StateData) error) { if entityId == "" { panic(errors.New("entity ID cannot be empty")) } topic := fmt.Sprintf("homeassistant.states.%s.>", entityId) l := logger.With("topic", topic, "entity_id", entityId) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return @@ -41,14 +41,14 @@ func GenericStateSubscriber(logger *slog.Logger, natsClient *NatsConnection, ent } } -func GenericScheduleSubscriber(logger *slog.Logger, natsClient *NatsConnection, scheduleName string, handler func() error) { +func (n *NatsConnection) GenericScheduleSubscriber(logger *slog.Logger, scheduleName string, handler func() error) { if scheduleName == "" { panic(errors.New("schedule name cannot be empty")) } topic := fmt.Sprintf("schedules.%s", scheduleName) l := logger.With("topic", topic, "schedule", scheduleName) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return @@ -67,14 +67,14 @@ func GenericScheduleSubscriber(logger *slog.Logger, natsClient *NatsConnection, } } -func GenericCommandSubscriber(logger *slog.Logger, natsClient *NatsConnection, commandName string, handler func([]byte) error) { +func (n *NatsConnection) GenericCommandSubscriber(logger *slog.Logger, commandName string, handler func([]byte) error) { if commandName == "" { panic(errors.New("command name cannot be empty")) } topic := fmt.Sprintf("command.%s", commandName) l := logger.With("topic", topic, "command", commandName) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return @@ -93,14 +93,14 @@ func GenericCommandSubscriber(logger *slog.Logger, natsClient *NatsConnection, c } } -func GenericNfcSubscriber(logger *slog.Logger, natsClient *NatsConnection, tagId string, handler func([]byte) error) { +func (n *NatsConnection) GenericNfcSubscriber(logger *slog.Logger, tagId string, handler func([]byte) error) { if tagId == "" { panic(errors.New("tag ID cannot be empty")) } topic := fmt.Sprintf("homeassistant.nfc.%s", tagId) l := logger.With("topic", topic, "tagId", tagId) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return @@ -119,14 +119,14 @@ func GenericNfcSubscriber(logger *slog.Logger, natsClient *NatsConnection, tagId } } -func GenericZhaSubscriber(logger *slog.Logger, natsClient *NatsConnection, deviceIeee string, handler func(ha.EventData) error) { +func (n *NatsConnection) GenericZhaSubscriber(logger *slog.Logger, deviceIeee string, handler func(ha.EventData) error) { if deviceIeee == "" { panic(errors.New("device IEEE cannot be empty")) } topic := fmt.Sprintf("homeassistant.zha.%s", deviceIeee) l := logger.With("topic", topic, "ieee", deviceIeee) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return @@ -151,7 +151,7 @@ func GenericZhaSubscriber(logger *slog.Logger, natsClient *NatsConnection, devic } } -func GenericZwaveLSubscriber(logger *slog.Logger, natsClient *NatsConnection, deviceId string, scene bool, handler func(ha.EventData) error) { +func (n *NatsConnection) GenericZwaveLSubscriber(logger *slog.Logger, deviceId string, scene bool, handler func(ha.EventData) error) { if deviceId == "" { panic(errors.New("device ID cannot be empty")) } @@ -162,7 +162,7 @@ func GenericZwaveLSubscriber(logger *slog.Logger, natsClient *NatsConnection, de topic := fmt.Sprintf("homeassistant.zwave%s.%s", scenePart, deviceId) l := logger.With("topic", topic, "deviceId", deviceId) l.Debug("Subscribing to topic") - sub, ch, err := natsClient.Subscribe(topic) + sub, ch, err := n.Subscribe(topic) if err != nil { l.Error("Error subscribing to topic", "error", err) return