lutronbroker

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCSRFToken = errors.New("failed to find csrf token")
	ErrLoginFailed = errors.New("login incorrect")
)
View Source
var ErrClosed = errors.New("broker connection is closed")

Functions

This section is empty.

Types

type Broker

type Broker struct {
	BrokerIdentifier  string            `json:"broker_identifier"`
	BrokerType        string            `json:"broker_type"`
	MQTTParams        MQTTBrokerParams  `json:"mqtt_broker_parameters"`
	ClientCertificate ClientCertificate `json:"client_certificate"`
	ETag              string            `json:"etag"`
	Priority          int               `json:"priority"`
}

Broker represents the available broker information

type BrokerConnection

type BrokerConnection[M any] struct {
	// contains filtered or unexported fields
}

A BrokerConnection manages communication channel between this process and a remote message broker.

Once a connection is established, the BrokerConnection itself has little interest in the structure of communication. The Send() method can be used to send payloads in a non-blocking fashion, and the Subscribe() method can be used to receive payloads indiscriminately. It is up to the user of these methods to filter messages and/or wait for responses to relevant API calls.

Messages sent and received are of the specified generic type M, which must be representable as JSON.

If a message is received which cannot be unmarshalled into an object of type M, then all active calls to Subscribe() will return with an error and the connection will be closed.

func NewBrokerConnection

func NewBrokerConnection[M any](
	ctx context.Context,
	creds *BrokerCredentials,
) (result *BrokerConnection[M], err error)

NewBrokerConnection connects to a remote broker.

The context ctx can be used to cancel an in-progress connection operation.

func (*BrokerConnection[M]) Call

func (b *BrokerConnection[M]) Call(
	ctx context.Context,
	msg M,
	f func(M) (bool, error),
) (incoming M, err error)

Call sends a message and then repeatedly calls f() with incoming messages until f() returns true or returns an error.

This may return an error if f() fails, or if the connection is closed, or if the context is cancelled.

If f() returns true, then the message which caused it to return true is returned with a nil error.

func (*BrokerConnection[M]) Close

func (b *BrokerConnection[M]) Close() (err error)

Close disconnects from the broker.

func (*BrokerConnection[M]) Error

func (b *BrokerConnection[M]) Error() error

Error returns an error if the connection was closed due to external reasons (rather than Close()).

func (*BrokerConnection[M]) Send

func (b *BrokerConnection[M]) Send(msg M) (err error)

Send asynchronously sends a message to the broker.

It is not guaranteed that the message has been received when this call returns, and an error may not be returned even if the message is destined to never be received.

func (*BrokerConnection[M]) Subscribe

func (b *BrokerConnection[M]) Subscribe(
	ctx context.Context,
	ch chan<- M,
	f ...func() error,
) (err error)

Subscribe listens to messages from the remote end until the context is completed, or the connection dies.

Zero or more functions may be passed, which are called after the listener is registered. This potentially allows the caller to send messages while guaranteeing that it is subscribed before a response for these messages is received.

This method will always return an error indicating why it returned. The error will wrap ErrClosed if the reason is due to the connection closing; otherwise it will wrap the context's error or an error from one of the functions f.

type BrokerCredentials

type BrokerCredentials struct {
	PrivateKey string
	DeviceCert string
	RootCA     string

	ClientID       string
	URL            string
	SubscribeTopic string
	PublishTopic   string
}

BrokerCredentials contains all of the information needed to authenticate with an MQTT broker.

func AuthenticateWithBroker

func AuthenticateWithBroker(
	ctx context.Context,
	token *OAuthToken,
	macAddr string,
	b *Broker,
) (creds *BrokerCredentials, err error)

AuthenticateWithBroker performs the steps needed to authenticate with a broker, returning the resulting credentials.

type ClientCertificate

type ClientCertificate struct {
	Subject struct {
		CommonName   string   `json:"cn"`
		Country      []string `json:"country"`
		Organization []string `json:"organization"`
		Locality     []string `json:"locality"`
		Province     []string `json:"province"`
	} `json:"subject"`
}

ClientCertificate stores the information needed to create a CSR for connecting to a broker.

type DeviceBrokerInfo

type DeviceBrokerInfo struct {
	MacAddress       string   `json:"mac_address"`
	DeviceType       string   `json:"device_type"`
	DeviceID         string   `json:"device_id"`
	AvailableBrokers []Broker `json:"available_brokers"`
}

DeviceBrokerInfo describes the available brokers for a divec. Device represents an individual device

func ListDeviceBrokers

func ListDeviceBrokers(
	ctx context.Context,
	token *OAuthToken,
	macAddr string,
) (devices []DeviceBrokerInfo, err error)

ListDeviceBrokers gets a list of brokers for the device.

type DeviceInfo

type DeviceInfo struct {
	SerialNumber string `json:"serialnumber"`
	DeviceType   string `json:"device_type"`
	FriendlyName string `json:"friendly_name"`
}

DeviceInfo describes a device returned by ListDevices().

func ListDevices

func ListDevices(ctx context.Context, token *OAuthToken) (devices []*DeviceInfo, err error)

ListDevices asks the server to list root devices.

A device can then be used in future calls to get and connect to a broker.

type MQTTBrokerParams

type MQTTBrokerParams struct {
	MQTTBrokerType string `json:"mqtt_broker_type"`
}

MQTTBrokerParams represents MQTT broker-specific parameters

type OAuthToken

type OAuthToken struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	CreatedAt    int64  `json:"created_at"`
}

func GetOAuthToken

func GetOAuthToken(ctx context.Context, email, password string) (result *OAuthToken, err error)

GetOAuthToken authenticates the user to derive an OAuth token which can be used in future HTTPS requests.

Jump to

Keyboard shortcuts

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