Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22.1
replace github.com/lorenzodonini/ocpp-go v0.18.0 => github.com/ChargePi/ocpp-go v0.21.0

require (
github.com/ChargePi/ocppManager-go v1.2.0
github.com/agrison/go-commons-lang v0.0.0-20240106075236-2e001e6401ef
github.com/lorenzodonini/ocpp-go v0.18.0
github.com/samber/lo v1.47.0
Expand Down
32 changes: 21 additions & 11 deletions ocpp_v16/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ type (
}

ManagerV16 struct {
ocppConfig *Config
mandatoryKeys []Key
keyValidator KeyValidator
ocppConfig Config
mu sync.RWMutex

mandatoryKeys []Key
keyValidator KeyValidator

onUpdateHandlers map[Key]OnUpdateHandler
mu sync.Mutex
}
)

Expand All @@ -47,10 +49,9 @@ func NewV16ConfigurationManager(defaultConfiguration Config, profiles ...string)
}

return &ManagerV16{
ocppConfig: &defaultConfiguration,
ocppConfig: defaultConfiguration,
mandatoryKeys: mandatoryKeys,
onUpdateHandlers: make(map[Key]OnUpdateHandler),
mu: sync.Mutex{},
}, nil
}

Expand All @@ -65,17 +66,23 @@ func (m *ManagerV16) SetConfiguration(configuration Config) error {
return err
}

m.ocppConfig = &configuration
m.ocppConfig = configuration
return nil
}

// RegisterCustomKeyValidator registers a custom key validator
func (m *ManagerV16) RegisterCustomKeyValidator(validator KeyValidator) {
m.mu.Lock()
defer m.mu.Unlock()

m.keyValidator = validator
}

// GetMandatoryKeys returns the mandatory keys for the configuration
func (m *ManagerV16) GetMandatoryKeys() []Key {
m.mu.RLock()
defer m.mu.RUnlock()

return m.mandatoryKeys
}

Expand Down Expand Up @@ -132,16 +139,16 @@ func (m *ManagerV16) UpdateKey(key Key, value *string) error {

// GetConfiguration returns the full current configuration
func (m *ManagerV16) GetConfiguration() ([]core.ConfigurationKey, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
defer m.mu.RUnlock()

return m.ocppConfig.GetConfig(), nil
}

// GetConfigurationValue returns the value of a specific key
func (m *ManagerV16) GetConfigurationValue(key Key) (*string, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
defer m.mu.RUnlock()

return m.ocppConfig.GetConfigurationValue(key.String())
}
Expand All @@ -166,6 +173,9 @@ func (m *ManagerV16) OnUpdateKey(key Key, handler OnUpdateHandler) error {
return ErrKeyCannotBeEmpty
}

m.mu.Lock()
defer m.mu.Unlock()

// Validate that the key exists
_, err := m.ocppConfig.GetConfigurationValue(key.String())
if err != nil {
Expand Down
Loading