You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
6 years ago
|
package core
|
||
|
|
||
6 years ago
|
import (
|
||
6 years ago
|
"crypto/sha256"
|
||
|
"encoding/hex"
|
||
6 years ago
|
"fmt"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
6 years ago
|
CheckOk = "OK"
|
||
|
CheckErr = "FAILED"
|
||
|
eventMeasurementAge = "measurement_age"
|
||
|
eventMeasurementValMin = "measurement_min"
|
||
|
eventMeasurementValMax = "measurement_max"
|
||
|
eventMeasurementValFaulty = "measurement_faulty"
|
||
|
eventTargetAll = "all" // if event.Target is this value, all sensors will be checked
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
type checkType = struct{ description string }
|
||
|
|
||
|
var checkTypes = map[string]checkType{
|
||
6 years ago
|
eventMeasurementAge: checkType{"No measurement from %s (%s) since %s"},
|
||
|
eventMeasurementValMin: checkType{"Sensor %s (%s) reads low value of %s"},
|
||
|
eventMeasurementValMax: checkType{"Sensor %s (%s) reads high value of %s"},
|
||
|
eventMeasurementValFaulty: checkType{"Sensor %s (%s) reads presumably faulty value of %s"},
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
type FaultyValue struct {
|
||
6 years ago
|
sensor string
|
||
|
val float64
|
||
|
}
|
||
|
|
||
6 years ago
|
var faultyVals = map[FaultyValue]bool{
|
||
|
FaultyValue{sensor: "BMP280", val: 0.0}: true,
|
||
|
FaultyValue{sensor: "HDC1008", val: 0.0}: true,
|
||
|
FaultyValue{sensor: "HDC1008", val: -40}: true,
|
||
|
FaultyValue{sensor: "SDS 011", val: 0.0}: true,
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
type CheckResult struct {
|
||
|
Status string
|
||
|
Event string
|
||
|
Target string
|
||
|
TargetName string
|
||
|
Value string
|
||
|
Threshold string
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
func (r CheckResult) EventID() string {
|
||
|
s := fmt.Sprintf("%s%s%s", r.Event, r.Target, r.Threshold)
|
||
|
hasher := sha256.New()
|
||
|
hasher.Write([]byte(s))
|
||
|
return hex.EncodeToString(hasher.Sum(nil))
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (r CheckResult) String() string {
|
||
|
if r.Status == CheckOk {
|
||
|
return fmt.Sprintf("%s %s (on sensor %s (%s) with value %s)\n", r.Event, r.Status, r.TargetName, r.Target, r.Value)
|
||
|
} else {
|
||
|
return fmt.Sprintf("%s: "+checkTypes[r.Event].description+"\n", r.Status, r.TargetName, r.Target, r.Value)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (box Box) RunChecks() ([]CheckResult, error) {
|
||
|
var results = []CheckResult{}
|
||
|
|
||
|
for _, event := range box.NotifyConf.Events {
|
||
|
for _, s := range box.Sensors {
|
||
|
// if a sensor never measured anything, thats ok. checks would fail anyway
|
||
|
if s.LastMeasurement == nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
6 years ago
|
// a validator must set these values
|
||
|
var (
|
||
6 years ago
|
status = CheckOk
|
||
|
target = s.Id
|
||
|
targetName = s.Phenomenon
|
||
|
value string
|
||
6 years ago
|
)
|
||
|
|
||
|
if event.Target == eventTargetAll || event.Target == s.Id {
|
||
6 years ago
|
|
||
6 years ago
|
switch event.Type {
|
||
|
case eventMeasurementAge:
|
||
|
thresh, err := time.ParseDuration(event.Threshold)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if time.Since(s.LastMeasurement.Date) > thresh {
|
||
|
status = CheckErr
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
value = s.LastMeasurement.Date.String()
|
||
6 years ago
|
|
||
6 years ago
|
case eventMeasurementValMin, eventMeasurementValMax:
|
||
|
thresh, err := strconv.ParseFloat(event.Threshold, 64)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
val, err2 := strconv.ParseFloat(s.LastMeasurement.Value, 64)
|
||
|
if err2 != nil {
|
||
|
return nil, err2
|
||
|
}
|
||
|
if event.Type == eventMeasurementValMax && val > thresh ||
|
||
|
event.Type == eventMeasurementValMin && val < thresh {
|
||
|
status = CheckErr
|
||
|
}
|
||
|
|
||
6 years ago
|
value = s.LastMeasurement.Value
|
||
6 years ago
|
|
||
6 years ago
|
case eventMeasurementValFaulty:
|
||
6 years ago
|
val, err := strconv.ParseFloat(s.LastMeasurement.Value, 64)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
if faultyVals[FaultyValue{
|
||
6 years ago
|
sensor: s.Type,
|
||
|
val: val,
|
||
|
}] {
|
||
|
status = CheckErr
|
||
|
}
|
||
|
|
||
6 years ago
|
value = s.LastMeasurement.Value
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
results = append(results, CheckResult{
|
||
6 years ago
|
Threshold: event.Threshold,
|
||
|
Event: event.Type,
|
||
|
Target: target,
|
||
|
TargetName: targetName,
|
||
|
Value: value,
|
||
|
Status: status,
|
||
6 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
return results, nil
|
||
6 years ago
|
}
|