1
0
Fork 0

get state logging

main
Jordan Hotmann 2023-11-17 12:15:15 -07:00
parent 21b45b4255
commit ffd1ff689b
No known key found for this signature in database
GPG Key ID: 01B504170C2A2EA3
1 changed files with 6 additions and 1 deletions

View File

@ -123,17 +123,22 @@ func loggerMiddleware(next http.Handler) http.Handler {
func getEntityStateHandler(w http.ResponseWriter, r *http.Request) { func getEntityStateHandler(w http.ResponseWriter, r *http.Request) {
entityId := chi.URLParam(r, "entityId") entityId := chi.URLParam(r, "entityId")
full := r.URL.Query().Get("full") == "true" full := r.URL.Query().Get("full") == "true"
l := logger.With("endpoint", "GET /api/state/{entityId}", "entityId", entityId, "full", full)
if !full { if !full {
l.Debug("Getting state from KV store")
kvVal, err := nats.GetKeyValue(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId)) kvVal, err := nats.GetKeyValue(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId))
if err == nil && len(kvVal) > 0 { if err == nil && len(kvVal) > 0 {
w.Write(kvVal) l.Debug("Returning", "value", string(kvVal))
render.PlainText(w, r, string(kvVal))
return return
} }
} }
l.Debug("Getting state from Home Assistant")
data, err := haClient.GetState(entityId) data, err := haClient.GetState(entityId)
if err != nil { if err != nil {
l.Error("Error getting state from Home Assistant", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }