add validation for sensor data and fix tests
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user