99
1010 "sensorbucket.nl/sensorbucket/internal/pagination"
1111 "sensorbucket.nl/sensorbucket/pkg/auth"
12+ "sensorbucket.nl/sensorbucket/services/core/featuresofinterest"
1213)
1314
1415type DeviceStore interface {
@@ -30,14 +31,16 @@ type SensorGroupStore interface {
3031}
3132
3233type Service struct {
33- store DeviceStore
34- sensorGroupStore SensorGroupStore
34+ store DeviceStore
35+ sensorGroupStore SensorGroupStore
36+ featureOfInterestService * featuresofinterest.Service
3537}
3638
37- func New (store DeviceStore , sensorGroupStore SensorGroupStore ) * Service {
39+ func New (store DeviceStore , sensorGroupStore SensorGroupStore , featureOfInterestService * featuresofinterest. Service ) * Service {
3840 return & Service {
39- store : store ,
40- sensorGroupStore : sensorGroupStore ,
41+ store : store ,
42+ sensorGroupStore : sensorGroupStore ,
43+ featureOfInterestService : featureOfInterestService ,
4144 }
4245}
4346
@@ -116,15 +119,14 @@ func (s *Service) GetDevice(ctx context.Context, id int64) (*Device, error) {
116119}
117120
118121type NewSensorDTO struct {
119- Code string `json:"code"`
120- Brand string `json:"brand"`
121- GoalID int64 `json:"goal_id"`
122- TypeID int64 `json:"type_id"`
123- Description string `json:"description"`
124- ExternalID string `json:"external_id"`
125- Properties json.RawMessage `json:"properties"`
126- ArchiveTime * int `json:"archive_time"`
127- IsFallback bool `json:"is_fallback"`
122+ Code string `json:"code"`
123+ Brand string `json:"brand"`
124+ Description string `json:"description"`
125+ ExternalID string `json:"external_id"`
126+ FeatureOfInterestID int64 `json:"feature_of_interest_id"`
127+ Properties json.RawMessage `json:"properties"`
128+ ArchiveTime * int `json:"archive_time"`
129+ IsFallback bool `json:"is_fallback"`
128130}
129131
130132func (s * Service ) AddSensor (ctx context.Context , dev * Device , dto NewSensorDTO ) error {
@@ -141,6 +143,14 @@ func (s *Service) AddSensor(ctx context.Context, dev *Device, dto NewSensorDTO)
141143 ArchiveTime : dto .ArchiveTime ,
142144 IsFallback : dto .IsFallback ,
143145 }
146+ if dto .FeatureOfInterestID > 0 {
147+ feature , err := s .featureOfInterestService .GetFeatureOfInterest (ctx , dto .FeatureOfInterestID )
148+ if err != nil {
149+ return fmt .Errorf ("in AddSensor: could not get feature of interest: %w" , err )
150+ }
151+ opts .FeatureOfInterest = feature
152+ }
153+
144154 if err := dev .AddSensor (opts ); err != nil {
145155 return err
146156 }
@@ -235,12 +245,13 @@ func (s *Service) GetSensor(ctx context.Context, id int64) (*Sensor, error) {
235245}
236246
237247type UpdateSensorOpts struct {
238- Description * string `json:"description"`
239- Brand * string `json:"brand"`
240- ArchiveTime * int `json:"archive_time"`
241- ExternalID * string `json:"external_id"`
242- IsFallback * bool `json:"is_fallback"`
243- Properties json.RawMessage `json:"properties"`
248+ Description * string `json:"description"`
249+ Brand * string `json:"brand"`
250+ ArchiveTime * int `json:"archive_time"`
251+ ExternalID * string `json:"external_id"`
252+ IsFallback * bool `json:"is_fallback"`
253+ Properties json.RawMessage `json:"properties"`
254+ FeatureOfInterestID * int64 `json:"feature_of_interest_id"`
244255}
245256
246257func (s * Service ) UpdateSensor (ctx context.Context , device * Device , sensor * Sensor , opt UpdateSensorOpts ) error {
@@ -270,6 +281,17 @@ func (s *Service) UpdateSensor(ctx context.Context, device *Device, sensor *Sens
270281 if opt .Properties != nil {
271282 sensor .Properties = opt .Properties
272283 }
284+ if opt .FeatureOfInterestID != nil {
285+ if * opt .FeatureOfInterestID == 0 {
286+ sensor .FeatureOfInterest = nil
287+ } else {
288+ feature , err := s .featureOfInterestService .GetFeatureOfInterest (ctx , * opt .FeatureOfInterestID )
289+ if err != nil {
290+ return fmt .Errorf ("in UpdateSensor: could not get feature of interest: %w" , err )
291+ }
292+ sensor .FeatureOfInterest = feature
293+ }
294+ }
273295
274296 if err := device .UpdateSensor (sensor ); err != nil {
275297 return err
0 commit comments