126 lines
3.1 KiB
Go
126 lines
3.1 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.SensorID.String()] = 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")
|
|
}
|
|
|
|
sensorID := sensorConfig.SensorID.String()
|
|
if _, ok := s.sensorConfigs[sensorID]; ok {
|
|
return fmt.Errorf("Sensor config[%s] is exist already", sensorID)
|
|
}
|
|
s.sensorConfigs[sensorID] = sensorConfig
|
|
|
|
if err := s.CollectorService.AddSensorConfigs([]*ocmsc.SensorConfig{sensorConfig}); nil != err {
|
|
return err
|
|
}
|
|
|
|
logging.Logger().Debugf("Sensor config[%s] was added", sensorID)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SensorConfigService) UpdateConfig(sensorConfig *ocmsc.SensorConfig) error {
|
|
if nil == sensorConfig {
|
|
return fmt.Errorf("Sensor config is not valid")
|
|
}
|
|
|
|
sensorID := sensorConfig.SensorID.String()
|
|
if _, ok := s.sensorConfigs[sensorID]; !ok {
|
|
return fmt.Errorf("Sensor config[%s] is not exist", sensorID)
|
|
}
|
|
delete(s.sensorConfigs, sensorID)
|
|
s.sensorConfigs[sensorID] = 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", sensorID)
|
|
|
|
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
|
|
}
|