Skip to content

Commit

Permalink
fix: set NaN when sensor down
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Dec 18, 2023
1 parent 469b0d2 commit 8fa71fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Previous changelog before 1.7 will not be noted here.

## [2.1.0] - 2023-12-18

- fix: set to NaN when sensor down

## [2.0.0] - 2023-12-16

- refactor: restructure project to be much better
Expand Down
8 changes: 8 additions & 0 deletions server/services/sensor_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (sensorManager *SensorManager) Register() {
sensorManager.SetValue(math.NaN(), math.NaN())
}

// Set prometheus metric values, this also set HealthStatus to true
func (sensorManager *SensorManager) SetValue(temperature float64, humidity float64) {
if !math.IsNaN(temperature) && !math.IsNaN(humidity) {
sensorManager.counters.pingReceived.Inc()
Expand All @@ -110,12 +111,19 @@ func (sensorManager *SensorManager) SetValue(temperature float64, humidity float
}
}

// Check sensor idle, if it is not updated for too long, set value to NaN
// and HealthStatus to false, and send Discord Alert after a period of time
func (sensorManager *SensorManager) HealthCheck() {
idleTime := time.Since(sensorManager.lastUpdated).Seconds()

if idleTime >= 15 {
sensorManager.gauges.healthStatus.Set(0)
sensorManager.values.healthStatus = false

sensorManager.gauges.temperature.Set(math.NaN())
sensorManager.values.temperature = math.NaN()
sensorManager.gauges.humidity.Set(math.NaN())
sensorManager.values.humidity = math.NaN()
}

if MeetsThreshold(sensorManager.alertLevel, idleTime) {
Expand Down

0 comments on commit 8fa71fc

Please sign in to comment.