diff --git a/internal/domains/sensors/domain.go b/internal/domains/sensors/domain.go index 30144e2..51cba65 100644 --- a/internal/domains/sensors/domain.go +++ b/internal/domains/sensors/domain.go @@ -38,12 +38,36 @@ func (s *Sensor) Validate() error { return nil } +func (d *SensorData) Validate() error { + if d.SensorID == "" { + return ErrInvalidSensorIdentifier + } + + if d.Value == nil { + return ErrMissingValue + } + + if d.Timestamp == nil { + now := time.Now() + d.Timestamp = &now + } + + return nil +} + +// TODO: implement this in service layer for alerts func (d *SensorData) IsOutOfRangeAbove(sensor Sensor) bool { - return d.Value > *sensor.ThresholdAbove + if d.Value == nil || sensor.ThresholdAbove == nil { + return false + } + return *d.Value > *sensor.ThresholdAbove } func (d *SensorData) IsOutOfRangeBelow(sensor Sensor) bool { - return d.Value < *sensor.ThresholdBelow + if d.Value == nil || sensor.ThresholdBelow == nil { + return false + } + return *d.Value < *sensor.ThresholdBelow } func (r *SensorRequest) Validate() error { diff --git a/internal/domains/sensors/domain_test.go b/internal/domains/sensors/domain_test.go index 860da70..038a8af 100644 --- a/internal/domains/sensors/domain_test.go +++ b/internal/domains/sensors/domain_test.go @@ -208,8 +208,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) { name: "value above threshold", data: SensorData{ SensorID: "temp-001", - Value: 150.0, - Timestamp: time.Now(), + Value: ptr(150.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -221,8 +221,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) { name: "value below threshold", data: SensorData{ SensorID: "temp-001", - Value: 50.0, - Timestamp: time.Now(), + Value: ptr(50.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -234,8 +234,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) { name: "value equal to threshold", data: SensorData{ SensorID: "temp-001", - Value: 100.0, - Timestamp: time.Now(), + Value: ptr(100.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -247,8 +247,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) { name: "negative value above negative threshold", data: SensorData{ SensorID: "temp-001", - Value: -5.0, - Timestamp: time.Now(), + Value: ptr(-5.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -282,8 +282,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) { name: "value below threshold", data: SensorData{ SensorID: "temp-001", - Value: 5.0, - Timestamp: time.Now(), + Value: ptr(5.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -295,8 +295,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) { name: "value above threshold", data: SensorData{ SensorID: "temp-001", - Value: 50.0, - Timestamp: time.Now(), + Value: ptr(50.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -308,8 +308,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) { name: "value equal to threshold", data: SensorData{ SensorID: "temp-001", - Value: 10.0, - Timestamp: time.Now(), + Value: ptr(10.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -321,8 +321,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) { name: "negative value below threshold", data: SensorData{ SensorID: "temp-001", - Value: -15.0, - Timestamp: time.Now(), + Value: ptr(-15.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", @@ -334,8 +334,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) { name: "zero value below positive threshold", data: SensorData{ SensorID: "temp-001", - Value: 0.0, - Timestamp: time.Now(), + Value: ptr(0.0), + Timestamp: ptr(time.Now()), }, sensor: Sensor{ SensorID: "temp-001", diff --git a/internal/domains/sensors/models.go b/internal/domains/sensors/models.go index 0c7f58a..04d0d4f 100644 --- a/internal/domains/sensors/models.go +++ b/internal/domains/sensors/models.go @@ -15,18 +15,18 @@ const ( ) type Sensor struct { - SensorID string `json:"sensor_id"` - SensorType SType `json:"sensor_type"` - SamplingInterval *time.Duration `json:"sampling"` - ThresholdAbove *float64 `json:"thresoldabove"` - ThresholdBelow *float64 `json:"thresoldbelow"` - SensorData *[]SensorData `json:"sensor_data,omitempty"` + SensorID string `json:"sensor_id"` + SensorType SType `json:"sensor_type"` + SamplingInterval *time.Duration `json:"sampling"` + ThresholdAbove *float64 `json:"thresoldabove"` + ThresholdBelow *float64 `json:"thresoldbelow"` + SensorData *map[int]SensorData `json:"sensor_data,omitempty"` } type SensorData struct { - SensorID string `json:"sensor_id"` - Value float64 `json:"value"` - Timestamp time.Time `json:"timestamp"` + SensorID string `json:"sensor_id"` + Value *float64 `json:"value"` + Timestamp *time.Time `json:"timestamp"` } type SensorRequest struct {