bridgeapi

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is issued as soon a a token is invalid or the token parameter is missing
	ErrInvalidToken = errors.New("token is invalid or a hashed token parameter is missing")

	// ErrUnknownDevice is issued when the given Nuki device is unknown
	ErrUnknownDevice = errors.New("the given Nuki device is unknown")

	// ErrDeviceIsTemporarilyOffline is issued when the Nuki device is temporarily offline
	ErrDeviceIsTemporarilyOffline = errors.New("the given Nuki device is offline")

	// ErrInvalidURL is issued when the given URL is invalid or too long
	ErrInvalidURL = errors.New("the given URL is invalid or too long")

	// ErrAnotherRequestIsAlreadyRunning is issued when the device is already busy
	ErrAnotherRequestIsAlreadyRunning = errors.New("another request already running on the device. Increase intervals between API calls sent to the Bridge as it can only handle one request at a time")

	// ErrDeviceIsOffline indicates that the device was not seen by the bridge for a longer period of time
	ErrDeviceIsOffline = errors.New("the device was noticed to be offline for a longer period of time")
)

Functions

func IsValidBridgeHost

func IsValidBridgeHost(bridgeHost string) (bool, error)

IsValidBridgeHost checks for validity of foven address

func ScanOnConnect

func ScanOnConnect() func(*Connection)

ScanOnConnect may be used as an options when connection and requests scanning info from the bridge on creation of the connection.

Example
package main

import (
	"github.com/christianschmizz/go-nukibridgeapi/pkg/nuki/bridgeapi"
)

func main() {
	_, err := bridgeapi.ConnectWithToken("192.168.1.11:8080", "abcdef", bridgeapi.ScanOnConnect())
	if err != nil {
		panic(err)
	}
}
Output:

func UseClient added in v1.0.2

func UseClient(client HTTPClient) func(*Connection)

UseClient uses the given client

Example
package main

import (
	"net/http"

	"github.com/christianschmizz/go-nukibridgeapi/pkg/nuki/bridgeapi"
)

func main() {
	_, err := bridgeapi.ConnectWithToken("192.168.1.11:8080", "abcdef", bridgeapi.UseClient(&http.Client{}))
	if err != nil {
		panic(err)
	}
}
Output:

func Wait

func Wait() func(*lockActionOptions)

Wait option is used for synchronously calling the API

Types

type APIResponseHandler added in v1.0.2

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

APIResponseHandler eases working with a http response

func NewAPIResponseHandler added in v1.0.2

func NewAPIResponseHandler(resp *http.Response) (*APIResponseHandler, error)

NewAPIResponseHandler returns a new response handler

func (*APIResponseHandler) Decode added in v1.0.2

func (r *APIResponseHandler) Decode(v interface{}) error

Decode decodes the response's body to the given value

func (*APIResponseHandler) Is added in v1.0.2

func (r *APIResponseHandler) Is(statusCode int) bool

Is checks the response for the given status code

type AddCallbackResponse

type AddCallbackResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
}

AddCallbackResponse represents the result of an request

type BridgeInfo

type BridgeInfo struct {
	BridgeID    int       `json:"bridgeId,"`
	IP          string    `json:"ip,"`
	Port        int       `json:"port,"`
	DateUpdated time.Time `json:"dateUpdated,"`
}

BridgeInfo contains the basic information of a bridge device

type BridgeType

type BridgeType int

BridgeType describes the type of a bridge

const (
	// TypeHardware represents a hardware based bridging device
	TypeHardware BridgeType = 1

	// TypeSoftware represents a software based bridge on Android
	TypeSoftware BridgeType = 2
)

type Callback

type Callback struct {
	ID  int    `json:"id"`
	URL string `json:"url"`
}

Callback is an item at the list of callbacks at a bridge

type Connection

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

Connection holds all information required for communication with a bridge

func ConnectWithToken

func ConnectWithToken(bridgeHost, token string, options ...func(*Connection)) (*Connection, error)

ConnectWithToken sets up a connection to the bridge using the given token for authentication

Example
package main

