Files
devplacepy/nodes/client/battery.go
T
retoorandClaude Sonnet 5.5 02ab747cd0
DevPlace CI / test (push) Failing after 44m45s
Code Farm: per-plot auto-replant pause and crop-lock check; community sync and node work
Continuous Delivery could never be turned off once a crop was planted. Add a
one-shot per-plot "stop after harvest" flag (POST /game/plot/stop, Devii tool
game_plot_stop, game_plots.stop_after_harvest), cleared by the next plant,
autoreplant claim or Refactor. Autoreplant now also respects crop locks through
the shared _crop_lock predicate. Plan and measurements in happyplanting.md.

Also includes the pending community sync, node network and related work in the
tree; the full suite passed (4999) with all of it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-17 02:23:43 +02:00

54 lines
1.1 KiB
Go

package main
import (
"strings"
"sync"
"time"
)
const batteryCacheSeconds = 15
type BatteryProbe func() (bool, error)
type batteryWatch struct {
mu sync.Mutex
probe BatteryProbe
now func() time.Time
debugf func(format string, args ...any)
checked time.Time
value bool
warned bool
}
func newBatteryWatch(probe BatteryProbe, now func() time.Time) *batteryWatch {
return &batteryWatch{probe: probe, now: now}
}
func (w *batteryWatch) OnBattery() bool {
w.mu.Lock()
defer w.mu.Unlock()
moment := w.now()
if !w.checked.IsZero() && moment.Sub(w.checked) < time.Duration(batteryCacheSeconds)*time.Second {
return w.value
}
w.checked = moment
value, err := w.probe()
if err != nil {
value = false
if !w.warned && w.debugf != nil {
w.debugf("battery state unknown, treating as mains power: %v", err)
}
w.warned = true
}
w.value = value
return value
}
func parsePmset(text string) bool {
return strings.Contains(text, "Now drawing from 'Battery Power'")
}
func windowsOnBattery(acLine, batteryFlag byte) bool {
return acLine == 0 && batteryFlag != 128 && batteryFlag != 255
}