Documentation ¶
Overview ¶
Package devicehive-go provides access to DeviceHive API through WebSocket or REST. Error handling is omitted to simplify examples:
client, _ := devicehive_go.ConnectWithCreds("ws://devicehive-address.com/api/websocket", "login", "password") // or client, _ := devicehive_go.ConnectWithCreds("http://devicehive-address.com/api/rest", "login", "password") device, _ := client.PutDevice("my-device", "", nil, 0, 0, false) subscription, _ := device.SubscribeInsertCommands(nil, time.Time{}) done := make(chan struct{}) go func() { for command := range subscription.CommandsChan { fmt.Printf("Received command with id %d\n", command.Id) close(done) } }() command, _ := device.SendCommand("my-command", nil, 120, time.Time{}, "", nil) fmt.Printf("Command with id %d has been sent\n", command.Id) <-done
In addition there is an ability to connect with tokens.
client, err := devicehive_go.ConnectWithToken("ws://devicehive-address.com/api/websocket", "some.JWT.accessToken", "some.JWT.refreshToken")
The client will be automatically reauthenticated by credentials or refresh token in case of access token expiration.
The SDK has an ability to send requests in non-blocking manner, writing each response and error to separate channels that you can read in a separate go routine. This API is called WebSocket low-level API. WS low-level API usage example:
wsclient, err := devicehive_go.WSConnect("ws://devicehive-address.com/api/websocket") ... done := make(chan struct{}) go func() { for { select { case d := <- wsclient.DataChan: res := make(map[string]interface{}) action := "" status := "" json.Unmarshal(d, &res) // If message was written to DataChan it must be valid JSON if a, ok := res["action"].(string); ok { action = a } if s, ok := res["status"].(string); ok { status = s } if action == "authenticate" && status == "success" { wsclient.SubscribeCommands(nil) } else { fmt.Println(string(d)) } case err := <- wsclient.ErrorChan: fmt.Println("Error", err) } } close(done) }() err = wsclient.Authenticate("some.JWT.accessToken") ... <-done fmt.Println("Done")
Index ¶
- Constants
- func ConnectWithCreds(url, login, password string, p *ConnectionParams) (*Client, *Error)
- func ConnectWithToken(url, accessToken, refreshToken string, p *ConnectionParams) (*Client, *Error)
- func WSConnect(url string, p *ConnectionParams) (*WSClient, *Error)
- type Client
- func (c *Client) CreateDeviceType(name, description string) (*DeviceType, *Error)
- func (c *Client) CreateNetwork(name, description string) (*Network, *Error)
- func (c *Client) CreateToken(userId int, expiration, refreshExpiration time.Time, ...) (accessToken, refreshToken string, err *Error)
- func (c *Client) CreateUser(login, password string, role int, data map[string]interface{}, ...) (*User, *Error)
- func (c *Client) DeleteProperty(name string) *Error
- func (c *Client) GetClusterInfo() (*ClusterInfo, *Error)
- func (c *Client) GetCurrentUser() (*User, *Error)
- func (c *Client) GetDevice(deviceId string) (device *Device, err *Error)
- func (c *Client) GetDeviceType(deviceTypeId int) (*DeviceType, *Error)
- func (c *Client) GetInfo() (*ServerInfo, *Error)
- func (c *Client) GetNetwork(networkId int) (*Network, *Error)
- func (c *Client) GetProperty(name string) (*Configuration, *Error)
- func (c *Client) GetUser(userId int) (*User, *Error)
- func (c *Client) ListDeviceTypes(params *ListParams) ([]*DeviceType, *Error)
- func (c *Client) ListDevices(params *ListParams) (list []*Device, err *Error)
- func (c *Client) ListNetworks(params *ListParams) ([]*Network, *Error)
- func (c *Client) ListUsers(params *ListParams) ([]*User, *Error)
- func (c *Client) NewCommand() *Command
- func (c *Client) NewDevice() *Device
- func (c *Client) NewDeviceType() *DeviceType
- func (c *Client) NewNetwork() *Network
- func (c *Client) NewNotification() *Notification
- func (c *Client) NewUser() *User
- func (c *Client) PutDevice(id, name string, data map[string]interface{}, networkId, deviceTypeId int, ...) (*Device, *Error)
- func (c *Client) RefreshToken() (accessToken string, err *Error)
- func (c *Client) SetProperty(name, value string) (entityVersion int, err *Error)
- func (c *Client) SubscribeCommands(params *SubscribeParams) (*CommandSubscription, *Error)
- func (c *Client) SubscribeNotifications(params *SubscribeParams) (*NotificationSubscription, *Error)
- type ClusterInfo
- type Command
- type CommandSubscription
- type Configuration
- type ConnectionParams
- type Device
- func (d *Device) ListCommands(params *ListParams) ([]*Command, *Error)
- func (d *Device) ListNotifications(params *ListParams) ([]*Notification, *Error)
- func (d *Device) Remove() *Error
- func (d *Device) Save() *Error
- func (d *Device) SendCommand(name string, params map[string]interface{}, lifetime int, timestamp time.Time, ...) (*Command, *Error)
- func (d *Device) SendNotification(name string, params map[string]interface{}, timestamp time.Time) (*Notification, *Error)
- func (d *Device) SubscribeInsertCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- func (d *Device) SubscribeNotifications(names []string, timestamp time.Time) (*NotificationSubscription, *Error)
- func (d *Device) SubscribeUpdateCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- type DeviceType
- func (dt *DeviceType) ForceRemove() *Error
- func (dt *DeviceType) Remove() *Error
- func (dt *DeviceType) Save() *Error
- func (dt *DeviceType) SubscribeInsertCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- func (dt *DeviceType) SubscribeNotifications(names []string, timestamp time.Time) (*NotificationSubscription, *Error)
- func (dt *DeviceType) SubscribeUpdateCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- type Error
- type ISO8601Time
- type ListParams
- type Network
- func (n *Network) ForceRemove() *Error
- func (n *Network) Remove() *Error
- func (n *Network) Save() *Error
- func (n *Network) SubscribeInsertCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- func (n *Network) SubscribeNotifications(names []string, timestamp time.Time) (*NotificationSubscription, *Error)
- func (n *Network) SubscribeUpdateCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
- type Notification
- type NotificationSubscription
- type ServerInfo
- type SubscribeParams
- type User
- func (u *User) AllowAllDeviceTypes() *Error
- func (u *User) AssignDeviceType(deviceTypeId int) *Error
- func (u *User) AssignNetwork(networkId int) *Error
- func (u *User) DisallowAllDeviceTypes() *Error
- func (u *User) ListDeviceTypes() (list []*DeviceType, err *Error)
- func (u *User) ListNetworks() (list []*Network, err *Error)
- func (u *User) Remove() *Error
- func (u *User) Save() *Error
- func (u *User) UnassignDeviceType(deviceTypeId int) *Error
- func (u *User) UnassignNetwork(networkId int) *Error
- func (u *User) UpdatePassword(password string) *Error
- type WSClient
- func (wsc *WSClient) AccessTokenByCreds(login, password string) *Error
- func (wsc *WSClient) AccessTokenByRefresh(refreshToken string) *Error
- func (wsc *WSClient) AllowAllDeviceTypes(userId int) *Error
- func (wsc *WSClient) Authenticate(accessToken string) *Error
- func (wsc *WSClient) CreateDeviceType(name, description string) *Error
- func (wsc *WSClient) CreateNetwork(name, description string) *Error
- func (wsc *WSClient) CreateToken(userId int, expiration time.Time, actions, networkIds, deviceTypeIds []string) *Error
- func (wsc *WSClient) CreateUser(login, password string, role int, data map[string]interface{}, ...) *Error
- func (wsc *WSClient) DeleteDevice(deviceId string) *Error
- func (wsc *WSClient) DeleteDeviceType(deviceTypeId int) *Error
- func (wsc *WSClient) DeleteNetwork(networkId int) *Error
- func (wsc *WSClient) DeleteProperty(name string) *Error
- func (wsc *WSClient) DeleteUser(userId int) *Error
- func (wsc *WSClient) DisallowAllDeviceTypes(userId int) *Error
- func (wsc *WSClient) GetClusterInfo() *Error
- func (wsc *WSClient) GetCurrentUser() *Error
- func (wsc *WSClient) GetDevice(deviceId string) *Error
- func (wsc *WSClient) GetDeviceType(deviceTypeId int) *Error
- func (wsc *WSClient) GetInfo() *Error
- func (wsc *WSClient) GetNetwork(networkId int) *Error
- func (wsc *WSClient) GetProperty(name string) *Error
- func (wsc *WSClient) GetUser(userId int) *Error
- func (wsc *WSClient) ListDeviceCommands(deviceId string, params *ListParams) *Error
- func (wsc *WSClient) ListDeviceNotifications(deviceId string, params *ListParams) *Error
- func (wsc *WSClient) ListDeviceTypes(params *ListParams) *Error
- func (wsc *WSClient) ListDevices(params *ListParams) *Error
- func (wsc *WSClient) ListNetworks(params *ListParams) *Error
- func (wsc *WSClient) ListUserDeviceTypes(userId int) *Error
- func (wsc *WSClient) ListUsers(params *ListParams) *Error
- func (wsc *WSClient) PutDevice(device Device) *Error
- func (wsc *WSClient) SendDeviceCommand(deviceId, name string, params map[string]interface{}, lifetime int, ...) *Error
- func (wsc *WSClient) SendDeviceNotification(deviceId, name string, params map[string]interface{}, timestamp time.Time) *Error
- func (wsc *WSClient) SetProperty(name, value string) *Error
- func (wsc *WSClient) SubscribeCommands(params *SubscribeParams) *Error
- func (wsc *WSClient) SubscribeNotifications(params *SubscribeParams) *Error
- func (wsc *WSClient) UnsubscribeCommands(subscriptionId string)
- func (wsc *WSClient) UnsubscribeNotifications(subscriptionId string)
- func (wsc *WSClient) UpdateDevice(deviceId string, device *Device) *Error
- func (wsc *WSClient) UpdateUser(userId int, user User) *Error
- func (wsc *WSClient) UserAssignDeviceType(userId, deviceTypeId int) *Error
- func (wsc *WSClient) UserAssignNetwork(userId, networkId int) *Error
- func (wsc *WSClient) UserUnassignDeviceType(userId, deviceTypeId int) *Error
- func (wsc *WSClient) UserUnassignNetwork(userId, networkId int) *Error
Constants ¶
const ( Timeout = 5 * time.Second UserStatusActive = 0 UserStatusLocked = 1 UserStatusDisabled = 2 UserRoleAdmin = 0 UserRoleClient = 1 InvalidResponseErr = "invalid response" InvalidRequestErr = "invalid request" InvalidSubscriptionEventData = "invalid subscription event data" ServiceErr = "service error" ConnectionFailedErr = "connection failed" DefaultPollingWaitTimeoutSeconds = 30 WrongURLErr = "wrong url" TokenExpiredErr = "401 token expired" )
Variables ¶
This section is empty.
Functions ¶
func ConnectWithCreds ¶
func ConnectWithCreds(url, login, password string, p *ConnectionParams) (*Client, *Error)
Method obtains access token by credentials and then connects It will recreate access token on expiration by given credentials
func ConnectWithToken ¶
func ConnectWithToken(url, accessToken, refreshToken string, p *ConnectionParams) (*Client, *Error)
Method uses access token directly to connect If access token is empty it will get access token by refresh token It will recreate access token on expiration by given refresh token
Types ¶
type Client ¶
type Client struct { PollingWaitTimeoutSeconds int // contains filtered or unexported fields }
Main struct which serves as entry point to DeviceHive API
func (*Client) CreateDeviceType ¶
func (c *Client) CreateDeviceType(name, description string) (*DeviceType, *Error)
func (*Client) CreateNetwork ¶
func (*Client) CreateToken ¶
func (*Client) CreateUser ¶
func (*Client) DeleteProperty ¶
func (*Client) GetClusterInfo ¶
func (c *Client) GetClusterInfo() (*ClusterInfo, *Error)
func (*Client) GetCurrentUser ¶
func (*Client) GetDeviceType ¶
func (c *Client) GetDeviceType(deviceTypeId int) (*DeviceType, *Error)
func (*Client) GetInfo ¶
func (c *Client) GetInfo() (*ServerInfo, *Error)
Gets information about DeviceHive server
func (*Client) GetProperty ¶
func (c *Client) GetProperty(name string) (*Configuration, *Error)
func (*Client) ListDeviceTypes ¶
func (c *Client) ListDeviceTypes(params *ListParams) ([]*DeviceType, *Error)
In case params is nil default values defined at DeviceHive take place
func (*Client) ListDevices ¶
func (c *Client) ListDevices(params *ListParams) (list []*Device, err *Error)
In case params is nil default values defined at DeviceHive take place
func (*Client) ListNetworks ¶
func (c *Client) ListNetworks(params *ListParams) ([]*Network, *Error)
In case params is nil default values defined at DeviceHive take place
func (*Client) NewCommand ¶
Constructor, doesn't create command at DH
func (*Client) NewDeviceType ¶
func (c *Client) NewDeviceType() *DeviceType
Constructor, doesn't create device type at DH
func (*Client) NewNetwork ¶
Constructor, doesn't create network at DH
func (*Client) NewNotification ¶
func (c *Client) NewNotification() *Notification
Constructor, doesn't create notification at DH
func (*Client) PutDevice ¶
func (c *Client) PutDevice(id, name string, data map[string]interface{}, networkId, deviceTypeId int, blocked bool) (*Device, *Error)
Id property of device must be non empty
func (*Client) RefreshToken ¶
func (*Client) SetProperty ¶
func (*Client) SubscribeCommands ¶
func (c *Client) SubscribeCommands(params *SubscribeParams) (*CommandSubscription, *Error)
Subscribes to commands by custom filter In case params is nil returns subscription for all commands
func (*Client) SubscribeNotifications ¶
func (c *Client) SubscribeNotifications(params *SubscribeParams) (*NotificationSubscription, *Error)
Subscribes to notifications by custom filter In case params is nil returns subscription for all notifications
type ClusterInfo ¶
type Command ¶
type Command struct { Id int `json:"id,omitempty"` Command string `json:"command,omitempty"` Timestamp ISO8601Time `json:"timestamp,omitempty"` LastUpdated ISO8601Time `json:"lastUpdated,omitempty"` UserId int `json:"userId,omitempty"` DeviceId string `json:"deviceId,omitempty"` NetworkId int `json:"networkId,omitempty"` Parameters interface{} `json:"parameters,omitempty"` Lifetime int `json:"lifetime,omitempty"` Status string `json:"status,omitempty"` Result interface{} `json:"result,omitempty"` // contains filtered or unexported fields }
type CommandSubscription ¶
type CommandSubscription struct { CommandsChan chan *Command ErrorChan chan *Error // contains filtered or unexported fields }
func (*CommandSubscription) Remove ¶
func (cs *CommandSubscription) Remove() *Error
Sends request to unsubscribe from current commands subscription
type Configuration ¶
type ConnectionParams ¶
type ConnectionParams struct { ReconnectionTries int ReconnectionInterval time.Duration RequestTimeout time.Duration }
func (*ConnectionParams) Timeout ¶
func (p *ConnectionParams) Timeout() time.Duration
type Device ¶
type Device struct { Id string `json:"id,omitempty"` Name string `json:"name,omitempty"` Data map[string]interface{} `json:"data,omitempty"` NetworkId int `json:"networkId,omitempty"` DeviceTypeId int `json:"deviceTypeId,omitempty"` IsBlocked bool `json:"isBlocked,omitempty"` // contains filtered or unexported fields }
func (*Device) ListCommands ¶
func (d *Device) ListCommands(params *ListParams) ([]*Command, *Error)
In case params is nil default values defined at DeviceHive take place
func (*Device) ListNotifications ¶
func (d *Device) ListNotifications(params *ListParams) ([]*Notification, *Error)
In case params is nil default values defined at DeviceHive take place
func (*Device) SendCommand ¶
func (*Device) SendNotification ¶
func (*Device) SubscribeInsertCommands ¶
func (*Device) SubscribeNotifications ¶
func (*Device) SubscribeUpdateCommands ¶
type DeviceType ¶
type DeviceType struct { Id int `json:"id,omitempty"` Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` // contains filtered or unexported fields }
func (*DeviceType) ForceRemove ¶
func (dt *DeviceType) ForceRemove() *Error
func (*DeviceType) Remove ¶
func (dt *DeviceType) Remove() *Error
func (*DeviceType) Save ¶
func (dt *DeviceType) Save() *Error
func (*DeviceType) SubscribeInsertCommands ¶
func (dt *DeviceType) SubscribeInsertCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
func (*DeviceType) SubscribeNotifications ¶
func (dt *DeviceType) SubscribeNotifications(names []string, timestamp time.Time) (*NotificationSubscription, *Error)
func (*DeviceType) SubscribeUpdateCommands ¶
func (dt *DeviceType) SubscribeUpdateCommands(names []string, timestamp time.Time) (*CommandSubscription, *Error)
type ISO8601Time ¶
Custom timestamp in ISO8601 format
func (*ISO8601Time) MarshalJSON ¶
func (t *ISO8601Time) MarshalJSON() (b []byte, err error)
func (*ISO8601Time) String ¶
func (t *ISO8601Time) String() string
func (*ISO8601Time) UnmarshalJSON ¶
func (t *ISO8601Time) UnmarshalJSON(b []byte) (err error)
type ListParams ¶
type ListParams struct { DeviceId string `json:"deviceId,omitempty"` Start time.Time `json:"start,omitempty"` End time.Time `json:"end,omitempty"` Notification string `json:"notification,omitempty"` Command string `json:"command,omitempty"` Status string `json:"status,omitempty"` SortField string `json:"sortField,omitempty"` SortOrder string `json:"sortOrder,omitempty"` Take int `json:"take,omitempty"` Skip int `json:"skip,omitempty"` Name string `json:"name,omitempty"` NamePattern string `json:"namePattern,omitempty"` Login string `json:"login,omitempty"` LoginPattern string `json:"loginPattern,omitempty"` NetworkId string `json:"networkId,omitempty"` NetworkName string `json:"networkName,omitempty"` UserRole int `json:"role,omitempty"` UserStatus int `json:"status,omitempty"` }
func (*ListParams) Map ¶
func (p *ListParams) Map() (map[string]interface{}, error)
type Network ¶
type Network struct { Id int `json:"id,omitempty"` Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` // contains filtered or unexported fields }
func (*Network) ForceRemove ¶
func (*Network) SubscribeInsertCommands ¶
func (*Network) SubscribeNotifications ¶
func (*Network) SubscribeUpdateCommands ¶
type Notification ¶
type NotificationSubscription ¶
type NotificationSubscription struct { NotificationChan chan *Notification ErrorChan chan *Error // contains filtered or unexported fields }
func (*NotificationSubscription) Remove ¶
func (ns *NotificationSubscription) Remove() *Error
Sends request to unsubscribe from current notifications subscription
type ServerInfo ¶
type ServerInfo struct { APIVersion string `json:"apiVersion"` ServerTimestamp ISO8601Time `json:"serverTimestamp"` RestServerURL string `json:"restServerUrl"` WebSocketServerURL string `json:"webSocketServerUrl"` }
type SubscribeParams ¶
type SubscribeParams struct { DeviceId string `json:"deviceId,omitempty"` NetworkIds []int `json:"networkIds,omitempty"` DeviceTypeIds []int `json:"deviceTypeIds,omitempty"` Names []string `json:"names,omitempty"` Timestamp time.Time `json:"timestamp,omitempty"` ReturnUpdatedCommands bool `json:"returnUpdatedCommands,omitempty"` Limit int `json:"limit,omitempty"` WaitTimeout int `json:"waitTimeout,omitempty"` }
func (*SubscribeParams) Map ¶
func (p *SubscribeParams) Map() (map[string]interface{}, error)
type User ¶
type User struct { Id int `json:"id,omitempty"` Login string `json:"login,omitempty"` Role int `json:"role,omitempty"` Status int `json:"status,omitempty"` LastLogin ISO8601Time `json:"lastLogin,omitempty"` Data map[string]interface{} `json:"data,omitempty"` IntroReviewed bool `json:"introReviewed,omitempty"` AllDeviceTypesAvailable bool `json:"allDeviceTypesAvailable,omitempty"` // contains filtered or unexported fields }
func (*User) AllowAllDeviceTypes ¶
func (*User) AssignDeviceType ¶
func (*User) AssignNetwork ¶
func (*User) DisallowAllDeviceTypes ¶
func (*User) ListDeviceTypes ¶
func (u *User) ListDeviceTypes() (list []*DeviceType, err *Error)
func (*User) ListNetworks ¶
func (*User) UnassignDeviceType ¶
func (*User) UnassignNetwork ¶
func (*User) UpdatePassword ¶
type WSClient ¶
type WSClient struct { // Channel for receiving responses DataChan chan []byte // Channel for receiving errors ErrorChan chan error // contains filtered or unexported fields }
func (*WSClient) AccessTokenByCreds ¶
func (*WSClient) AccessTokenByRefresh ¶
func (*WSClient) AllowAllDeviceTypes ¶
func (*WSClient) Authenticate ¶
func (*WSClient) CreateDeviceType ¶
func (*WSClient) CreateNetwork ¶
func (*WSClient) CreateToken ¶
func (*WSClient) CreateUser ¶
func (*WSClient) DeleteDevice ¶
func (*WSClient) DeleteDeviceType ¶
func (*WSClient) DeleteNetwork ¶
func (*WSClient) DeleteProperty ¶
func (*WSClient) DeleteUser ¶
func (*WSClient) DisallowAllDeviceTypes ¶
func (*WSClient) GetClusterInfo ¶
func (*WSClient) GetCurrentUser ¶
func (*WSClient) GetDeviceType ¶
func (*WSClient) GetNetwork ¶
func (*WSClient) GetProperty ¶
func (*WSClient) ListDeviceCommands ¶
func (wsc *WSClient) ListDeviceCommands(deviceId string, params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) ListDeviceNotifications ¶
func (wsc *WSClient) ListDeviceNotifications(deviceId string, params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) ListDeviceTypes ¶
func (wsc *WSClient) ListDeviceTypes(params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) ListDevices ¶
func (wsc *WSClient) ListDevices(params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) ListNetworks ¶
func (wsc *WSClient) ListNetworks(params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) ListUserDeviceTypes ¶
func (*WSClient) ListUsers ¶
func (wsc *WSClient) ListUsers(params *ListParams) *Error
In case params is nil default values defined at DeviceHive take place
func (*WSClient) SendDeviceCommand ¶
func (*WSClient) SendDeviceNotification ¶
func (*WSClient) SetProperty ¶
func (*WSClient) SubscribeCommands ¶
func (wsc *WSClient) SubscribeCommands(params *SubscribeParams) *Error
Subscribes for commands with given params. If params is nil then default values take place. After successful subscription JSON object with only property "subscriptionId" is sent to the main data channel.
func (*WSClient) SubscribeNotifications ¶
func (wsc *WSClient) SubscribeNotifications(params *SubscribeParams) *Error
Subscribes for notifications with given params. If params is nil then default values take place. After successful subscription JSON object with only property "subscriptionId" is sent to the main data channel.