import (
	"fmt"

	"github.com/christianschmizz/go-nukibridgeapi/pkg/nuki/bridgeapi"
)

func main() {
	conn, err := bridgeapi.ConnectWithToken("192.168.1.11:8080", "abcdef")
	if err != nil {
		panic(err)
	}
	info, err := conn.Info()
	if err != nil {
		panic(err)
	}
	for _, result := range info.ScanResults {
		fmt.Println(result.Name)
	}
}
Output:

func (*Connection) AddCallback

func (c *Connection) AddCallback(callbackURL string) (*AddCallbackResponse, error)

AddCallback requests the addition of the given callback

func (*Connection) Info

func (c *Connection) Info() (*InfoResponse, error)

Info requests comprehensive information from the bridge

func (*Connection) ListCallbacks

func (c *Connection) ListCallbacks() (*ListCallbacksResponse, error)

ListCallbacks request a list of registered callbacks from the bridge

func (*Connection) ListPairedDevices

func (c *Connection) ListPairedDevices() (ListPairedDevicesResponse, error)

ListPairedDevices retrieves a list of all devices paired with the bridge

func (*Connection) Lock

func (c *Connection) Lock(nukiID nuki.ID, options ...func(*lockOptions)) (*LockResponse, error)

Lock sends a simple lock action "lock" to the given device

func (*Connection) LockAction

func (c *Connection) LockAction(nukiID nuki.ID, action nuki.LockAction, options ...func(*lockActionOptions)) (*LockActionResponse, error)

LockAction performs a action on the device with the given ID.

func (*Connection) LockState

func (c *Connection) LockState(nukiID nuki.ID) (*LockStateResponse, error)

LockState retrieves the current state of the given device

func (*Connection) Log

func (c *Connection) Log(offset, count int) (Log, error)

Log fetches the given number of logs from the bridge starting with the given offset.

func (*Connection) RemoveCallback

func (c *Connection) RemoveCallback(callbackID int) (*RemoveCallbackResponse, error)

RemoveCallback requests the removal of the given callback from the bridge

func (*Connection) Unlock

func (c *Connection) Unlock(nukiID nuki.ID, options ...func(*unlockOptions)) (*UnlockResponse, error)

Unlock sends a simple lock action "lock" to the given device

type DeviceInfo

type DeviceInfo struct {
	ID              int             `json:"nukiId"`
	Type            nuki.DeviceType `json:"deviceType"`
	Name            string          `json:"name"`
	FirmwareVersion string          `json:"firmwareVersion"`
	LastKnownState  LastKnownState  `json:"lastKnownState"`
}

DeviceInfo describes some basic information of a device

type DiscoverResponse

type DiscoverResponse struct {
	Bridges   []BridgeInfo `json:"bridges"`
	ErrorCode int          `json:"errorCode,"`
}

DiscoverResponse represents the result of a discovery request

func Discover

func Discover() (*DiscoverResponse, error)

Discover requests a list of registered bridges from the public nuki API at the web

type ErrInvalidAction added in v1.0.2

type ErrInvalidAction struct {
	Action nuki.LockAction
}

ErrInvalidAction is issued when the given action was invalid

func (*ErrInvalidAction) Error added in v1.0.2

func (e *ErrInvalidAction) Error() string

type HTTPClient added in v1.0.2

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient interface

type InfoResponse

type InfoResponse struct {
	BridgeType BridgeType `json:"bridgeType"`
	IDs        struct {
		HardwareID int `json:"hardwareId"`
		ServerID   int `json:"serverId"`
	} `json:"ids"`
	Versions struct {
		FirmwareVersion     string `json:"firmwareVersion"`
		WifiFirmwareVersion string `json:"wifiFirmwareVersion"`
	} `json:"versions"`
	Uptime          int          `json:"uptime"`
	CurrentTime     time.Time    `json:"currentTime"`
	ServerConnected bool         `json:"serverConnected"`
	ScanResults     []ScanResult `json:"scanResults"`
}

InfoResponse represents the result of an info request

type LastKnownState

