nutclient

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 10 Imported by: 1

README

nutclient

Go Reference MIT License

This package provides a very simple NUT client for Go.

Usage

To use the package in your program, begin by importing it:

import "github.com/nathan-osman/nutclient"

Next, create a Client instance using nutclient.New().

The Config struct passed to New specifies the address of the NUT server and the name of the UPS you are connecting to. It also allows you to specify the callbacks that will be invoked when power events occur:

c := nutclient.New(&nutclient.Config{
    Addr: "localhost:3493",
    Name: "ups",
    ConnectedFn: func() {
        fmt.Println("Connected!")
    },
    DisconnectedFn: func() {
        fmt.Println("Disconnected!")
    },
    PowerLostFn: func() {
        fmt.Println("Power lost!")
    },
    PowerRestoredFn: func() {
        fmt.Println("Power restored!")
    },
})
defer c.Close()

To obtain all status values, use Status(). For example, to manually obtain the UPS battery status, use:

var statusVars map[string]string
statusVars = c.Status()
if statusVars != nil {
    fmt.Printf(
        "Battery: %s\n",
        statusVars["ups.status"],
    )
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client connects to a NUT server and monitors it for events.

func New

func New(cfg *Config) *Client

New creates a new Client instance for the specified server.

func (*Client) Close

func (c *Client) Close()

Close shuts down the client. It is guaranteed that no more callbacks will be invoked after this method returns.

func (*Client) Status added in v1.1.0

func (c *Client) Status() map[string]string

Status returns the current status of the UPS. This will be the value from the last time it was polled. If an error occurred or the status is not yet available, nil is returned.

type Config

type Config struct {

	// Addr specifies the address and port of the NUT server. If unset,
	// "localhost:3493" is assumed.
	Addr string

	// Name specifies the name of the UPS to monitor. If unset, "ups" is used.
	Name string

	// ReconnectInterval specifies the duration between attempts to reconnect
	// to the server when the connection is lost. If unset, the default is 30
	// seconds.
	ReconnectInterval time.Duration

	// PollInterval specifies how often the status of the UPS should be polled.
	// If unset, the default is 5 seconds.
	PollInterval time.Duration

	// ConnectedFn is invoked every time a connection is established with the
	// server.
	ConnectedFn func()

	// DisconnectedFn is invoked every time the connection to the server is
	// lost.
	DisconnectedFn func()

	// PowerLostFn is invoked every time line power is disconnected.
	PowerLostFn func()

	// PowerRestoredFn is invoked every time line power is restored.
	PowerRestoredFn func()
}

Config provides a set of configuration parameters for the client and callback functions that can be used for reacting to events.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL