Add "GetVar" function for checking the value of a specific variable (statelessly)

This commit is contained in:
2025-10-18 16:27:30 -04:00
parent 3e3341ac1a
commit e15cb96069
+16 -2
View File
@@ -36,8 +36,8 @@ func (c *Client) Close() error {
return c.conn.Close()
}
// GetListVar updates NutKeyValMap with the current status of all variables from <upsID>.
func (c *Client) GetListVar(upsID string) error {
// ListVar updates NutKeyValMap with the current status of all variables from <upsID>.
func (c *Client) ListVar(upsID string) error {
cmd := "LIST VAR \"" + upsID + "\""
if err := c.write(cmd); err != nil {
return err
@@ -68,6 +68,20 @@ outer:
return nil
}
// GetVar returns the value of the specified variable for <upsID>.
func (c *Client) GetVar(upsID, varName string) (string, error) {
cmd := "GET VAR \"" + upsID + "\" \"" + varName + "\""
if err := c.write(cmd); err != nil {
return "", err
}
l, err := c.read()
if err != nil {
return "", err
}
value := strings.Trim(strings.Join(strings.Split(l, " ")[3:], " "), "\"")
return value, nil
}
// newClient wraps an existing net.Conn.
func newClient(conn net.Conn) *Client {
return &Client{conn, bufio.NewReader(conn)}