overflow_probe_container/service/SensorConfigService.go

90 lines
2.5 KiB
Go
Raw Normal View History

2018-03-27 07:37:39 +00:00
package service
import (
"context"
"fmt"
"reflect"
cda "git.loafle.net/commons_go/di/annotation"
cdr "git.loafle.net/commons_go/di/registry"
2018-03-27 09:46:46 +00:00
"git.loafle.net/commons_go/logging"
2018-03-27 07:37:39 +00:00
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
}
2018-03-27 09:46:46 +00:00
logging.Logger().Debugf("Sensor configs[%d] were added", len(sensorConfigs))
2018-03-27 07:37:39 +00:00
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
2018-03-27 09:46:46 +00:00
logging.Logger().Debugf("Sensor config[%d] was added", sensorConfigID)
2018-03-27 07:37:39 +00:00
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
2018-03-27 09:46:46 +00:00
logging.Logger().Debugf("Sensor config[%d] was updated", sensorConfigID)
2018-03-27 07:37:39 +00:00
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)
2018-03-27 09:46:46 +00:00
logging.Logger().Debugf("Sensor config[%d] was removed", sensorConfigID)
2018-03-27 07:37:39 +00:00
return nil
}