better models and improve validation

This commit is contained in:
Pedro Pérez 2025-10-09 15:46:43 +02:00
parent 5b281bb295
commit 15b1e9c82f
2 changed files with 37 additions and 29 deletions

View File

@ -12,28 +12,31 @@ func (s *Sensor) Validate() error {
if s.SensorType == "" { if s.SensorType == "" {
return ErrInvalidSensorType return ErrInvalidSensorType
} }
return nil
if s.SamplingInterval == nil {
defaultInterval := time.Second * 3600
s.SamplingInterval = &defaultInterval
} }
func (d *SensorData) Validate() error { if s.ThresholdAbove == nil {
if d.SensorID == "" { defaultAbove := 100.0
return ErrInvalidSensorIdentifier s.ThresholdAbove = &defaultAbove
} }
if d.Value == nil {
return ErrMissingValue if s.ThresholdBelow == nil {
} defaultBelow := 0.0
if d.Timestamp.IsZero() { s.ThresholdBelow = &defaultBelow
d.Timestamp = time.Now()
} }
return nil return nil
} }
func (d *SensorData) IsOutOfRangeAbove(sensor Sensor) bool { func (d *SensorData) IsOutOfRangeAbove(sensor Sensor) bool {
return *d.Value > sensor.ThresholdAbove return d.Value > *sensor.ThresholdAbove
} }
func (d *SensorData) IsOutOfRangeBelow(sensor Sensor) bool { func (d *SensorData) IsOutOfRangeBelow(sensor Sensor) bool {
return *d.Value < sensor.ThresholdBelow return d.Value < *sensor.ThresholdBelow
} }
func (r *SensorRequest) Validate() error { func (r *SensorRequest) Validate() error {
@ -48,19 +51,23 @@ func (r *SensorDataRequest) Validate() error {
return ErrInvalidSensorIdentifier return ErrInvalidSensorIdentifier
} }
if r.To == "" { if r.To == nil || *r.To == "" {
r.To = time.Now().Format(time.RFC3339) defaultTo := time.Now().Format(time.RFC3339)
r.To = &defaultTo
} else { } else {
if _, err := time.Parse(time.RFC3339, r.To); err != nil { if _, err := time.Parse(time.RFC3339, *r.To); err != nil {
r.To = time.Now().Format(time.RFC3339) defaultTo := time.Now().Format(time.RFC3339)
r.To = &defaultTo
} }
} }
if r.From == "" { if r.From == nil || *r.From == "" {
r.From = time.Now().AddDate(0, 0, -7).Format(time.RFC3339) defaultFrom := time.Now().AddDate(0, 0, -7).Format(time.RFC3339)
r.From = &defaultFrom
} else { } else {
if _, err := time.Parse(time.RFC3339, r.From); err != nil { if _, err := time.Parse(time.RFC3339, *r.From); err != nil {
r.From = time.Now().AddDate(0, 0, -7).Format(time.RFC3339) defaultFrom := time.Now().AddDate(0, 0, -7).Format(time.RFC3339)
r.From = &defaultFrom
} }
} }
@ -72,4 +79,5 @@ var (
ErrInvalidSensorType = errors.New("sensor type is required") ErrInvalidSensorType = errors.New("sensor type is required")
ErrSensorNotFound = errors.New("sensor not found") ErrSensorNotFound = errors.New("sensor not found")
ErrMissingValue = errors.New("sensor value no provided") ErrMissingValue = errors.New("sensor value no provided")
ErrSensorAlreadyExists = errors.New("sensor already exists")
) )

View File

@ -17,15 +17,15 @@ 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 *[]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"`
} }
@ -35,6 +35,6 @@ type SensorRequest struct {
type SensorDataRequest struct { type SensorDataRequest struct {
SensorID string `json:"sensor_id"` SensorID string `json:"sensor_id"`
From string `json:"from"` From *string `json:"from"`
To string `json:"to"` To *string `json:"to"`
} }