1
0
Fork 0

Turn subscribers into method

main v0.13.2
Jordan Hotmann 2023-11-27 21:50:39 -07:00
parent 9de52311fc
commit b0859ed4f0
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
1 changed files with 12 additions and 12 deletions

View File

@ -9,14 +9,14 @@ import (
ha "code.jhot.me/jhot/hats/pkg/homeassistant" 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 == "" { if entityId == "" {
panic(errors.New("entity ID cannot be empty")) panic(errors.New("entity ID cannot be empty"))
} }
topic := fmt.Sprintf("homeassistant.states.%s.>", entityId) topic := fmt.Sprintf("homeassistant.states.%s.>", entityId)
l := logger.With("topic", topic, "entity_id", entityId) l := logger.With("topic", topic, "entity_id", entityId)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return 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 == "" { if scheduleName == "" {
panic(errors.New("schedule name cannot be empty")) panic(errors.New("schedule name cannot be empty"))
} }
topic := fmt.Sprintf("schedules.%s", scheduleName) topic := fmt.Sprintf("schedules.%s", scheduleName)
l := logger.With("topic", topic, "schedule", scheduleName) l := logger.With("topic", topic, "schedule", scheduleName)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return 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 == "" { if commandName == "" {
panic(errors.New("command name cannot be empty")) panic(errors.New("command name cannot be empty"))
} }
topic := fmt.Sprintf("command.%s", commandName) topic := fmt.Sprintf("command.%s", commandName)
l := logger.With("topic", topic, "command", commandName) l := logger.With("topic", topic, "command", commandName)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return 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 == "" { if tagId == "" {
panic(errors.New("tag ID cannot be empty")) panic(errors.New("tag ID cannot be empty"))
} }
topic := fmt.Sprintf("homeassistant.nfc.%s", tagId) topic := fmt.Sprintf("homeassistant.nfc.%s", tagId)
l := logger.With("topic", topic, "tagId", tagId) l := logger.With("topic", topic, "tagId", tagId)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return 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 == "" { if deviceIeee == "" {
panic(errors.New("device IEEE cannot be empty")) panic(errors.New("device IEEE cannot be empty"))
} }
topic := fmt.Sprintf("homeassistant.zha.%s", deviceIeee) topic := fmt.Sprintf("homeassistant.zha.%s", deviceIeee)
l := logger.With("topic", topic, "ieee", deviceIeee) l := logger.With("topic", topic, "ieee", deviceIeee)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return 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 == "" { if deviceId == "" {
panic(errors.New("device ID cannot be empty")) 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) topic := fmt.Sprintf("homeassistant.zwave%s.%s", scenePart, deviceId)
l := logger.With("topic", topic, "deviceId", deviceId) l := logger.With("topic", topic, "deviceId", deviceId)
l.Debug("Subscribing to topic") l.Debug("Subscribing to topic")
sub, ch, err := natsClient.Subscribe(topic) sub, ch, err := n.Subscribe(topic)
if err != nil { if err != nil {
l.Error("Error subscribing to topic", "error", err) l.Error("Error subscribing to topic", "error", err)
return return