Skip to content

Commit

Permalink
mutex for entityStates map
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenderistDev committed Aug 29, 2024
1 parent eef5e9d commit a7af37d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/homeassistant/entitymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package homeassistant

import (
"fmt"
"sync"
"time"

"keeneticToMqtt/internal/dto"
Expand Down Expand Up @@ -34,13 +35,14 @@ type logger interface {

// EntityManager entity manager for keenetic client entities in home assistant.
type EntityManager struct {
entities []Entity
clientList clientList
mqtt mqtt
pollingInterval time.Duration
logger logger
clients map[string]dto.Client
entityStates map[string]map[string]string
entities []Entity
clientList clientList
mqtt mqtt
pollingInterval time.Duration
logger logger
clients map[string]dto.Client
entityStates map[string]map[string]string
entityStatesMutex sync.RWMutex
}

// NewEntityManager creates new EntityManager.
Expand Down Expand Up @@ -117,6 +119,7 @@ func (m *EntityManager) updateEntitiesState(client dto.Client) {
)
return
}
m.entityStatesMutex.Lock()
entityStorage, ok := m.entityStates[stateTopic]
if ok {
storageState, ok := entityStorage[client.Mac]
Expand All @@ -128,6 +131,7 @@ func (m *EntityManager) updateEntitiesState(client dto.Client) {
m.entityStates[stateTopic] = make(map[string]string)
}
m.entityStates[stateTopic][client.Mac] = state
m.entityStatesMutex.Unlock()
m.mqtt.SendMessage(stateTopic, state, false)
}
}
Expand Down

0 comments on commit a7af37d

Please sign in to comment.