container-go/service/SensorConfigService.go

126 lines
3.0 KiB
Go
Raw Normal View History

2018-04-19 06:24:45 +00:00
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"
2018-04-26 08:46:16 +00:00
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
2018-04-19 06:24:45 +00:00
// 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()"`
2018-05-03 11:00:07 +00:00
CollectorService *CollectorService `annotation:"@Inject()"`
2018-04-26 08:46:16 +00:00
sensorConfigs map[string]*ocmsc.SensorConfig
2018-04-19 06:24:45 +00:00
}
func (s *SensorConfigService) InitService() error {
2018-04-26 08:46:16 +00:00
s.sensorConfigs = make(map[string]*ocmsc.SensorConfig, 0)
2018-04-19 06:24:45 +00:00
return nil
}
func (s *SensorConfigService) StartService() error {
return nil
}
func (s *SensorConfigService) StopService() {
}
func (s *SensorConfigService) DestroyService() {
}
2018-04-26 08:46:16 +00:00
func (s *SensorConfigService) InitConfig(sensorConfigs []*ocmsc.SensorConfig) error {
2018-04-19 06:24:45 +00:00
if nil == sensorConfigs || 0 == len(sensorConfigs) {
return nil
}
for _, sensorConfig := range sensorConfigs {
s.sensorConfigs[sensorConfig.ConfigID] = sensorConfig
}
2018-05-03 11:00:07 +00:00
if err := s.CollectorService.AddSensorConfigs(sensorConfigs); nil != err {
return err
}
2018-04-19 06:24:45 +00:00
logging.Logger().Debugf("Sensor configs[%d] were added", len(sensorConfigs))
return nil
}
2018-04-26 08:46:16 +00:00
func (s *SensorConfigService) AddConfig(sensorConfig *ocmsc.SensorConfig) error {
2018-04-19 06:24:45 +00:00
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
2018-04-23 10:21:10 +00:00
configID := sensorConfig.ConfigID
if _, ok := s.sensorConfigs[configID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", configID)
2018-04-19 06:24:45 +00:00
}
2018-04-23 10:21:10 +00:00
s.sensorConfigs[configID] = sensorConfig
2018-04-19 06:24:45 +00:00
2018-05-03 11:00:07 +00:00
if err := s.CollectorService.AddSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
}
2018-04-23 10:21:10 +00:00
logging.Logger().Debugf("Sensor config[%s] was added", configID)
2018-04-19 06:24:45 +00:00
return nil
}
2018-04-26 08:46:16 +00:00
func (s *SensorConfigService) UpdateConfig(sensorConfig *ocmsc.SensorConfig) error {
2018-04-19 06:24:45 +00:00
if nil == sensorConfig {
return fmt.Errorf("Sensor config is not valid")
}
2018-04-23 10:21:10 +00:00
configID := sensorConfig.ConfigID
if _, ok := s.sensorConfigs[configID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", configID)
2018-04-19 06:24:45 +00:00
}
2018-04-23 10:21:10 +00:00
delete(s.sensorConfigs, configID)
s.sensorConfigs[configID] = sensorConfig
2018-04-19 06:24:45 +00:00
2018-05-03 11:00:07 +00:00
if err := s.CollectorService.RemoveSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
}
2018-06-13 10:45:06 +00:00
2018-05-03 11:00:07 +00:00
if err := s.CollectorService.AddSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
}
2018-04-23 10:21:10 +00:00
logging.Logger().Debugf("Sensor config[%s] was updated", configID)
2018-04-19 06:24:45 +00:00
return nil
}
func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error {
2018-05-03 11:00:07 +00:00
sensorConfig, ok := s.sensorConfigs[sensorConfigID]
if !ok {
2018-04-19 06:24:45 +00:00
return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID)
}
delete(s.sensorConfigs, sensorConfigID)
2018-05-03 11:00:07 +00:00
if err := s.CollectorService.RemoveSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
return err
}
2018-04-19 06:24:45 +00:00
logging.Logger().Debugf("Sensor config[%d] was removed", sensorConfigID)
return nil
}