This commit is contained in:
2026-01-09 23:55:14 +01:00
16 changed files with 1514 additions and 0 deletions

View File

@@ -3,6 +3,10 @@ package powergenius
import (
"bufio"
"fmt"
<<<<<<< HEAD
=======
"log"
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
"math"
"net"
"strconv"
@@ -41,6 +45,10 @@ type Status struct {
BandB string `json:"band_b"`
FaultPresent bool `json:"fault_present"`
Connected bool `json:"connected"`
<<<<<<< HEAD
=======
Meffa string `json:"meffa"`
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
}
func New(host string, port int) *Client {
@@ -88,14 +96,24 @@ 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()
@@ -110,6 +128,7 @@ func (c *Client) pollLoop() {
for {
select {
case <-ticker.C:
<<<<<<< HEAD
// Try to reconnect if not connected
c.connMu.Lock()
if c.conn == nil {
@@ -133,6 +152,12 @@ 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()
@@ -140,6 +165,7 @@ func (c *Client) pollLoop() {
}
c.connMu.Unlock()
<<<<<<< HEAD
// Mark as disconnected and reset all values
c.statusMu.Lock()
c.lastStatus = &Status{
@@ -152,6 +178,14 @@ func (c *Client) pollLoop() {
// Mark as connected
status.Connected = true
=======
if err := c.Connect(); err != nil {
log.Printf("PowerGenius reconnect failed: %v", err)
}
continue
}
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7
// Merge with existing status (spontaneous messages may only update some fields)
c.statusMu.Lock()
if c.lastStatus != nil {
@@ -327,6 +361,11 @@ 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":
@@ -399,6 +438,7 @@ func (c *Client) SetFanMode(mode string) error {
_, err := c.sendCommand(cmd)
return err
}
<<<<<<< HEAD
// SetOperate sets the operate mode
// value can be: 0 (STANDBY) or 1 (OPERATE)
@@ -411,3 +451,5 @@ func (c *Client) SetOperate(value int) error {
_, err := c.sendCommand(cmd)
return err
}
=======
>>>>>>> 4ab192418e21065c68d59777493ea03b76c061e7