add models and validation
This commit is contained in:
parent
37e65ddd76
commit
d342efadbc
@ -1 +1,75 @@
|
|||||||
package sensors
|
package sensors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Sensor) Validate() error {
|
||||||
|
if s.SensorID == "" {
|
||||||
|
return ErrInvalidSensorIdentifier
|
||||||
|
}
|
||||||
|
if s.SensorType == "" {
|
||||||
|
return ErrInvalidSensorType
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SensorData) Validate() error {
|
||||||
|
if d.SensorID == "" {
|
||||||
|
return ErrInvalidSensorIdentifier
|
||||||
|
}
|
||||||
|
if d.Value == nil {
|
||||||
|
return ErrMissingValue
|
||||||
|
}
|
||||||
|
if d.Timestamp.IsZero() {
|
||||||
|
d.Timestamp = time.Now()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SensorData) IsOutOfRangeAbove(sensor Sensor) bool {
|
||||||
|
return *d.Value > sensor.ThresholdAbove
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *SensorData) IsOutOfRangeBelow(sensor Sensor) bool {
|
||||||
|
return *d.Value < sensor.ThresholdBelow
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SensorRequest) Validate() error {
|
||||||
|
if r.SensorID == "" {
|
||||||
|
return ErrInvalidSensorIdentifier
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SensorDataRequest) Validate() error {
|
||||||
|
if r.SensorID == "" {
|
||||||
|
return ErrInvalidSensorIdentifier
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.To == "" {
|
||||||
|
r.To = time.Now().Format(time.RFC3339)
|
||||||
|
} else {
|
||||||
|
if _, err := time.Parse(time.RFC3339, r.To); err != nil {
|
||||||
|
r.To = time.Now().Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.From == "" {
|
||||||
|
r.From = time.Now().AddDate(0, 0, -7).Format(time.RFC3339)
|
||||||
|
} else {
|
||||||
|
if _, err := time.Parse(time.RFC3339, r.From); err != nil {
|
||||||
|
r.From = time.Now().AddDate(0, 0, -7).Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidSensorIdentifier = errors.New("sensor identifier is required")
|
||||||
|
ErrInvalidSensorType = errors.New("sensor type is required")
|
||||||
|
ErrSensorNotFound = errors.New("sensor not found")
|
||||||
|
ErrMissingValue = errors.New("sensor value no provided")
|
||||||
|
)
|
||||||
|
|||||||
@ -24,6 +24,17 @@ type Sensor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SensorData struct {
|
type SensorData struct {
|
||||||
Value float64 `json:"value"`
|
SensorID string `json:"sensor_id"`
|
||||||
|
Value *float64 `json:"value"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SensorRequest struct {
|
||||||
|
SensorID string `json:"sensor_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SensorDataRequest struct {
|
||||||
|
SensorID string `json:"sensor_id"`
|
||||||
|
From string `json:"from"`
|
||||||
|
To string `json:"to"`
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user