overflow_probe_container/service/SensorConfigService.go
crusader c281424878 ing
2018-03-27 16:37:39 +09:00

81 lines
2.1 KiB
Go

package service
import (
"context"
"fmt"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
oocmci "git.loafle.net/overflow/overflow_commons_go/modules/commons/interfaces"
sensorConfigM "git.loafle.net/overflow/overflow_commons_go/modules/sensor_config/model"
)
func init() {
cdr.RegisterType(reflect.TypeOf((*SensorConfigService)(nil)))
}
type SensorConfigService struct {
cda.TypeAnnotation `annotation:"@overFlow:Service()"`
oocmci.Service
sensorConfigs map[string]*sensorConfigM.SensorConfig
}
func (scs *SensorConfigService) Start() error {
scs.sensorConfigs = make(map[string]*sensorConfigM.SensorConfig, 0)
return nil
}
func (scs *SensorConfigService) Stop(ctx context.Context) error {
return nil
}
func (scs *SensorConfigService) Init(sensorConfigs []*sensorConfigM.SensorConfig) error {
if nil == sensorConfigs || 0 == len(sensorConfigs) {
return nil
}
for _, sensorConfig := range sensorConfigs {
scs.sensorConfigs[sensorConfig.ID.String()] = sensorConfig
}
return nil
}
func (scs *SensorConfigService) AddConfig(sensorConfig *sensorConfigM.SensorConfig) error {
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
sensorConfigID := sensorConfig.ID.String()
if _, ok := scs.sensorConfigs[sensorConfigID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", sensorConfigID)
}
scs.sensorConfigs[sensorConfigID] = sensorConfig
return nil
}
func (scs *SensorConfigService) UpdateConfig(sensorConfig *sensorConfigM.SensorConfig) error {
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
sensorConfigID := sensorConfig.ID.String()
if _, ok := scs.sensorConfigs[sensorConfigID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID)
}
delete(scs.sensorConfigs, sensorConfigID)
scs.sensorConfigs[sensorConfigID] = sensorConfig
return nil
}
func (scs *SensorConfigService) RemoveConfig(sensorConfigID string) error {
if _, ok := scs.sensorConfigs[sensorConfigID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID)
}
delete(scs.sensorConfigs, sensorConfigID)
return nil
}