This commit is contained in:
2026-01-10 03:26:17 +01:00
parent bceac40518
commit 8de9a0dd87
20 changed files with 179 additions and 1636 deletions

View File

@@ -3,10 +3,7 @@ package powergenius
import (
"bufio"
"fmt"
<<<<<<< HEAD
=======
"log"
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
"math"
"net"
"strconv"
@@ -26,6 +23,10 @@ type Client struct {
statusMu sync.RWMutex
stopChan chan struct{}
running bool
// Auto fan management
autoFanEnabled bool
lastFanMode string // Remember last manual mode
}
type Status struct {
@@ -45,17 +46,19 @@ type Status struct {
BandB string `json:"band_b"`
FaultPresent bool `json:"fault_present"`
Connected bool `json:"connected"`
<<<<<<< HEAD
=======
Meffa string `json:"meffa"`
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
// Peak hold for display (internal)
displayPower float64
peakTime time.Time
}
func New(host string, port int) *Client {
return &Client{
host: host,
port: port,
stopChan: make(chan struct{}),
host: host,
port: port,
stopChan: make(chan struct{}),
autoFanEnabled: true, // Auto fan management enabled by default
lastFanMode: "Contest", // Default to Contest mode
}
}
@@ -96,24 +99,14 @@ func (c *Client) Close() error {
// Start begins continuous polling of the device
func (c *Client) Start() error {
<<<<<<< HEAD
=======
if err := c.Connect(); err != nil {
return err
}
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
if c.running {
return nil
}
<<<<<<< HEAD
// Try to connect, but don't fail if it doesn't work
// The poll loop will keep trying
_ = c.Connect()
=======
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
c.running = true
go c.pollLoop()
@@ -128,7 +121,6 @@ func (c *Client) pollLoop() {
for {
select {
case <-ticker.C:
<<<<<<< HEAD
// Try to reconnect if not connected
c.connMu.Lock()
if c.conn == nil {
@@ -152,12 +144,6 @@ func (c *Client) pollLoop() {
status, err := c.queryStatus()
if err != nil {
// Connection lost, close and retry next tick
=======
status, err := c.queryStatus()
if err != nil {
log.Printf("PowerGenius query error: %v", err)
// Try to reconnect
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
c.connMu.Lock()
if c.conn != nil {
c.conn.Close()
@@ -165,7 +151,6 @@ func (c *Client) pollLoop() {
}
c.connMu.Unlock()
<<<<<<< HEAD
// Mark as disconnected and reset all values
c.statusMu.Lock()
c.lastStatus = &Status{
@@ -178,21 +163,37 @@ func (c *Client) pollLoop() {
// Mark as connected
status.Connected = true
=======
if err := c.Connect(); err != nil {
log.Printf("PowerGenius reconnect failed: %v", err)
// Peak hold logic - keep highest power for 1 second
now := time.Now()
if c.lastStatus != nil {
// If new power is higher, update peak
if status.PowerForward > c.lastStatus.displayPower {
status.displayPower = status.PowerForward
status.peakTime = now
} else {
// Check if peak has expired (1 second)
if now.Sub(c.lastStatus.peakTime) < 1*time.Second {
// Keep old peak
status.displayPower = c.lastStatus.displayPower
status.peakTime = c.lastStatus.peakTime
} else {
// Peak expired, use current value
status.displayPower = status.PowerForward
status.peakTime = now
}
}
continue
} else {
status.displayPower = status.PowerForward
status.peakTime = now
}
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
// Override PowerForward with display power for frontend
status.PowerForward = status.displayPower
// Merge with existing status (spontaneous messages may only update some fields)
c.statusMu.Lock()
if c.lastStatus != nil {
// Keep existing values for fields not in the new status
if status.PowerForward == 0 && c.lastStatus.PowerForward != 0 {
status.PowerForward = c.lastStatus.PowerForward
}
if status.Temperature == 0 && c.lastStatus.Temperature != 0 {
status.Temperature = c.lastStatus.Temperature
}
@@ -215,6 +216,32 @@ func (c *Client) pollLoop() {
c.lastStatus = status
c.statusMu.Unlock()
// Auto fan management based on temperature
if c.autoFanEnabled {
temp := status.Temperature
currentMode := status.FanMode
// If temp >= 60°C, switch to Broadcast
if temp >= 60.0 && currentMode != "Broadcast" {
log.Printf("PowerGenius: Temperature %.1f°C >= 60°C, switching fan to Broadcast mode", temp)
if err := c.SetFanMode("Broadcast"); err != nil {
log.Printf("PowerGenius: Failed to set fan mode: %v", err)
}
}
// If temp <= 55°C, switch back to Contest (or last manual mode)
if temp <= 55.0 && currentMode == "Broadcast" {
targetMode := c.lastFanMode
if targetMode == "" || targetMode == "Broadcast" {
targetMode = "Contest"
}
log.Printf("PowerGenius: Temperature %.1f°C <= 55°C, switching fan back to %s mode", temp, targetMode)
if err := c.SetFanMode(targetMode); err != nil {
log.Printf("PowerGenius: Failed to set fan mode: %v", err)
}
}
}
case <-c.stopChan:
return
}
@@ -361,11 +388,6 @@ func (c *Client) parseStatus(resp string) (*Status, error) {
}
case "vac":
status.Voltage, _ = strconv.ParseFloat(value, 64)
<<<<<<< HEAD
=======
case "meffa":
status.Meffa = value
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
case "vdd":
status.VDD, _ = strconv.ParseFloat(value, 64)
case "id":
@@ -430,15 +452,20 @@ func (c *Client) SetFanMode(mode string) error {
"BROADCAST": true,
}
if !validModes[mode] {
// Normalize mode to title case for comparison
modeUpper := strings.ToUpper(mode)
if !validModes[modeUpper] {
return fmt.Errorf("invalid fan mode: %s (must be STANDARD, CONTEST, or BROADCAST)", mode)
}
cmd := fmt.Sprintf("setup fanmode=%s", mode)
// Remember last manual mode (if not triggered by auto-fan)
// We store it in title case: "Standard", "Contest", "Broadcast"
c.lastFanMode = strings.Title(strings.ToLower(mode))
cmd := fmt.Sprintf("setup fanmode=%s", modeUpper)
_, err := c.sendCommand(cmd)
return err
}
<<<<<<< HEAD
// SetOperate sets the operate mode
// value can be: 0 (STANDBY) or 1 (OPERATE)
@@ -451,5 +478,3 @@ func (c *Client) SetOperate(value int) error {
_, err := c.sendCommand(cmd)
return err
}
=======
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7