add validation for sensor data and fix tests
This commit is contained in:
parent
9d86fd394c
commit
b012db856c
@ -38,12 +38,36 @@ func (s *Sensor) Validate() error {
|
|||||||
return nil
|
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 {
|
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 {
|
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 {
|
func (r *SensorRequest) Validate() error {
|
||||||
|
|||||||
@ -208,8 +208,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
|||||||
name: "value above threshold",
|
name: "value above threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 150.0,
|
Value: ptr(150.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -221,8 +221,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
|||||||
name: "value below threshold",
|
name: "value below threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 50.0,
|
Value: ptr(50.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -234,8 +234,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
|||||||
name: "value equal to threshold",
|
name: "value equal to threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 100.0,
|
Value: ptr(100.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -247,8 +247,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
|||||||
name: "negative value above negative threshold",
|
name: "negative value above negative threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: -5.0,
|
Value: ptr(-5.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -282,8 +282,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
|||||||
name: "value below threshold",
|
name: "value below threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 5.0,
|
Value: ptr(5.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -295,8 +295,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
|||||||
name: "value above threshold",
|
name: "value above threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 50.0,
|
Value: ptr(50.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -308,8 +308,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
|||||||
name: "value equal to threshold",
|
name: "value equal to threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 10.0,
|
Value: ptr(10.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -321,8 +321,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
|||||||
name: "negative value below threshold",
|
name: "negative value below threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: -15.0,
|
Value: ptr(-15.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
@ -334,8 +334,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
|||||||
name: "zero value below positive threshold",
|
name: "zero value below positive threshold",
|
||||||
data: SensorData{
|
data: SensorData{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
Value: 0.0,
|
Value: ptr(0.0),
|
||||||
Timestamp: time.Now(),
|
Timestamp: ptr(time.Now()),
|
||||||
},
|
},
|
||||||
sensor: Sensor{
|
sensor: Sensor{
|
||||||
SensorID: "temp-001",
|
SensorID: "temp-001",
|
||||||
|
|||||||
@ -15,18 +15,18 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Sensor struct {
|
type Sensor struct {
|
||||||
SensorID string `json:"sensor_id"`
|
SensorID string `json:"sensor_id"`
|
||||||
SensorType SType `json:"sensor_type"`
|
SensorType SType `json:"sensor_type"`
|
||||||
SamplingInterval *time.Duration `json:"sampling"`
|
SamplingInterval *time.Duration `json:"sampling"`
|
||||||
ThresholdAbove *float64 `json:"thresoldabove"`
|
ThresholdAbove *float64 `json:"thresoldabove"`
|
||||||
ThresholdBelow *float64 `json:"thresoldbelow"`
|
ThresholdBelow *float64 `json:"thresoldbelow"`
|
||||||
SensorData *[]SensorData `json:"sensor_data,omitempty"`
|
SensorData *map[int]SensorData `json:"sensor_data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SensorData struct {
|
type SensorData struct {
|
||||||
SensorID string `json:"sensor_id"`
|
SensorID string `json:"sensor_id"`
|
||||||
Value float64 `json:"value"`
|
Value *float64 `json:"value"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp *time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SensorRequest struct {
|
type SensorRequest struct {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user