Documentation
¶
Overview ¶
Package sdk implements a device SDK for AllThingsTalk IoT Platform.
AllThingsTalk Maker is a IoT device prototyping platform and this SDK aims to help developers who prefer Go language use it.
Let's start with the simplest example possible - creating a device which counts from 1 to 10. Start off by going to AllThingsTalk Maker and creating your account and a device (a Custom device will do just fine). Next, let's write some code:
import ( "fmt" "time" "github.com/allthingstalk/go-sdk" ) func main() { device, err := sdk.NewDevice("<DEVICE_ID>", "<DEVICE_TOKEN>") if err != nil { panic(err) } counter, _ := device.AddInteger("counter") for i := 1; i <= 10; i++ { time.Sleep(1 * time.Second) fmt.Println(i) device.Publish(counter, i) } }
You should replace <DEVICE_ID> and <DEVICE_TOKEN> with values that can be found in Device Settings page. If you run the example, the 'counter' asset should be created, and you should see it counting from 1 to 10.
That's it! For more comprehensive examples please check out the repository's README.
Index ¶
- Constants
- Variables
- type Asset
- type Command
- type CommandHandler
- type Device
- func (device *Device) Add(a *Asset) error
- func (device *Device) AddBoolean(name string) (*Asset, error)
- func (device *Device) AddInteger(name string) (*Asset, error)
- func (device *Device) AddNumber(name string) (*Asset, error)
- func (device *Device) AddString(name string) (*Asset, error)
- func (device *Device) GetState(asset *Asset) (*State, error)
- func (device *Device) Publish(a *Asset, value interface{})
- func (device *Device) PublishState(asset *Asset, state State)
- func (device *Device) SetCommandHandler(handler CommandHandler)
- type Kind
- type Option
- type Options
- type State
Constants ¶
const (
// UA identifies the SDK
UA = "ATTalk-GoSDK/1.0.1"
)
Constants
Variables ¶
var ( // ErrAssetNotAdded is returned when asset could not be added ErrAssetNotAdded = errors.New("Could not Add Asset") // ErrCountNotFetchState is returned when current state could not be obtained ErrCountNotFetchState = errors.New("Count not obtain current Asset state") )
HTTP Errors
Loggers
var ( // ErrMqttNotEstablished is returned when there's an issue with establishing MQTT connection ErrMqttNotEstablished = errors.New("Unable to connect to MQTT server, please verify your settings") )
MQTT Errors
Functions ¶
This section is empty.
Types ¶
type Asset ¶
type Asset struct { Kind Kind `json:"is"` Name string `json:"name"` Profile interface{} `json:"profile"` }
Asset defines a structure holding some basic information about an asset, like it's kind and supported data type (profile).
func NewActuator ¶
NewActuator creates a new Actuator asset with a given profile.
type Command ¶
type Command struct { Name string Timestamp time.Time `json:"at"` Value interface{} `json:"value"` }
Command message describing command intended for an asset.
type CommandHandler ¶
type CommandHandler func(command Command)
CommandHandler can be attached to function to listen to incoming commands.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is a representation of a device on AllThingsTalk platform.
func (*Device) AddBoolean ¶
AddBoolean adds an Boolean sensor to a device.
func (*Device) AddInteger ¶
AddInteger adds an Integer sensor to a device.
func (*Device) PublishState ¶
PublishState publishes asset state. Client can supply value and timestamp.
func (*Device) SetCommandHandler ¶
func (device *Device) SetCommandHandler(handler CommandHandler)
SetCommandHandler allows for setting a function to handle incoming commands.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a single option configuration of a device