Documentation ¶
Index ¶
- Variables
- type ActivationHandler
- type AppEventHandler
- type ApplicationTopic
- type ApplicationTopicType
- type Client
- type DefaultClient
- func (c *DefaultClient) Connect() error
- func (c *DefaultClient) Disconnect()
- func (c *DefaultClient) IsConnected() bool
- func (c *DefaultClient) PublishActivation(activation types.Activation) Token
- func (c *DefaultClient) PublishAppEvent(appID string, eventType types.EventType, payload interface{}) Token
- func (c *DefaultClient) PublishDeviceEvent(appID string, devID string, eventType types.EventType, payload interface{}) Token
- func (c *DefaultClient) PublishDownlink(dataDown types.DownlinkMessage) Token
- func (c *DefaultClient) PublishUplink(dataUp types.UplinkMessage) Token
- func (c *DefaultClient) PublishUplinkFields(appID string, devID string, fields map[string]interface{}) Token
- func (c *DefaultClient) SubscribeActivations(handler ActivationHandler) Token
- func (c *DefaultClient) SubscribeAppActivations(appID string, handler ActivationHandler) Token
- func (c *DefaultClient) SubscribeAppDownlink(appID string, handler DownlinkHandler) Token
- func (c *DefaultClient) SubscribeAppEvents(appID string, eventType types.EventType, handler AppEventHandler) Token
- func (c *DefaultClient) SubscribeAppUplink(appID string, handler UplinkHandler) Token
- func (c *DefaultClient) SubscribeDeviceActivations(appID string, devID string, handler ActivationHandler) Token
- func (c *DefaultClient) SubscribeDeviceDownlink(appID string, devID string, handler DownlinkHandler) Token
- func (c *DefaultClient) SubscribeDeviceEvents(appID string, devID string, eventType types.EventType, ...) Token
- func (c *DefaultClient) SubscribeDeviceUplink(appID string, devID string, handler UplinkHandler) Token
- func (c *DefaultClient) SubscribeDownlink(handler DownlinkHandler) Token
- func (c *DefaultClient) SubscribeUplink(handler UplinkHandler) Token
- func (c *DefaultClient) UnsubscribeActivations() Token
- func (c *DefaultClient) UnsubscribeAppActivations(appID string) Token
- func (c *DefaultClient) UnsubscribeAppDownlink(appID string) Token
- func (c *DefaultClient) UnsubscribeAppEvents(appID string, eventType types.EventType) Token
- func (c *DefaultClient) UnsubscribeAppUplink(appID string) Token
- func (c *DefaultClient) UnsubscribeDeviceActivations(appID string, devID string) Token
- func (c *DefaultClient) UnsubscribeDeviceDownlink(appID string, devID string) Token
- func (c *DefaultClient) UnsubscribeDeviceEvents(appID string, devID string, eventType types.EventType) Token
- func (c *DefaultClient) UnsubscribeDeviceUplink(appID string, devID string) Token
- func (c *DefaultClient) UnsubscribeDownlink() Token
- func (c *DefaultClient) UnsubscribeUplink() Token
- type DeviceEventHandler
- type DeviceTopic
- type DeviceTopicType
- type DownlinkHandler
- type Token
- type UplinkHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( PublishQoS byte = 0x00 SubscribeQoS byte = 0x00 )
QoS indicates the MQTT Quality of Service level. 0: The broker/client will deliver the message once, with no confirmation. 1: The broker/client will deliver the message at least once, with confirmation required. 2: The broker/client will deliver the message exactly once by using a four step handshake.
var ( // ConnectRetries says how many times the client should retry a failed connection ConnectRetries = 10 // ConnectRetryDelay says how long the client should wait between retries ConnectRetryDelay = time.Second )
var RootCAs *x509.CertPool
RootCAs to use in API connections
Functions ¶
This section is empty.
Types ¶
type ActivationHandler ¶
type ActivationHandler func(client Client, appID string, devID string, req types.Activation)
ActivationHandler is called for activations
type AppEventHandler ¶
AppEventHandler is called for events
type ApplicationTopic ¶
type ApplicationTopic struct { AppID string Type ApplicationTopicType Field string }
ApplicationTopic represents an MQTT topic for applications
func ParseApplicationTopic ¶
func ParseApplicationTopic(topic string) (*ApplicationTopic, error)
ParseApplicationTopic parses an MQTT device topic string to an ApplicationTopic struct
func (ApplicationTopic) String ¶
func (t ApplicationTopic) String() string
String implements the Stringer interface
type ApplicationTopicType ¶
type ApplicationTopicType string
ApplicationTopicType represents the type of an application topic
const (
AppEvents ApplicationTopicType = "events"
)
Topic types for Applications
type Client ¶
type Client interface { Connect() error Disconnect() IsConnected() bool // Uplink pub/sub PublishUplink(payload types.UplinkMessage) Token PublishUplinkFields(appID string, devID string, fields map[string]interface{}) Token SubscribeDeviceUplink(appID string, devID string, handler UplinkHandler) Token SubscribeAppUplink(appID string, handler UplinkHandler) Token SubscribeUplink(handler UplinkHandler) Token UnsubscribeDeviceUplink(appID string, devID string) Token UnsubscribeAppUplink(appID string) Token UnsubscribeUplink() Token // Downlink pub/sub PublishDownlink(payload types.DownlinkMessage) Token SubscribeDeviceDownlink(appID string, devID string, handler DownlinkHandler) Token SubscribeAppDownlink(appID string, handler DownlinkHandler) Token SubscribeDownlink(handler DownlinkHandler) Token UnsubscribeDeviceDownlink(appID string, devID string) Token UnsubscribeAppDownlink(appID string) Token UnsubscribeDownlink() Token // Event pub/sub PublishAppEvent(appID string, eventType types.EventType, payload interface{}) Token PublishDeviceEvent(appID string, devID string, eventType types.EventType, payload interface{}) Token SubscribeAppEvents(appID string, eventType types.EventType, handler AppEventHandler) Token SubscribeDeviceEvents(appID string, devID string, eventType types.EventType, handler DeviceEventHandler) Token UnsubscribeAppEvents(appID string, eventType types.EventType) Token UnsubscribeDeviceEvents(appID string, devID string, eventType types.EventType) Token // Activation pub/sub PublishActivation(payload types.Activation) Token SubscribeDeviceActivations(appID string, devID string, handler ActivationHandler) Token SubscribeAppActivations(appID string, handler ActivationHandler) Token SubscribeActivations(handler ActivationHandler) Token UnsubscribeDeviceActivations(appID string, devID string) Token UnsubscribeAppActivations(appID string) Token UnsubscribeActivations() Token }
Client connects to the MQTT server and can publish/subscribe on uplink, downlink and activations from devices
func NewClient ¶
NewClient creates a new DefaultClient
Example ¶
ctx := apex.Wrap(log.WithField("Example", "NewClient")) exampleClient := NewClient(ctx, "ttnctl", "my-app-id", "my-access-key", "eu.thethings.network:1883") err := exampleClient.Connect() if err != nil { ctx.WithError(err).Fatal("Could not connect") }
Output:
type DefaultClient ¶
type DefaultClient struct {
// contains filtered or unexported fields
}
DefaultClient is the default MQTT client for The Things Network
func (*DefaultClient) Connect ¶
func (c *DefaultClient) Connect() error
Connect to the MQTT broker. It will retry for ConnectRetries times with a delay of ConnectRetryDelay between retries
func (*DefaultClient) Disconnect ¶
func (c *DefaultClient) Disconnect()
Disconnect from the MQTT broker
func (*DefaultClient) IsConnected ¶
func (c *DefaultClient) IsConnected() bool
IsConnected returns true if there is a connection to the MQTT broker
func (*DefaultClient) PublishActivation ¶
func (c *DefaultClient) PublishActivation(activation types.Activation) Token
PublishActivation publishes an activation
func (*DefaultClient) PublishAppEvent ¶
func (c *DefaultClient) PublishAppEvent(appID string, eventType types.EventType, payload interface{}) Token
PublishAppEvent publishes an event to the topic for application events of the given type it will marshal the payload to json
func (*DefaultClient) PublishDeviceEvent ¶
func (c *DefaultClient) PublishDeviceEvent(appID string, devID string, eventType types.EventType, payload interface{}) Token
PublishDeviceEvent publishes an event to the topic for device events of the given type it will marshal the payload to json
func (*DefaultClient) PublishDownlink ¶
func (c *DefaultClient) PublishDownlink(dataDown types.DownlinkMessage) Token
PublishDownlink publishes a downlink message
Example ¶
token := exampleClient.PublishDownlink(types.DownlinkMessage{ AppID: "my-app-id", DevID: "my-dev-id", FPort: 1, PayloadRaw: []byte{0x01, 0x02, 0x03, 0x04}, }) token.Wait() if err := token.Error(); err != nil { panic(err) }
Output:
func (*DefaultClient) PublishUplink ¶
func (c *DefaultClient) PublishUplink(dataUp types.UplinkMessage) Token
PublishUplink publishes an uplink message to the MQTT broker
func (*DefaultClient) PublishUplinkFields ¶
func (c *DefaultClient) PublishUplinkFields(appID string, devID string, fields map[string]interface{}) Token
PublishUplinkFields publishes uplink fields to MQTT
func (*DefaultClient) SubscribeActivations ¶
func (c *DefaultClient) SubscribeActivations(handler ActivationHandler) Token
SubscribeActivations subscribes to all activations that the current user has access to
func (*DefaultClient) SubscribeAppActivations ¶
func (c *DefaultClient) SubscribeAppActivations(appID string, handler ActivationHandler) Token
SubscribeAppActivations subscribes to all activations for the given application
func (*DefaultClient) SubscribeAppDownlink ¶
func (c *DefaultClient) SubscribeAppDownlink(appID string, handler DownlinkHandler) Token
SubscribeAppDownlink subscribes to all downlink messages for the given application
func (*DefaultClient) SubscribeAppEvents ¶
func (c *DefaultClient) SubscribeAppEvents(appID string, eventType types.EventType, handler AppEventHandler) Token
SubscribeAppEvents subscribes to events of the given type for the given application. In order to subscribe to application events from all applications the user has access to, pass an empty string as appID.
func (*DefaultClient) SubscribeAppUplink ¶
func (c *DefaultClient) SubscribeAppUplink(appID string, handler UplinkHandler) Token
SubscribeAppUplink subscribes to all uplink messages for the given application
func (*DefaultClient) SubscribeDeviceActivations ¶
func (c *DefaultClient) SubscribeDeviceActivations(appID string, devID string, handler ActivationHandler) Token
SubscribeDeviceActivations subscribes to all activations for the given application and device
func (*DefaultClient) SubscribeDeviceDownlink ¶
func (c *DefaultClient) SubscribeDeviceDownlink(appID string, devID string, handler DownlinkHandler) Token
SubscribeDeviceDownlink subscribes to all downlink messages for the given application and device
func (*DefaultClient) SubscribeDeviceEvents ¶
func (c *DefaultClient) SubscribeDeviceEvents(appID string, devID string, eventType types.EventType, handler DeviceEventHandler) Token
SubscribeDeviceEvents subscribes to events of the given type for the given device. In order to subscribe to events from all devices within an application, pass an empty string as devID. In order to subscribe to all events from all devices in all applications the user has access to, pass an empty string as appID.
func (*DefaultClient) SubscribeDeviceUplink ¶
func (c *DefaultClient) SubscribeDeviceUplink(appID string, devID string, handler UplinkHandler) Token
SubscribeDeviceUplink subscribes to all uplink messages for the given application and device
Example ¶
token := exampleClient.SubscribeDeviceUplink("my-app-id", "my-dev-id", func(client Client, appID string, devID string, req types.UplinkMessage) { // Do something with the message }) token.Wait() if err := token.Error(); err != nil { panic(err) }
Output:
func (*DefaultClient) SubscribeDownlink ¶
func (c *DefaultClient) SubscribeDownlink(handler DownlinkHandler) Token
SubscribeDownlink subscribes to all downlink messages that the current user has access to
func (*DefaultClient) SubscribeUplink ¶
func (c *DefaultClient) SubscribeUplink(handler UplinkHandler) Token
SubscribeUplink subscribes to all uplink messages that the current user has access to
func (*DefaultClient) UnsubscribeActivations ¶
func (c *DefaultClient) UnsubscribeActivations() Token
UnsubscribeActivations unsubscribes from the activations that the current user has access to
func (*DefaultClient) UnsubscribeAppActivations ¶
func (c *DefaultClient) UnsubscribeAppActivations(appID string) Token
UnsubscribeAppActivations unsubscribes from the activations for the given application
func (*DefaultClient) UnsubscribeAppDownlink ¶
func (c *DefaultClient) UnsubscribeAppDownlink(appID string) Token
UnsubscribeAppDownlink unsubscribes from the downlink messages for the given application
func (*DefaultClient) UnsubscribeAppEvents ¶
func (c *DefaultClient) UnsubscribeAppEvents(appID string, eventType types.EventType) Token
UnsubscribeAppEvents unsubscribes from the events that were subscribed to by SubscribeAppEvents
func (*DefaultClient) UnsubscribeAppUplink ¶
func (c *DefaultClient) UnsubscribeAppUplink(appID string) Token
UnsubscribeAppUplink unsubscribes from the uplink messages for the given application
func (*DefaultClient) UnsubscribeDeviceActivations ¶
func (c *DefaultClient) UnsubscribeDeviceActivations(appID string, devID string) Token
UnsubscribeDeviceActivations unsubscribes from the activations for the given application and device
func (*DefaultClient) UnsubscribeDeviceDownlink ¶
func (c *DefaultClient) UnsubscribeDeviceDownlink(appID string, devID string) Token
UnsubscribeDeviceDownlink unsubscribes from the downlink messages for the given application and device
func (*DefaultClient) UnsubscribeDeviceEvents ¶
func (c *DefaultClient) UnsubscribeDeviceEvents(appID string, devID string, eventType types.EventType) Token
UnsubscribeDeviceEvents unsubscribes from the events that were subscribed to by SubscribeDeviceEvents
func (*DefaultClient) UnsubscribeDeviceUplink ¶
func (c *DefaultClient) UnsubscribeDeviceUplink(appID string, devID string) Token
UnsubscribeDeviceUplink unsubscribes from the uplink messages for the given application and device
func (*DefaultClient) UnsubscribeDownlink ¶
func (c *DefaultClient) UnsubscribeDownlink() Token
UnsubscribeDownlink unsubscribes from the downlink messages that the current user has access to
func (*DefaultClient) UnsubscribeUplink ¶
func (c *DefaultClient) UnsubscribeUplink() Token
UnsubscribeUplink unsubscribes from the uplink messages that the current user has access to
type DeviceEventHandler ¶
type DeviceEventHandler func(client Client, appID string, devID string, eventType types.EventType, payload []byte)
DeviceEventHandler is called for events
type DeviceTopic ¶
type DeviceTopic struct { AppID string DevID string Type DeviceTopicType Field string }
DeviceTopic represents an MQTT topic for devices
func ParseDeviceTopic ¶
func ParseDeviceTopic(topic string) (*DeviceTopic, error)
ParseDeviceTopic parses an MQTT device topic string to a DeviceTopic struct
func (DeviceTopic) String ¶
func (t DeviceTopic) String() string
String implements the Stringer interface
type DeviceTopicType ¶
type DeviceTopicType string
DeviceTopicType represents the type of a device topic
const ( DeviceEvents DeviceTopicType = "events" DeviceUplink DeviceTopicType = "up" DeviceDownlink DeviceTopicType = "down" )
Topic types for Devices
type DownlinkHandler ¶
type DownlinkHandler func(client Client, appID string, devID string, req types.DownlinkMessage)
DownlinkHandler is called for downlink messages
type Token ¶
type Token interface { // Wait for the function to finish Wait() bool // Wait for the function to finish or return false after a certain time WaitTimeout(time.Duration) bool // The error associated with the result of the function (nil if everything okay) Error() error }
Token is returned on asyncronous functions
type UplinkHandler ¶
type UplinkHandler func(client Client, appID string, devID string, req types.UplinkMessage)
UplinkHandler is called for uplink messages