package service import ( "fmt" "reflect" cda "git.loafle.net/commons/di-go/annotation" cdr "git.loafle.net/commons/di-go/registry" "git.loafle.net/commons/logging-go" ocsm "git.loafle.net/overflow/commons-go/sensorconfig/model" // For annotation _ "git.loafle.net/overflow/commons-go/core/annotation" ) var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil)) func init() { cdr.RegisterType(SensorConfigServiceType) } type SensorConfigService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` sensorConfigs map[string]*ocsm.SensorConfig } func (s *SensorConfigService) InitService() error { s.sensorConfigs = make(map[string]*ocsm.SensorConfig, 0) return nil } func (s *SensorConfigService) StartService() error { return nil } func (s *SensorConfigService) StopService() { } func (s *SensorConfigService) DestroyService() { } func (s *SensorConfigService) InitConfig(sensorConfigs []*ocsm.SensorConfig) error { if nil == sensorConfigs || 0 == len(sensorConfigs) { return nil } for _, sensorConfig := range sensorConfigs { s.sensorConfigs[sensorConfig.ConfigID] = sensorConfig } logging.Logger().Debugf("Sensor configs[%d] were added", len(sensorConfigs)) return nil } func (s *SensorConfigService) AddConfig(sensorConfig *ocsm.SensorConfig) error { if nil == sensorConfig { return fmt.Errorf("Sensor config is not valid") } sensorConfigID := sensorConfig.ConfigID if _, ok := s.sensorConfigs[sensorConfigID]; ok { return fmt.Errorf("Sensor config[%s] is exist already", sensorConfigID) } s.sensorConfigs[sensorConfigID] = sensorConfig logging.Logger().Debugf("Sensor config[%d] was added", sensorConfigID) return nil } func (s *SensorConfigService) UpdateConfig(sensorConfig *ocsm.SensorConfig) error { if nil == sensorConfig { return fmt.Errorf("Sensor config is not valid") } sensorConfigID := sensorConfig.ConfigID if _, ok := s.sensorConfigs[sensorConfigID]; !ok { return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID) } delete(s.sensorConfigs, sensorConfigID) s.sensorConfigs[sensorConfigID] = sensorConfig logging.Logger().Debugf("Sensor config[%d] was updated", sensorConfigID) return nil } func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error { if _, ok := s.sensorConfigs[sensorConfigID]; !ok { return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID) } delete(s.sensorConfigs, sensorConfigID) logging.Logger().Debugf("Sensor config[%d] was removed", sensorConfigID) return nil }