Setup repeat camera down notifications #24
This commit is contained in:
1 parent
7b24dad5b9
commit
e6ccd8f1d7
8 files changed
+38
-46
No files matched your search
+18
-22
@@ -13,44 +13,40 @@ import (
|
||||
)
|
||||
|
||||
type CameraMonitor struct {
|
||||
pingInterval time.Duration
|
||||
pingTimeout time.Duration
|
||||
consecutiveDownThreshold int
|
||||
downtime map[string]int
|
||||
pingInterval time.Duration
|
||||
pingTimeout time.Duration
|
||||
consecutiveDownThreshold int
|
||||
notificationRepeatThreshold int
|
||||
downtime map[string]int
|
||||
notify.Notifier
|
||||
}
|
||||
|
||||
func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, consecutiveDownThreshold int, notifier notify.Notifier) CameraMonitor {
|
||||
func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, consecutiveDownThreshold int, notificationRepeatThreshold int, notifier notify.Notifier) CameraMonitor {
|
||||
return CameraMonitor{
|
||||
pingInterval: pingInterval,
|
||||
pingTimeout: pingTimeout,
|
||||
consecutiveDownThreshold: consecutiveDownThreshold,
|
||||
downtime: make(map[string]int),
|
||||
Notifier: notifier,
|
||||
pingInterval: pingInterval,
|
||||
pingTimeout: pingTimeout,
|
||||
consecutiveDownThreshold: consecutiveDownThreshold,
|
||||
notificationRepeatThreshold: notificationRepeatThreshold,
|
||||
downtime: make(map[string]int),
|
||||
Notifier: notifier,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraUp(camera string) {
|
||||
c.SendNotification(camera, true)
|
||||
slog.Info(fmt.Sprintf("%s camera is back online!", camera))
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraDown(camera string) {
|
||||
c.SendNotification(camera, false)
|
||||
slog.Info(fmt.Sprintf("%s camera is offline!", camera))
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
|
||||
if online {
|
||||
if c.downtime[camera] >= c.consecutiveDownThreshold {
|
||||
c.onCameraUp(camera)
|
||||
c.SendNotification(camera, true)
|
||||
slog.Info(fmt.Sprintf("%s camera is back online!", camera))
|
||||
}
|
||||
c.downtime[camera] = 0
|
||||
} else {
|
||||
c.downtime[camera] += 1
|
||||
if c.downtime[camera] == c.consecutiveDownThreshold {
|
||||
c.onCameraDown(camera)
|
||||
c.SendNotification(camera, false)
|
||||
slog.Info(fmt.Sprintf("%s camera is offline!", camera))
|
||||
} else if c.notificationRepeatThreshold >= c.consecutiveDownThreshold && c.downtime[camera]%c.notificationRepeatThreshold == c.consecutiveDownThreshold {
|
||||
c.SendNotification(camera, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user