add validation for sensor data and fix tests

This commit is contained in:
2025-10-09 23:11:37 +02:00
parent 9d86fd394c
commit b012db856c
3 changed files with 53 additions and 29 deletions
+26 -2
View File
@@ -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 {