package service import ( "context" "fmt" "reflect" cda "git.loafle.net/commons_go/di/annotation" cdr "git.loafle.net/commons_go/di/registry" "git.loafle.net/commons_go/logging" 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) InitConfig(sensorConfigs []*sensorConfigM.SensorConfig) error { if nil == sensorConfigs || 0 == len(sensorConfigs) { return nil } for _, sensorConfig := range sensorConfigs { scs.sensorConfigs[sensorConfig.ID.String()] = sensorConfig } logging.Logger().Debugf("Sensor configs[%d] were added", len(sensorConfigs)) 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 logging.Logger().Debugf("Sensor config[%d] was added", sensorConfigID) 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 logging.Logger().Debugf("Sensor config[%d] was updated", sensorConfigID) 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) logging.Logger().Debugf("Sensor config[%d] was removed", sensorConfigID) return nil }