1
0
Fork 0

Test requests with timers

main v0.16.0
Jordan Hotmann 2023-12-06 22:19:52 -07:00
parent 64f800fdd0
commit c9a1cdb50d
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
3 changed files with 18 additions and 2 deletions

View File

@ -156,7 +156,8 @@ func handleMessages() {
data, _ := json.Marshal(message.Event.Data) data, _ := json.Marshal(message.Event.Data)
nats.Publish(fmt.Sprintf("homeassistant.zwave-scene.%s", message.Event.Data.DeviceId), data) nats.Publish(fmt.Sprintf("homeassistant.zwave-scene.%s", message.Event.Data.DeviceId), data)
case timerFinishedEventId: case timerFinishedEventId:
nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished") // nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished")
nats.PublishRequest(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), []byte("finished"), 500*time.Millisecond, 3)
} }
} }
} }

View File

@ -83,6 +83,21 @@ func PublishString(subject, message string) {
Publish(subject, []byte(message)) Publish(subject, []byte(message))
} }
func PublishRequest(subject string, message []byte, timeout time.Duration, retries int) {
attempts := 0
for {
attempts += 1
if attempts > retries {
logger.Error("Request retries exceeded", "subject", subject)
}
resp, err := client.Conn.Request(subject, message, timeout)
if err == nil {
logger.Debug("Request response received", "response", string(resp.Data))
}
}
}
func GetKeyValue(key string) ([]byte, error) { func GetKeyValue(key string) ([]byte, error) {
value, err := kv.Get(ctx, key) value, err := kv.Get(ctx, key)
if err != nil { if err != nil {

View File

@ -57,7 +57,7 @@ func (n *NatsConnection) GenericTimerListener(logger *slog.Logger, timerName str
defer sub.Unsubscribe() defer sub.Unsubscribe()
for msg := range ch { for msg := range ch {
go msg.Ack() go msg.Respond([]byte("got it"))
l.Debug("Timer ended", "name", timerName) l.Debug("Timer ended", "name", timerName)
err = handler() err = handler()
if err != nil { if err != nil {