disconnected reboot timer for pi

This commit is contained in:
legop3
2026-01-23 16:42:13 -05:00
parent dd55514cb4
commit ec4e969340
3 changed files with 28 additions and 0 deletions
Vendored
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+28
View File
@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"os/exec"
"sync" "sync"
"time" "time"
@@ -27,7 +28,9 @@ type WSClient struct {
connMu sync.Mutex connMu sync.Mutex
connected bool connected bool
disconnectT *time.Timer disconnectT *time.Timer
rebootT *time.Timer
seekIssued bool seekIssued bool
rebootIssued bool
} }
func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient { func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient {
@@ -359,6 +362,7 @@ func (c *WSClient) ensureSensorStream() error {
} }
const disconnectSeekDelay = time.Minute const disconnectSeekDelay = time.Minute
const disconnectRebootDelay = 6 * time.Minute
const dialTimeout = 10 * time.Second const dialTimeout = 10 * time.Second
const pingInterval = 15 * time.Second const pingInterval = 15 * time.Second
const pingTimeout = 5 * time.Second const pingTimeout = 5 * time.Second
@@ -386,10 +390,15 @@ func (c *WSClient) markConnected() {
c.connMu.Lock() c.connMu.Lock()
c.connected = true c.connected = true
c.seekIssued = false c.seekIssued = false
c.rebootIssued = false
if c.disconnectT != nil { if c.disconnectT != nil {
c.disconnectT.Stop() c.disconnectT.Stop()
c.disconnectT = nil c.disconnectT = nil
} }
if c.rebootT != nil {
c.rebootT.Stop()
c.rebootT = nil
}
c.connMu.Unlock() c.connMu.Unlock()
} }
@@ -401,6 +410,9 @@ func (c *WSClient) markDisconnected() {
if c.disconnectT == nil { if c.disconnectT == nil {
c.disconnectT = time.AfterFunc(disconnectSeekDelay, c.handleDisconnectTimeout) c.disconnectT = time.AfterFunc(disconnectSeekDelay, c.handleDisconnectTimeout)
} }
if c.rebootT == nil {
c.rebootT = time.AfterFunc(disconnectRebootDelay, c.handleRebootTimeout)
}
c.connMu.Unlock() c.connMu.Unlock()
} }
@@ -420,6 +432,22 @@ func (c *WSClient) handleDisconnectTimeout() {
c.log.Printf("seek dock issued after websocket disconnect") c.log.Printf("seek dock issued after websocket disconnect")
} }
func (c *WSClient) handleRebootTimeout() {
c.connMu.Lock()
if c.connected || c.rebootIssued {
c.connMu.Unlock()
return
}
c.rebootIssued = true
c.connMu.Unlock()
c.log.Printf("rebooting pi after prolonged websocket disconnect")
cmd := exec.Command("systemctl", "reboot")
if err := cmd.Start(); err != nil {
c.log.Printf("reboot command failed: %v", err)
}
}
func (c *WSClient) recoverSensorStream(idleFor time.Duration, cmdPause time.Duration) { func (c *WSClient) recoverSensorStream(idleFor time.Duration, cmdPause time.Duration) {
c.recoverMu.Lock() c.recoverMu.Lock()
if c.recovering { if c.recovering {