type LastKnownState struct {
	Mode                  nuki.LockMode        `json:"mode"`
	State                 nuki.LockState       `json:"state"`
	StateName             string               `json:"stateName"`
	BatteryCritical       bool                 `json:"batteryCritical"`
	BatteryCharging       bool                 `json:"batteryCharging"`
	BatteryChargeState    uint8                `json:"batteryChargeState"`
	KeypadBatteryCritical bool                 `json:"keypadBatteryCritical,omitempty"`
	DoorsensorState       nuki.DoorsensorState `json:"doorsensorState,omitempty"`
	DoorsensorStateName   string               `json:"doorsensorStateName,omitempty"`
	RingactionState       bool                 `json:"ringactionState,omitempty"`
	RingactionTimestamp   time.Time            `json:"ringactionTimestamp,omitempty"`
	Timestamp             time.Time            `json:"timestamp"`
}

LastKnownState describes the last known state of a device

type ListCallbacksResponse

type ListCallbacksResponse struct {
	Callbacks []Callback `json:"callbacks"`
}

ListCallbacksResponse represents the result of an listing request

type ListPairedDevicesResponse

type ListPairedDevicesResponse []DeviceInfo

ListPairedDevicesResponse represents the results of querying the paired devices

type LockActionResponse

type LockActionResponse struct {
	Success         bool `json:"success"`
	BatteryCritical bool `json:"batteryCritical"`
}

LockActionResponse represents the result of a request to /lockAction

type LockResponse

type LockResponse struct {
	Success         bool `json:"success"`
	BatteryCritical bool `json:"batteryCritical"`
}

LockResponse represents the result of an locking request

type LockStateResponse

type LockStateResponse struct {
	Mode                  nuki.LockMode        `json:"mode"`
	State                 nuki.LockState       `json:"state"`
	StateName             string               `json:"stateName"`
	BatteryCritical       bool                 `json:"batteryCritical"`
	BatteryCharging       bool                 `json:"batteryCharging"`
	BatteryChargeState    uint8                `json:"batteryChargeState"`
	KeypadBatteryCritical bool                 `json:"keypadBatteryCritical"`
	DoorsensorState       nuki.DoorsensorState `json:"doorsensorState,omitempty"`
	DoorsensorStateName   string               `json:"doorsensorStateName,omitempty"`
	RingactionState       bool                 `json:"ringactionState,omitempty"`
	RingactionTimestamp   time.Time            `json:"ringactionTimestamp,omitempty"`
	Success               bool                 `json:"success"`
}

LockStateResponse contains the response results from the respective API call

type Log

type Log []LogEntry

Log is a group of logging items

type LogEntry

type LogEntry struct {
	Timestamp time.Time `json:"timestamp"`
	Type      string    `json:"type"`
	ID        string    `json:"nukiId,omitempty"`
	PairIndex int       `json:"pairIndex,omitempty"`
	BleHandle string    `json:"bleHandle,omitempty"`
	MacAddr   string    `json:"macAddr,omitempty"`
	Bytes     int       `json:"bytes,omitempty"`
	Count     int       `json:"count,omitempty"`
	ServerNum int       `json:"serverNum,omitempty"`
}

LogEntry is a single logging item from the bridge

type RemoveCallbackResponse

type RemoveCallbackResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
}

RemoveCallbackResponse represents the result of an callback removal request

type ScanResult

type ScanResult struct {
	ID     int             `json:"nukiId"`
	Type   nuki.DeviceType `json:"deviceType"`
	Name   string          `json:"name"`
	Rssi   int             `json:"rssi"`
	Paired bool            `json:"paired"`
}

ScanResult represents a device found in reach of the bridge

func (*ScanResult) NukiID

func (r *ScanResult) NukiID() *nuki.ID

NukiID assembles the ID from a result

type UnlockResponse

type UnlockResponse struct {
	Success         bool `json:"success"`
	BatteryCritical bool `json:"batteryCritical"`
}

UnlockResponse represents the result of a unlocking request

Jump to

Keyboard shortcuts

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