Documentation
¶
Overview ¶
Package dhdcontrolapi provides a client for the DHD control API.
Both the WebSocket and REST API are supported.
Index ¶
- Variables
- type AccessEvent
- type Client
- type ClientOptions
- type ExtListEntry
- type ExtListNotifiedEvent
- type MessageEvent
- type NodeValueEvent
- type Request
- func NewAuthRequest(token string) Request
- func NewExtListRegisterRequest(list int) Request
- func NewExtListUnregisterRequest(list int) Request
- func NewGetAccessRequest() Request
- func NewGetRequest(path string) Request
- func NewRPCRequest(payload any) Request
- func NewSetExtListRequest(list int, entries []ExtListEntry, selectedEntry int) Request
- func NewSetRequest(path string, payload any) Request
- func NewSubscribeRequest(path string) Request
- func NewUnsubscribeRequest(path string) Request
- type WebSocket
- func (ws *WebSocket) Close() error
- func (ws *WebSocket) Connect() error
- func (ws *WebSocket) Get(path string) (any, error)
- func (ws *WebSocket) GetAccess() (any, error)
- func (ws *WebSocket) RPC(payload any) (any, error)
- func (ws *WebSocket) RegisterExtList(list int) error
- func (ws *WebSocket) RequestAndWait(request Request) (WebSocketMessage, error)
- func (ws *WebSocket) RequestAndWaitForPayload(request Request) (any, error)
- func (ws *WebSocket) Run(eventChannel chan any)
- func (ws *WebSocket) Send(request Request)
- func (ws *WebSocket) Set(path string, payload any) (any, error)
- func (ws *WebSocket) SetExtList(list int, entries []ExtListEntry, selectedEntry int) error
- func (ws *WebSocket) Subscribe(path string) error
- func (ws *WebSocket) UnregisterExtList(list int) error
- func (ws *WebSocket) Unsubscribe(path string) error
- type WebSocketMessage
- type WebSocketMessageError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnexpectedAuthReply is returned when an unexpected reply to an auth request is received. ErrUnexpectedAuthReply = errors.New("unexpected reply to auth request") // ErrAuthFailed is returned when authentication failed. ErrAuthFailed = errors.New("authentication failed") // ErrAwaitTimeout is returned when a reply to a request was not received in time. ErrAwaitTimeout = errors.New("timeout awaiting reply") )
Functions ¶
This section is empty.
Types ¶
type AccessEvent ¶
AccessEvent is a WebSocket event that occurs when the access state of a mixer changes.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a connection to the DHD control API.
func NewClient ¶
func NewClient(options *ClientOptions) *Client
NewClient creates a new Control API client.
func (*Client) GetFlat ¶
GetFlat performs a "get" request on the specified path with the "flat" option, which returns a list of all nodes under the specified path.
func (*Client) NewWebSocket ¶
NewWebSocket creates a new WebSocket connection to the DHD Control API.
Any error occuring during connection or authentication phase will be reported through the returned error.
If an (optional) event channel is supplied, the WebSocket will start sending notifications to that channel whenver an event is received and parsed.
The channel will be closed when the connection is terminated.
The caller must call Close on the WebSocket when done.
func (*Client) Options ¶
func (c *Client) Options() ClientOptions
Options returns the options of the client.
type ClientOptions ¶
type ClientOptions struct { // Base URL of the DHD control API, including the protocol and port, but exluding the /api/ws or /api/rest part. // The URL may contain basic authentication credentials, like http://user:pass@host:port. URL string // Token to authenticate with the DHD control API. Token string // InsecureTLS allows to disable TLS certificate verification. InsecureTLS bool // The duration that the WebSocket request methods wait for a reply before timing out. // If set to 0, the default timeout of 5 seconds will be used. WebSocketReplyTimeout time.Duration }
ClientOptions represents the options for a ControlAPI client.
type ExtListEntry ¶
type ExtListEntry struct { Label string `json:"label"` Color string `json:"color,omitempty"` Background bool `json:"background,omitempty"` Disabled bool `json:"disabled,omitempty"` Pagination bool `json:"pagination,omitempty"` }
ExtListEntry represents an entry in an external button list
type ExtListNotifiedEvent ¶
ExtListNotifiedEvent is a WebSocket event that occurs when the user presses or releases an external button.
type MessageEvent ¶
type MessageEvent struct {
Message any
}
MessageEvent is a basic event that carries the payload of a WebSocket message
type NodeValueEvent ¶
NodeValueEvent is a WebSocket event that occurs when a node value changes, or as a reply to a get request. If the get reply (or update) contains values for multiple nodes, each node will be reported as a separate event.
type Request ¶
Request represents a request to the DHD Control WebSocket API, which is generally a map of string keys to any values, marshalled to a JSON object when sent through the WebSocket.
func NewAuthRequest ¶
NewAuthRequest creates an authentication request.
func NewExtListRegisterRequest ¶
NewExtListRegisterRequest creates an RPC request to register an external button list.
func NewExtListUnregisterRequest ¶
NewExtListUnregisterRequest creates an RPC request to unregister an external button list.
func NewGetAccessRequest ¶
func NewGetAccessRequest() Request
NewGetAccessRequest creates an RPC request to get and subscribe to the access state.
func NewRPCRequest ¶
NewRPCRequest creates an RPC request with the given payload.
func NewSetExtListRequest ¶
func NewSetExtListRequest(list int, entries []ExtListEntry, selectedEntry int) Request
NewSetExtListRequest creates an RPC request to update an external button list.
func NewSetRequest ¶
NewSetRequest creates a set request.
func NewSubscribeRequest ¶
NewSubscribeRequest creates a subscribe request.
func NewUnsubscribeRequest ¶
NewUnsubscribeRequest creates an unsubscribe request.
type WebSocket ¶
type WebSocket struct {
// contains filtered or unexported fields
}
WebSocket represents a connection to a DHD device via the WebSocket API.
func NewWebSocket ¶
func NewWebSocket(options *ClientOptions) *WebSocket
NewWebSocket creates a new WebSocket connection. The url must be the full URL to the WebSocket API, including the protocol and port, and the "/api/ws" part. http/https are automatically converted to ws/wss.
func (*WebSocket) Connect ¶
Connect establishes a connection to the DHD device, and authenticates if a token is configured.
func (*WebSocket) Get ¶
Get sends a "get" request to the DHD device.
This method is synchronous. It sends the request, waits for the reply, and returns the requested value, or an error.
func (*WebSocket) GetAccess ¶
GetAccess sends a "getaccess" RPC request to the DHD device.
It returns the current access state of the device, or an error.
The command will also enable subscription for access state changes, which are notified through the event channel as AccessEvent.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) RPC ¶
RPC sends an RPC request to the DHD device.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) RegisterExtList ¶
RegisterExtList sends a "registerextlist" RPC request to the DHD device.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) RequestAndWait ¶
func (ws *WebSocket) RequestAndWait(request Request) (WebSocketMessage, error)
RequestAndWait sends a request to the DHD device, and waits for a reply.
If no reply is received within awaitReplyTimeout, an error is returned.
func (*WebSocket) RequestAndWaitForPayload ¶
RequestAndWaitForPayload sends a request to the DHD device, and waits for a reply. On success, the payload will be extracted and returned.
If no reply is received within awaitReplyTimeout, an error is returned.
func (*WebSocket) Run ¶
Run reads messages from the websocket connection and parses them.
If an event channel is supplied, the raw and parsed messages will be sent to that channel. The channel will be closed when the connection is closed.
If no event channel is supplied, the function will only handle awaited replies.
func (*WebSocket) Send ¶
Send sends a request to the DHD device over the WebSocket connection. The request will be marshalled to JSON.
The message will be sent asynchronously, and the function returns immediately. Any reply from the device will be handled by the Run function and sent to the event channel.
func (*WebSocket) Set ¶
Set sends a "set" request to the DHD device. It returns the new value as confirmed by the device, or an error.
This method is synchronous. It sends the request, waits for the reply, and returns the requested value, or an error.
func (*WebSocket) SetExtList ¶
func (ws *WebSocket) SetExtList(list int, entries []ExtListEntry, selectedEntry int) error
SetExtList sends a "setextlist" RPC request to the DHD device.
This will populate the external button list with the specified entries, and select the specified entry.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) Subscribe ¶
Subscribe sends a "subscribe" request to the DHD device, and waits for confirmation.
The device will start sending events when any parameters at or below the specified path changes, until the connection is terminated, or an "unsubscribe" request is sent.
Received events will be sent to the event channel as NodeValueEvent.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) UnregisterExtList ¶
UnregisterExtList sends an "unregisterextlist" RPC request to the DHD device.
This method is synchronous. It sends the request and waits for the reply.
func (*WebSocket) Unsubscribe ¶
Unsubscribe sends an "unsubscribe" request to the DHD device.
This method is synchronous. It sends the request and waits for the reply.
type WebSocketMessage ¶
type WebSocketMessage struct { MsgID string `mapstructure:"msgID"` Method string `mapstructure:"method"` Path string `mapstructure:"path"` Payload any `mapstructure:"payload"` Success bool `mapstructure:"success"` Error WebSocketMessageError `mapstructure:"error"` }
WebSocketMessage is an unmarshalled message received from the API over WebSocket.
Structs of this type are sent to the event channel whenever a well-formed message is received.
type WebSocketMessageError ¶
type WebSocketMessageError struct { Code int `mapstructure:"code"` Message string `mapstructure:"message"` }
WebSocketMessageError is the "error" field in a WebSocket message received from the API.
It also implements the Error interface, and is being used to signal errors in the various synchronous request methods.
func (WebSocketMessageError) Error ¶
func (e WebSocketMessageError) Error() string
Error implements the error interface