add validation for sensor data and fix tests
This commit is contained in:
parent
9d86fd394c
commit
b012db856c
@ -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 {
|
||||
|
||||
@ -208,8 +208,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
||||
name: "value above threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 150.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(150.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -221,8 +221,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
||||
name: "value below threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 50.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(50.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -234,8 +234,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
||||
name: "value equal to threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 100.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(100.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -247,8 +247,8 @@ func Test_SensorData_IsOutOfRangeAbove(t *testing.T) {
|
||||
name: "negative value above negative threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: -5.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(-5.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -282,8 +282,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
||||
name: "value below threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 5.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(5.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -295,8 +295,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
||||
name: "value above threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 50.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(50.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -308,8 +308,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
||||
name: "value equal to threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 10.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(10.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -321,8 +321,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
||||
name: "negative value below threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: -15.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(-15.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
@ -334,8 +334,8 @@ func Test_SensorData_IsOutOfRangeBelow(t *testing.T) {
|
||||
name: "zero value below positive threshold",
|
||||
data: SensorData{
|
||||
SensorID: "temp-001",
|
||||
Value: 0.0,
|
||||
Timestamp: time.Now(),
|
||||
Value: ptr(0.0),
|
||||
Timestamp: ptr(time.Now()),
|
||||
},
|
||||
sensor: Sensor{
|
||||
SensorID: "temp-001",
|
||||
|
||||
@ -20,13 +20,13 @@ type Sensor struct {
|
||||
SamplingInterval *time.Duration `json:"sampling"`
|
||||
ThresholdAbove *float64 `json:"thresoldabove"`
|
||||
ThresholdBelow *float64 `json:"thresoldbelow"`
|
||||
SensorData *[]SensorData `json:"sensor_data,omitempty"`
|
||||
SensorData *map[int]SensorData `json:"sensor_data,omitempty"`
|
||||
}
|
||||
|
||||
type SensorData struct {
|
||||
SensorID string `json:"sensor_id"`
|
||||
Value float64 `json:"value"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Value *float64 `json:"value"`
|
||||
Timestamp *time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
type SensorRequest struct {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user