container-go/service/SensorConfigService.go
crusader 7b191518b0 ing
2018-05-03 20:00:07 +09:00

125 lines
3.0 KiB
Go

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