better models and improve validation
This commit is contained in:
parent
5b281bb295
commit
15b1e9c82f
@ -12,28 +12,31 @@ func (s *Sensor) Validate() error {
|
|||||||
if s.SensorType == "" {
|
if s.SensorType == "" {
|
||||||
return ErrInvalidSensorType
|
return ErrInvalidSensorType
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *SensorData) Validate() error {
|
if s.SamplingInterval == nil {
|
||||||
if d.SensorID == "" {
|
defaultInterval := time.Second * 3600
|
||||||
return ErrInvalidSensorIdentifier
|
s.SamplingInterval = &defaultInterval
|
||||||
}
|
}
|
||||||
if d.Value == nil {
|
|
||||||
return ErrMissingValue
|
if s.ThresholdAbove == nil {
|
||||||
|
defaultAbove := 100.0
|
||||||
|
s.ThresholdAbove = &defaultAbove
|
||||||
}
|
}
|
||||||
if d.Timestamp.IsZero() {
|
|
||||||
d.Timestamp = time.Now()
|
if s.ThresholdBelow == nil {
|
||||||
|
defaultBelow := 0.0
|
||||||
|
s.ThresholdBelow = &defaultBelow
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
)
|
)
|
||||||
|
|||||||
@ -15,17 +15,17 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user