container-go/service/SensorConfigService.go

126 lines
3.1 KiB
Go
Raw Permalink 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 {
2018-07-04 02:35:52 +00:00
s.sensorConfigs[sensorConfig.SensorID.String()] = sensorConfig
2018-04-19 06:24:45 +00:00
}
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-07-04 02:35:52 +00:00
sensorID := sensorConfig.SensorID.String()
if _, ok := s.sensorConfigs[sensorID]; ok {
return fmt.Errorf("Sensor config[%s] is exist already", sensorID)
2018-04-19 06:24:45 +00:00
}
2018-07-04 02:35:52 +00:00
s.sensorConfigs[sensorID] = 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-07-04 02:35:52 +00:00
logging.Logger().Debugf("Sensor config[%s] was added", sensorID)
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-07-04 02:35:52 +00:00
sensorID := sensorConfig.SensorID.String()
if _, ok := s.sensorConfigs[sensorID]; !ok {
return fmt.Errorf("Sensor config[%s] is not exist", sensorID)
2018-04-19 06:24:45 +00:00
}
2018-07-04 02:35:52 +00:00
delete(s.sensorConfigs, sensorID)
s.sensorConfigs[sensorID] = 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-07-04 02:35:52 +00:00
logging.Logger().Debugf("Sensor config[%s] was updated", sensorID)
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
}